final ConventionBundleSource conventionSource = OpenGammaExecutionContext.getConventionBundleSource(executionContext);
final ConventionBundle bundle = conventionSource.getConventionBundle(ExternalId.of(InMemoryConventionBundleMaster.SIMPLE_NAME_SCHEME, "USD_CAPM"));
final ValueRequirement desiredValue = desiredValues.iterator().next();
final ValueProperties constraints = desiredValue.getConstraints();
final HistoricalTimeSeriesBundle timeSeries = HistoricalTimeSeriesFunctionUtils.getHistoricalTimeSeriesInputs(executionContext, inputs);
final HistoricalTimeSeries marketTS = timeSeries.get(MarketDataRequirementNames.MARKET_VALUE, bundle.getCAPMMarket());
if (marketTS == null) {
throw new OpenGammaRuntimeException("Market value series was not availble");
}
final HistoricalTimeSeries riskFreeRateTS = timeSeries.get(MarketDataRequirementNames.MARKET_VALUE, bundle.getCAPMRiskFreeRate());
if (riskFreeRateTS == null) {
throw new OpenGammaRuntimeException("Risk free rate series was not available");
}
final Object assetPnLObject = inputs.getValue(new ValueRequirement(ValueRequirementNames.PNL_SERIES, targetSpec)); //TODO replace with return series when portfolio weights are in
if (assetPnLObject == null) {
throw new OpenGammaRuntimeException("Asset P&L was null");
}
final Object assetFairValueObject = inputs.getValue(new ValueRequirement(ValueRequirementNames.FAIR_VALUE, targetSpec));
if (assetFairValueObject == null) {
throw new OpenGammaRuntimeException("Asset fair value was null");
}
final Object betaObject = inputs.getValue(new ValueRequirement(ValueRequirementNames.CAPM_BETA, targetSpec));
if (betaObject == null) {
throw new OpenGammaRuntimeException("Beta was null");
}
final double beta = (Double) betaObject;
final double fairValue = (Double) assetFairValueObject;
DoubleTimeSeries<?> assetReturnTS = ((DoubleTimeSeries<?>) assetPnLObject).divide(fairValue);
DoubleTimeSeries<?> riskFreeReturnTS = riskFreeRateTS.getTimeSeries().divide(100 * DAYS_PER_YEAR);
final TimeSeriesReturnCalculator returnCalculator = getReturnCalculator(constraints.getValues(ValuePropertyNames.RETURN_CALCULATOR));
DoubleTimeSeries<?> marketReturnTS = returnCalculator.evaluate(marketTS.getTimeSeries());
final DoubleTimeSeries<?>[] series = TimeSeriesIntersector.intersect(assetReturnTS, riskFreeReturnTS, marketReturnTS);
assetReturnTS = series[0];
riskFreeReturnTS = series[1];