use of org.ojalgo.random.LogNormal 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);
}
use of org.ojalgo.random.LogNormal in project ojAlgo by optimatika.
the class GeometricBrownianMotionTest method testDistributionConsistency.
@Test
public void testDistributionConsistency() {
final double tmpError = new NumberContext(7, 9).epsilon();
GeometricBrownianMotion tmpProcess;
LogNormal tmpDistribution;
for (int tmpCreateHorizon = 1; tmpCreateHorizon < 10; tmpCreateHorizon++) {
for (double tmpExpected = 1.0; tmpExpected <= 2.0; tmpExpected += 0.1) {
for (double tmpVariance = 0.0; tmpVariance <= 1.0; tmpVariance += 0.1) {
tmpProcess = GeometricBrownianMotion.make(tmpExpected, tmpVariance, tmpCreateHorizon);
TestUtils.assertEquals(tmpExpected, tmpProcess.getExpected(tmpCreateHorizon), tmpError);
TestUtils.assertEquals(tmpVariance, tmpProcess.getVariance(tmpCreateHorizon), tmpError);
tmpDistribution = tmpProcess.getDistribution(tmpCreateHorizon);
TestUtils.assertEquals(tmpExpected, tmpDistribution.getExpected(), tmpError);
TestUtils.assertEquals(tmpVariance, tmpDistribution.getVariance(), tmpError);
for (int tmpTestHorison = 0; tmpTestHorison < 10; tmpTestHorison++) {
tmpDistribution = tmpProcess.getDistribution(tmpTestHorison);
TestUtils.assertEquals(tmpDistribution.getExpected(), tmpProcess.getExpected(tmpTestHorison), tmpError);
TestUtils.assertEquals(tmpDistribution.getVariance(), tmpProcess.getVariance(tmpTestHorison), tmpError);
TestUtils.assertEquals(tmpDistribution.getStandardDeviation(), tmpProcess.getStandardDeviation(tmpTestHorison), tmpError);
TestUtils.assertEquals(tmpDistribution.getUpperConfidenceQuantile(0.95), tmpProcess.getUpperConfidenceQuantile(tmpTestHorison, 0.95), tmpError);
TestUtils.assertEquals(tmpDistribution.getLowerConfidenceQuantile(0.95), tmpProcess.getLowerConfidenceQuantile(tmpTestHorison, 0.95), tmpError);
TestUtils.assertEquals(tmpDistribution.getUpperConfidenceQuantile(0.05), tmpProcess.getUpperConfidenceQuantile(tmpTestHorison, 0.05), tmpError);
TestUtils.assertEquals(tmpDistribution.getLowerConfidenceQuantile(0.05), tmpProcess.getLowerConfidenceQuantile(tmpTestHorison, 0.05), tmpError);
}
}
}
}
}
use of org.ojalgo.random.LogNormal in project ojAlgo by optimatika.
the class GeometricBrownianMotionTest method testWikipediaCases.
@Test
public void testWikipediaCases() {
new GeometricBrownianMotion(1.0, 0.2);
final GeometricBrownianMotion tmpGreenProc = new GeometricBrownianMotion(0.5, 0.5);
final GeometricBrownianMotion tmpProc = tmpGreenProc;
for (int t = 1; t <= 100; t++) {
final double tmpStep = t / HUNDRED;
final LogNormal tmpDist = tmpProc.getDistribution(tmpStep);
RandomUtils.erfi(0.95);
final double tmpProcUpper = tmpProc.getUpperConfidenceQuantile(tmpStep, 0.95);
final double tmpProcFact = PrimitiveFunction.SQRT.invoke(tmpProcUpper / tmpProc.getLowerConfidenceQuantile(tmpStep, 0.95));
final double tmpDistUpper = tmpDist.getQuantile(0.975);
final double tmpDistFact = PrimitiveFunction.SQRT.invoke(tmpDistUpper / tmpDist.getQuantile(0.025));
if (RandomProcessTests.DEBUG) {
BasicLogger.debug("Step={} ProcFact={} DistFact={} ProcMedian={} DistMedian={} expected={} median={}", tmpStep, tmpProcFact, tmpDistFact, tmpProcUpper / tmpProcFact, tmpDistUpper / tmpDistFact, tmpDist.getExpected(), tmpDist.getGeometricMean());
}
}
}
use of org.ojalgo.random.LogNormal in project ojAlgo-finance by optimatika.
the class SymbolDataTest method testYahooWeeklyAAPL.
@Test
public void testYahooWeeklyAAPL() {
final YahooSymbol tmpYahoo = new YahooSymbol("AAPL", CalendarDateUnit.WEEK);
final List<? extends DatePrice> tmpRows = tmpYahoo.getHistoricalPrices();
final CalendarDateSeries<Double> tmpDaySeries = new CalendarDateSeries<>(CalendarDateUnit.DAY);
tmpDaySeries.putAll(tmpRows);
final CalendarDateSeries<Double> tmpYearSeries = tmpDaySeries.resample(CalendarDateUnit.YEAR);
final CalendarDateSeries<Double> tmpMonthSeries = tmpDaySeries.resample(CalendarDateUnit.MONTH);
final PrimitiveSeries tmpDataY = tmpYearSeries.asPrimitive();
final PrimitiveSeries tmpDataM = tmpMonthSeries.asPrimitive();
final SampleSet tmpSetY = SampleSet.wrap(tmpDataY.log().differences());
final SampleSet tmpSetM = SampleSet.wrap(tmpDataM.log().differences());
final GeometricBrownianMotion tmpProcY = GeometricBrownianMotion.estimate(tmpDataY, 1.0);
tmpProcY.setValue(1.0);
final GeometricBrownianMotion tmpProcM = GeometricBrownianMotion.estimate(tmpDataM, 1.0 / 12.0);
tmpProcM.setValue(1.0);
LogNormal tmpExpDistr = new LogNormal(tmpSetY.getMean(), tmpSetY.getStandardDeviation());
LogNormal tmpActDistr = tmpProcY.getDistribution(1.0);
TestUtils.assertEquals("Yearly Expected", tmpExpDistr.getExpected(), tmpActDistr.getExpected(), 1E-14 / PrimitiveMath.THREE);
TestUtils.assertEquals("Yearly Var", tmpExpDistr.getVariance(), tmpActDistr.getVariance(), 1E-14 / PrimitiveMath.THREE);
TestUtils.assertEquals("Yearly StdDev", tmpExpDistr.getStandardDeviation(), tmpActDistr.getStandardDeviation(), 1E-14 / PrimitiveMath.THREE);
tmpExpDistr = new LogNormal(tmpSetM.getMean() * 12.0, tmpSetM.getStandardDeviation() * PrimitiveFunction.SQRT.invoke(12.0));
tmpActDistr = tmpProcM.getDistribution(1.0);
TestUtils.assertEquals("Monthly Expected", tmpExpDistr.getExpected(), tmpActDistr.getExpected(), 1E-14 / PrimitiveMath.THREE);
TestUtils.assertEquals("Monthly Var", tmpExpDistr.getVariance(), tmpActDistr.getVariance(), 1E-14 / PrimitiveMath.THREE);
TestUtils.assertEquals("Monthly StdDev", tmpExpDistr.getStandardDeviation(), tmpActDistr.getStandardDeviation(), 1E-14 / PrimitiveMath.THREE);
}
Aggregations