lls.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * linear least squares model
  3. *
  4. * Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * linear least squares model
  25. */
  26. #include <math.h>
  27. #include <string.h>
  28. #include "lls.h"
  29. void av_init_lls(LLSModel *m, int indep_count)
  30. {
  31. memset(m, 0, sizeof(LLSModel));
  32. m->indep_count = indep_count;
  33. }
  34. void av_update_lls(LLSModel *m, double *var, double decay)
  35. {
  36. int i, j;
  37. for (i = 0; i <= m->indep_count; i++) {
  38. for (j = i; j <= m->indep_count; j++) {
  39. m->covariance[i][j] *= decay;
  40. m->covariance[i][j] += var[i] * var[j];
  41. }
  42. }
  43. }
  44. void av_solve_lls(LLSModel *m, double threshold, int min_order)
  45. {
  46. int i, j, k;
  47. double (*factor)[MAX_VARS + 1] = (void *) &m->covariance[1][0];
  48. double (*covar) [MAX_VARS + 1] = (void *) &m->covariance[1][1];
  49. double *covar_y = m->covariance[0];
  50. int count = m->indep_count;
  51. for (i = 0; i < count; i++) {
  52. for (j = i; j < count; j++) {
  53. double sum = covar[i][j];
  54. for (k = i - 1; k >= 0; k--)
  55. sum -= factor[i][k] * factor[j][k];
  56. if (i == j) {
  57. if (sum < threshold)
  58. sum = 1.0;
  59. factor[i][i] = sqrt(sum);
  60. } else {
  61. factor[j][i] = sum / factor[i][i];
  62. }
  63. }
  64. }
  65. for (i = 0; i < count; i++) {
  66. double sum = covar_y[i + 1];
  67. for (k = i - 1; k >= 0; k--)
  68. sum -= factor[i][k] * m->coeff[0][k];
  69. m->coeff[0][i] = sum / factor[i][i];
  70. }
  71. for (j = count - 1; j >= min_order; j--) {
  72. for (i = j; i >= 0; i--) {
  73. double sum = m->coeff[0][i];
  74. for (k = i + 1; k <= j; k++)
  75. sum -= factor[k][i] * m->coeff[j][k];
  76. m->coeff[j][i] = sum / factor[i][i];
  77. }
  78. m->variance[j] = covar_y[0];
  79. for (i = 0; i <= j; i++) {
  80. double sum = m->coeff[j][i] * covar[i][i] - 2 * covar_y[i + 1];
  81. for (k = 0; k < i; k++)
  82. sum += 2 * m->coeff[j][k] * covar[k][i];
  83. m->variance[j] += m->coeff[j][i] * sum;
  84. }
  85. }
  86. }
  87. double av_evaluate_lls(LLSModel *m, double *param, int order)
  88. {
  89. int i;
  90. double out = 0;
  91. for (i = 0; i <= order; i++)
  92. out += param[i] * m->coeff[order][i];
  93. return out;
  94. }
  95. #ifdef TEST
  96. #include <stdio.h>
  97. #include <limits.h>
  98. #include "lfg.h"
  99. int main(void)
  100. {
  101. LLSModel m;
  102. int i, order;
  103. AVLFG lfg;
  104. av_lfg_init(&lfg, 1);
  105. av_init_lls(&m, 3);
  106. for (i = 0; i < 100; i++) {
  107. double var[4];
  108. double eval;
  109. var[0] = (av_lfg_get(&lfg) / (double) UINT_MAX - 0.5) * 2;
  110. var[1] = var[0] + av_lfg_get(&lfg) / (double) UINT_MAX - 0.5;
  111. var[2] = var[1] + av_lfg_get(&lfg) / (double) UINT_MAX - 0.5;
  112. var[3] = var[2] + av_lfg_get(&lfg) / (double) UINT_MAX - 0.5;
  113. av_update_lls(&m, var, 0.99);
  114. av_solve_lls(&m, 0.001, 0);
  115. for (order = 0; order < 3; order++) {
  116. eval = av_evaluate_lls(&m, var + 1, order);
  117. printf("real:%9f order:%d pred:%9f var:%f coeffs:%f %9f %9f\n",
  118. var[0], order, eval, sqrt(m.variance[order] / (i + 1)),
  119. m.coeff[order][0], m.coeff[order][1],
  120. m.coeff[order][2]);
  121. }
  122. }
  123. return 0;
  124. }
  125. #endif