-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathKOPLS.js
295 lines (255 loc) · 9.24 KB
/
KOPLS.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
import { Matrix, SingularValueDecomposition, inverse } from 'ml-matrix';
import { initializeMatrices } from './util/utils';
/**
* @class KOPLS
*/
export class KOPLS {
/**
* Constructor for Kernel-based Orthogonal Projections to Latent Structures (K-OPLS)
* @param {object} options
* @param {number} [options.predictiveComponents] - Number of predictive components to use.
* @param {number} [options.orthogonalComponents] - Number of Y-Orthogonal components.
* @param {Kernel} [options.kernel] - Kernel object to apply, see [ml-kernel](https://github.com/mljs/kernel).
* @param {object} model - for load purposes.
*/
constructor(options, model) {
if (options === true) {
this.trainingSet = new Matrix(model.trainingSet);
this.YLoadingMat = new Matrix(model.YLoadingMat);
this.SigmaPow = new Matrix(model.SigmaPow);
this.YScoreMat = new Matrix(model.YScoreMat);
this.predScoreMat = initializeMatrices(model.predScoreMat, false);
this.YOrthLoadingVec = initializeMatrices(model.YOrthLoadingVec, false);
this.YOrthEigen = model.YOrthEigen;
this.YOrthScoreMat = initializeMatrices(model.YOrthScoreMat, false);
this.toNorm = initializeMatrices(model.toNorm, false);
this.TURegressionCoeff = initializeMatrices(
model.TURegressionCoeff,
false,
);
this.kernelX = initializeMatrices(model.kernelX, true);
this.kernel = model.kernel;
this.orthogonalComp = model.orthogonalComp;
this.predictiveComp = model.predictiveComp;
} else {
if (options.predictiveComponents === undefined) {
throw new RangeError('no predictive components found!');
}
if (options.orthogonalComponents === undefined) {
throw new RangeError('no orthogonal components found!');
}
if (options.kernel === undefined) {
throw new RangeError('no kernel found!');
}
this.orthogonalComp = options.orthogonalComponents;
this.predictiveComp = options.predictiveComponents;
this.kernel = options.kernel;
}
}
/**
* Train the K-OPLS model with the given training set and labels.
* @param {Matrix|Array} trainingSet
* @param {Matrix|Array} trainingValues
*/
train(trainingSet, trainingValues) {
trainingSet = Matrix.checkMatrix(trainingSet);
trainingValues = Matrix.checkMatrix(trainingValues);
// to save and compute kernel with the prediction dataset.
this.trainingSet = trainingSet.clone();
let kernelX = this.kernel.compute(trainingSet);
let Identity = Matrix.eye(kernelX.rows, kernelX.rows, 1);
let temp = kernelX;
kernelX = new Array(this.orthogonalComp + 1);
for (let i = 0; i < this.orthogonalComp + 1; i++) {
kernelX[i] = new Array(this.orthogonalComp + 1);
}
kernelX[0][0] = temp;
let result = new SingularValueDecomposition(
trainingValues.transpose().mmul(kernelX[0][0]).mmul(trainingValues),
{
computeLeftSingularVectors: true,
computeRightSingularVectors: false,
},
);
let YLoadingMat = result.leftSingularVectors;
let Sigma = result.diagonalMatrix;
YLoadingMat = YLoadingMat.subMatrix(
0,
YLoadingMat.rows - 1,
0,
this.predictiveComp - 1,
);
Sigma = Sigma.subMatrix(
0,
this.predictiveComp - 1,
0,
this.predictiveComp - 1,
);
let YScoreMat = trainingValues.mmul(YLoadingMat);
let predScoreMat = new Array(this.orthogonalComp + 1);
let TURegressionCoeff = new Array(this.orthogonalComp + 1);
let YOrthScoreMat = new Array(this.orthogonalComp);
let YOrthLoadingVec = new Array(this.orthogonalComp);
let YOrthEigen = new Array(this.orthogonalComp);
let YOrthScoreNorm = new Array(this.orthogonalComp);
let SigmaPow = Matrix.pow(Sigma, -0.5);
// to avoid errors, check infinity
function removeInfinity(i, j) {
if (this.get(i, j) === Infinity) {
this.set(i, j, 0);
}
}
SigmaPow.apply(removeInfinity);
for (let i = 0; i < this.orthogonalComp; ++i) {
predScoreMat[i] = kernelX[0][i]
.transpose()
.mmul(YScoreMat)
.mmul(SigmaPow);
let TpiPrime = predScoreMat[i].transpose();
TURegressionCoeff[i] = inverse(TpiPrime.mmul(predScoreMat[i]))
.mmul(TpiPrime)
.mmul(YScoreMat);
result = new SingularValueDecomposition(
TpiPrime.mmul(
Matrix.sub(kernelX[i][i], predScoreMat[i].mmul(TpiPrime)),
).mmul(predScoreMat[i]),
{
computeLeftSingularVectors: true,
computeRightSingularVectors: false,
},
);
let CoTemp = result.leftSingularVectors;
let SoTemp = result.diagonalMatrix;
YOrthLoadingVec[i] = CoTemp.subMatrix(0, CoTemp.rows - 1, 0, 0);
YOrthEigen[i] = SoTemp.get(0, 0);
YOrthScoreMat[i] = Matrix.sub(
kernelX[i][i],
predScoreMat[i].mmul(TpiPrime),
)
.mmul(predScoreMat[i])
.mmul(YOrthLoadingVec[i])
.mul(Math.pow(YOrthEigen[i], -0.5));
let toiPrime = YOrthScoreMat[i].transpose();
YOrthScoreNorm[i] = Matrix.sqrt(toiPrime.mmul(YOrthScoreMat[i]));
YOrthScoreMat[i] = YOrthScoreMat[i].divRowVector(YOrthScoreNorm[i]);
let ITo = Matrix.sub(
Identity,
YOrthScoreMat[i].mmul(YOrthScoreMat[i].transpose()),
);
kernelX[0][i + 1] = kernelX[0][i].mmul(ITo);
kernelX[i + 1][i + 1] = ITo.mmul(kernelX[i][i]).mmul(ITo);
}
let lastScoreMat = kernelX[0][this.orthogonalComp]
.transpose()
.mmul(YScoreMat)
.mmul(SigmaPow);
predScoreMat[this.orthogonalComp] = lastScoreMat.clone();
let lastTpPrime = lastScoreMat.transpose();
TURegressionCoeff[this.orthogonalComp] = inverse(
lastTpPrime.mmul(lastScoreMat),
)
.mmul(lastTpPrime)
.mmul(YScoreMat);
this.YLoadingMat = YLoadingMat;
this.SigmaPow = SigmaPow;
this.YScoreMat = YScoreMat;
this.predScoreMat = predScoreMat;
this.YOrthLoadingVec = YOrthLoadingVec;
this.YOrthEigen = YOrthEigen;
this.YOrthScoreMat = YOrthScoreMat;
this.toNorm = YOrthScoreNorm;
this.TURegressionCoeff = TURegressionCoeff;
this.kernelX = kernelX;
}
/**
* Predicts the output given the matrix to predict.
* @param {Matrix|Array} toPredict
* @return {{y: Matrix, predScoreMat: Array<Matrix>, predYOrthVectors: Array<Matrix>}} predictions
*/
predict(toPredict) {
let KTestTrain = this.kernel.compute(toPredict, this.trainingSet);
let temp = KTestTrain;
KTestTrain = new Array(this.orthogonalComp + 1);
for (let i = 0; i < this.orthogonalComp + 1; i++) {
KTestTrain[i] = new Array(this.orthogonalComp + 1);
}
KTestTrain[0][0] = temp;
let YOrthScoreVector = new Array(this.orthogonalComp);
let predScoreMat = new Array(this.orthogonalComp);
let i;
for (i = 0; i < this.orthogonalComp; ++i) {
predScoreMat[i] = KTestTrain[i][0]
.mmul(this.YScoreMat)
.mmul(this.SigmaPow);
YOrthScoreVector[i] = Matrix.sub(
KTestTrain[i][i],
predScoreMat[i].mmul(this.predScoreMat[i].transpose()),
)
.mmul(this.predScoreMat[i])
.mmul(this.YOrthLoadingVec[i])
.mul(Math.pow(this.YOrthEigen[i], -0.5));
YOrthScoreVector[i] = YOrthScoreVector[i].divRowVector(this.toNorm[i]);
let scoreMatPrime = this.YOrthScoreMat[i].transpose();
KTestTrain[i + 1][0] = Matrix.sub(
KTestTrain[i][0],
YOrthScoreVector[i]
.mmul(scoreMatPrime)
.mmul(this.kernelX[0][i].transpose()),
);
let p1 = Matrix.sub(
KTestTrain[i][0],
KTestTrain[i][i].mmul(this.YOrthScoreMat[i]).mmul(scoreMatPrime),
);
let p2 = YOrthScoreVector[i].mmul(scoreMatPrime).mmul(this.kernelX[i][i]);
let p3 = p2.mmul(this.YOrthScoreMat[i]).mmul(scoreMatPrime);
KTestTrain[i + 1][i + 1] = p1.sub(p2).add(p3);
}
predScoreMat[i] = KTestTrain[i][0].mmul(this.YScoreMat).mmul(this.SigmaPow);
let prediction = predScoreMat[i]
.mmul(this.TURegressionCoeff[i])
.mmul(this.YLoadingMat.transpose());
return {
prediction,
predScoreMat,
predYOrthVectors: YOrthScoreVector,
};
}
/**
* Export the current model to JSON.
* @return {object} - Current model.
*/
toJSON() {
return {
name: 'K-OPLS',
YLoadingMat: this.YLoadingMat,
SigmaPow: this.SigmaPow,
YScoreMat: this.YScoreMat,
predScoreMat: this.predScoreMat,
YOrthLoadingVec: this.YOrthLoadingVec,
YOrthEigen: this.YOrthEigen,
YOrthScoreMat: this.YOrthScoreMat,
toNorm: this.toNorm,
TURegressionCoeff: this.TURegressionCoeff,
kernelX: this.kernelX,
trainingSet: this.trainingSet,
orthogonalComp: this.orthogonalComp,
predictiveComp: this.predictiveComp,
};
}
/**
* Load a K-OPLS with the given model.
* @param {object} model
* @param {Kernel} kernel - kernel used on the model, see [ml-kernel](https://github.com/mljs/kernel).
* @return {KOPLS}
*/
static load(model, kernel) {
if (model.name !== 'K-OPLS') {
throw new RangeError(`Invalid model: ${model.name}`);
}
if (!kernel) {
throw new RangeError('You must provide a kernel for the model!');
}
model.kernel = kernel;
return new KOPLS(true, model);
}
}