use of org.ojalgo.type.CalendarDateUnit 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());
}
use of org.ojalgo.type.CalendarDateUnit in project ojAlgo-finance by optimatika.
the class FinanceUtils method makeExcessGrowthRateSampleSet.
/**
* @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 SampleSet makeExcessGrowthRateSampleSet(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 double[] tmpPrices = priceSeries.asPrimitive().toRawCopy1D();
final double[] tmpRiskFreeInterestRates = riskFreeInterestRateSeries.asPrimitive().toRawCopy1D();
final Array1D<Double> retVal = Array1D.PRIMITIVE64.makeZero(tmpPrices.length - 1);
final CalendarDateUnit tmpUnit = priceSeries.getResolution();
double tmpThisRiskFree, tmpNextRiskFree, tmpAvgRiskFree, tmpRiskFreeGrowthRate, tmpThisPrice, tmpNextPrice, tmpPriceGrowthFactor, tmpPriceGrowthRate, tmpAdjustedPriceGrowthRate;
for (int i = 0; i < retVal.size(); i++) {
tmpThisRiskFree = tmpRiskFreeInterestRates[i] / PrimitiveMath.HUNDRED;
tmpNextRiskFree = tmpRiskFreeInterestRates[i + 1] / PrimitiveMath.HUNDRED;
tmpAvgRiskFree = (tmpThisRiskFree + tmpNextRiskFree) / PrimitiveMath.TWO;
tmpRiskFreeGrowthRate = FinanceUtils.toGrowthRateFromAnnualReturn(tmpAvgRiskFree, tmpUnit);
tmpThisPrice = tmpPrices[i];
tmpNextPrice = tmpPrices[i + 1];
tmpPriceGrowthFactor = tmpNextPrice / tmpThisPrice;
tmpPriceGrowthRate = PrimitiveFunction.LOG.invoke(tmpPriceGrowthFactor);
tmpAdjustedPriceGrowthRate = tmpPriceGrowthRate - tmpRiskFreeGrowthRate;
retVal.set(i, tmpAdjustedPriceGrowthRate);
}
return SampleSet.wrap(retVal);
}
use of org.ojalgo.type.CalendarDateUnit 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.type.CalendarDateUnit in project ojAlgo by optimatika.
the class SeriesForecaster method invoke.
@Override
public Map<String, Access1D<?>> invoke(final CalendarDate... key) {
final CalendarDate tmpLastKey = this.getLastKey();
final CalendarDateUnit tmpResolution = this.getResolution();
final CalendarDateDuration[] tmpHorizon = new CalendarDateDuration[key.length];
for (int h = 0; h < tmpHorizon.length; h++) {
final double tmpMeassure = tmpResolution.count(tmpLastKey.millis, key[h].millis);
tmpHorizon[h] = new CalendarDateDuration(tmpMeassure, tmpResolution);
}
return this.invoke(tmpHorizon);
}
Aggregations