Search in sources :

Example 1 with CalendarDateSeries

use of org.ojalgo.series.CalendarDateSeries in project ojAlgo-finance by optimatika.

the class FinanceUtils method makeNormalisedExcessPrice.

/**
 * @param priceSeries A series of prices
 * @param riskFreeInterestRateSeries A series of interest rates (risk free return expressed in %, 5.0
 *        means 5.0% annualized risk free return)
 * @return A sample set of price growth rates adjusted for risk free return
 */
public static CalendarDateSeries<Double> makeNormalisedExcessPrice(final CalendarDateSeries<?> priceSeries, final CalendarDateSeries<?> riskFreeInterestRateSeries) {
    if (priceSeries.size() != riskFreeInterestRateSeries.size()) {
        throw new IllegalArgumentException("The two series must have the same size (number of elements).");
    }
    if (!priceSeries.firstKey().equals(riskFreeInterestRateSeries.firstKey())) {
        throw new IllegalArgumentException("The two series must have the same first key (date or calendar).");
    }
    if (!priceSeries.lastKey().equals(riskFreeInterestRateSeries.lastKey())) {
        throw new IllegalArgumentException("The two series must have the same last key (date or calendar).");
    }
    final long[] tmpDates = priceSeries.getPrimitiveKeys();
    final double[] tmpPrices = priceSeries.asPrimitive().toRawCopy1D();
    final double[] tmpRiskFreeInterestRates = riskFreeInterestRateSeries.asPrimitive().toRawCopy1D();
    final CalendarDateUnit tmpResolution = priceSeries.getResolution();
    final CalendarDateSeries<Double> retVal = new CalendarDateSeries<>(tmpResolution);
    double tmpThisRiskFree, tmpLastRiskFree, tmpAvgRiskFree, tmpRiskFreeGrowthFactor, tmpThisPrice, tmpLastPrice, tmpPriceGrowthFactor, tmpAdjustedPriceGrowthFactor;
    double tmpAggregatedExcessPrice = PrimitiveMath.ONE;
    retVal.put(new CalendarDate(tmpDates[0]), tmpAggregatedExcessPrice);
    for (int i = 1; i < priceSeries.size(); i++) {
        tmpThisRiskFree = tmpRiskFreeInterestRates[i] / PrimitiveMath.HUNDRED;
        tmpLastRiskFree = tmpRiskFreeInterestRates[i - 1] / PrimitiveMath.HUNDRED;
        tmpAvgRiskFree = (tmpThisRiskFree + tmpLastRiskFree) / PrimitiveMath.TWO;
        tmpRiskFreeGrowthFactor = FinanceUtils.toGrowthFactorFromAnnualReturn(tmpAvgRiskFree, tmpResolution);
        tmpThisPrice = tmpPrices[i];
        tmpLastPrice = tmpPrices[i - 1];
        tmpPriceGrowthFactor = tmpThisPrice / tmpLastPrice;
        tmpAdjustedPriceGrowthFactor = tmpPriceGrowthFactor / tmpRiskFreeGrowthFactor;
        tmpAggregatedExcessPrice *= tmpAdjustedPriceGrowthFactor;
        retVal.put(new CalendarDate(tmpDates[i]), tmpAggregatedExcessPrice);
    }
    return retVal.name(priceSeries.getName()).colour(priceSeries.getColour());
}
Also used : CalendarDateUnit(org.ojalgo.type.CalendarDateUnit) CalendarDate(org.ojalgo.type.CalendarDate) CalendarDateSeries(org.ojalgo.series.CalendarDateSeries)

Example 2 with CalendarDateSeries

use of org.ojalgo.series.CalendarDateSeries 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;
}
Also used : CalendarDate(org.ojalgo.type.CalendarDate) Deterministic(org.ojalgo.random.Deterministic) RandomNumber(org.ojalgo.random.RandomNumber) CalendarDateSeries(org.ojalgo.series.CalendarDateSeries) GeometricBrownianMotion(org.ojalgo.random.process.GeometricBrownianMotion)

Example 3 with CalendarDateSeries

use of org.ojalgo.series.CalendarDateSeries in project ojAlgo-finance by optimatika.

the class FinanceUtils method makeCalendarPriceSeries.

public static CalendarDateSeries<BigDecimal> makeCalendarPriceSeries(final double[] prices, final Calendar startCalendar, final CalendarDateUnit resolution) {
    final CalendarDateSeries<BigDecimal> retVal = new CalendarDateSeries<>(resolution);
    FinanceUtils.copyValues(retVal, new CalendarDate(startCalendar), prices);
    return retVal;
}
Also used : CalendarDate(org.ojalgo.type.CalendarDate) BigDecimal(java.math.BigDecimal) CalendarDateSeries(org.ojalgo.series.CalendarDateSeries)

Example 4 with CalendarDateSeries

use of org.ojalgo.series.CalendarDateSeries 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);
}
Also used : GeometricBrownian1D(org.ojalgo.random.process.GeometricBrownian1D) CalendarDate(org.ojalgo.type.CalendarDate) ArrayList(java.util.ArrayList) SimpleAsset(org.ojalgo.finance.portfolio.SimpleAsset) PrimitiveDenseStore(org.ojalgo.matrix.store.PrimitiveDenseStore) GeometricBrownianMotion(org.ojalgo.random.process.GeometricBrownianMotion) CalendarDateSeries(org.ojalgo.series.CalendarDateSeries) Test(org.junit.jupiter.api.Test)

Example 5 with CalendarDateSeries

use of org.ojalgo.series.CalendarDateSeries 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);
}
Also used : SampleSet(org.ojalgo.random.SampleSet) LogNormal(org.ojalgo.random.LogNormal) PrimitiveSeries(org.ojalgo.series.primitive.PrimitiveSeries) CalendarDateSeries(org.ojalgo.series.CalendarDateSeries) GeometricBrownianMotion(org.ojalgo.random.process.GeometricBrownianMotion) Test(org.junit.jupiter.api.Test)

Aggregations

CalendarDateSeries (org.ojalgo.series.CalendarDateSeries)6 CalendarDate (org.ojalgo.type.CalendarDate)5 GeometricBrownianMotion (org.ojalgo.random.process.GeometricBrownianMotion)3 BigDecimal (java.math.BigDecimal)2 Test (org.junit.jupiter.api.Test)2 ArrayList (java.util.ArrayList)1 SimpleAsset (org.ojalgo.finance.portfolio.SimpleAsset)1 PrimitiveDenseStore (org.ojalgo.matrix.store.PrimitiveDenseStore)1 Deterministic (org.ojalgo.random.Deterministic)1 LogNormal (org.ojalgo.random.LogNormal)1 RandomNumber (org.ojalgo.random.RandomNumber)1 SampleSet (org.ojalgo.random.SampleSet)1 GeometricBrownian1D (org.ojalgo.random.process.GeometricBrownian1D)1 PrimitiveSeries (org.ojalgo.series.primitive.PrimitiveSeries)1 CalendarDateUnit (org.ojalgo.type.CalendarDateUnit)1