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.

142 lines
3.6KB

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