-
Notifications
You must be signed in to change notification settings - Fork 0
/
PolynomialRegression.js
411 lines (348 loc) · 11 KB
/
PolynomialRegression.js
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
MIT License
Copyright (c) 2017 mljs
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
class Matrix {
/**
* performs backward substitution on a matrix
* @param anyMatrix - a matrix that has already undergone forward substitution
* @param arr - an array that will ultimately be the final output for A0 - Ak
* @param row - last row index
* @param col - column index
* @returns {*}
*/
backwardSubstitution (anyMatrix, arr, row, col) {
if (row < 0 || col < 0) {
return arr;
}
else {
const rows = anyMatrix.length;
const cols = anyMatrix[0].length - 1;
let current = 0;
let counter = 0;
for (let i = cols - 1; i >= col; i--) {
if (i === col) {
current = anyMatrix[row][cols] / anyMatrix[row][i];
} else {
anyMatrix[row][cols] -= anyMatrix[row][i] * arr[rows - 1 - counter];
counter++;
}
}
arr[row] = current;
return this.backwardSubstitution(anyMatrix, arr, row - 1, col - 1);
}
}
/**
* Combines a square matrix with a matrix with K rows and only 1 column for GJ Elimination
* @param left
* @param right
* @returns {*[]}
*/
combineMatrices (left, right){
const rows = right.length;
const cols = left[0].length;
const returnMatrix = [];
for (let i = 0; i < rows; i++) {
returnMatrix.push([]);
for (let j = 0; j <= cols; j++) {
if (j === cols) {
returnMatrix[i][j] = right[i];
} else {
returnMatrix[i][j] = left[i][j];
}
}
}
return returnMatrix;
};
/**
* Performs forward elimination for GJ elimination to form an upper right triangle matrix
* @param anyMatrix
* @returns {*[]}
*/
forwardElimination(anyMatrix){
const rows = anyMatrix.length;
const cols = anyMatrix[0].length;
const matrix = [];
//returnMatrix = anyMatrix;
for (let i = 0; i < rows; i++) {
matrix.push([]);
for (let j = 0; j < cols; j++) {
matrix[i][j] = anyMatrix[i][j];
}
}
for (let x = 0; x < rows - 1; x++) {
for (let z = x; z < rows - 1; z++) {
const numerator = matrix[z + 1][x];
const denominator = matrix[x][x];
const result = numerator / denominator;
for (let i = 0; i < cols; i++) {
matrix[z + 1][i] = matrix[z + 1][i] - (result * matrix[x][i]);
}
}
}
return matrix;
};
/**
* THIS METHOD ACTS LIKE A CONTROLLER AND PERFORMS ALL THE NECESSARY STEPS OF GJ ELIMINATION TO PRODUCE
* THE TERMS NECESSARY FOR POLYNOMIAL REGRESSION USING THE LEAST SQUARES METHOD WHERE SUM(RESIDUALS) = 0
* @param leftMatrix
* @param rightMatrix
* @returns {*}
*/
gaussianJordanElimination(leftMatrix, rightMatrix) {
const combined = this.combineMatrices(leftMatrix, rightMatrix);
const fwdIntegration = this.forwardElimination(combined);
//NOW, FINAL STEP IS BACKWARD SUBSTITUTION WHICH RETURNS THE TERMS NECESSARY FOR POLYNOMIAL REGRESSION
return this.backwardSubstitution(fwdIntegration, [], fwdIntegration.length - 1, fwdIntegration[0].length - 2);
}
/**
* returns the identity matrix for a matrix such that anyMatrix * identitymatrix = anyMatrix
* This is useful for inverting a matrix
* @param anyMatrix
* @returns {*[]}
*/
identityMatrix (anyMatrix){
const rows = anyMatrix.length;
const cols = anyMatrix[0].length;
const identityMatrix = [[]];
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
if (j == i) {
identityMatrix[i][j] = 1;
} else {
identityMatrix[i][j] = 0;
}
}
}
return identityMatrix;
}
/**
* calculates the product of 2 matrices
* @param matrix1
* @param matrix2
* @returns {*}
*/
matrixProduct (matrix1, matrix2) {
const numCols1 = matrix1[0].length;
const numRows2 = matrix2.length;
if (numCols1 != numRows2) {
return false;
}
const product = [[]];
for (let rows = 0; rows < numRows2; rows++) {
for (let cols = 0; cols < numCols1; cols++) {
product[rows][cols] = this.doMultiplication(matrix1, matrix2, rows,
cols, numCols1);
}
}
return product;
};
/**
* performs multiplication for an individual matrix cell
* @param matrix1
* @param matrix2
* @param row
* @param col
* @param numCol
* @returns {number}
*/
doMultiplication (matrix1, matrix2, row, col, numCol) {
let counter = 0;
let result = 0;
while (counter < numCol) {
result += matrix1[row][counter] * matrix2[counter][col];
counter++;
}
return result;
}
/**
* Multiplies a row of a matrix - 1 of the fundamental matrix operations
* @param anyMatrix
* @param rowNum
* @param multiplier
* @returns {*[]}
*/
multiplyRow (anyMatrix, rowNum, multiplier){
const rows = anyMatrix.length;
const cols = anyMatrix[0].length;
const mMatrix = [[]];
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
if (i == rowNum) {
mMatrix[i][j] = anyMatrix[i][j] * multiplier;
} else {
mMatrix[i][j] = anyMatrix[i][j];
}
}
}
return mMatrix;
}
}
/**
* Simple data point object for use as a consistent data storage mechanism
* @param x
* @param y
* @constructor
*/
class DataPoint {
constructor(x,y){
this.x = x;
this.y = y;
}
}
/**
* The constructor for a PolynomialRegression object an example of it's usage is below
*
*
* var someData = [];
* someData.push(new DataPoint(0.0, 1.0));
* someData.push(new DataPoint(1.0, 3.0));
* someData.push(new DataPoint(2.0, 6.0));
* someData.push(new DataPoint(3.0, 9.0));
* someData.push(new DataPoint(4.0, 12.0));
* someData.push(new DataPoint(5.0, 15.0));
* someData.push(new DataPoint(6.0, 18.0));
*
* var poly = new PolynomialRegression(someData, 3);
* var terms = poly.getTerms();
*
* for(var i = 0; i < terms.length; i++){
* console.log("term " + i, terms[i]);
* }
* console.log(poly.predictY(terms, 5.0));
*
*
*
* @param theData
* @param degrees
* @constructor
*/
class PolynomialRegression {
/**
*
* @param {Array} list
* @param {Number} degrees
* @returns {PolynomialRegression}
*/
static read(list, degrees){
const data_points = list.map(item => {
return new DataPoint(item.x, item.y);
});
return new PolynomialRegression(data_points, degrees);
}
constructor(data_points, degrees) {
//private object variables
this.data = data_points;
this.degree = degrees;
this.matrix = new Matrix();
this.leftMatrix = [];
this.rightMatrix = [];
this.generateLeftMatrix();
this.generateRightMatrix();
}
/**
* Sums up all x coordinates raised to a power
* @param anyData
* @param power
* @returns {number}
*/
sumX (anyData, power) {
let sum = 0;
for (let i = 0; i < anyData.length; i++) {
sum += Math.pow(anyData[i].x, power);
}
return sum;
}
/**
* sums up all x * y where x is raised to a power
* @param anyData
* @param power
* @returns {number}
*/
sumXTimesY(anyData, power){
let sum = 0;
for (let i = 0; i < anyData.length; i++) {
sum += Math.pow(anyData[i].x, power) * anyData[i].y;
}
return sum;
}
/**
* Sums up all Y's raised to a power
* @param anyData
* @param power
* @returns {number}
*/
sumY (anyData, power){
let sum = 0;
for (let i = 0; i < anyData.length; i++) {
sum += Math.pow(anyData[i].y, power);
}
return sum;
}
/**
* generate the left matrix
*/
generateLeftMatrix(){
for (let i = 0; i <= this.degree; i++) {
this.leftMatrix.push([]);
for (let j = 0; j <= this.degree; j++) {
if (i === 0 && j === 0) {
this.leftMatrix[i][j] = this.data.length;
} else {
this.leftMatrix[i][j] = this.sumX(this.data, (i + j));
}
}
}
}
/**
* generates the right hand matrix
*/
generateRightMatrix(){
for (let i = 0; i <= this.degree; i++) {
if (i === 0) {
this.rightMatrix[i] = this.sumY(this.data, 1);
} else {
this.rightMatrix[i] = this.sumXTimesY(this.data, i);
}
}
}
/**
* gets the terms for a polynomial
* @returns {*}
*/
getTerms(){
return this.matrix.gaussianJordanElimination(this.leftMatrix, this.rightMatrix);
}
/**
* Predicts the Y value of a data set based on polynomial coefficients and the value of an independent variable
* @param terms
* @param x
* @returns {number}
*/
predictY(terms, x){
let result = 0;
for (let i = terms.length - 1; i >= 0; i--) {
if (i === 0) {
result += terms[i];
} else {
result += terms[i] * Math.pow(x, i);
}
}
return result;
}
}