Example usage for org.apache.commons.math.linear RealVector outerProduct

List of usage examples for org.apache.commons.math.linear RealVector outerProduct

Introduction

In this page you can find the example usage for org.apache.commons.math.linear RealVector outerProduct.

Prototype

RealMatrix outerProduct(double[] v);

Source Link

Document

Compute the outer product.

Usage

From source file:com.opengamma.analytics.math.matrix.CommonsMatrixAlgebra.java

/**
 * {@inheritDoc}//ww  w  .  j  a v  a 2  s. co m
 */
@Override
public DoubleMatrix2D getOuterProduct(final Matrix<?> m1, final Matrix<?> m2) {
    Validate.notNull(m1, "m1");
    Validate.notNull(m2, "m2");
    if (m1 instanceof DoubleMatrix1D && m2 instanceof DoubleMatrix1D) {
        final RealVector t1 = CommonsMathWrapper.wrap((DoubleMatrix1D) m1);
        final RealVector t2 = CommonsMathWrapper.wrap((DoubleMatrix1D) m2);
        return CommonsMathWrapper.unwrap(t1.outerProduct(t2));
    }
    throw new IllegalArgumentException(
            "Can only find outer product of DoubleMatrix1D; have " + m1.getClass() + " and " + m2.getClass());
}

From source file:eu.amidst.core.exponentialfamily.EF_Normal_NormalParents.java

/**
 * {@inheritDoc}//from   w  w w .j a v  a  2  s. c  o m
 */
@Override
public SufficientStatistics getSufficientStatistics(Assignment data) {
    CompoundVector vectorSS = this.createEmtpyCompoundVector();

    double[] Xarray = { data.getValue(this.var) };

    double[] Yarray = this.parents.stream().mapToDouble(w -> data.getValue(w)).toArray();
    RealVector XYRealVector = new ArrayRealVector(Xarray, Yarray);
    vectorSS.setXYbaseVector(XYRealVector);

    RealMatrix covRealmatrix = XYRealVector.outerProduct(XYRealVector);

    vectorSS.setcovbaseVector(covRealmatrix);

    return vectorSS;
}

From source file:eu.amidst.core.exponentialfamily.EF_Normal_NormalParents2.java

/**
 * {@inheritDoc}//from ww  w.  j a v a  2  s .  com
 */
@Override
public double getExpectedLogNormalizer(Map<Variable, MomentParameters> momentParents) {
    CompoundVector globalNaturalParameters = (CompoundVector) this.naturalParameters;
    double logNorm = -0.5 * Math.log(-2 * globalNaturalParameters.getTheta_Minus1());

    double[] Yarray = new double[nOfParents];
    double[] YYarray = new double[nOfParents];

    for (int i = 0; i < nOfParents; i++) {
        Yarray[i] = momentParents.get(this.getConditioningVariables().get(i)).get(0);
        YYarray[i] = momentParents.get(this.getConditioningVariables().get(i)).get(1);
    }
    RealVector Y = new ArrayRealVector(Yarray);

    logNorm -= globalNaturalParameters.getTheta_beta0BetaRV().dotProduct(new ArrayRealVector(Y));

    RealMatrix YY = Y.outerProduct(Y);
    for (int i = 0; i < nOfParents; i++) {
        YY.setEntry(i, i, YYarray[i]);
    }

    logNorm -= IntStream.range(0, nOfParents).mapToDouble(p -> {
        return globalNaturalParameters.getTheta_BetaBetaRM().getRowVector(p).dotProduct(YY.getRowVector(p));
    }).sum();

    logNorm -= Math.pow(globalNaturalParameters.getTheta_beta0(), 2)
            / (4 * globalNaturalParameters.getTheta_Minus1());

    return logNorm;
}

From source file:eu.amidst.core.exponentialfamily.EF_Normal_NormalParents.java

/**
 * {@inheritDoc}/*w  w  w . ja v  a2 s .c  o m*/
 */
