You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
4.6KB

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