lls.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 "version.h"
  29. #include "lls.h"
  30. void avpriv_init_lls(LLSModel *m, int indep_count)
  31. {
  32. memset(m, 0, sizeof(LLSModel));
  33. m->indep_count = indep_count;
  34. }
  35. void avpriv_update_lls(LLSModel *m, double *var, double decay)
  36. {
  37. int i, j;
  38. for (i = 0; i <= m->indep_count; i++) {
  39. for (j = i; j <= m->indep_count; j++) {
  40. m->covariance[i][j] *= decay;
  41. m->covariance[i][j] += var[i] * var[j];
  42. }
  43. }
  44. }
  45. void avpriv_solve_lls(LLSModel *m, double threshold, unsigned short min_order)
  46. {
  47. int i, j, k;
  48. double (*factor)[MAX_VARS + 1] = (void *) &m->covariance[1][0];
  49. double (*covar) [MAX_VARS + 1] = (void *) &m->covariance[1][1];
  50. double *covar_y = m->covariance[0];
  51. int count = m->indep_count;
  52. for (i = 0; i < count; i++) {
  53. for (j = i; j < count; j++) {
  54. double sum = covar[i][j];
  55. for (k = i - 1; k >= 0; k--)
  56. sum -= factor[i][k] * factor[j][k];
  57. if (i == j) {
  58. if (sum < threshold)
  59. sum = 1.0;
  60. factor[i][i] = sqrt(sum);
  61. } else {
  62. factor[j][i] = sum / factor[i][i];
  63. }
  64. }
  65. }
  66. for (i = 0; i < count; i++) {
  67. double sum = covar_y[i + 1];
  68. for (k = i - 1; k >= 0; k--)
  69. sum -= factor[i][k] * m->coeff[0][k];
  70. m->coeff[0][i] = sum / factor[i][i];
  71. }
  72. for (j = count - 1; j >= min_order; j--) {
  73. for (i = j; i >= 0; i--) {
  74. double sum = m->coeff[0][i];
  75. for (k = i + 1; k <= j; k++)
  76. sum -= factor[k][i] * m->coeff[j][k];
  77. m->coeff[j][i] = sum / factor[i][i];
  78. }
  79. m->variance[j] = covar_y[0];
  80. for (i = 0; i <= j; i++) {
  81. double sum = m->coeff[j][i] * covar[i][i] - 2 * covar_y[i + 1];
  82. for (k = 0; k < i; k++)
  83. sum += 2 * m->coeff[j][k] * covar[k][i];
  84. m->variance[j] += m->coeff[j][i] * sum;
  85. }
  86. }
  87. }
  88. double avpriv_evaluate_lls(LLSModel *m, double *param, int order)
  89. {
  90. int i;
  91. double out = 0;
  92. for (i = 0; i <= order; i++)
  93. out += param[i] * m->coeff[order][i];
  94. return out;
  95. }
  96. #if FF_API_LLS_PRIVATE
  97. void av_init_lls(LLSModel *m, int indep_count)
  98. {
  99. avpriv_init_lls(m, indep_count);
  100. }
  101. void av_update_lls(LLSModel *m, double *param, double decay)
  102. {
  103. avpriv_update_lls(m, param, decay);
  104. }
  105. void av_solve_lls(LLSModel *m, double threshold, int min_order)
  106. {
  107. avpriv_solve_lls(m, threshold, min_order);
  108. }
  109. double av_evaluate_lls(LLSModel *m, double *param, int order)
  110. {
  111. return avpriv_evaluate_lls(m, param, order);
  112. }
  113. #endif /* FF_API_LLS_PRIVATE */
  114. #ifdef TEST
  115. #include <stdio.h>
  116. #include <limits.h>
  117. #include "lfg.h"
  118. int main(void)
  119. {
  120. LLSModel m;
  121. int i, order;
  122. AVLFG lfg;
  123. av_lfg_init(&lfg, 1);
  124. avpriv_init_lls(&m, 3);
  125. for (i = 0; i < 100; i++) {
  126. double var[4];
  127. double eval;
  128. var[0] = (av_lfg_get(&lfg) / (double) UINT_MAX - 0.5) * 2;
  129. var[1] = var[0] + av_lfg_get(&lfg) / (double) UINT_MAX - 0.5;
  130. var[2] = var[1] + av_lfg_get(&lfg) / (double) UINT_MAX - 0.5;
  131. var[3] = var[2] + av_lfg_get(&lfg) / (double) UINT_MAX - 0.5;
  132. avpriv_update_lls(&m, var, 0.99);
  133. avpriv_solve_lls(&m, 0.001, 0);
  134. for (order = 0; order < 3; order++) {
  135. eval = avpriv_evaluate_lls(&m, var + 1, order);
  136. printf("real:%9f order:%d pred:%9f var:%f coeffs:%f %9f %9f\n",
  137. var[0], order, eval, sqrt(m.variance[order] / (i + 1)),
  138. m.coeff[order][0], m.coeff[order][1],
  139. m.coeff[order][2]);
  140. }
  141. }
  142. return 0;
  143. }
  144. #endif