Plotting Financial Data With Python: Variance
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:
 Part 1  History
 Part 2  Variance (you are here)
 Part 3  Comparing Returns
 Part 4  Efficient Frontier (2 Assets)
 Part 5  Efficient Frontier (N Assets)
Source Code
You can get the full source code here: https://github.com/bubelov/marketplots
Finding Variance
It’s super easy to find a variance if you have the returns data. Here is how to do that:
 Find mean returns (mean of all data points)
 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)
 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:


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:


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