-
-
Notifications
You must be signed in to change notification settings - Fork 209
Financial Data
Ryan Rickerts edited this page Mar 15, 2018
·
4 revisions
An example that demonstrates some of ojAlgo's features related to working with financial data. The example shows how to:
- Download historical data for Apple and Google stock from Yahoo Finance.
- Synchronize/coordinate the two series so that they have the same length, beginning and end, as well as resample them to be monthly data.
- Statistics are compared for Apple and Google stock, and the correlation between them is calculated.
- Use that data to estimate (the parameters of) stochastic processes. For each time series two processes are created - one with a monthly scale and one annualized.
- For Apple stock the two generated processes are verified to produce the same probability distributions, repectively, at one month and one year in to the future.
- The probability distributions, one year into the future, of Apple and Google are compared.
- The stochastic process for Google is used to simulate a 1000 future scenarios, and the scenmarios' value one year into the future is compared to theoretical distribution.
import java.util.Map.Entry;
import org.ojalgo.OjAlgoUtils;
import org.ojalgo.finance.data.YahooSymbol; //need ojalgo-finance for this
import org.ojalgo.netio.BasicLogger;
import org.ojalgo.random.SampleSet;
import org.ojalgo.random.process.GeometricBrownianMotion;
import org.ojalgo.random.process.RandomProcess.SimulationResults;
import org.ojalgo.series.CalendarDateSeries;
import org.ojalgo.series.CoordinationSet;
import org.ojalgo.series.primitive.DataSeries;
import org.ojalgo.type.CalendarDateUnit;
public abstract class FinancialData {
public static void main(final String[] args) {
BasicLogger.debug();
BasicLogger.debug(FinancialData.class.getSimpleName());
BasicLogger.debug(OjAlgoUtils.getTitle());
BasicLogger.debug(OjAlgoUtils.getDate());
BasicLogger.debug();
final YahooSymbol tmpAAPL = new YahooSymbol("AAPL", CalendarDateUnit.DAY);
final YahooSymbol tmpGOOG = new YahooSymbol("GOOG", CalendarDateUnit.DAY);
final CalendarDateSeries<Double> tmpSeriesAAPL = tmpAAPL.getPriceSeries();
final CalendarDateSeries<Double> tmpSeriesGOOG = tmpGOOG.getPriceSeries();
final CoordinationSet<Double> tmpUncoordinated = new CoordinationSet<>();
tmpUncoordinated.put("Apple", tmpSeriesAAPL);
tmpUncoordinated.put("Google", tmpSeriesGOOG);
BasicLogger.debug("Downloaded daily data");
for (final Entry<String, CalendarDateSeries<Double>> tmpEntry : tmpUncoordinated.entrySet()) {
BasicLogger.debug("\tSeries: {} = {}", tmpEntry.getKey(), tmpEntry.getValue());
}
final CoordinationSet<Double> tmpCoordinated = tmpUncoordinated.prune(CalendarDateUnit.MONTH);
BasicLogger.debug();
BasicLogger.debug("Coordinated monthly data");
for (final Entry<String, CalendarDateSeries<Double>> tmpEntry : tmpCoordinated.entrySet()) {
BasicLogger.debug("\tSeries: {} = {}", tmpEntry.getKey(), tmpEntry.getValue());
}
final DataSeries tmpMonthlyDataAAPL = tmpCoordinated.get("Apple").getDataSeries();
final DataSeries tmpMonthlyDataGoogle = tmpCoordinated.get("Google").getDataSeries();
final SampleSet tmpYahooSet = SampleSet.wrap(tmpMonthlyDataAAPL.log().differences());
final SampleSet tmpGoolgleSet = SampleSet.wrap(tmpMonthlyDataGoogle.log().differences());
BasicLogger.debug();
BasicLogger.debug("Sample statistics (logarithmic differences on monthly data)");
BasicLogger.debug("\tApple: {}", tmpYahooSet);
BasicLogger.debug("\tGoogle: {}", tmpGoolgleSet);
BasicLogger.debug("\tCorrelation: {}", tmpGoolgleSet.getCorrelation(tmpYahooSet));
final GeometricBrownianMotion tmpAppleProcMonth = GeometricBrownianMotion.estimate(tmpMonthlyDataAAPL, 1.0);
final GeometricBrownianMotion tmpGoogleProcMonth = GeometricBrownianMotion.estimate(tmpMonthlyDataGoogle, 1.0);
tmpAppleProcMonth.setValue(1.0); // To normalize the current value
tmpGoogleProcMonth.setValue(1.0);
final double tmpYearsPerMonth = CalendarDateUnit.YEAR.convert(tmpCoordinated.getResolution());
final GeometricBrownianMotion tmpAppleProcYear = GeometricBrownianMotion.estimate(tmpMonthlyDataAAPL, tmpYearsPerMonth);
final GeometricBrownianMotion tmpGoogleProcYear = GeometricBrownianMotion.estimate(tmpMonthlyDataGoogle, tmpYearsPerMonth);
tmpAppleProcYear.setValue(1.0); // To normalize the current value
tmpGoogleProcYear.setValue(1.0);
// Comparing the month and year based stochastic processes for AAPL
BasicLogger.debug();
BasicLogger.debug(" Apple \t Monthly proc \t\t\t Annual proc \t\t (6 months from now)");
BasicLogger.debug("Expected: {} \t {}", tmpAppleProcMonth.getDistribution(6.0).getExpected(), tmpAppleProcYear.getDistribution(0.5).getExpected());
BasicLogger.debug("StdDev: {} \t {}", tmpAppleProcMonth.getDistribution(6.0).getStandardDeviation(),
tmpAppleProcYear.getDistribution(0.5).getStandardDeviation());
BasicLogger.debug("Var: {} \t {}", tmpAppleProcMonth.getDistribution(6.0).getVariance(), tmpAppleProcYear.getDistribution(0.5).getVariance());
// Comparing the annualized stochastic processes for AAPL and GOOG
BasicLogger.debug();
BasicLogger.debug(" \t\t\t Apple \t\t\t\t\t Google");
BasicLogger.debug("Current: {} \t\t\t\t\t {}", tmpAppleProcYear.getValue(), tmpGoogleProcYear.getValue());
// getExpected() is a shortcut to getDistribution(1.0).getExpected()
BasicLogger.debug("Expected: {} \t {}", tmpAppleProcYear.getExpected(), tmpGoogleProcYear.getExpected());
// getStandardDeviation() is a shortcut to getDistribution(1.0).getStandardDeviation()
BasicLogger.debug("StdDev: {} \t {}", tmpAppleProcYear.getStandardDeviation(), tmpGoogleProcYear.getStandardDeviation());
// getVariance() is a shortcut to getDistribution(1.0).getVariance()
BasicLogger.debug("Var: {} \t {}", tmpAppleProcYear.getVariance(), tmpGoogleProcYear.getVariance());
final SimulationResults tmpSimResults = tmpGoogleProcYear.simulate(1000, 12, tmpYearsPerMonth);
BasicLogger.debug();
BasicLogger.debug("Simulate future Google");
BasicLogger.debug("Simulated sample set: {}", tmpSimResults.getSampleSet(11));
}
}
FinancialData
ojAlgo
2016-05-14
Downloaded daily data
Series: Google = GOOG DAY First:2004-08-19 12:00:00=50.119968 Last:2016-05-13 12:00:00=710.830017 Size:2955
Series: Apple = AAPL DAY First:1980-12-12 12:00:00=0.431358 Last:2016-05-13 12:00:00=90.519997 Size:8932
Coordinated monthly data
Series: Google = GOOG MONTH First:2004-08-31 12:00:00=51.133953 Last:2016-05-31 12:00:00=710.830017 Size:142
Series: Apple = AAPL MONTH First:2004-08-31 12:00:00=2.26768 Last:2016-05-31 12:00:00=90.519997 Size:142
Sample statistics (logarithmic differences on monthly data)
Apple: Sample set Size=141, Mean=0.02614761351196476, Median=0.033231314721613, Var=0.01049149860307574, StdDev=0.10242801669014069, Min=-0.39981831885234254, Max=0.30177586998374273
Google: Sample set Size=141, Mean=0.01866655749244666, Median=0.01752020055681669, Var=0.008705486904812838, StdDev=0.09330319879196446, Min=-0.20428855960758607, Max=0.3859340135368754
Correlation: 0.492084812100738
Apple Monthly proc Annual proc (6 months from now)
Expected: 1.2072682671701467 1.2072682671701467
StdDev: 0.307729141701079 0.30772914170107896
Var: 0.09469722465208277 0.09469722465208275
Apple Google
Current: 1.0 1.0
Expected: 1.4574966689160085 1.3181531278846041
StdDev: 0.5338626633590423 0.43741531662489624
Var: 0.28500934332881017 0.1913321592180582
Simulate future Google
Simulated sample set: Sample set Size=1000, Mean=1.325627179042084, Median=1.2687505025091892, Var=0.1856145903552972, StdDev=0.43083011774398644, Min=0.4581958805895357, Max=3.6433282195001504