Monday 5 September 2011

Wolfram Alpha to Mathematica: the Case of Financial Data

After taking a break from this blog in order to finalize my PhD thesis, I return with a demo of a new functionality of Mathematica 8, making it possible to import data from the Wolfram Alpha project.

Suppose we need to get hold of the price history of a particular mutual fund, say 'The Growth Fund of America Class A' (ticker symbol AGTHX) from the 1st of January 2004 until the present day. The Wolfram Alpha answer engine will display the corresponding time series plot upon entering:


This is of course very nice and the recognition of informal language is appealing to the general public, but suppose we actually want to import the raw price data into Mathematica for further (more extended) usage. This is done by means of the WolframAlpha command, but the main difficulty is that this imports a complete Wolfram Alpha object, i.e. the data we seek is mingled with other stuff, like pictures, explanatory labels, etc.

The first step is then to get a glimpse of what we've got there, which is done using the extra "PodIDs" instruction, like this:

It looks like it's that last component we want, but that's not the end of it, as it too reveals some various bits and pieces:


Once again, it seems that the element in the end is the desired one, so we just need to copy/paste it into the WolframAlpha command:

This gives a list of elements that each look like this:

i.e. we have a {year,month,day} date, followed by the corresponding daily price.

We will now use this to obtain a list of price histories for fifteen of the biggest US mutual funds as follows:

where the list is saved as PriceHistories and constructed like this:



In other words, PriceHistories is a Table, the i-th element of which is the price history of the fund that is i-th on our ListOfFunds. This is obtained by by taking the i-th name on the list and joining it with the rest of the command using <>, finally sending the whole query to Wolfram Alpha and extracting the actual prices from the result in the way explained before for AGTHX.

Note that we could, in principle, ask Alpha for more than one price history in one go, by including a list of fund names separated by spaces in the query instead of the single name. However, this may overload the answer engine and it is safer to do things in turns.

We may now forget about Wolfram Alpha, and focus on utilizing the acquired data in Mathematica. In particular, let's look at how the financial crisis of 2008 affected the mean return and volatility measures of the funds in question. The daily returns are extracted from the price data above in the following way:

The Function specified below gives a list of Mean - Standard Deviation pairs, one for each fund, calculated for the period of 100 data points (~ 1.5 years) prior to the one specified:



while this yields the corresponding graphical representation (each Mean - Standard Deviation pair is a point in the ListPlot):


Finally, we may see how this picture has evolved over the period from January 2008 to January 2011.