Skip to content

Commit

Permalink
feat: add NIPALS loop for factorization (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwist authored and targos committed Jul 20, 2019
1 parent 47ab51a commit 043c8b6
Show file tree
Hide file tree
Showing 10 changed files with 381 additions and 46 deletions.
1 change: 1 addition & 0 deletions data/irisPC1-4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[[-2.2571,-2.074,-2.3563,-2.2917,-2.3819,-2.0687,-2.4359,-2.2254,-2.3268,-2.177,-2.1591,-2.3184,-2.211,-2.6243,-2.1914,-2.2547,-2.2002,-2.183,-1.8922,-2.3355,-1.9079,-2.1996,-2.7651,-1.8126,-2.2197,-1.9453,-2.0443,-2.1613,-2.1324,-2.2577,-2.133,-1.8255,-2.6062,-2.438,-2.1029,-2.2004,-2.0383,-2.5189,-2.4215,-2.1625,-2.2788,-1.8519,-2.5451,-1.9579,-2.1299,-2.0628,-2.3768,-2.3864,-2.222,-2.1965,1.0981,0.7289,1.2368,0.4061,1.0719,0.3874,0.744,-0.4857,0.9248,0.0114,-0.1098,0.4392,0.5602,0.7172,-0.0332,0.8725,0.3491,0.1583,1.221,0.1644,0.7352,0.4747,1.2301,0.6307,0.7003,0.8714,1.2523,1.3539,0.6626,-0.0401,0.1304,0.0234,0.2407,1.0576,0.2232,0.4277,1.0452,1.041,0.0694,0.2825,0.2781,0.6225,0.3354,-0.361,0.2876,0.0911,0.227,0.5745,-0.4462,0.2559,1.8384,1.154,2.1979,1.4353,1.8616,2.7427,0.3658,2.2948,2,2.2522,1.3596,1.5973,1.8776,1.2559,1.4627,1.5848,1.4665,2.4182,3.2996,1.2595,2.0309,0.9747,2.888,1.3288,1.6951,1.9478,1.1712,1.0175,1.7824,1.8574,2.4278,2.2972,1.8565,1.1104,1.1985,2.7894,1.571,1.3418,0.9217,1.8459,2.0081,1.8954,1.154,2.0337,1.9915,1.8643,1.5594,1.5161,1.3682,0.9574],[-0.4784,0.6719,0.3408,0.5954,-0.6447,-1.4842,-0.0475,-0.2224,1.1116,0.4674,-1.0402,-0.1326,0.7262,0.9583,-1.8538,-2.6773,-1.4787,-0.4872,-1.4003,-1.1241,-0.4075,-0.921,-0.4568,-0.0853,-0.1368,0.6235,-0.2414,-0.5254,-0.3122,0.3366,0.5029,-0.4223,-1.7876,-2.1435,0.4587,0.2054,-0.6593,-0.5903,0.9012,-0.268,-0.4402,2.3296,0.4775,-0.4707,-1.1384,0.7087,-1.1167,0.385,-0.9946,-0.0092,-0.8601,-0.5926,-0.6142,1.7485,0.2077,0.5913,-0.7704,1.8462,-0.0321,1.0306,2.6452,0.0631,1.7588,0.1856,0.4375,-0.5074,0.1957,0.7895,1.6168,1.2983,-0.3952,0.4159,0.9302,0.415,0.0632,-0.25,0.077,-0.3302,0.2252,1.0552,1.5571,1.5672,0.7747,0.6317,0.2868,-0.8428,-0.5203,1.3784,0.2188,1.3249,1.1163,-0.0248,0.9851,2.0125,0.8529,0.1806,0.3836,0.1544,1.5386,0.5969,-0.8675,0.6965,-0.5601,0.0468,-0.2941,-0.7977,1.5563,-0.4187,0.7091,-1.9146,-0.6904,0.4203,-0.4178,1.1584,0.4408,-0.674,-0.2548,-2.5481,-0.0177,1.701,-0.9074,0.5699,-0.4123,0.4802,-1.0105,-1.0044,0.3153,-0.0641,0.1867,-0.5604,-0.2584,-2.6176,0.178,0.2919,0.8086,-0.8539,-1.065,-0.421,-0.0172,-0.6739,-0.6118,-0.6873,0.6965,-0.8646,-1.0457,-0.3857,0.8937,-0.2682,-1.0079,0.0243],[0.1273,0.2338,-0.0441,-0.091,-0.0157,-0.0269,-0.3344,0.0884,-0.1446,0.2529,0.2678,-0.0934,0.2301,-0.1802,0.4713,-0.0304,0.0053,0.0441,0.3731,-0.1322,0.4199,-0.1593,-0.3311,-0.0344,-0.1176,0.3046,-0.0861,0.2061,0.2702,-0.0682,0.0748,0.2696,-0.0471,0.0824,0.1697,0.2247,0.4829,-0.0194,-0.1926,0.1753,-0.0348,0.2036,-0.3047,-0.3086,-0.2476,0.0637,-0.057,-0.139,0.1809,0.1525,0.6823,0.0938,0.5522,0.023,0.3969,-0.1238,-0.1485,-0.2484,0.5942,-0.5371,0.0466,-0.2044,0.7632,0.0684,-0.1943,0.5018,-0.4892,0.301,0.4807,0.1723,-0.6145,0.2641,0.3672,0.2909,0.4445,0.471,0.7247,0.26,-0.0856,0.3185,0.1495,0.2407,0.1507,-0.105,-0.663,-0.4491,0.3945,0.686,-0.2906,-0.0891,-0.0942,0.0204,0.1987,-0.1055,-0.1305,-0.1285,-0.1557,0.2707,-0.1898,-0.0916,-1.002,-0.5284,0.2022,-0.1631,-0.3943,0.5804,-0.9836,0.6495,0.3927,-0.3962,-0.2837,-0.0231,-0.0263,-0.5783,-1.0005,-0.6363,-0.0373,0.1275,0.701,0.2666,-0.234,-0.8254,0.8546,0.0054,-0.2975,0.4186,-0.1295,-0.3366,-0.2698,0.7132,0.7254,0.4918,-0.353,0.1829,0.1642,0.5411,-0.9427,-0.1803,-0.4154,0.0126,-0.4269,-0.1296,-0.5284,-0.337,-0.6303,-0.2554,0.0263,-0.1796,-0.9303,-0.5265],[0.0241,0.1027,0.0283,-0.0657,-0.0358,0.0066,-0.0367,-0.0245,-0.0268,-0.0398,0.0167,-0.133,0.0024,-0.0192,0.1941,0.0504,0.1882,0.0928,0.0609,-0.0376,0.0109,0.0594,0.0196,0.1506,-0.2692,0.0434,0.0675,0.0102,0.084,-0.1079,-0.048,0.2391,-0.2285,-0.0481,0.0289,0.1683,0.1957,-0.136,-0.0097,0.007,0.1066,0.2889,-0.0664,0.1765,-0.1505,0.1398,-0.1517,-0.0487,-0.0149,0.0492,0.0347,0.0049,0.0094,0.0655,0.1044,-0.24,-0.0771,-0.0404,-0.0298,-0.0284,0.0137,0.04,0.0456,-0.1643,0.1087,0.1046,-0.1909,-0.2046,0.2251,-0.0516,-0.083,0.1132,-0.0099,-0.2733,0.0433,0.1014,0.0396,0.0666,-0.0363,0.0646,-0.0094,-0.0327,0.0236,-0.1834,-0.254,-0.1093,0.0371,0.1364,-0.1467,0.0089,-0.2698,-0.1472,0.0065,0.0195,-0.107,-0.2292,-0.1322,-0.0198,0.1993,-0.0584,-0.0491,-0.0404,0.059,-0.235,-0.0162,-0.101,-0.1327,-0.2372,-0.0862,0.1045,0.1075,0.0581,0.1459,0.0988,0.2747,0.1912,-0.1548,-0.2729,0.045,-0.065,0.1674,0.0277,-0.1269,0.1395,-0.0614,-0.2176,0.125,-0.0086,0.031,-0.2075,-0.0179,-0.211,0.0997,-0.1857,-0.4878,0.2949,0.0355,-0.2147,0.0052,0.1945,0.2467,0.4681,-0.0404,0.045,0.2133,0.388,0.2195,0.1188,0.026,-0.1625]]
1 change: 1 addition & 0 deletions data/irisScaledClasses.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,-1.220655561573371,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371,1.220655561573371]
1 change: 1 addition & 0 deletions data/simpleDataset.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[50, 67, 90, 98, 120, 55, 71, 93, 102, 129, 65, 76, 95, 105, 134, 50, 80, 102, 130, 138, 60, 82, 97, 135, 151, 65, 89, 106, 137, 153, 75, 95, 117, 133, 155]
1 change: 1 addition & 0 deletions data/simpleDatasetLoadings1-5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[[0.3909,0.4867,0.454,0.4265,0.4715],[0.7503,0.0056,0.1197,-0.6255,-0.1772],[-0.357,0.1387,0.7821,-0.1442,-0.4699],[-0.2697,-0.3226,0.2786,-0.477,0.7198],[-0.2899,0.7999,-0.3004,-0.4225,0.0862]]
1 change: 1 addition & 0 deletions data/simpleDatasetPC1-5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[[-0.5595,-0.3653,-0.1612,-0.0309,0.1408,0.3646,0.6114],[0.0515,0.1522,0.5122,-0.6008,-0.3845,-0.154,0.4234],[0.2206,0.0096,-0.3074,0.4943,-0.6641,-0.1418,0.389],[-0.2907,0.7844,-0.4533,-0.1505,0.1374,-0.1762,0.1489],[-0.1868,0.2597,0.0418,-0.0455,-0.4711,0.7446,-0.3426]]
117 changes: 74 additions & 43 deletions matrix.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,17 @@ export abstract class AbstractMatrix {
*/
readonly columns: number;

/**
* Constructs a matrix with the chosen dimensions from a 1D array.
* @param newRows - Number of rows.
* @param newColumns - Number of columns.
* @param newData - A 1D array containing data for the matrix.
* @returns The new matrix.
*/
/**
* Constructs a matrix with the chosen dimensions from a 1D array.
* @param newRows - Number of rows.
* @param newColumns - Number of columns.
* @param newData - A 1D array containing data for the matrix.
* @returns The new matrix.
*/
static from1DArray(
newRows: number,
newColumns: number,
newData: number[]
newData: number[],
): Matrix;

/**
Expand Down Expand Up @@ -159,15 +159,11 @@ export abstract class AbstractMatrix {
* @param options - Options object.
* @returns The new matrix.
*/
static rand(
rows: number,
columns: number,
options?: IRandomOptions
): Matrix;
static rand(rows: number, columns: number, options?: IRandomOptions): Matrix;
static random(
rows: number,
columns: number,
options?: IRandomOptions
options?: IRandomOptions,
): Matrix;

/**
Expand All @@ -179,7 +175,7 @@ export abstract class AbstractMatrix {
static randInt(
rows: number,
columns: number,
options?: IRandomIntOptions
options?: IRandomIntOptions,
): Matrix;

/**
Expand Down Expand Up @@ -217,8 +213,8 @@ export abstract class AbstractMatrix {

/**
* Returns a matrix whose elements are the maximum between `matrix1` and `matrix2`.
* @param matrix1
* @param matrix2
* @param matrix1
* @param matrix2
*/
static max(matrix1: MaybeMatrix, matrix2: MaybeMatrix): Matrix;

Expand Down Expand Up @@ -318,7 +314,7 @@ export abstract class AbstractMatrix {
/**
* Creates a new matrix that is a repetition of the current matrix. New matrix has rows times the number of
* rows of the original matrix, and columns times the number of columns of the original matrix.
*
*
* @example
* var matrix = new Matrix([[1, 2]]);
* matrix.repeat({ rows: 2 }); // [[1, 2], [1, 2]]
Expand Down Expand Up @@ -569,7 +565,7 @@ export abstract class AbstractMatrix {
scaleRows(options?: IScaleOptions): Matrix;

/**
* Returns a new column-by-column scaled matrix.
* Returns a new column-by-column scaled matrix.
* @param options
* @example
* var matrix = new Matrix([[1, 2], [-1, 0]]);
Expand Down Expand Up @@ -621,7 +617,7 @@ export abstract class AbstractMatrix {
startRow: number,
endRow: number,
startColumn: number,
endColumn: number
endColumn: number,
): Matrix;

/**
Expand All @@ -633,7 +629,7 @@ export abstract class AbstractMatrix {
subMatrixRow(
indices: number[],
startColumn?: number,
endColumn?: number
endColumn?: number,
): Matrix;

/**
Expand All @@ -645,7 +641,7 @@ export abstract class AbstractMatrix {
subMatrixColumn(
indices: number[],
startRow?: number,
endRow?: number
endRow?: number,
): Matrix;

/**
Expand All @@ -657,7 +653,7 @@ export abstract class AbstractMatrix {
setSubMatrix(
matrix: MaybeMatrix | number[],
startRow: number,
startColumn: number
startColumn: number,
): this;

/**
Expand Down Expand Up @@ -737,7 +733,7 @@ export abstract class AbstractMatrix {
*/
standardDeviation(
by: MatrixDimension,
options?: IVarianceByOptions
options?: IVarianceByOptions,
): number[];

/**
Expand Down Expand Up @@ -806,13 +802,10 @@ export abstract class AbstractMatrix {
static leftShift(matrix: MaybeMatrix, value: ScalarOrMatrix): Matrix;
static signPropagatingRightShift(
matrix: MaybeMatrix,
value: ScalarOrMatrix
value: ScalarOrMatrix,
): Matrix;
static rightShift(matrix: MaybeMatrix, value: ScalarOrMatrix): Matrix;
static zeroFillRightShift(
matrix: MaybeMatrix,
value: ScalarOrMatrix
): Matrix;
static zeroFillRightShift(matrix: MaybeMatrix, value: ScalarOrMatrix): Matrix;

// Functional operators (one arg)
// inplace
Expand Down Expand Up @@ -919,7 +912,7 @@ export class MatrixSelectionView extends AbstractMatrix {
constructor(
matrix: AbstractMatrix,
rowIndices: number[],
columnIndices: number[]
columnIndices: number[],
);
}

Expand All @@ -929,7 +922,7 @@ export class MatrixSubView extends AbstractMatrix {
startRow: number,
endRow: number,
startColumn: number,
endColumn: number
endColumn: number,
);
}

Expand All @@ -946,7 +939,7 @@ export interface IWrap1DOptions {

export function wrap(
array: number[],
options?: IWrap1DOptions
options?: IWrap1DOptions,
): WrapperMatrix1D;

export function wrap(twoDAray: number[][]): WrapperMatrix2D;
Expand All @@ -965,7 +958,7 @@ export class WrapperMatrix2D extends AbstractMatrix {
export function solve(
leftHandSide: MaybeMatrix,
rightHandSide: MaybeMatrix,
useSVD?: boolean
useSVD?: boolean,
): Matrix;

/**
Expand Down Expand Up @@ -1005,7 +998,7 @@ export interface ILinearDependenciesOptions {
*/
export function linearDependencies(
matrix: MaybeMatrix,
options?: ILinearDependenciesOptions
options?: ILinearDependenciesOptions,
): Matrix;

/**
Expand All @@ -1014,31 +1007,28 @@ export function linearDependencies(
* @param threshold - Threshold for taking inverse of singular values. Default: Number.EPSILON.
* @returns - The (pseudo)inverted matrix.
*/
export function pseudoInverse(
matrix: MaybeMatrix,
threshold?: number
): Matrix;
export function pseudoInverse(matrix: MaybeMatrix, threshold?: number): Matrix;

export function covariance(
matrix: MaybeMatrix,
options?: ICovarianceOptions
options?: ICovarianceOptions,
): Matrix;

export function covariance(
xMatrix: MaybeMatrix,
yMatrix: MaybeMatrix,
options?: ICovarianceOptions
options?: ICovarianceOptions,
): Matrix;

export function correlation(
matrix: MaybeMatrix,
options?: ICorrelationOptions
options?: ICorrelationOptions,
): Matrix;

export function correlation(
xMatrix: MaybeMatrix,
yMatrix: MaybeMatrix,
options?: ICorrelationOptions
options?: ICorrelationOptions,
): Matrix;

export interface ISVDOptions {
Expand Down Expand Up @@ -1119,7 +1109,7 @@ export { EigenvalueDecomposition as EVD };
*/
export class CholeskyDecomposition {
/**
*
*
* @param value - The matrix to decompose
*/
constructor(value: MaybeMatrix);
Expand Down Expand Up @@ -1165,3 +1155,44 @@ export class QrDecomposition {
}

export { QrDecomposition as QR };

export interface INipalsOptions {
/**
* A column vector of length `X.rows` that contains known labels for supervised PLS.
*/
Y?: MaybeMatrix | number[];
/**
* The maximum number of allowed iterations before beraking the loop if convergence is not achieved.
* Default: 1000
*/
maxIterations?: boolean;
/**
* Termination criteria
* Default: 1e-10
*/
terminationCriteria?: number;
}

export class Nipals {
/**
* Implementation of the NIPALS algorithm.
* Geladi, P and Kowalski, B.R. (1986)
* Partial least squares and regression:
* a tutorial.
* Analytica Chimica Acta 185, 1-17.
* @param X - A matrix to be factored
* @param options
*/
constructor(X: MaybeMatrix, options?: INipalsOptions);
w: Matrix;
s: Matrix;
t: number;
xResidual: Matrix;
p: Matrix;
q: Matrix;
u: number;
yResidual: Matrix;
betas: number;
}

export { Nipals as NIPALS };
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"jest": "^24.8.0",
"jest-matcher-deep-close-to": "^1.3.0",
"mathjs": "^6.0.2",
"ml-dataset-iris": "^1.0.0",
"numeric": "^1.2.6",
"pretty-hrtime": "^1.0.3",
"rollup": "^1.16.3",
Expand Down
Loading

0 comments on commit 043c8b6

Please sign in to comment.