annotate lls.c @ 102:262c44b493f9 libavutil

compatibility warning
author michael
date Sun, 20 Aug 2006 18:10:45 +0000
parents b916c714f77b
children d76a36742464
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
76
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
1 /*
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
2 * linear least squares model
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
3 *
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
4 * Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
5 *
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
6 * This library is free software; you can redistribute it and/or
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
7 * modify it under the terms of the GNU Lesser General Public
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
8 * License as published by the Free Software Foundation; either
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
9 * version 2 of the License, or (at your option) any later version.
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
10 *
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
11 * This library is distributed in the hope that it will be useful,
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
14 * Lesser General Public License for more details.
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
15 *
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
16 * You should have received a copy of the GNU Lesser General Public
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
17 * License along with this library; if not, write to the Free Software
90
b916c714f77b Fix FSF postal address.
diego
parents: 79
diff changeset
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
76
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
19 */
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
20
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
21 /**
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
22 * @file lls.c
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
23 * linear least squares model
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
24 */
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
25
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
26 #include <math.h>
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
27 #include <string.h>
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
28
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
29 #include "lls.h"
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
30
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
31 #ifdef TEST
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
32 #define av_log(a,b,...) printf(__VA_ARGS__)
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
33 #endif
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
34
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
35 void av_init_lls(LLSModel *m, int indep_count){
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
36 memset(m, 0, sizeof(LLSModel));
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
37
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
38 m->indep_count= indep_count;
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
39 }
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
40
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
41 void av_update_lls(LLSModel *m, double *var, double decay){
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
42 int i,j;
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
43
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
44 for(i=0; i<=m->indep_count; i++){
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
45 for(j=i; j<=m->indep_count; j++){
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
46 m->covariance[i][j] *= decay;
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
47 m->covariance[i][j] += var[i]*var[j];
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
48 }
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
49 }
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
50 }
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
51
79
adbb5540fa47 calculate all coefficients for several orders during cholesky factorization, the resulting coefficients are not strictly optimal though as there is a small difference in the autocorrelation matrixes which is ignored for the smaller orders
michael
parents: 78
diff changeset
52 void av_solve_lls(LLSModel *m, double threshold, int min_order){
76
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
53 int i,j,k;
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
54 double (*factor)[MAX_VARS+1]= &m->covariance[1][0];
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
55 double (*covar )[MAX_VARS+1]= &m->covariance[1][1];
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
56 double *covar_y = m->covariance[0];
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
57 int count= m->indep_count;
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
58
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
59 for(i=0; i<count; i++){
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
60 for(j=i; j<count; j++){
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
61 double sum= covar[i][j];
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
62
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
63 for(k=i-1; k>=0; k--)
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
64 sum -= factor[i][k]*factor[j][k];
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
65
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
66 if(i==j){
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
67 if(sum < threshold)
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
68 sum= 1.0;
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
69 factor[i][i]= sqrt(sum);
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
70 }else
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
71 factor[j][i]= sum / factor[i][i];
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
72 }
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
73 }
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
74 for(i=0; i<count; i++){
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
75 double sum= covar_y[i+1];
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
76 for(k=i-1; k>=0; k--)
79
adbb5540fa47 calculate all coefficients for several orders during cholesky factorization, the resulting coefficients are not strictly optimal though as there is a small difference in the autocorrelation matrixes which is ignored for the smaller orders
michael
parents: 78
diff changeset
77 sum -= factor[i][k]*m->coeff[0][k];
adbb5540fa47 calculate all coefficients for several orders during cholesky factorization, the resulting coefficients are not strictly optimal though as there is a small difference in the autocorrelation matrixes which is ignored for the smaller orders
michael
parents: 78
diff changeset
78 m->coeff[0][i]= sum / factor[i][i];
76
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
79 }
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
80
79
adbb5540fa47 calculate all coefficients for several orders during cholesky factorization, the resulting coefficients are not strictly optimal though as there is a small difference in the autocorrelation matrixes which is ignored for the smaller orders
michael
parents: 78
diff changeset
81 for(j=count-1; j>=min_order; j--){
adbb5540fa47 calculate all coefficients for several orders during cholesky factorization, the resulting coefficients are not strictly optimal though as there is a small difference in the autocorrelation matrixes which is ignored for the smaller orders
michael
parents: 78
diff changeset
82 for(i=j; i>=0; i--){
adbb5540fa47 calculate all coefficients for several orders during cholesky factorization, the resulting coefficients are not strictly optimal though as there is a small difference in the autocorrelation matrixes which is ignored for the smaller orders
michael
parents: 78
diff changeset
83 double sum= m->coeff[0][i];
adbb5540fa47 calculate all coefficients for several orders during cholesky factorization, the resulting coefficients are not strictly optimal though as there is a small difference in the autocorrelation matrixes which is ignored for the smaller orders
michael
parents: 78
diff changeset
84 for(k=i+1; k<=j; k++)
adbb5540fa47 calculate all coefficients for several orders during cholesky factorization, the resulting coefficients are not strictly optimal though as there is a small difference in the autocorrelation matrixes which is ignored for the smaller orders
michael
parents: 78
diff changeset
85 sum -= factor[k][i]*m->coeff[j][k];
adbb5540fa47 calculate all coefficients for several orders during cholesky factorization, the resulting coefficients are not strictly optimal though as there is a small difference in the autocorrelation matrixes which is ignored for the smaller orders
michael
parents: 78
diff changeset
86 m->coeff[j][i]= sum / factor[i][i];
adbb5540fa47 calculate all coefficients for several orders during cholesky factorization, the resulting coefficients are not strictly optimal though as there is a small difference in the autocorrelation matrixes which is ignored for the smaller orders
michael
parents: 78
diff changeset
87 }
adbb5540fa47 calculate all coefficients for several orders during cholesky factorization, the resulting coefficients are not strictly optimal though as there is a small difference in the autocorrelation matrixes which is ignored for the smaller orders
michael
parents: 78
diff changeset
88
adbb5540fa47 calculate all coefficients for several orders during cholesky factorization, the resulting coefficients are not strictly optimal though as there is a small difference in the autocorrelation matrixes which is ignored for the smaller orders
michael
parents: 78
diff changeset
89 m->variance[j]= covar_y[0];
adbb5540fa47 calculate all coefficients for several orders during cholesky factorization, the resulting coefficients are not strictly optimal though as there is a small difference in the autocorrelation matrixes which is ignored for the smaller orders
michael
parents: 78
diff changeset
90 for(i=0; i<=j; i++){
adbb5540fa47 calculate all coefficients for several orders during cholesky factorization, the resulting coefficients are not strictly optimal though as there is a small difference in the autocorrelation matrixes which is ignored for the smaller orders
michael
parents: 78
diff changeset
91 double sum= m->coeff[j][i]*covar[i][i] - 2*covar_y[i+1];
adbb5540fa47 calculate all coefficients for several orders during cholesky factorization, the resulting coefficients are not strictly optimal though as there is a small difference in the autocorrelation matrixes which is ignored for the smaller orders
michael
parents: 78
diff changeset
92 for(k=0; k<i; k++)
adbb5540fa47 calculate all coefficients for several orders during cholesky factorization, the resulting coefficients are not strictly optimal though as there is a small difference in the autocorrelation matrixes which is ignored for the smaller orders
michael
parents: 78
diff changeset
93 sum += 2*m->coeff[j][k]*covar[k][i];
adbb5540fa47 calculate all coefficients for several orders during cholesky factorization, the resulting coefficients are not strictly optimal though as there is a small difference in the autocorrelation matrixes which is ignored for the smaller orders
michael
parents: 78
diff changeset
94 m->variance[j] += m->coeff[j][i]*sum;
adbb5540fa47 calculate all coefficients for several orders during cholesky factorization, the resulting coefficients are not strictly optimal though as there is a small difference in the autocorrelation matrixes which is ignored for the smaller orders
michael
parents: 78
diff changeset
95 }
76
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
96 }
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
97 }
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
98
79
adbb5540fa47 calculate all coefficients for several orders during cholesky factorization, the resulting coefficients are not strictly optimal though as there is a small difference in the autocorrelation matrixes which is ignored for the smaller orders
michael
parents: 78
diff changeset
99 double av_evaluate_lls(LLSModel *m, double *param, int order){
76
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
100 int i;
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
101 double out= 0;
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
102
79
adbb5540fa47 calculate all coefficients for several orders during cholesky factorization, the resulting coefficients are not strictly optimal though as there is a small difference in the autocorrelation matrixes which is ignored for the smaller orders
michael
parents: 78
diff changeset
103 for(i=0; i<=order; i++)
adbb5540fa47 calculate all coefficients for several orders during cholesky factorization, the resulting coefficients are not strictly optimal though as there is a small difference in the autocorrelation matrixes which is ignored for the smaller orders
michael
parents: 78
diff changeset
104 out+= param[i]*m->coeff[order][i];
76
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
105
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
106 return out;
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
107 }
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
108
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
109 #ifdef TEST
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
110
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
111 #include <stdlib.h>
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
112 #include <stdio.h>
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
113
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
114 int main(){
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
115 LLSModel m;
79
adbb5540fa47 calculate all coefficients for several orders during cholesky factorization, the resulting coefficients are not strictly optimal though as there is a small difference in the autocorrelation matrixes which is ignored for the smaller orders
michael
parents: 78
diff changeset
116 int i, order;
76
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
117
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
118 av_init_lls(&m, 3);
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
119
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
120 for(i=0; i<100; i++){
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
121 double var[4];
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
122 double eval, variance;
79
adbb5540fa47 calculate all coefficients for several orders during cholesky factorization, the resulting coefficients are not strictly optimal though as there is a small difference in the autocorrelation matrixes which is ignored for the smaller orders
michael
parents: 78
diff changeset
123 #if 0
76
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
124 var[1] = rand() / (double)RAND_MAX;
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
125 var[2] = rand() / (double)RAND_MAX;
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
126 var[3] = rand() / (double)RAND_MAX;
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
127
79
adbb5540fa47 calculate all coefficients for several orders during cholesky factorization, the resulting coefficients are not strictly optimal though as there is a small difference in the autocorrelation matrixes which is ignored for the smaller orders
michael
parents: 78
diff changeset
128 var[2]= var[1] + var[3]/2;
76
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
129
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
130 var[0] = var[1] + var[2] + var[3] + var[1]*var[2]/100;
79
adbb5540fa47 calculate all coefficients for several orders during cholesky factorization, the resulting coefficients are not strictly optimal though as there is a small difference in the autocorrelation matrixes which is ignored for the smaller orders
michael
parents: 78
diff changeset
131 #else
adbb5540fa47 calculate all coefficients for several orders during cholesky factorization, the resulting coefficients are not strictly optimal though as there is a small difference in the autocorrelation matrixes which is ignored for the smaller orders
michael
parents: 78
diff changeset
132 var[0] = (rand() / (double)RAND_MAX - 0.5)*2;
adbb5540fa47 calculate all coefficients for several orders during cholesky factorization, the resulting coefficients are not strictly optimal though as there is a small difference in the autocorrelation matrixes which is ignored for the smaller orders
michael
parents: 78
diff changeset
133 var[1] = var[0] + rand() / (double)RAND_MAX - 0.5;
adbb5540fa47 calculate all coefficients for several orders during cholesky factorization, the resulting coefficients are not strictly optimal though as there is a small difference in the autocorrelation matrixes which is ignored for the smaller orders
michael
parents: 78
diff changeset
134 var[2] = var[1] + rand() / (double)RAND_MAX - 0.5;
adbb5540fa47 calculate all coefficients for several orders during cholesky factorization, the resulting coefficients are not strictly optimal though as there is a small difference in the autocorrelation matrixes which is ignored for the smaller orders
michael
parents: 78
diff changeset
135 var[3] = var[2] + rand() / (double)RAND_MAX - 0.5;
adbb5540fa47 calculate all coefficients for several orders during cholesky factorization, the resulting coefficients are not strictly optimal though as there is a small difference in the autocorrelation matrixes which is ignored for the smaller orders
michael
parents: 78
diff changeset
136 #endif
76
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
137 av_update_lls(&m, var, 0.99);
79
adbb5540fa47 calculate all coefficients for several orders during cholesky factorization, the resulting coefficients are not strictly optimal though as there is a small difference in the autocorrelation matrixes which is ignored for the smaller orders
michael
parents: 78
diff changeset
138 av_solve_lls(&m, 0.001, 0);
adbb5540fa47 calculate all coefficients for several orders during cholesky factorization, the resulting coefficients are not strictly optimal though as there is a small difference in the autocorrelation matrixes which is ignored for the smaller orders
michael
parents: 78
diff changeset
139 for(order=0; order<3; order++){
adbb5540fa47 calculate all coefficients for several orders during cholesky factorization, the resulting coefficients are not strictly optimal though as there is a small difference in the autocorrelation matrixes which is ignored for the smaller orders
michael
parents: 78
diff changeset
140 eval= av_evaluate_lls(&m, var+1, order);
adbb5540fa47 calculate all coefficients for several orders during cholesky factorization, the resulting coefficients are not strictly optimal though as there is a small difference in the autocorrelation matrixes which is ignored for the smaller orders
michael
parents: 78
diff changeset
141 av_log(NULL, AV_LOG_DEBUG, "real:%f order:%d pred:%f var:%f coeffs:%f %f %f\n",
adbb5540fa47 calculate all coefficients for several orders during cholesky factorization, the resulting coefficients are not strictly optimal though as there is a small difference in the autocorrelation matrixes which is ignored for the smaller orders
michael
parents: 78
diff changeset
142 var[0], order, eval, sqrt(m.variance[order] / (i+1)),
adbb5540fa47 calculate all coefficients for several orders during cholesky factorization, the resulting coefficients are not strictly optimal though as there is a small difference in the autocorrelation matrixes which is ignored for the smaller orders
michael
parents: 78
diff changeset
143 m.coeff[order][0], m.coeff[order][1], m.coeff[order][2]);
adbb5540fa47 calculate all coefficients for several orders during cholesky factorization, the resulting coefficients are not strictly optimal though as there is a small difference in the autocorrelation matrixes which is ignored for the smaller orders
michael
parents: 78
diff changeset
144 }
76
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
145 }
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
146 return 0;
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
147 }
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
148
8c75234388b5 linear least squares solver using cholesky factorization
michael
parents:
diff changeset
149 #endif