Search in sources :

Example 1 with PrimitiveSeries

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

the class FinanceUtils method makeCovarianceMatrix.

/**
 * @param listOfTimeSeries An ordered collection of time series
 * @param mayBeMissingValues Individual series may be missing some values - try to fix this or not
 * @return Annualised covariances
 */
public static <N extends Number> PrimitiveMatrix makeCovarianceMatrix(final List<CalendarDateSeries<N>> listOfTimeSeries, final boolean mayBeMissingValues) {
    final int tmpSize = listOfTimeSeries.size();
    final CoordinationSet<N> tmpUncoordinated = new CoordinationSet<>(listOfTimeSeries);
    final CalendarDateUnit tmpDataResolution = tmpUncoordinated.getResolution();
    if (mayBeMissingValues) {
        tmpUncoordinated.complete();
    }
    final CoordinationSet<N> tmpCoordinated = tmpUncoordinated.prune(tmpDataResolution);
    final Builder<PrimitiveMatrix> tmpMatrixBuilder = PrimitiveMatrix.FACTORY.getBuilder(tmpSize, tmpSize);
    final double tmpToYearFactor = (double) CalendarDateUnit.YEAR.size() / (double) tmpDataResolution.size();
    SampleSet tmpSampleSet;
    final SampleSet[] tmpSampleSets = new SampleSet[tmpSize];
    for (int j = 0; j < tmpSize; j++) {
        final PrimitiveSeries tmpPrimitiveSeries = tmpCoordinated.get(listOfTimeSeries.get(j).getName()).asPrimitive();
        tmpSampleSet = SampleSet.wrap(tmpPrimitiveSeries.quotients().log().toDataSeries());
        tmpMatrixBuilder.set(j, j, tmpToYearFactor * tmpSampleSet.getVariance());
        for (int i = 0; i < j; i++) {
            final double tmpCovariance = tmpToYearFactor * tmpSampleSets[i].getCovariance(tmpSampleSet);
            tmpMatrixBuilder.set(i, j, tmpCovariance);
            tmpMatrixBuilder.set(j, i, tmpCovariance);
        }
        tmpSampleSets[j] = tmpSampleSet;
    }
    return tmpMatrixBuilder.get();
}
Also used : CalendarDateUnit(org.ojalgo.type.CalendarDateUnit) SampleSet(org.ojalgo.random.SampleSet) PrimitiveMatrix(org.ojalgo.matrix.PrimitiveMatrix) CoordinationSet(org.ojalgo.series.CoordinationSet) PrimitiveSeries(org.ojalgo.series.primitive.PrimitiveSeries)

Example 2 with PrimitiveSeries

use of org.ojalgo.series.primitive.PrimitiveSeries 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

SampleSet (org.ojalgo.random.SampleSet)2 PrimitiveSeries (org.ojalgo.series.primitive.PrimitiveSeries)2 Test (org.junit.jupiter.api.Test)1 PrimitiveMatrix (org.ojalgo.matrix.PrimitiveMatrix)1 LogNormal (org.ojalgo.random.LogNormal)1 GeometricBrownianMotion (org.ojalgo.random.process.GeometricBrownianMotion)1 CalendarDateSeries (org.ojalgo.series.CalendarDateSeries)1 CoordinationSet (org.ojalgo.series.CoordinationSet)1 CalendarDateUnit (org.ojalgo.type.CalendarDateUnit)1