/** * Computes the term used in the exponent (see definition of the distribution). * * @param values Values at which to compute density. * @return the multiplication factor of density calculations. */ private double getExponentTerm(final double[] values) { final double[] centered = new double[values.length]; for (int i = 0; i < centered.length; i++) { centered[i] = values[i] - getMeans()[i]; } final double[] preMultiplied = covarianceMatrixInverse.preMultiply(centered); double sum = 0; for (int i = 0; i < preMultiplied.length; i++) { sum += preMultiplied[i] * centered[i]; } return FastMath.exp(-0.5 * sum); } }
double yhat = coef.preMultiply(xVector(x))[0]; // apply coefs to xVector if (logY()) yhat = (Math.exp(yhat)); // if we predicted ln y, we still need to get y return yhat;
RealVector vector = ... RealMatrix matrix = ... RealVector result = matrix.preMultiply(vector);
/** * Computes the term used in the exponent (see definition of the distribution). * * @param values Values at which to compute density. * @return the multiplication factor of density calculations. */ private double getExponentTerm(final double[] values) { final double[] centered = new double[values.length]; for (int i = 0; i < centered.length; i++) { centered[i] = values[i] - getMeans()[i]; } final double[] preMultiplied = covarianceMatrixInverse.preMultiply(centered); double sum = 0; for (int i = 0; i < preMultiplied.length; i++) { sum += preMultiplied[i] * centered[i]; } return Math.exp(-0.5 * sum); } }
@Override public double predict(double x) { double yhat = coef.preMultiply(xVector(x))[0]; // apply coefs to xVector if (logY()) yhat = (Math.exp(yhat)); // if we predicted ln y, we still need to get y return yhat; }
@Override public double predict(double x) { double yhat = coef.preMultiply(xVector(x))[0]; // apply coefs to xVector if (logY()) yhat = (Math.exp(yhat)); // if we predicted ln y, we still need to get y return yhat; }
@Override public double predict(double x) { double yhat = coef.preMultiply(xVector(x))[0]; // apply coefs to xVector if (logY()) yhat = (Math.exp(yhat)); // if we predicted ln y, we still need to get y return yhat; }
public double logDensity(RealVector x){ RealVector diff = x.subtract(mean); int dim = mean.getDimension(); return -0.5*dim*Math.log(2*Math.PI)-0.5*logDeterminant -0.5*(inverseCovariance.preMultiply(diff).dotProduct(diff)); }
/** * Computes the term used in the exponent (see definition of the distribution). * * @param values Values at which to compute density. * @return the multiplication factor of density calculations. */ private double getExponentTerm(final double[] values) { final double[] centered = new double[values.length]; for (int i = 0; i < centered.length; i++) { centered[i] = values[i] - getMeans()[i]; } final double[] preMultiplied = covarianceMatrixInverse.preMultiply(centered); double sum = 0; for (int i = 0; i < preMultiplied.length; i++) { sum += preMultiplied[i] * centered[i]; } return FastMath.exp(-0.5 * sum); } }
/** * @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 */ 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; }
RealVector premultiplied = invSigma.preMultiply(diff); double sum = premultiplied.dotProduct(diff); double numerator = Math.exp(-0.5d * sum);
public static double sumWalks(RealMatrix m, RealVector s, RealVector t) { int n = m.getColumnDimension(); if (n != m.getRowDimension()) { throw new InputMismatchException("sum walks can only be computed on square matrices"); } LUDecomposition d = new LUDecomposition(MatrixUtils.createRealIdentityMatrix(n).subtract(m)); RealMatrix sum = d.getSolver().getInverse(); if (s == null) s = new ArrayRealVector(n, 1.0); if (t == null) t = new ArrayRealVector(n, 1.0); return sum.preMultiply(s).dotProduct(t); }
subR.subtract(x.outerProduct(subR.preMultiply(x)).scalarMultiply(2)).getData(), i, 0); subQt.subtract(x.outerProduct(subQt.preMultiply(x)).scalarMultiply(2)).getData(), i, 0);
public LeastSquaresRegressionResult regress(double[][] x, double[] weights, double[] y, boolean useIntercept) { if (weights == null) { throw new IllegalArgumentException("Cannot perform WLS regression without an array of weights"); } checkData(x, weights, y); double[][] dep = addInterceptVariable(x, useIntercept); double[] w = new double[weights.length]; for (int i = 0; i < y.length; i++) { w[i] = weights[i]; } DoubleMatrix matrix = DoubleMatrix.copyOf(dep); DoubleArray vector = DoubleArray.copyOf(y); RealMatrix wDiag = new DiagonalMatrix(w); DoubleMatrix transpose = ALGEBRA.getTranspose(matrix); DoubleMatrix wDiagTimesMatrix = DoubleMatrix.ofUnsafe(wDiag.multiply( new Array2DRowRealMatrix(matrix.toArrayUnsafe())).getData()); DoubleMatrix tmp = (DoubleMatrix) ALGEBRA.multiply( ALGEBRA.getInverse(ALGEBRA.multiply(transpose, wDiagTimesMatrix)), transpose); DoubleMatrix wTmpTimesDiag = DoubleMatrix.copyOf(wDiag.preMultiply(new Array2DRowRealMatrix(tmp.toArrayUnsafe())).getData()); DoubleMatrix betasVector = (DoubleMatrix) ALGEBRA.multiply(wTmpTimesDiag, vector); double[] yModel = super.writeArrayAsVector(((DoubleMatrix) ALGEBRA.multiply(matrix, betasVector)).toArray()); double[] betas = super.writeArrayAsVector(betasVector.toArray()); return getResultWithStatistics(x, convertArray(wDiag.getData()), y, betas, yModel, transpose, matrix, useIntercept); }
double expectedSumWalks = sum.preMultiply(ones).dotProduct(ones); RealVector s = new ArrayRealVector(new double[] {1, 2, 3}); RealVector t = new ArrayRealVector(new double[] {5, 7, 4});