Search in sources :

Example 1 with CalendarDate

use of org.ojalgo.type.CalendarDate in project ojAlgo-finance by optimatika.

the class FinanceUtils method copyValues.

private static <K extends Comparable<? super K>> void copyValues(final CalendarDateSeries<BigDecimal> series, final CalendarDate firstKey, final double[] values) {
    CalendarDate tmpKey = firstKey;
    for (int tmpValueIndex = 0; tmpValueIndex < values.length; tmpValueIndex++) {
        series.put(tmpKey, new BigDecimal(values[tmpValueIndex]));
        tmpKey = series.step(tmpKey);
    }
}
Also used : CalendarDate(org.ojalgo.type.CalendarDate) BigDecimal(java.math.BigDecimal)

Example 2 with CalendarDate

use of org.ojalgo.type.CalendarDate 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 3 with CalendarDate

use of org.ojalgo.type.CalendarDate 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 4 with CalendarDate

use of org.ojalgo.type.CalendarDate 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 5 with CalendarDate

use of org.ojalgo.type.CalendarDate 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)

Aggregations

CalendarDate (org.ojalgo.type.CalendarDate)14 CalendarDateSeries (org.ojalgo.series.CalendarDateSeries)5 BigDecimal (java.math.BigDecimal)4 GeometricBrownianMotion (org.ojalgo.random.process.GeometricBrownianMotion)2 CalendarDateUnit (org.ojalgo.type.CalendarDateUnit)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Test (org.junit.jupiter.api.Test)1 ProgrammingError (org.ojalgo.ProgrammingError)1 SimpleAsset (org.ojalgo.finance.portfolio.SimpleAsset)1 PrimitiveDenseStore (org.ojalgo.matrix.store.PrimitiveDenseStore)1 Deterministic (org.ojalgo.random.Deterministic)1 RandomNumber (org.ojalgo.random.RandomNumber)1 GeometricBrownian1D (org.ojalgo.random.process.GeometricBrownian1D)1 CalendarDateDuration (org.ojalgo.type.CalendarDateDuration)1