@Test()
public void testCrossSectionalReturns() throws Exception {
LocalDate startDate = LocalDate.MIN;
LocalDate endDate = LocalDate.MAX;
final Index rowKeys = Index.of(LocalDate.class, 100);
final Index tickers = Index.of("BLK", "CSCO", "SPY", "YHOO", "VNQI", "VGLT", "VCLT");
final DataFrame closePrices = DataFrame.ofDoubles(rowKeys, tickers);
for (String ticker : tickers) {
System.out.println("Loading data for ticker " + ticker);
final DataFrame quotes = TestDataFrames.getQuotes(ticker);
quotes.tail(10).out().print();
closePrices.rows().addAll(quotes.rows().keyArray());
final LocalDate firstKey = quotes.rows().firstKey().get();
final LocalDate lastKey = quotes.rows().lastKey().get();
startDate = firstKey.isAfter(startDate) ? firstKey : startDate;
endDate = lastKey.isBefore(endDate) ? lastKey : endDate;
quotes.rows().forEach(row -> {
final LocalDate date = row.key();
final double price = row.getDouble("Adj Close");
closePrices.data().setDouble(date, ticker, price);
});
}
final Set nanDates = new HashSet<>();
closePrices.rows().forEach(row -> row.forEachValue(v -> {
final double value = v.getDouble();
if (Double.isNaN(value)) {
final LocalDate rowKey = row.key();
nanDates.add(rowKey);
if (rowKey.getYear() == 2014) {
System.out.println(rowKey);
}
}
}));
final DataFrame selection = closePrices.rows().select(row -> !nanDates.contains(row.key()));
final DataFrame sorted = selection.rows().sort((row1, row2) -> row1.key().compareTo(row2.key()));
final DataFrame returns = sorted.calc().percentChanges();
returns.rows().first().get().applyDoubles(v -> 0d);
returns.head(10).out().print();
returns.cols().stats().correlation().out().print();
}