-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlsq.cpp
252 lines (207 loc) · 4.33 KB
/
lsq.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Please see license.txt for licensing and copyright information //
// Author: Paul Zimmerman, University of Michigan //
#include "lsq.h"
using namespace std;
//do ridge regression?
void LSQ::init()
{
params = NULL;
quiet = 0;
return;
}
void LSQ::go_test()
{
printf("\n in LSQ go_test \n");
double RANDNOISE = 0.1;
N = 10;
double* y = new double[N];
for (int i=0;i<N;i++)
y[i] = i/2.0 + 0.0 + RANDNOISE/2.0-randomf(RANDNOISE);
p = 2;
double* x = new double[p*N];
for (int i=0;i<N;i++)
x[p*i+0] = i;
if (p==2)
for (int i=0;i<N;i++)
x[p*i+1] = 0.;
printf(" x: \n");
for (int i=0;i<N;i++)
{
for (int j=0;j<p;j++)
printf(" %4.3f",x[p*i+j]);
printf("\n");
}
printf(" y: \n");
for (int i=0;i<N;i++)
printf(" %4.3f\n",y[i]);
printf("\n");
double error = do_least_squares(x,p,y,N);
printf(" total unsigned error: %4.3f \n",error);
printf(" average unsigned error: %4.3f \n",error/N);
delete [] x;
delete [] y;
return;
}
double LSQ::do_least_squares(double* x0, int p0, double* y, int N0)
{
p = p0;
N = N0;
if (!quiet)
printf(" creating regression (p: %i N: %i) \n",p,N);
double* x = x0;
double* xT = new double[p*N];
trans(xT,x,p,N);
#if 0
printf(" xT: \n");
for (int i=0;i<p;i++)
{
for (int j=0;j<N;j++)
printf(" %4.3f",xT[i*N+j]);
printf("\n");
}
#endif
double* xTx = new double[p*p];
for (int i=0;i<p*p;i++) xTx[i] = 0.;
for (int i=0;i<p;i++)
for (int j=0;j<p;j++)
for (int k=0;k<N;k++)
xTx[i*p+j] += xT[i*N+k] * x[k*p+j];
#if 0
printf(" xTx: \n");
for (int i=0;i<p;i++)
{
for (int j=0;j<p;j++)
printf(" %4.3f",xTx[i*p+j]);
printf("\n");
}
#endif
//adjust zero diagonals so invert works
for (int i=0;i<p;i++)
if (fabs(xTx[i*p+i])<0.000001)
xTx[i*p+i] = 10000.;
#if 0
//debug
for (int i=0;i<p*p;i++)
xTx[i] = 0.;
for (int i=0;i<p;i++)
xTx[i*p+i] = 1.;
#endif
#if 1
//debug
double* tmp = new double[p*p];
for (int i=0;i<p*p;i++) tmp[i] = xTx[i];
double* eig = new double[p];
for (int i=0;i<p;i++) eig[i] = 0.;
Diagonalize(tmp,eig,p);
if (!quiet)
{
printf("\n eigenvalues:");
for (int i=0;i<p;i++)
printf(" %8.6f",eig[i]);
printf("\n");
}
int zeroeig = 0;
for (int i=0;i<p;i++)
if (fabs(eig[i])<0.0000001)
zeroeig++;
if (zeroeig)
{
if (!quiet)
{
printf(" found zero eigenvalue(s) (%i), cannot invert \n",zeroeig);
printf(" p: %i N: %i \n",p,N);
}
delete [] xT;
delete [] xTx;
delete [] tmp;
delete [] eig;
return 99999.;
}
delete [] tmp;
delete [] eig;
#endif
int error1 = 0;
if (p>1) error1 = Invert(xTx,p);
else xTx[0] = 1./xTx[0];
if (error1)
{
printf("\n failed to invert xTx \n");
return 99999.;
}
#if 0
printf(" xTxi: \n");
for (int i=0;i<p;i++)
{
for (int j=0;j<p;j++)
printf(" %4.3f",xTx[i*p+j]);
printf("\n");
}
fflush(stdout);
#endif
double* A = new double[p*N];
for (int i=0;i<p*N;i++) A[i] = 0.;
for (int i=0;i<p;i++)
for (int j=0;j<N;j++)
for (int k=0;k<p;k++)
A[i*N+j] += xTx[i*p+k] * xT[k*N+j];
#if 0
printf(" A: \n");
for (int i=0;i<p;i++)
{
for (int j=0;j<N;j++)
printf(" %4.3f",A[i*N+j]);
printf("\n");
}
#endif
if (params!=NULL) delete [] params;
params = new double[p];
for (int i=0;i<p;i++) params[i] = 0.;
for (int i=0;i<p;i++)
for (int k=0;k<N;k++)
params[i] += A[i*N+k] * y[k];
if (!quiet)
{
printf("\n params:");
for (int i=0;i<p;i++)
printf(" %6.4f",params[i]);
printf("\n");
fflush(stdout);
}
if (!quiet)
{
int non_contribute = 0;
for (int i=0;i<p;i++)
if (close_val(params[i],0.,0.00001))
non_contribute++;
printf(" %i parameters doing nothing \n\n",non_contribute);
}
double error = 0.;
double err,val;
for (int i=0;i<N;i++)
{
val = estimate_point(&x[p*i]);
#if 1
err = val-y[i];
error += err*err*(1+y[i]); //weighted error
#elif 0
err = val-y[i];
error += err*err;
#else
error += fabs(val-y[i]);
#endif
if (!quiet)
printf(" actual pred: %6.3f %6.3f \n",y[i],val);
}
error = sqrt(error / N);
delete [] A;
delete [] xTx;
delete [] xT;
return error;
}
double LSQ::estimate_point(double* x)
{
double val = 0.;
for (int j=0;j<p;j++)
val += x[j] * params[j];
return val;
}