List of usage examples for org.apache.commons.math3.linear LUDecomposition getSolver
public DecompositionSolver getSolver()
From source file:hulo.localization.utils.ArrayUtils.java
public static double[][] inverseMat(double[][] mat) { RealMatrix realMatrix = MatrixUtils.createRealMatrix(mat); LUDecomposition lu = new LUDecomposition(realMatrix); RealMatrix invMat = lu.getSolver().getInverse(); return invMat.getData(); }
From source file:com.joptimizer.util.Utils.java
/** * @see http://en.wikipedia.org/wiki/Machine_epsilon#Approximation_using_Java *//* w w w . j a v a2s . c o m*/ // public static final float calculateFloatMachineEpsilon() { // float eps = 1.0f; // do { // eps /= 2.0f; // } while ((float) (1.0 + (eps / 2.0)) != 1.0); // Log.d(MainActivity.JOPTIMIZER_LOGTAG,"Calculated float machine epsilon: " + eps); // return eps; // } public static RealMatrix squareMatrixInverse(RealMatrix M) throws SingularMatrixException { if (!M.isSquare()) { throw new IllegalArgumentException("Not square matrix!"); } // try commons-math cholesky try { CholeskyDecomposition cd = new CholeskyDecomposition(M); return cd.getSolver().getInverse(); } catch (SingularMatrixException e) { throw e; } catch (Exception e) { Log.d(MainActivity.JOPTIMIZER_LOGTAG, e.getMessage()); } // try joptimizer cholesky try { CholeskyFactorization cd = new CholeskyFactorization(M.getData()); double[][] MInv = cd.getInverse(); return MatrixUtils.createRealMatrix(MInv); } catch (Exception e) { Log.d(MainActivity.JOPTIMIZER_LOGTAG, e.getMessage()); } // try LU try { LUDecomposition ld = new LUDecomposition(M); return ld.getSolver().getInverse(); } catch (SingularMatrixException e) { throw e; } catch (Exception e) { Log.d(MainActivity.JOPTIMIZER_LOGTAG, e.getMessage()); } // try QR try { QRDecomposition qr = new org.apache.commons.math3.linear.QRDecomposition(M); return qr.getSolver().getInverse(); } catch (SingularMatrixException e) { throw e; } catch (Exception e) { Log.d(MainActivity.JOPTIMIZER_LOGTAG, e.getMessage()); } return null; }
From source file:hivemall.utils.math.StatsUtils.java
/** * @param mu1 mean vector of the first normal distribution * @param sigma1 covariance matrix of the first normal distribution * @param mu2 mean vector of the second normal distribution * @param sigma2 covariance matrix of the second normal distribution * @return the Hellinger distance between two multivariate normal distributions * @link https://en.wikipedia.org/wiki/Hellinger_distance#Examples *///from www . j a v a 2 s .c o m public static double hellingerDistance(@Nonnull final RealVector mu1, @Nonnull final RealMatrix sigma1, @Nonnull final RealVector mu2, @Nonnull final RealMatrix sigma2) { RealVector muSub = mu1.subtract(mu2); RealMatrix sigmaMean = sigma1.add(sigma2).scalarMultiply(0.5d); LUDecomposition LUsigmaMean = new LUDecomposition(sigmaMean); double denominator = Math.sqrt(LUsigmaMean.getDeterminant()); if (denominator == 0.d) { return 1.d; // avoid divide by zero } RealMatrix sigmaMeanInv = LUsigmaMean.getSolver().getInverse(); // has inverse iff det != 0 double sigma1Det = MatrixUtils.det(sigma1); double sigma2Det = MatrixUtils.det(sigma2); double numerator = Math.pow(sigma1Det, 0.25d) * Math.pow(sigma2Det, 0.25d) * Math.exp(-0.125d * sigmaMeanInv.preMultiply(muSub).dotProduct(muSub)); return 1.d - numerator / denominator; }
From source file:hivemall.utils.math.StatsUtils.java
/** * pdf(x, x_hat) = exp(-0.5 * (x-x_hat) * inv() * (x-x_hat)T) / ( 2^0.5d * det()^0.5) * /* ww w . j a v a 2s . com*/ * @return value of probabilistic density function * @link https://en.wikipedia.org/wiki/Multivariate_normal_distribution#Density_function */ public static double pdf(@Nonnull final RealVector x, @Nonnull final RealVector x_hat, @Nonnull final RealMatrix sigma) { final int dim = x.getDimension(); Preconditions.checkArgument(x_hat.getDimension() == dim, "|x| != |x_hat|, |x|=" + dim + ", |x_hat|=" + x_hat.getDimension()); Preconditions.checkArgument(sigma.getRowDimension() == dim, "|x| != |sigma|, |x|=" + dim + ", |sigma|=" + sigma.getRowDimension()); Preconditions.checkArgument(sigma.isSquare(), "Sigma is not square matrix"); LUDecomposition LU = new LUDecomposition(sigma); final double detSigma = LU.getDeterminant(); double denominator = Math.pow(2.d * Math.PI, 0.5d * dim) * Math.pow(detSigma, 0.5d); if (denominator == 0.d) { // avoid divide by zero return 0.d; } final RealMatrix invSigma; DecompositionSolver solver = LU.getSolver(); if (solver.isNonSingular() == false) { SingularValueDecomposition svd = new SingularValueDecomposition(sigma); invSigma = svd.getSolver().getInverse(); // least square solution } else { invSigma = solver.getInverse(); } //EigenDecomposition eigen = new EigenDecomposition(sigma); //double detSigma = eigen.getDeterminant(); //RealMatrix invSigma = eigen.getSolver().getInverse(); RealVector diff = x.subtract(x_hat); RealVector premultiplied = invSigma.preMultiply(diff); double sum = premultiplied.dotProduct(diff); double numerator = Math.exp(-0.5d * sum); return numerator / denominator; }
From source file:hivemall.utils.math.MatrixUtils.java
@Nonnull public static RealMatrix inverse(@Nonnull final RealMatrix m, final boolean exact) throws SingularMatrixException { LUDecomposition LU = new LUDecomposition(m); DecompositionSolver solver = LU.getSolver(); final RealMatrix inv; if (exact || solver.isNonSingular()) { inv = solver.getInverse();//from w w w.ja v a 2 s. com } else { SingularValueDecomposition SVD = new SingularValueDecomposition(m); inv = SVD.getSolver().getInverse(); } return inv; }
From source file:hivemall.utils.math.MatrixUtils.java
/** * L = A x R//from w ww. j a va 2 s.c o m * * @return a matrix A that minimizes A x R - L */ @Nonnull public static RealMatrix solve(@Nonnull final RealMatrix L, @Nonnull final RealMatrix R, final boolean exact) throws SingularMatrixException { LUDecomposition LU = new LUDecomposition(R); DecompositionSolver solver = LU.getSolver(); final RealMatrix A; if (exact || solver.isNonSingular()) { A = LU.getSolver().solve(L); } else { SingularValueDecomposition SVD = new SingularValueDecomposition(R); A = SVD.getSolver().solve(L); } return A; }
From source file:com.itemanalysis.psychometrics.polycor.PolychoricTwoStepVariance.java
public double[][] variance(double[] x) { RealMatrix m = this.hessianAt(x); LUDecomposition SLUD = new LUDecomposition(m); RealMatrix inv = SLUD.getSolver().getInverse(); return inv.getData(); }
From source file:com.itemanalysis.psychometrics.cfa.MaximumLikelihoodEstimation.java
public double[] gradientAt(double[] x) { model.setParameters(x);//from www . j a v a 2 s.c om double[] g = null; SIGMA = model.getImpliedCovariance(x); LUDecomposition SLUD = new LUDecomposition(SIGMA); RealMatrix Sinv = SLUD.getSolver().getInverse(); RealMatrix D = SIGMA.subtract(varcov); RealMatrix derLambda = Sinv.multiply(D).multiply(Sinv).multiply(model.getBeta(x)); RealMatrix derError = Sinv.multiply(D).multiply(Sinv); g = model.getGradient(derLambda, derError); return g; }
From source file:com.itemanalysis.psychometrics.cfa.MaximumLikelihoodEstimation.java
public double gfi() { double fit = 0.0; double q = Double.valueOf(model.getNumberOfItems()).doubleValue(); RealMatrix I = new IdentityMatrix(nItems); LUDecomposition SLUD = new LUDecomposition(SIGMA); RealMatrix Sinv = SLUD.getSolver().getInverse(); RealMatrix P1 = Sinv.multiply(varcov); RealMatrix D = P1.subtract(I);// w w w. java2s . co m double numerator = D.multiply(D).getTrace(); RealMatrix P2 = Sinv.multiply(varcov); double denom = P2.multiply(P2).getTrace(); fit = 1.0 - numerator / denom; return fit; }
From source file:com.itemanalysis.psychometrics.polycor.PolychoricMaximumLikelihood.java
private void computeStandardError(double[][] hessian) { RealMatrix m = new Array2DRowRealMatrix(hessian); LUDecomposition SLUD = new LUDecomposition(m); RealMatrix inv = SLUD.getSolver().getInverse(); variance = inv.getData();/* w ww . j a v a2s. c o m*/ }