Example usage for org.apache.commons.math3.random CorrelatedRandomVectorGenerator CorrelatedRandomVectorGenerator

List of usage examples for org.apache.commons.math3.random CorrelatedRandomVectorGenerator CorrelatedRandomVectorGenerator

Introduction

In this page you can find the example usage for org.apache.commons.math3.random CorrelatedRandomVectorGenerator CorrelatedRandomVectorGenerator.

Prototype

public CorrelatedRandomVectorGenerator(double[] mean, RealMatrix covariance, double small,
        NormalizedRandomGenerator generator) 

Source Link

Document

Builds a correlated random vector generator from its mean vector and covariance matrix.

Usage

From source file:com.itemanalysis.psychometrics.mixture.MvNormalComponentDistribution.java

public void generateStartValues(RealMatrix x, RealMatrix mean, RealMatrix cov) {
    JDKRandomGenerator jg = new JDKRandomGenerator();
    NormalizedRandomGenerator rg = new GaussianRandomGenerator(jg);
    CorrelatedRandomVectorGenerator sg = null;
    sg = new CorrelatedRandomVectorGenerator(mean.getColumn(0), cov, 0.00001, rg);
    mu = new Array2DRowRealMatrix(sg.nextVector());
}

From source file:com.analog.lyric.dimple.test.solvers.sumproduct.TestSampledFactors.java

/**
 * Adapted from MATLAB test4 in tests/algoGaussian/testSampledFactors.m
 *///from   w ww. ja  v  a  2  s  . c om
@Test
public void sampledComplexProduct() {
    // NOTE: test may fail if seed is changed! We keep the number of samples down so that the test doesn't
    // take too long. Increasing the samples produces better results.

    testRand.setSeed(42);

    try (CurrentModel cur = using(new FactorGraph())) {
        final Complex a = complex("a");
        final Complex b = complex("b");
        final Complex c = product(a, b);

        double[] aMean = new double[] { 10, 10 };
        RealMatrix aCovariance = randCovariance(2);
        a.setPrior(new MultivariateNormal(aMean, aCovariance.getData()));

        double[] bMean = new double[] { -20, 20 };
        RealMatrix bCovariance = randCovariance(2);
        b.setPrior(new MultivariateNormalParameters(bMean, bCovariance.getData()));

        GaussianRandomGenerator normalGenerator = new GaussianRandomGenerator(testRand);
        CorrelatedRandomVectorGenerator aGenerator = new CorrelatedRandomVectorGenerator(aMean, aCovariance,
                1e-12, normalGenerator);
        CorrelatedRandomVectorGenerator bGenerator = new CorrelatedRandomVectorGenerator(bMean, bCovariance,
                1e-12, normalGenerator);

        StorelessCovariance expectedCov = new StorelessCovariance(2);

        final int nSamples = 10000;

        RealVector expectedMean = MatrixUtils.createRealVector(new double[2]);
        double[] cSample = new double[2];

        for (int i = 0; i < nSamples; ++i) {
            double[] aSample = aGenerator.nextVector();
            double[] bSample = bGenerator.nextVector();

            // Compute complex product
            cSample[0] = aSample[0] * bSample[0] - aSample[1] * bSample[1];
            cSample[1] = aSample[0] * bSample[1] + aSample[1] * bSample[0];

            expectedMean.addToEntry(0, cSample[0]);
            expectedMean.addToEntry(1, cSample[1]);

            expectedCov.increment(cSample);
        }

        expectedMean.mapDivideToSelf(nSamples); // normalize

        SumProductSolverGraph sfg = requireNonNull(cur.graph.setSolverFactory(new SumProductSolver()));
        sfg.setOption(GibbsOptions.numSamples, nSamples);

        sfg.solve();

        MultivariateNormalParameters cBelief = requireNonNull(c.getBelief());

        RealVector observedMean = MatrixUtils.createRealVector(cBelief.getMean());
        double scaledMeanDistance = expectedMean.getDistance(observedMean) / expectedMean.getNorm();

        //         System.out.format("expectedMean = %s\n", expectedMean);
        //         System.out.format("observedMean = %s\n", observedMean);
        //         System.out.println(scaledMeanDistance);

        assertEquals(0.0, scaledMeanDistance, .02);

        RealMatrix expectedCovariance = expectedCov.getCovarianceMatrix();
        RealMatrix observedCovariance = MatrixUtils.createRealMatrix(cBelief.getCovariance());
        RealMatrix diffCovariance = expectedCovariance.subtract(observedCovariance);

        double scaledCovarianceDistance = diffCovariance.getNorm() / expectedCovariance.getNorm();

        //         System.out.println(expectedCovariance);
        //         System.out.println(expectedCovariance.getNorm());
        //         System.out.println(diffCovariance);
        //         System.out.println(diffCovariance.getNorm());
        //         System.out.println(diffCovariance.getNorm() / expectedCovariance.getNorm());

        assertEquals(0.0, scaledCovarianceDistance, .2);
    }
}

From source file:inputHandling.DataGenCorrCommons.java

