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();
}
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);
}
Aggregations