Search in sources :

Example 1 with Normal

use of org.ojalgo.random.Normal in project ojAlgo by optimatika.

the class GaussianProcess method getDistribution.

public Normal getDistribution(final double evaluationPoint) {
    final Normal1D tmpVal = this.getDistribution(new Double[] { evaluationPoint });
    final double tmpLocation = tmpVal.getExpected().doubleValue(0);
    final double tmpScale = tmpVal.getStandardDeviation().doubleValue(0);
    return new Normal(tmpLocation, tmpScale);
}
Also used : Normal(org.ojalgo.random.Normal) Normal1D(org.ojalgo.random.Normal1D)

Example 2 with Normal

use of org.ojalgo.random.Normal in project ojAlgo by optimatika.

the class GaussianProcess method step.

@Override
protected double step(final double currentValue, final double stepSize, final double normalisedRandomIncrement) {
    final Normal tmpDistr = this.getDistribution(stepSize);
    final double retVal = (normalisedRandomIncrement * tmpDistr.getStandardDeviation()) + tmpDistr.getExpected();
    this.addObservation(this.getObservations().last().key + stepSize, retVal);
    return retVal;
}
Also used : Normal(org.ojalgo.random.Normal)

Example 3 with Normal

use of org.ojalgo.random.Normal in project ojAlgo by optimatika.

the class DecompositionProblems method testP20160419.

/**
 * A user reported problems related to calculating the pseudoinverse for large (2000x2000) matrices.
 */
@Test
@Tag("slow")
public void testP20160419() {
    final PrimitiveDenseStore tmpOrg = PrimitiveDenseStore.FACTORY.makeFilled(2000, 2000, new Normal());
    final SingularValue<Double> tmpRaw = new RawSingularValue();
    try {
        final MatrixStore<Double> tmpInv = tmpRaw.invert(tmpOrg);
        TestUtils.assertEquals(tmpOrg, tmpOrg.multiply(tmpInv).multiply(tmpOrg), NumberContext.getGeneral(6, 6));
    } catch (final RecoverableCondition exception) {
        exception.printStackTrace();
    }
}
Also used : RecoverableCondition(org.ojalgo.RecoverableCondition) Normal(org.ojalgo.random.Normal) PrimitiveDenseStore(org.ojalgo.matrix.store.PrimitiveDenseStore) Test(org.junit.jupiter.api.Test) Tag(org.junit.jupiter.api.Tag)

Example 4 with Normal

use of org.ojalgo.random.Normal in project ojAlgo by optimatika.

the class GeometricBrownianMotionTest method testLogNormal.

