CAPMFromRegressionCalculator implements Function<DoubleTimeSeries<?>, LeastSquaresRegressionResult> { @Override public LeastSquaresRegressionResult evaluate(final DoubleTimeSeries<?>... ts) { Validate.notNull(ts, "ts"); final int n = ts.length; Validate.isTrue(n > 1); if (n > 2) { s_logger.warn("Found more than two time series; will only use the first two"); } final DoubleTimeSeries<?> assetTS = ts[0]; final DoubleTimeSeries<?> marketTS = ts[1]; testNotNullOrEmpty(assetTS); testNotNullOrEmpty(marketTS); testTimeSeriesSize(assetTS, 2); testTimeSeriesSize(marketTS, 2); testTimeSeriesDates(assetTS, marketTS); final double[] asset = assetTS.valuesArrayFast(); final double[][] market = new double[asset.length][1]; for (int i = 0; i < asset.length; i++) { market[i][0] = marketTS.getValueAtIndex(i); } final LeastSquaresRegressionResult result = OLS.regress(market, asset, true); return result; }  @Override LeastSquaresRegressionResult evaluate(final DoubleTimeSeries<?>... ts);  }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullData() { CAPM.evaluate((DoubleTimeSeries<?>[]) null); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNoMarketData() { CAPM.evaluate(ASSET_1); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testInsufficientData() { CAPM.evaluate(ImmutableInstantDoubleTimeSeries.EMPTY_SERIES, ImmutableInstantDoubleTimeSeries.EMPTY_SERIES); }
@Test public void testBetas() { double[] betas = CAPM.evaluate(MARKET, MARKET).getBetas(); assertEquals(betas[0], 0, EPS); assertEquals(betas[1], 1, EPS); betas = CAPM.evaluate(ASSET_1, MARKET).getBetas(); assertEquals(betas[0], 0, EPS); assertEquals(betas[1], BETA1, EPS); betas = CAPM.evaluate(ASSET_2, MARKET).getBetas(); assertEquals(betas[0], 0, EPS); assertEquals(betas[1], BETA2, EPS); betas = CAPM.evaluate(ASSET_3, MARKET).getBetas(); assertEquals(betas[0], RF, EPS); assertEquals(betas[1], 0, EPS); betas = CAPM.evaluate(ASSET_4, MARKET).getBetas(); assertEquals(betas[0], ALPHA1, EPS); assertEquals(betas[1], BETA1, EPS); betas = CAPM.evaluate(ASSET_5, MARKET).getBetas(); assertEquals(betas[0], ALPHA2, EPS); assertEquals(betas[1], BETA2, EPS); }