Skip to content

Commit

Permalink
feat: add xBackwardLinearPrediction
Browse files Browse the repository at this point in the history
  • Loading branch information
jobo322 committed Sep 26, 2023
1 parent bcde023 commit ff4dc11
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/x/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,4 @@ export * from './xSubtract';
export * from './xSum';
export * from './xUniqueSorted';
export * from './xVariance';
export * from './xBackwardLinearPrediction';
64 changes: 64 additions & 0 deletions src/x/xBackwardLinearPrediction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { DoubleArray } from 'cheminfo-types';
import { SVD, Matrix } from 'ml-matrix';

export interface BackwardLPOptions {
/**
* Number of coefficients to be calculated in the SVD.
*/
nbCoefficients: number;
/**
* Number of points used in the prediction.
*/
nbInputs: number;
/**
* Number of points to predict
*/
nbPoints: number;
/**
* Output array that could be used for in-place modification.
*/
output?: Float64Array[]
}


/**
* Predict back points by singular value decomposition.
* to append the predicted points is it needed to append nbPoints zeros at the beginning of input data.
*/
export function xBackwardLinearPrediction(
data: DoubleArray,
options: BackwardLPOptions,

Check warning on line 30 in src/x/xBackwardLinearPrediction.ts

View check run for this annotation

Codecov / codecov/patch

src/x/xBackwardLinearPrediction.ts#L30

Added line #L30 was not covered by tests
) {
const {
nbCoefficients,
nbInputs,
nbPoints,
} = options;

Check warning on line 36 in src/x/xBackwardLinearPrediction.ts

View check run for this annotation

Codecov / codecov/patch

src/x/xBackwardLinearPrediction.ts#L36

Added line #L36 was not covered by tests

const lpMatrix: Float64Array[] = [];
for (let i = 0; i < nbInputs; i++) {
const row = new Float64Array(nbCoefficients);
for (let j = 0; j < nbCoefficients; j++) {
row[j] = data[i + j + nbPoints + 1];

Check warning on line 42 in src/x/xBackwardLinearPrediction.ts

View check run for this annotation

Codecov / codecov/patch

src/x/xBackwardLinearPrediction.ts#L38-L42

Added lines #L38 - L42 were not covered by tests
}
lpMatrix.push(row);

Check warning on line 44 in src/x/xBackwardLinearPrediction.ts

View check run for this annotation

Codecov / codecov/patch

src/x/xBackwardLinearPrediction.ts#L44

Added line #L44 was not covered by tests
}
const svd = new SVD(lpMatrix);
const dataInput = new Float64Array(nbInputs);
for (let i = 0; i < nbInputs; i++) {
dataInput[i] = data[i + nbPoints];

Check warning on line 49 in src/x/xBackwardLinearPrediction.ts

View check run for this annotation

Codecov / codecov/patch

src/x/xBackwardLinearPrediction.ts#L46-L49

Added lines #L46 - L49 were not covered by tests
}
const coefficients = svd

Check warning on line 51 in src/x/xBackwardLinearPrediction.ts

View check run for this annotation

Codecov / codecov/patch

src/x/xBackwardLinearPrediction.ts#L51

Added line #L51 was not covered by tests
.solve(Matrix.from1DArray(dataInput.length, 1, dataInput))
.to1DArray();

const { output = Float64Array.from(data) } = options;
for (let m = 0; m < nbPoints; m++) {
let sum = 0;
for (let i = 0; i < coefficients.length; i++) {
sum += coefficients[i] * data[i + nbPoints - m];

Check warning on line 59 in src/x/xBackwardLinearPrediction.ts

View check run for this annotation

Codecov / codecov/patch

src/x/xBackwardLinearPrediction.ts#L56-L59

Added lines #L56 - L59 were not covered by tests
}
output[nbPoints - m - 1] = sum;

Check warning on line 61 in src/x/xBackwardLinearPrediction.ts

View check run for this annotation

Codecov / codecov/patch

src/x/xBackwardLinearPrediction.ts#L61

Added line #L61 was not covered by tests
}
return { output, predicted: output.slice(0, nbPoints) };

Check warning on line 63 in src/x/xBackwardLinearPrediction.ts

View check run for this annotation

Codecov / codecov/patch

src/x/xBackwardLinearPrediction.ts#L63

Added line #L63 was not covered by tests
}

0 comments on commit ff4dc11

Please sign in to comment.