Data Science Dividends – A Gentle Introduction to Financial Data Analysis

This post outlines some very basic methods for performing financial data analysis using Python, Pandas, and Matplotlib, focusing mainly on stock price data. A good place for beginners to start.



Comparing Stock Performance

 
Now let's bring a few other stocks in, for comparison.

Pandas dataframe

Pandas dataframe

OK, let's plot GOOG, FB and AMZN's adjusted close prices against each other.

Comparison

Well, that's interesting and somewhat helpful. Unfortunately, the stocks' prices are on not very similar, and, hence, an apples to apples comparison is not possible.

A more apt comparison would be to plot the stocks' relative returns against one another. This can be accomplished via a price transformation, which will set the initial adjusted closing prices for all 3 stocks on the first day of the period of interest to equal, and change subsequent adjusted closing prices to reflect the relative change in price. This effectively "zeroes out" the stocks and treats them as being on equal footing at the start of the period of interest.

Applying the price transformation:

Comparison dataframe

And now we can look at the relative returns against one another, providing for more meaningful analysis:

Returns for period

Note how the relative final performance of the stocks during this time period could not have easily been anticipated based on the earlier comparative graph.

How did GOOG and AMZN Perform During FB's IPO? Facebook went public May 18, 2012, so for fun let's grab May 2016 adjusted close prices for the 3 stocks and look at their performance.

You can tell that there is a slight dip in both of the other stocks around that day. Of course, given that the economy and financial analysis are both much more complex than single events, spurious correleation is all this can be assumed to be without further investigation.

GOOG IPO period comparison graph

And Now for Something Completely Different

 
We mentioned at the onset of the article that there are other sources beyond Yahoo! Finance that can be exploited via pandas-datareader. Along with Google Finance -- similar to Yahoo! Finance in what it offers -- there are complementary sources sources such as the Federal Reserve Bank of St. Louis.

Using this source (known as 'FRED'), let's have a look at how the Canadian dollar exchange rate fared against the US dollar during 2016.

CADUSD

From these few lines of code, it's easy to see when the Canadian dollar performed best (May) and worst (January) against the greenback during 2016.

Related: