Search in sources :

Example 6 with Primitive64Array

use of org.ojalgo.array.Primitive64Array in project ojAlgo by optimatika.

the class SampleSetTest method testQuartileSize1.

@Test
public void testQuartileSize1() {
    final Primitive64Array tmpSamples = Primitive64Array.wrap(new double[] { 100.0 });
    final SampleSet tmpSampleSet = SampleSet.wrap(tmpSamples);
    TestUtils.assertEquals(100.0, tmpSampleSet.getQuartile1());
    TestUtils.assertEquals(100.0, tmpSampleSet.getQuartile2());
    TestUtils.assertEquals(100.0, tmpSampleSet.getQuartile3());
}
Also used : Primitive64Array(org.ojalgo.array.Primitive64Array) Test(org.junit.jupiter.api.Test)

Example 7 with Primitive64Array

use of org.ojalgo.array.Primitive64Array in project ojAlgo by optimatika.

the class SampleSetTest method testEmptySet.

@Test
public void testEmptySet() {
    final Primitive64Array tmpSamples = Primitive64Array.wrap(new double[] {});
    final SampleSet tmpSampleSet = SampleSet.wrap(tmpSamples);
    try {
        // The key thing is that all methods return something
        // 0.0, NaN, Inf... doesn't really matter...
        TestUtils.assertEquals(0.0, tmpSampleSet.getFirst());
        TestUtils.assertEquals(0.0, tmpSampleSet.getInterquartileRange());
        TestUtils.assertEquals(0.0, tmpSampleSet.getLargest());
        TestUtils.assertEquals(0.0, tmpSampleSet.getLast());
        TestUtils.assertEquals(0.0, tmpSampleSet.getMaximum());
        TestUtils.assertEquals(Double.NaN, tmpSampleSet.getMean());
        TestUtils.assertEquals(0.0, tmpSampleSet.getMedian());
        TestUtils.assertEquals(0.0, tmpSampleSet.getMinimum());
        TestUtils.assertEquals(0.0, tmpSampleSet.getQuartile1());
        TestUtils.assertEquals(0.0, tmpSampleSet.getQuartile2());
        TestUtils.assertEquals(0.0, tmpSampleSet.getQuartile3());
        TestUtils.assertTrue(Double.isInfinite(tmpSampleSet.getSmallest()));
        TestUtils.assertEquals(0.0, tmpSampleSet.getStandardDeviation());
        TestUtils.assertEquals(0.0, tmpSampleSet.getSumOfSquares());
        TestUtils.assertEquals(0.0, tmpSampleSet.getVariance());
    } catch (final Exception exception) {
        // Important NOT to throw an exception!
        TestUtils.fail(exception.getMessage());
    }
}
Also used : Primitive64Array(org.ojalgo.array.Primitive64Array) Test(org.junit.jupiter.api.Test)

Example 8 with Primitive64Array

use of org.ojalgo.array.Primitive64Array in project ojAlgo by optimatika.

the class SampleSetTest method testQuartileSize3.

@Test
public void testQuartileSize3() {
    final Primitive64Array tmpSamples = Primitive64Array.wrap(new double[] { 100.0, 200.0, 300.0 });
    final SampleSet tmpSampleSet = SampleSet.wrap(tmpSamples);
    TestUtils.assertEquals(125.0, tmpSampleSet.getQuartile1());
    TestUtils.assertEquals(200.0, tmpSampleSet.getQuartile2());
    TestUtils.assertEquals(275.0, tmpSampleSet.getQuartile3());
}
Also used : Primitive64Array(org.ojalgo.array.Primitive64Array) Test(org.junit.jupiter.api.Test)

Example 9 with Primitive64Array

use of org.ojalgo.array.Primitive64Array in project ojAlgo by optimatika.

the class SampleSetTest method testQuartileSize8.

@Test
public void testQuartileSize8() {
    final Primitive64Array tmpSamples = Primitive64Array.wrap(new double[] { 100.0, 200.0, 300.0, 400.0, 500.0, 600.0, 700.0, 800.0 });
    final SampleSet tmpSampleSet = SampleSet.wrap(tmpSamples);
    TestUtils.assertEquals(250.0, tmpSampleSet.getQuartile1());
    TestUtils.assertEquals(450.0, tmpSampleSet.getQuartile2());
    TestUtils.assertEquals(650.0, tmpSampleSet.getQuartile3());
}
Also used : Primitive64Array(org.ojalgo.array.Primitive64Array) Test(org.junit.jupiter.api.Test)

Example 10 with Primitive64Array

use of org.ojalgo.array.Primitive64Array 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)

Aggregations

Primitive64Array (org.ojalgo.array.Primitive64Array)20 Test (org.junit.jupiter.api.Test)13 DivideAndConquer (org.ojalgo.concurrent.DivideAndConquer)3 Tag (org.junit.jupiter.api.Tag)2 BigDecimal (java.math.BigDecimal)1 SimplePortfolio (org.ojalgo.finance.portfolio.SimplePortfolio)1 Result (org.ojalgo.optimisation.Optimisation.Result)1 LogNormal (org.ojalgo.random.LogNormal)1 Normal (org.ojalgo.random.Normal)1 SampleSet (org.ojalgo.random.SampleSet)1 NumberContext (org.ojalgo.type.context.NumberContext)1