Plotting Financial Data With Python: Variance

Programming |

Updated on

Variance is an important indicator if you want to know the level of risk associated with a given security. It’s important to understand that past variance might not be a good predictor of future variance but most of the time it works and we don’t have other options anyway. Let’s create a script for displaying returns distribution, variance and standard deviation of any given security. Contents

This is the second part of the “Plotting Financial Data With Python” series and it’s better if you read it in chronological order:

1. Part 1 - History
2. Part 2 - Variance (you are here)
3. Part 3 - Comparing Returns
4. Part 4 - Efficient Frontier (2 Assets)
5. Part 5 - Efficient Frontier (N Assets)

Source Code

You can get the full source code here: https://github.com/bubelov/market-plots

Finding Variance

It’s super easy to find a variance if you have the returns data. Here is how to do that:

1. Find mean returns (mean of all data points)
2. For each data point, subtract it’s value from the mean returns and square the result of subtraction (note that we made it impossible to have a negative result since no number in the power of 2 can be negative)
3. Sum all of the results and divide this sum by the number of data points

Thats all, now you have a variance. If you also want to find a standard deviation, just take the square root of a variance value.

Obtaining The Data

Let’s extend our alpha_vantage.py module to add one more function:

 1 2 3 4 5 6 7 8 9 10 11 12 13 def get_stock_returns_history(symbol, interval): price_history = get_stock_price_history(symbol, interval, adjusted=True) returns = [] prev_price = None for price in price_history.values(): if prev_price != None: returns.append((price - prev_price) / prev_price) prev_price = price return returns

This data is based on the price history data but now we are not interested in absolute numbers so we have to calculate relative changes and return them as a simple array. For instance, with a MONTHLY interval this array would contain the month to month changes in the price of a given security.

Plotting The Data

Let’s create a new file and call it variance.py, it should contain the following code:

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 import sys import numpy as np import matplotlib import matplotlib.pyplot as plt import pathlib import matplotlib.style as style import alpha_vantage def show_variance(symbol, interval='MONTHLY'): returns = alpha_vantage.get_stock_returns_history(symbol, interval) variance = np.var(returns) standard_deviation = np.sqrt(variance) mean_return = np.mean(returns) n, bins, patches = plt.hist(returns, density=True, bins=25) for item in patches: item.set_height(item.get_height() / sum(n)) max_y = max(n) / sum(n) plt.ylim(0, max_y + max_y / 10) plt.gca().set_xticklabels(['{:.0f}%'.format(x*100) for x in plt.gca().get_xticks()]) plt.gca().set_yticklabels(['{:.0f}%'.format(y*100) for y in plt.gca().get_yticks()]) title_line_1 = f'{symbol} {interval} return distribution' title_line_2 = 'Standard deviation = %.2f%% Mean return = %.2f%%' % ( standard_deviation * 100, mean_return * 100) plt.title(f'{title_line_1}\n{title_line_2}') plt.xlabel('Return') plt.ylabel('Probability') pathlib.Path('img/variance').mkdir(parents=True, exist_ok=True) plt.savefig(f'img/variance/{symbol}.png') plt.close() show_variance(sys.argv)

First of all, this script checks the number of input parameters, we need this check to find out whether we have a period specified or should we use the default value (MONTHLY). Next, this code fetches the returns history data and calculates the variance based on that data. The final step is to plot the returns distribution as a histogram so we can see the relative frequencies of any given returns.

Testing

Let’s test our new module by requiring it to draw a couple of charts:

\$ python variance.py TSLA

You should see the chart similar to this one: Now let’s check the variance of S&P 500 index:

\$ python variance.py SPX It’s easy to see why TSLA is more risky to hold than SPX but it does not mean that it’s a bad choice. Why does anyone want to hold such an unpredictable stock? You can look at the mean returns or plot the price history to see the answer.

Conclusion

Now that we can get a hint of the risk of holding any particular security, it would be nice to have a way of comparing the returns. It might be a good idea to hold a risky security if it gives exceptional returns and you are not worried about price volatility in the short term.

Next Post: Part 3 - Comparing Returns

This site doesn't have ads and the reasons are simple:

• Most people don't want to see ads, that's not what they look for when they open web pages.
• Ad scripts can track visitors, exposing private data to third parties.

If you found this post valuable and you wish to leave a tip, you can do it with Bitcoin:

34CXtg7c4Vbw8DZjAwFQVsrbu9eDEbTzbA 