@Test
@Tag("unstable")
public void testLogNormal() {
    final int tmpPeriods = 10000;
    final double tmpFactoryExpected = 1.05;
    final double tmpFactoryStdDev = PrimitiveFunction.ABS.invoke(new Normal(0.0, (tmpFactoryExpected - ONE)).doubleValue());
    final Normal tmpFactoryDistr = new Normal(tmpFactoryExpected, tmpFactoryStdDev);
    TestUtils.assertEquals("Factory Expected", tmpFactoryExpected, tmpFactoryDistr.getExpected(), 1E-14 / PrimitiveMath.THREE);
    TestUtils.assertEquals("Factory Std Dev", tmpFactoryStdDev, tmpFactoryDistr.getStandardDeviation(), 1E-14 / PrimitiveMath.THREE);
    final Primitive64Array tmpRawValues = Primitive64Array.make(tmpPeriods + 1);
    tmpRawValues.data[0] = ONE;
    for (int t = 1; t < tmpRawValues.count(); t++) {
        tmpRawValues.data[t] = tmpRawValues.data[t - 1] * tmpFactoryDistr.doubleValue();
    }
    final Primitive64Array tmpQuotient = Primitive64Array.make(tmpPeriods);
    final Primitive64Array tmpLogDiffs = Primitive64Array.make(tmpPeriods);
    for (int t = 0; t < tmpPeriods; t++) {
        tmpQuotient.data[t] = tmpRawValues.data[t + 1] / tmpRawValues.data[t];
        tmpLogDiffs.data[t] = PrimitiveFunction.LOG.invoke(tmpRawValues.data[t + 1]) - PrimitiveFunction.LOG.invoke(tmpRawValues.data[t]);
    }
    final SampleSet tmpQuotientSet = SampleSet.wrap(tmpQuotient);
    final SampleSet tmpLogDiffsSet = SampleSet.wrap(tmpLogDiffs);
    final GeometricBrownianMotion tmpProcess = GeometricBrownianMotion.estimate(tmpRawValues, ONE);
    final Normal tmpQuotienDistr = new Normal(tmpQuotientSet.getMean(), tmpQuotientSet.getStandardDeviation());
    final LogNormal tmpLogDiffDistr = new LogNormal(tmpLogDiffsSet.getMean(), tmpLogDiffsSet.getStandardDeviation());
    final LogNormal tmpProcessDistr = tmpProcess.getDistribution(ONE);
    TestUtils.assertEquals("Expected", tmpLogDiffDistr.getExpected(), tmpProcessDistr.getExpected(), 1E-14 / PrimitiveMath.THREE);
    TestUtils.assertEquals("Geometric Mean", tmpLogDiffDistr.getGeometricMean(), tmpProcessDistr.getGeometricMean(), 1E-14 / PrimitiveMath.THREE);
    TestUtils.assertEquals("Geometric Standard Deviation", tmpLogDiffDistr.getGeometricStandardDeviation(), tmpProcessDistr.getGeometricStandardDeviation(), 1E-14 / PrimitiveMath.THREE);
    TestUtils.assertEquals("Standard Deviation", tmpLogDiffDistr.getStandardDeviation(), tmpProcessDistr.getStandardDeviation(), 1E-14 / PrimitiveMath.THREE);
    TestUtils.assertEquals("Variance", tmpLogDiffDistr.getVariance(), tmpProcessDistr.getVariance(), 1E-14 / PrimitiveMath.THREE);
    double tmpFactoryVal = tmpFactoryDistr.getExpected();
    double tmpQuotienVal = tmpQuotienDistr.getExpected();
    double tmpLogDiffVal = tmpLogDiffDistr.getExpected();
    double tmpProcessVal = tmpProcessDistr.getExpected();
    double tmpGeometrVal = tmpProcessDistr.getGeometricMean();
    if (RandomProcessTests.DEBUG) {
        this.logDebug("Expected", tmpFactoryVal, tmpQuotienVal, tmpLogDiffVal, tmpProcessVal, tmpGeometrVal);
    }
    final double tmpDeltaExpected = (1E-14 / THREE) * THOUSAND * THOUSAND * THOUSAND * HUNDRED;
    TestUtils.assertEquals(tmpQuotienVal, tmpLogDiffVal, tmpDeltaExpected);
    TestUtils.assertEquals(tmpQuotienVal, tmpProcessVal, tmpDeltaExpected);
    TestUtils.assertEquals(true, tmpGeometrVal <= tmpProcessVal);
    tmpFactoryVal = tmpFactoryDistr.getStandardDeviation();
    tmpQuotienVal = tmpQuotienDistr.getStandardDeviation();
    tmpLogDiffVal = tmpLogDiffDistr.getStandardDeviation();
    tmpProcessVal = tmpProcessDistr.getStandardDeviation();
    tmpGeometrVal = tmpProcessDistr.getGeometricStandardDeviation();
    if (RandomProcessTests.DEBUG) {
        this.logDebug("Std Dev", tmpFactoryVal, tmpQuotienVal, tmpLogDiffVal, tmpProcessVal, tmpGeometrVal);
    }
    final double tmpDeltaStdDev = (1E-14 / THREE) * THOUSAND * THOUSAND * THOUSAND * THOUSAND;
    TestUtils.assertEquals(tmpQuotienVal, tmpLogDiffVal, tmpDeltaStdDev);
    TestUtils.assertEquals(tmpQuotienVal, tmpProcessVal, tmpDeltaStdDev);
    tmpFactoryVal = tmpFactoryDistr.getVariance();
    tmpQuotienVal = tmpQuotienDistr.getVariance();
    tmpLogDiffVal = tmpLogDiffDistr.getVariance();
    tmpProcessVal = tmpProcessDistr.getVariance();
    tmpGeometrVal = tmpProcessDistr.getGeometricStandardDeviation() * tmpLogDiffDistr.getGeometricStandardDeviation();
    if (RandomProcessTests.DEBUG) {
        this.logDebug("Var", tmpFactoryVal, tmpQuotienVal, tmpLogDiffVal, tmpProcessVal, tmpGeometrVal);
    }
    final double tmpDeltaVar = (1E-14 / THREE) * THOUSAND * THOUSAND * THOUSAND * HUNDRED;
    TestUtils.assertEquals(tmpQuotienVal, tmpLogDiffVal, tmpDeltaVar);
    TestUtils.assertEquals(tmpQuotienVal, tmpProcessVal, tmpDeltaVar);
    tmpFactoryVal = tmpRawValues.data[tmpPeriods];
    tmpQuotienVal = PrimitiveFunction.POW.invoke(tmpQuotienDistr.getExpected(), tmpPeriods);
    tmpLogDiffVal = PrimitiveFunction.POW.invoke(tmpLogDiffDistr.getExpected(), tmpPeriods);
    tmpProcessVal = tmpProcess.getExpected(tmpPeriods);
    tmpGeometrVal = PrimitiveFunction.POW.invoke(tmpProcessDistr.getGeometricMean(), tmpPeriods);
    if (RandomProcessTests.DEBUG) {
        this.logDebug("Final Value", tmpFactoryVal, tmpQuotienVal, tmpLogDiffVal, tmpProcessVal, tmpGeometrVal);
    }
    final double tmpDeltaFinal = (1E-14 / THREE) * THOUSAND;
    TestUtils.assertEquals(ONE, tmpGeometrVal / tmpFactoryVal, tmpDeltaFinal);
}
Also used : SampleSet(org.ojalgo.random.SampleSet) Primitive64Array(org.ojalgo.array.Primitive64Array) LogNormal(org.ojalgo.random.LogNormal) Normal(org.ojalgo.random.Normal) LogNormal(org.ojalgo.random.LogNormal) Test(org.junit.jupiter.api.Test) Tag(org.junit.jupiter.api.Tag)

Example 5 with Normal

use of org.ojalgo.random.Normal in project ojAlgo by optimatika.

the class DesignCase method testRandomUnderdetermined.

@Test
public void testRandomUnderdetermined() {
    final PhysicalStore<Double> tmpA = PrimitiveDenseStore.FACTORY.makeFilled(3, 9, new Normal());
    final PhysicalStore<Double> tmpB = PrimitiveDenseStore.FACTORY.makeFilled(3, 1, new Normal());
    final QR<Double> tmpQR = QR.PRIMITIVE.make(tmpA);
    tmpQR.setFullSize(false);
    tmpQR.decompose(tmpA);
    final PhysicalStore<Double> tmpX = tmpQR.getSolution(tmpB).copy();
    // BasicLogger.debug("Straigt X: " + tmpX.toString());
    tmpB.modifyMatching(PrimitiveFunction.SUBTRACT, tmpA.multiply(tmpX));
// BasicLogger.debug("Residual B: " + tmpB.toString());
}
Also used : Normal(org.ojalgo.random.Normal) Test(org.junit.jupiter.api.Test)

Aggregations

Normal (org.ojalgo.random.Normal)8 Test (org.junit.jupiter.api.Test)5 Tag (org.junit.jupiter.api.Tag)3 PrimitiveDenseStore (org.ojalgo.matrix.store.PrimitiveDenseStore)2 ComplexNumber (org.ojalgo.scalar.ComplexNumber)2 NumberContext (org.ojalgo.type.context.NumberContext)2 BigDecimal (java.math.BigDecimal)1 Collection (java.util.Collection)1 RecoverableCondition (org.ojalgo.RecoverableCondition)1 Primitive64Array (org.ojalgo.array.Primitive64Array)1 BasicMatrix (org.ojalgo.matrix.BasicMatrix)1 LogNormal (org.ojalgo.random.LogNormal)1 Normal1D (org.ojalgo.random.Normal1D)1 SampleSet (org.ojalgo.random.SampleSet)1 Mean (org.ojalgo.random.process.GaussianField.Mean)1 ComparableToDouble (org.ojalgo.type.keyvalue.ComparableToDouble)1