Permalink
Browse files

Fix QR() implementation and other additions

* Reimplement QR() decomposition function.
* Enhance col() and row() to support a list select
* Introduce cola(), rowa(), arange(), slice(), sliceAssign(), copy() and
  diagonal().
* Implement lu() and cholesky() decomposition.
* Add triaUpSolve() and triaLowSolve() to solve triangle matrices.
* Add lstsq() and outer() to linearalgebra.
* Add file models.js to reimplement OLS regress by QR decomposition.
* Add t-test, f-test, 95% confidence interval, R2 and adjust R2 to
  regress.

Fixes: #55
PR-URL: #191
Reviewed-by: Trevor Norris <trev.norris@gmail.com>
  • Loading branch information...
1 parent d7e1e80 commit 8d713bef860f336cf09cc01ca0a28b33dc38c3e0 @yiyuezhuo yiyuezhuo committed with trevnorris May 8, 2016
View
@@ -22,7 +22,8 @@ jstat.js: \
src/special.js \
src/distribution.js \
src/linearalgebra.js \
- src/test.js
+ src/test.js \
+ src/models.js
@echo 'Building jStat'
@mkdir -p $(DIST_DIR)
@cat $^ > $(DIST_DIR)/$@
View
@@ -7,3 +7,4 @@
* [Distributions](distributions.md)
* [Linear Algebra](linear-algebra.md)
* [Statistical Tests](test.md)
+* [Regress Models](models.md)
View
@@ -79,6 +79,12 @@ Also note `this` within the callback refers to the calling jStat object.
// d === 2
});
+### rowa()
+
+Return a array from matrix row
+
+ rowa([[1,2],[3,4]]) === [1,2];
+
### cols()
Returns the number of columns in a matrix.
@@ -100,6 +106,29 @@ Also note `this` within the callback refers to the calling jStat object.
// d === 3
});
+### cola()
+
+Return a array from matrix column (`col()` will return a matrix form instead of array form)
+
+ cola([[1,2],[3,4]]) === [1,3];
+
+### slice()
+
+Slice matrix as numpy style
+
+ A=[[1,2,3],[4,5,6],[7,8,9]];
+ slice(A,{row:{end:2},col:{start:1}}) === [[2,3],[5,6]];
+ slice(A,1,{start:1}) === [5,6];
+
+### sliceAssign()
+
+Do slice assign as numpy style
+
+ A = [[1,2,3],[4,5,6],[7,8,9]];
+ sliceAssign(A,{row : {start : 1}, col : {start : 1}},[[0,0],[0,0]]);
+ A = [[1,2,3],[4,0,0],[7,0,0]];
+
+
### dimensions()
Returns and object with the dimensions of a matrix.
@@ -127,8 +156,9 @@ Returns a specified row of a matrix.
**row( array, index )**
- var matrix = [[1,2,3],[4,5,6]];
+ var matrix = [[1,2,3],[4,5,6],[7,8,9]];
jStat.row( matrix, 0 ) === [1,2,3];
+ jStat.row( matrix, [0,1] ) === [[1,2,3],[4,5,6]]
**fn.row( index[, callback] )**
@@ -148,12 +178,13 @@ Returns the specified column as a column vector.
**col( index )**
- var matrix = [[1,2],[3,4]];
- jStat.col( matrix, 0 ) === [[1],[3]];
+ var matrix = [[1,2,3],[4,5,6],[7,8,9]];
+ jStat.col( matrix, 0 ) === [[1],[4],[7]];
+ jStat.col( matrix,[0,1] ) === [[1,2],[4,5],[7,8]]
**fn.col( index[, callback] )**
- jStat( matrix ).col( 0 ) === jStat([[1],[3]]);
+ jStat( matrix ).col( 0 ) === jStat([[1],[4],[7]]);
Or pass a callback to run the calculation asynchronously and pass on the calculation.
This allows for continued chaining of methods to the jStat object.
@@ -205,6 +236,12 @@ Also note `this` within the callback refers to the calling jStat object.
// d === jStat([[3],[5],[7]])
});
+### diagonal()
+
+Create a new diagonal matrix by given 1d diag array
+
+ jStat.diagonal([1,2,3]) === [[1,0,0],[0,2,0],[0,0,3]];
+
### transpose()
Transpose a matrix.
@@ -351,6 +388,10 @@ Use this technique for creating matrices in jStat instances.
jStat().rand( 3 );
+### copy()
+
+Return a copy from given matrix
+
### identity()
Create an identity matrix of row by col.
@@ -367,6 +408,22 @@ Use this technique for creating matrices in jStat instances.
jStat().identity( 2 );
+### seq()
+
+Create an arithmetic sequence by given length
+
+ jStat.seq(1,5,9) === [1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5];
+
+### arange()
+
+Create an arithmetic sequence by given step
+
+ arange(5) === [0,1,2,3,4]
+ arange(1,5) === [1,2,3,4]
+ arange(5,1,-1) === [5,4,3,2]
+
+
+
### clear()
Set all values in the vector or matrix to zero.
@@ -86,6 +86,12 @@ Multiply all entries of the array by arg.
Take dot product of array 1 and array 2.
+### outer( A, B )
+
+Take outer product of A and B.
+
+ outer([1,2,3],[4,5,6]) === [[4,5,6],[8,10,12],[12,15,18]]
+
### pow( arr, arg )
Raise all entries of the array to the power of arg
@@ -131,14 +137,32 @@ Performs Gaussian Elimination on matrix A augmented by matrix B.
Performs Gauss-Jordan Elimination on matrix A augmented by matrix B.
-### lu(A,B)
+### lu(A)
+
+Perform the LU decomposition on matrix A.
+
+A -> [L,U]
+
+st.
-Performs the LU-Decomposition on matrix A.
+A=LU
+
+L is lower triangular matrix
+
+U is upper triangular matrix
### cholesky(A)
Performs the Cholesky decomposition on matrix A.
+A -> T
+
+st.
+
+A=TT'
+
+T is lower triangular matrix
+
### gauss_jacobi(A,b,x,r)
Solves the linear system Ax = b using the Gauss-Jacobi method with an initial guess of r.
@@ -155,7 +179,26 @@ Solves the linear system Ax = b using the sucessive over-relaxation method with
Performs the householder transformation on the matrix A.
-### QR()
+### QR(A)
+
+Performs the Cholesky decomposition on matrix A.
+
+A -> [Q,R]
+
+Q is orthogonal matrix
+
+R is upper triangular
+
+### lstsq(A,b)
+
+solve least squard problem for Ax=b as QR decomposition way.
+
+if b is [[b1],[b2],[b3]] form will return [[x1],[x2],[x3]] array form solution.
+
+else b is [b1,b2,b3] form will return [x1,x2,x3] array form solution.
+
+
+
### jacobi()
View
@@ -0,0 +1,40 @@
+## Regress Models
+
+## Instance Functionality
+
+### ols(endog,exog)
+
+What's the `endog`, `exog`?
+
+Please see:
+
+http://statsmodels.sourceforge.net/stable/endog_exog.html
+
+`ols` use ordinary least square(OLS) method to estimate linear model and return
+a `model`object.
+
+`model` object attribute is vrey like to `statsmodels` result object attribute
+(nobs,coef,...).
+
+The following example is compared by `statsmodels`. They take same result
+exactly.
+
+ var A=[[1,2,3],
+ [1,1,0],
+ [1,-2,3],
+ [1,3,4],
+ [1,-10,2],
+ [1,4,4],
+ [1,10,2],
+ [1,3,2],
+ [1,4,-1]];
+ var b=[1,-2,3,4,-5,6,7,-8,9];
+ var model=jStat.models.ols(b,A);
+ //R2
+ assert.epsilon(tol,model.R2,0.309);
+ // t test
+ assert.epsilon(tol,model.t.p[0],0.8377444317889267);
+ assert.epsilon(tol,model.t.p[1],0.1529673615844231);
+ assert.epsilon(tol,model.t.p[2],0.9909627983826588);
+ // f test
+ assert.epsilon(tol,model.f.pvalue,0.33063636718598743);
Oops, something went wrong.

0 comments on commit 8d713be

Please sign in to comment.