Search in sources :

Example 6 with ComparableToDouble

use of org.ojalgo.type.keyvalue.ComparableToDouble in project ojAlgo by optimatika.

the class GaussianProcessTest method testTutorial.

@Test
public void testTutorial() {
    final GaussianField.Covariance<Double> tmpCovar = new GaussianField.Covariance<Double>() {

        public void calibrate(final Collection<ComparableToDouble<Double>> observations, final Mean<Double> mean) {
        }

        public double invoke(final Double anArg1, final Double anArg2) {
            return this.invoke(anArg1.doubleValue(), anArg2.doubleValue());
        }

        double invoke(final double anArg1, final double anArg2) {
            final double tmpSF = 1.27;
            final double tmpSN = 0.3;
            final double tmpL = 1.0;
            double retVal = tmpSF * tmpSF * PrimitiveFunction.EXP.invoke(-PrimitiveFunction.POW.invoke(anArg1 - anArg2, TWO) / (TWO * tmpL * tmpL));
            if (anArg1 == anArg2) {
                retVal += tmpSN * tmpSN;
            }
            return retVal;
        }
    };
    final GaussianProcess tmpProc = new GaussianProcess(tmpCovar);
    tmpProc.addObservation(-1.5, -1.6);
    tmpProc.addObservation(-1.0, -1.1);
    tmpProc.addObservation(-0.75, -0.4);
    tmpProc.addObservation(-0.4, 0.1);
    tmpProc.addObservation(-0.25, 0.5);
    tmpProc.addObservation(0.0, 0.8);
    final PrimitiveDenseStore tmpExpected = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 1.7029, 1.423379254178694, 1.2174807940480699, 0.8807634427271873, 0.7384394292014367, 0.5236319646022823 }, { 1.423379254178694, 1.7029, 1.5632762838868954, 1.3472073239852407, 1.2174807940480699, 0.9782733010505065 }, { 1.2174807940480699, 1.5632762838868954, 1.7029, 1.5170744874003474, 1.423379254178694, 1.2174807940480699 }, { 0.8807634427271873, 1.3472073239852407, 1.5170744874003474, 1.7029, 1.5948565596534579, 1.4888943550870049 }, { 0.7384394292014367, 1.2174807940480699, 1.423379254178694, 1.5948565596534579, 1.7029, 1.5632762838868954 }, { 0.5236319646022823, 0.9782733010505065, 1.2174807940480699, 1.4888943550870049, 1.5632762838868954, 1.7029 } });
    TestUtils.assertEquals(tmpExpected, tmpProc.getCovariances(), new NumberContext(8, 2));
    final Normal tmpDistr = tmpProc.getDistribution(0.2);
    TestUtils.assertEquals("Mean", 0.911277527445648, tmpDistr.getExpected(), 0.005);
    TestUtils.assertEquals("Variance", 0.20604504349662636, tmpDistr.getVariance(), 0.005);
}
Also used : Mean(org.ojalgo.random.process.GaussianField.Mean) Collection(java.util.Collection) NumberContext(org.ojalgo.type.context.NumberContext) Normal(org.ojalgo.random.Normal) ComparableToDouble(org.ojalgo.type.keyvalue.ComparableToDouble) PrimitiveDenseStore(org.ojalgo.matrix.store.PrimitiveDenseStore) Test(org.junit.jupiter.api.Test)

Aggregations

PrimitiveDenseStore (org.ojalgo.matrix.store.PrimitiveDenseStore)6 ComparableToDouble (org.ojalgo.type.keyvalue.ComparableToDouble)6 Collection (java.util.Collection)1 Test (org.junit.jupiter.api.Test)1 Normal (org.ojalgo.random.Normal)1 Mean (org.ojalgo.random.process.GaussianField.Mean)1 NumberContext (org.ojalgo.type.context.NumberContext)1