@Test(expectedExceptions = IllegalArgumentException.class) public void testNullObjectMatrix() { QR.apply((DoubleMatrix) null); }
private LeastSquareWithPenaltyResults finish( DoubleMatrix alpha, DecompositionResult decmp, double chiSqr, double penalty, DoubleMatrix jacobian, DoubleArray newTheta, DoubleArray sigma) { DoubleMatrix covariance = decmp.solve(DoubleMatrix.identity(alpha.rowCount())); DoubleMatrix bT = getBTranspose(jacobian, sigma); DoubleMatrix inverseJacobian = decmp.solve(bT); return new LeastSquareWithPenaltyResults(chiSqr, penalty, newTheta, covariance, inverseJacobian); }
/** * * the inverse-Jacobian where the i-j entry is the sensitivity of the ith (fitted) parameter (a_i) to the jth data * point (y_j). * @param sigma Set of measurement errors * @param func The model as a function of its parameters only * @param jac The model sensitivity to its parameters (i.e. the Jacobian matrix) as a function of its parameters only * @param originalSolution The value of the parameters at a converged solution * @return inverse-Jacobian */ public DoubleMatrix calInverseJacobian( DoubleArray sigma, Function<DoubleArray, DoubleArray> func, Function<DoubleArray, DoubleMatrix> jac, DoubleArray originalSolution) { DoubleMatrix jacobian = getJacobian(jac, sigma, originalSolution); DoubleMatrix a = getModifiedCurvatureMatrix(jacobian, 0.0); DoubleMatrix bT = getBTranspose(jacobian, sigma); DecompositionResult decRes = _decomposition.apply(a); return decRes.solve(bT); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullObjectMatrix() { LU.apply((DoubleMatrix) null); }
private double[] weights(double forward, double strike, double[] strikes, double timeToExpiry, double atmVol) { double[][] mat = new double[3][3]; double[] vec = new double[3]; for (int i = 0; i < 3; ++i) { mat[0][i] = BlackFormulaRepository.vega(forward, strikes[i], timeToExpiry, atmVol); mat[1][i] = BlackFormulaRepository.vanna(forward, strikes[i], timeToExpiry, atmVol); mat[2][i] = BlackFormulaRepository.volga(forward, strikes[i], timeToExpiry, atmVol); } vec[0] = BlackFormulaRepository.vega(forward, strike, timeToExpiry, atmVol); vec[1] = BlackFormulaRepository.vanna(forward, strike, timeToExpiry, atmVol); vec[2] = BlackFormulaRepository.volga(forward, strike, timeToExpiry, atmVol); DecompositionResult res = SVD.apply(DoubleMatrix.ofUnsafe(mat)); return res.solve(vec); }
private LeastSquareResults finish( DoubleMatrix alpha, DecompositionResult decmp, double newChiSqr, DoubleMatrix jacobian, DoubleArray newTheta, DoubleArray sigma) { DoubleMatrix covariance = decmp.solve(DoubleMatrix.identity(alpha.rowCount())); DoubleMatrix bT = getBTranspose(jacobian, sigma); DoubleMatrix inverseJacobian = decmp.solve(bT); return new LeastSquareResults(newChiSqr, newTheta, covariance, inverseJacobian); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullObjectMatrix() { CH.apply((DoubleMatrix) null); }
@Override public DoubleMatrix getInitializedMatrix(Function<DoubleArray, DoubleMatrix> jacobianFunction, DoubleArray x) { ArgChecker.notNull(jacobianFunction, "jacobianFunction"); ArgChecker.notNull(x, "x"); DoubleMatrix estimate = jacobianFunction.apply(x); DecompositionResult decompositionResult = _decomposition.apply(estimate); return decompositionResult.solve(DoubleMatrix.identity(x.size())); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullObjectMatrix() { getSVD().apply((DoubleMatrix) null); }
/** * This regression method is private and called in other regression methods * @param xDataMatrix _nData x (_degree + 1) matrix whose low vector is (xData[i]^0, xData[i]^1, ..., xData[i]^{_degree}) * @param yDataVector the y-values * @param nData Number of data points * @param degree the degree */ private LeastSquaresRegressionResult regress( DoubleMatrix xDataMatrix, DoubleArray yDataVector, int nData, int degree) { Decomposition<QRDecompositionResult> qrComm = new QRDecompositionCommons(); DecompositionResult decompResult = qrComm.apply(xDataMatrix); _qrResult = (QRDecompositionResult) decompResult; DoubleMatrix qMatrix = _qrResult.getQ(); DoubleMatrix rMatrix = _qrResult.getR(); double[] betas = backSubstitution(qMatrix, rMatrix, yDataVector, degree); double[] residuals = residualsSolver(xDataMatrix, betas, yDataVector); for (int i = 0; i < degree + 1; ++i) { ArgChecker.isFalse(Double.isNaN(betas[i]), "Input is too large or small"); } for (int i = 0; i < nData; ++i) { ArgChecker.isFalse(Double.isNaN(residuals[i]), "Input is too large or small"); } return new LeastSquaresRegressionResult(betas, residuals, 0.0, null, 0.0, 0.0, null, null, true); }
@Test public void testRecoverOrginal() { final DecompositionResult result = CH.apply(A); assertTrue(result instanceof CholeskyDecompositionResult); final CholeskyDecompositionResult ch = (CholeskyDecompositionResult) result; final DoubleMatrix a = (DoubleMatrix) ALGEBRA.multiply(ch.getL(), ch.getLT()); checkEquals(A, a); }
@Test public void testRecoverOrginal() { final DecompositionResult result = QR.apply(A); assertTrue(result instanceof QRDecompositionResult); final QRDecompositionResult qr = (QRDecompositionResult) result; final DoubleMatrix q = qr.getQ(); final DoubleMatrix r = qr.getR(); final DoubleMatrix a = (DoubleMatrix) ALGEBRA.multiply(q, r); checkEquals(A, a); }
@Test public void testRecoverOrginal() { final DecompositionResult result = LU.apply(A); assertTrue(result instanceof LUDecompositionResult); final LUDecompositionResult lu = (LUDecompositionResult) result; final DoubleMatrix a = (DoubleMatrix) ALGEBRA.multiply(lu.getL(), lu.getU()); checkEquals((DoubleMatrix) ALGEBRA.multiply(lu.getP(), A), a); }
/** * Cubic spline is obtained by solving a linear problem Ax=b where A is a square matrix and x,b are vector * This can be done by LU decomposition * @param doubMat Matrix A * @param doubVec Vector B * @return Solution to the linear equation, x */ protected double[] matrixEqnSolver(double[][] doubMat, double[] doubVec) { LUDecompositionResult result = _luObj.apply(DoubleMatrix.copyOf(doubMat)); double[][] lMat = result.getL().toArray(); double[][] uMat = result.getU().toArray(); DoubleArray doubVecMod = ((DoubleArray) OG_ALGEBRA.multiply(result.getP(), DoubleArray.copyOf(doubVec))); return backSubstitution(uMat, forwardSubstitution(lMat, doubVecMod)); }
@Test public void testRecoverOrginal() { final MatrixAlgebra algebra = getAlgebra(); final DecompositionResult result = getSVD().apply(A); assertTrue(result instanceof SVDecompositionResult); final SVDecompositionResult svd_result = (SVDecompositionResult) result; final DoubleMatrix u = svd_result.getU(); final DoubleMatrix w = DoubleMatrix.diagonal(DoubleArray.copyOf(svd_result.getSingularValues())); final DoubleMatrix vt = svd_result.getVT(); final DoubleMatrix a = (DoubleMatrix) algebra.multiply(algebra.multiply(u, w), vt); checkEquals(A, a); }
/** * Compare results with Common decomposition */ public void compareCommon() { final CholeskyDecompositionResult resultOG = CDOG.apply(A3); final CholeskyDecompositionResult resultC = CDC.apply(A3); checkEquals(resultC.getL(), resultOG.getL()); checkEquals(ALGEBRA.getTranspose(resultC.getL()), resultOG.getLT()); assertEquals("Determinant", resultC.getDeterminant(), resultOG.getDeterminant(), 1.0E-10); }
/** * Cubic spline and its node sensitivity are respectively obtained by solving a linear problem Ax=b where A is a square matrix and x,b are vector and AN=L where N,L are matrices * @param doubMat1 The matrix A * @param doubVec The vector b * @param doubMat2 The matrix L * @return The solutions to the linear systems, x,N */ protected DoubleArray[] combinedMatrixEqnSolver(double[][] doubMat1, double[] doubVec, double[][] doubMat2) { int nDataPts = doubVec.length; LUDecompositionResult result = _luObj.apply(DoubleMatrix.copyOf(doubMat1)); double[][] lMat = result.getL().toArray(); double[][] uMat = result.getU().toArray(); DoubleMatrix pMat = result.getP(); DoubleArray doubVecMod = ((DoubleArray) OG_ALGEBRA.multiply(pMat, DoubleArray.copyOf(doubVec))); DoubleMatrix doubMat2Matrix = DoubleMatrix.copyOf(doubMat2); DoubleArray[] res = new DoubleArray[nDataPts + 1]; res[0] = DoubleArray.copyOf(backSubstitution(uMat, forwardSubstitution(lMat, doubVecMod))); for (int i = 0; i < nDataPts; ++i) { DoubleArray doubMat2Colum = doubMat2Matrix.column(i); DoubleArray doubVecMod2 = ((DoubleArray) OG_ALGEBRA.multiply(pMat, doubMat2Colum)); res[i + 1] = DoubleArray.copyOf(backSubstitution(uMat, forwardSubstitution(lMat, doubVecMod2))); } return res; }
@Test public void testInvert() { final MatrixAlgebra algebra = getAlgebra(); final SVDecompositionResult result = getSVD().apply(A); final DoubleMatrix ut = result.getUT(); final DoubleMatrix v = result.getV(); final double[] sv = result.getSingularValues(); final int n = sv.length; final double[] svinv = new double[n]; for (int i = 0; i < n; i++) { if (sv[i] == 0.0) { svinv[i] = 0.0; } else { svinv[i] = 1.0 / sv[i]; } } final DoubleMatrix winv = DoubleMatrix.diagonal(DoubleArray.copyOf(svinv)); final DoubleMatrix ainv = (DoubleMatrix) algebra.multiply(algebra.multiply(v, winv), ut); final DoubleMatrix identity = (DoubleMatrix) algebra.multiply(A, ainv); checkIdentity(identity); }