use of org.ojalgo.random.process.GeometricBrownianMotion in project ojAlgo-finance by optimatika.
the class FinanceUtils method forecast.
public static CalendarDateSeries<RandomNumber> forecast(final CalendarDateSeries<? extends Number> series, final int pointCount, final CalendarDateUnit timeUnit, final boolean includeOriginalSeries) {
final CalendarDateSeries<RandomNumber> retVal = new CalendarDateSeries<>(timeUnit);
retVal.name(series.getName()).colour(series.getColour());
final double tmpSamplePeriod = (double) series.getAverageStepSize() / (double) timeUnit.size();
final GeometricBrownianMotion tmpProcess = GeometricBrownianMotion.estimate(series.asPrimitive(), tmpSamplePeriod);
if (includeOriginalSeries) {
for (final Entry<CalendarDate, ? extends Number> tmpEntry : series.entrySet()) {
retVal.put(tmpEntry.getKey(), new Deterministic(tmpEntry.getValue()));
}
}
final CalendarDate tmpLastKey = series.lastKey();
final double tmpLastValue = series.lastValue().doubleValue();
tmpProcess.setValue(tmpLastValue);
for (int i = 1; i <= pointCount; i++) {
retVal.put(tmpLastKey.millis + (i * timeUnit.size()), tmpProcess.getDistribution(i));
}
return retVal;
}
use of org.ojalgo.random.process.GeometricBrownianMotion in project ojAlgo-finance by optimatika.
the class FinancePortfolio method getLossProbability.
public final double getLossProbability(final Number timePeriod) {
final GeometricBrownianMotion tmpProc = this.forecast();
final double tmpDoubleValue = timePeriod.doubleValue();
final double tmpValue = tmpProc.getValue();
return tmpProc.getDistribution(tmpDoubleValue).getDistribution(tmpValue);
}
use of org.ojalgo.random.process.GeometricBrownianMotion in project ojAlgo-finance by optimatika.
the class MultidimensionalSimulatorTest method testStepping.
@Test
public void testStepping() {
final PrimitiveDenseStore tmpCorrelation = PrimitiveDenseStore.FACTORY.makeEye(3, 3);
final GeometricBrownianMotion tmpOrgProc1 = new SimpleAsset(0.0, 0.01, PrimitiveMath.THIRD).forecast();
final GeometricBrownianMotion tmpOrgProc2 = new SimpleAsset(0.0, 0.02, PrimitiveMath.THIRD).forecast();
final GeometricBrownianMotion tmpOrgProc3 = new SimpleAsset(0.0, 0.03, PrimitiveMath.THIRD).forecast();
TestUtils.assertEquals(0.01, tmpOrgProc1.getStandardDeviation(), 0.005);
TestUtils.assertEquals(0.02, tmpOrgProc2.getStandardDeviation(), 0.005);
TestUtils.assertEquals(0.03, tmpOrgProc3.getStandardDeviation(), 0.005);
final ArrayList<GeometricBrownianMotion> tmpProcs = new ArrayList<>();
tmpProcs.add(tmpOrgProc1);
tmpProcs.add(tmpOrgProc2);
tmpProcs.add(tmpOrgProc3);
final GeometricBrownian1D tmpGB1D = new GeometricBrownian1D(tmpCorrelation, tmpProcs);
final List<CalendarDateSeries<Double>> tmpSeries = new ArrayList<>();
tmpSeries.add(new CalendarDateSeries<Double>(CalendarDateUnit.MONTH));
tmpSeries.add(new CalendarDateSeries<Double>(CalendarDateUnit.MONTH));
tmpSeries.add(new CalendarDateSeries<Double>(CalendarDateUnit.MONTH));
CalendarDate tmpCalendarDateKey = CalendarDate.make(CalendarDateUnit.MONTH);
tmpSeries.get(0).put(tmpCalendarDateKey, tmpOrgProc1.getValue());
tmpSeries.get(1).put(tmpCalendarDateKey, tmpOrgProc2.getValue());
tmpSeries.get(2).put(tmpCalendarDateKey, tmpOrgProc3.getValue());
for (int t = 0; t < 1000; t++) {
tmpGB1D.step(1.0 / 12.0);
tmpCalendarDateKey = tmpCalendarDateKey.step(CalendarDateUnit.MONTH);
tmpSeries.get(0).put(tmpCalendarDateKey, tmpGB1D.getValue(0));
tmpSeries.get(1).put(tmpCalendarDateKey, tmpGB1D.getValue(1));
tmpSeries.get(2).put(tmpCalendarDateKey, tmpGB1D.getValue(2));
}
final GeometricBrownianMotion tmpNewProc1 = GeometricBrownianMotion.estimate(tmpSeries.get(0).asPrimitive(), 1.0 / 12.0);
final GeometricBrownianMotion tmpNewProc2 = GeometricBrownianMotion.estimate(tmpSeries.get(1).asPrimitive(), 1.0 / 12.0);
final GeometricBrownianMotion tmpNewProc3 = GeometricBrownianMotion.estimate(tmpSeries.get(2).asPrimitive(), 1.0 / 12.0);
TestUtils.assertEquals(0.01, tmpNewProc1.getStandardDeviation(), 0.005);
TestUtils.assertEquals(0.02, tmpNewProc2.getStandardDeviation(), 0.005);
TestUtils.assertEquals(0.03, tmpNewProc3.getStandardDeviation(), 0.005);
}
use of org.ojalgo.random.process.GeometricBrownianMotion 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);
}
use of org.ojalgo.random.process.GeometricBrownianMotion in project ojAlgo-finance by optimatika.
the class FinanceUtils method estimateExcessDiffusionProcess.
public static GeometricBrownianMotion estimateExcessDiffusionProcess(final CalendarDateSeries<?> priceSeries, final CalendarDateSeries<?> riskFreeInterestRateSeries, final CalendarDateUnit timeUnit) {
final SampleSet tmpSampleSet = FinanceUtils.makeExcessGrowthRateSampleSet(priceSeries, riskFreeInterestRateSeries);
// The average number of millis between to subsequent keys in the series.
double tmpStepSize = priceSeries.getResolution().size();
// The time between to keys expressed in terms of the specified time meassure and unit.
tmpStepSize /= timeUnit.size();
final double tmpExp = tmpSampleSet.getMean();
final double tmpVar = tmpSampleSet.getVariance();
final double tmpDiff = PrimitiveFunction.SQRT.invoke(tmpVar / tmpStepSize);
final double tmpDrift = (tmpExp / tmpStepSize) + ((tmpDiff * tmpDiff) / TWO);
final GeometricBrownianMotion retVal = new GeometricBrownianMotion(tmpDrift, tmpDiff);
return retVal;
}
Aggregations