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.

180 lines
4.7KB

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