@Override
public TupleList genData(int dimensions, int tupleCount) {
    logger.info("Generating uniform correlated Data with " + tupleCount + " Tuples in " + dimensions
            + " dimensions, Coeff.: -" + this.coeff);
    genMatrices(dimensions);/*from  w  w  w . j ava 2 s . c  o m*/
    RealMatrix covariance = MatrixUtils.createRealMatrix(cov);
    RandomGenerator rg = new JDKRandomGenerator(Math.round(seed));
    UniformRandomGenerator rawGenerator = new UniformRandomGenerator(rg);
    double small = 1.0e-12 * covariance.getNorm();
    CorrelatedRandomVectorGenerator generator = new CorrelatedRandomVectorGenerator(mean, covariance, small,
            rawGenerator);

    // Generate the Tuples
    TupleList tupleList = new TupleList(dimensions);
    for (int j = 0; j < tupleCount; j++) {
        double[] randomVector = generator.nextVector();
        Tuple tuple = new Tuple(randomVector);
        tupleList.add(tuple);
    }
    return tupleList;
}

From source file:inputHandling.DataGenCorrGaussCommons.java

@Override
public TupleList genData(int dimensions, int tupleCount) {
    logger.info("Generating gaussian correlated Data with " + tupleCount + " Tuples in " + dimensions
            + " dimensions, Coeff.: -" + this.coeff);
    genMatrices(dimensions);/* ww  w. j  ava 2s.  c o m*/
    RealMatrix covariance = MatrixUtils.createRealMatrix(cov);
    RandomGenerator rg = new JDKRandomGenerator(Math.round(seed));
    GaussianRandomGenerator rawGenerator = new GaussianRandomGenerator(rg);
    double small = 1.0e-12 * covariance.getNorm();
    CorrelatedRandomVectorGenerator generator = new CorrelatedRandomVectorGenerator(mean, covariance, small,
            rawGenerator);

    // Generate the Tuples
    TupleList tupleList = new TupleList(dimensions);
    for (int j = 0; j < tupleCount; j++) {
        double[] randomVector = generator.nextVector();
        Tuple tuple = new Tuple(randomVector);
        tupleList.add(tuple);
    }
    return tupleList;
}

From source file:inputHandling.DataGenAntiCorrCommons.java

@Override
public TupleList genData(int dimensions, int tupleCount) {
    logger.info("Generating uniform anti-correlated Data with " + tupleCount + " Tuples in " + dimensions
            + " dimensions, Coeff.: -" + this.coeff);
    genMatrices(dimensions);//from  w  w w  .  j  ava2  s  .  co m
    RealMatrix covariance = MatrixUtils.createRealMatrix(cov);
    RandomGenerator rg = new JDKRandomGenerator(Math.round(seed));
    UniformRandomGenerator rawGenerator = new UniformRandomGenerator(rg);
    double small = 1.0e-12 * covariance.getNorm();
    CorrelatedRandomVectorGenerator generator = new CorrelatedRandomVectorGenerator(mean, covariance, small,
            rawGenerator);

    TupleList tupleList = new TupleList(dimensions);
    // Invert the Values, to receive Anti-Correlation
    for (int j = 0; j < tupleCount; j++) {
        double[] randomVector = generator.nextVector();
        for (int i = 0; i < dimensions; i++) {
            if (j % 2 == 0 && i % 2 == 0)
                randomVector[i] = 2 * mean[i] - randomVector[i];
            else if (j % 2 != 0 && i % 2 != 0)
                randomVector[i] = 2 * mean[i] - randomVector[i];
            else
                randomVector[i] = randomVector[i];
        }
        Tuple tuple = new Tuple(randomVector);
        tupleList.add(tuple);
    }
    return tupleList;
}

From source file:inputHandling.DataGenAntiCorrGaussCommons.java

@Override
public TupleList genData(int dimensions, int tupleCount) {
    logger.info("Generating gaussian anti-correlated Data with " + tupleCount + " Tuples in " + dimensions
            + " dimensions, Coeff.: -" + this.coeff);
    genMatrices(dimensions);/*from w w w .ja  v  a  2  s.  c  om*/
    RealMatrix covariance = MatrixUtils.createRealMatrix(cov);
    RandomGenerator rg = new JDKRandomGenerator(Math.round(seed));
    GaussianRandomGenerator rawGenerator = new GaussianRandomGenerator(rg);
    double small = 1.0e-12 * covariance.getNorm();
    CorrelatedRandomVectorGenerator generator = new CorrelatedRandomVectorGenerator(mean, covariance, small,
            rawGenerator);

    TupleList tupleList = new TupleList(dimensions);
    // Invert the Values, to receive Anti-Correlation
    for (int j = 0; j < tupleCount; j++) {
        double[] randomVector = generator.nextVector();
        for (int i = 0; i < dimensions; i++) {
            if (j % 2 == 0 && i % 2 == 0)
                randomVector[i] = 2 * mean[i] - randomVector[i];
            else if (j % 2 != 0 && i % 2 != 0)
                randomVector[i] = 2 * mean[i] - randomVector[i];
            else
                randomVector[i] = randomVector[i];
        }
        Tuple tuple = new Tuple(randomVector);
        tupleList.add(tuple);
    }
    return tupleList;
}