This repository was archived by the owner on Apr 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregression.js
More file actions
164 lines (125 loc) · 3.27 KB
/
Copy pathregression.js
File metadata and controls
164 lines (125 loc) · 3.27 KB
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
(function() {
"use strict";
var linear, sinusoidal, regression;
linear = (function() {
var solve;
/* Solve Ax=B for x. A should be an NxN square matrix, B should be an Nx1
* column vector. */
/* FIXME: This is solved using Cramer's rule naively, which is (hideously)
* inefficient and exhibits numerical stability issues. This solver should
* be replaced with, for example, something like this:
* http://web.eecs.utk.edu/~itamar/Papers/JDA2011.pdf */
solve = (function() {
var determinant;
determinant = function(a, n) {
var b, d, i, j, m, s, x, y;
switch(n >>> 0) {
case 0:
return NaN;
case 1:
return 1.0 / a[0];
case 2:
return a[0] * a[3] - a[1] * a[2];
default:
d = 0.0;
m = n - 1;
b = new Array(m * m);
s = 1.0;
for(i = 0; i < n; i++) {
j = 0;
for(y = 1; y < n; y++) {
for(x = 0; x < n; x++) {
if(x === i) {
continue;
}
b[j++] = a[x + y * n];
}
}
d += s * a[i] * determinant(b, m);
s = -s;
}
return d;
}
};
return function(a, b) {
var d, i, j, n, t, x;
n = b.length;
d = determinant(a, n);
x = new Array(n);
t = new Array(n);
for(i = n; i--; ) {
for(j = n; j--; ) {
t[j] = a[i + j * n];
a[i + j * n] = b[j];
}
x[i] = determinant(a, n) / d;
for(j = n; j--; ) {
a[i + j * n] = t[j];
}
}
return x;
};
})();
return function(x, y, m) {
var a, b, c, i, j, k, n;
n = y.length;
i = m * m;
a = new Array(i);
while(i--) {
a[i] = 0.0;
}
i = m;
b = new Array(i);
while(i--) {
b[i] = 0.0;
}
for(i = n; i--; ) {
for(j = m; j--; ) {
for(k = m; k--; ) {
a[k + j * m] += x[k + i * m] * x[j + i * m];
}
b[j] += x[j + i * m] * y[i];
}
}
return solve(a, b);
};
})();
sinusoidal = function(x, y, frequency, phase) {
var a, b, i, fit, u;
frequency = +frequency;
phase = +phase;
if(isFinite(phase)) {
u = new Array(x.length * 2);
for(i = x.length; i--; ) {
u[i * 2 + 0] = 1.0;
u[i * 2 + 1] = Math.sin(x[i] * frequency + phase);
}
fit = linear(u, y, 2);
fit.push(phase, frequency);
}
else {
u = new Array(x.length * 3);
for(i = x.length; i--; ) {
u[i * 3 + 0] = 1.0;
u[i * 3 + 1] = Math.sin(x[i] * frequency);
u[i * 3 + 2] = Math.cos(x[i] * frequency);
}
fit = linear(u, y, 3);
fit.push(frequency);
a = fit[1];
b = fit[2];
fit[1] = Math.sqrt(a * a + b * b);
fit[2] = Math.atan2(b, a);
}
return fit;
};
if(typeof exports !== "undefined") {
regression = exports;
}
else {
regression = {};
window.regression = regression;
}
regression.linear = linear;
regression.sinusoidal = sinusoidal;
})();