@Override
public void updateNaturalFromMomentParameters() {

    /*
     * First step: means and convariances
     */
    CompoundVector globalMomentParam = (CompoundVector) this.momentParameters;
    double mean_X = globalMomentParam.getXYbaseMatrix().getEntry(0);
    RealVector mean_Y = globalMomentParam.getTheta_beta0BetaRV();

    double cov_XX = globalMomentParam.getcovbaseMatrix().getEntry(0, 0) - mean_X * mean_X;
    RealMatrix cov_YY = globalMomentParam.getcovbaseMatrix().getSubMatrix(1, nOfParents, 1, nOfParents)
            .subtract(mean_Y.outerProduct(mean_Y));
    RealVector cov_XY = globalMomentParam.getcovbaseMatrix().getSubMatrix(0, 0, 1, nOfParents).getRowVector(0)
            .subtract(mean_Y.mapMultiply(mean_X));
    //RealVector cov_YX = cov_XY; //outerProduct transposes the vector automatically

    /*
     * Second step: betas and variance
     */
    RealMatrix cov_YYInverse = new LUDecompositionImpl(cov_YY).getSolver().getInverse();
    RealVector beta = cov_YYInverse.preMultiply(cov_XY);

    this.betas = new double[beta.getDimension()];
    for (int i = 0; i < beta.getDimension(); i++) {
        this.betas[i] = beta.getEntry(i);
    }
    this.beta0 = mean_X - beta.dotProduct(mean_Y);
    this.variance = cov_XX - beta.dotProduct(cov_XY);

}

From source file:eu.amidst.core.exponentialfamily.EF_Normal_NormalParents2.java

/**
 * {@inheritDoc}//from w w w  . j  a va  2s.  co m
 */
@Override
public void updateNaturalFromMomentParameters() {

    /*
     * First step: means and convariances
     */
    CompoundVector globalMomentParam = (CompoundVector) this.momentParameters;
    double mean_X = globalMomentParam.getXYbaseMatrix().getEntry(0);
    RealVector mean_Y = globalMomentParam.getTheta_beta0BetaRV();

    double cov_XX = globalMomentParam.getcovbaseMatrix().getEntry(0, 0) - mean_X * mean_X;
    RealMatrix cov_YY = globalMomentParam.getcovbaseMatrix().getSubMatrix(1, nOfParents, 1, nOfParents)
            .subtract(mean_Y.outerProduct(mean_Y));
    RealVector cov_XY = globalMomentParam.getcovbaseMatrix().getSubMatrix(0, 0, 1, nOfParents).getRowVector(0)
            .subtract(mean_Y.mapMultiply(mean_X));
    //RealVector cov_YX = cov_XY; //outerProduct transposes the vector automatically

    /*
     * Second step: betas and variance
     */
    RealMatrix cov_YYInverse = new LUDecompositionImpl(cov_YY).getSolver().getInverse();
    RealVector beta = cov_YYInverse.preMultiply(cov_XY);

    double beta_0 = mean_X - beta.dotProduct(mean_Y);
    double variance = cov_XX - beta.dotProduct(cov_XY);

    /*
     * Third step: natural parameters (5 in total)
     */

    /*
     * 1) theta_0
     */
    double theta_0 = beta_0 / variance;
    double[] theta_0array = { theta_0 };

    /*
     * 2) theta_0Theta
     */
    double variance2Inv = 1.0 / (2 * variance);
    RealVector theta_0Theta = beta.mapMultiply(-beta_0 / variance);
    ((CompoundVector) this.naturalParameters)
            .setXYbaseVector(new ArrayRealVector(theta_0array, theta_0Theta.getData()));

    /*
     * 3) theta_Minus1
     */
    double theta_Minus1 = -variance2Inv;

    /*
     * 4) theta_beta
     */
    RealVector theta_beta = beta.mapMultiply(variance2Inv);

    /*
     * 5) theta_betaBeta
     */
    RealMatrix theta_betaBeta = beta.outerProduct(beta).scalarMultiply(-variance2Inv * 2);

    /*
     * Store natural parameters
     */
    RealMatrix natural_XY = new Array2DRowRealMatrix(nOfParents + 1, nOfParents + 1);
    double[] theta_Minus1array = { theta_Minus1 };
    RealVector covXY = new ArrayRealVector(theta_Minus1array, theta_beta.getData());
    natural_XY.setColumnVector(0, covXY);
    natural_XY.setRowVector(0, covXY);
    natural_XY.setSubMatrix(theta_betaBeta.getData(), 1, 1);
    ((CompoundVector) this.naturalParameters).setcovbaseVector(natural_XY);

}