By Christopher van Hoecke, Maxwell Margenot, and Delaney Mackenzie
https://www.quantopian.com/lectures/variance
This lecture corresponds to the Variance lecture, which is part of the Quantopian lecture series. This homework expects you to rely heavily on the code presented in the corresponding lecture. Please copy and paste regularly from that lecture when starting to work on the problems, as trying to do them from scratch will likely be too difficult.
Part of the Quantopian Lecture Series:
In [1]:
# Useful Libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
In [2]:
X = np.random.randint(100, size = 100)
In [3]:
# Range of X
print 'Range of X: %s' %(np.ptp(X))
In [4]:
# Mean Absolute Deviation
# First calculate the value of mu (the mean)
mu = np.mean(X)
abs_dispersion = [np.abs(mu - x) for x in X]
MAD = np.sum(abs_dispersion)/len(abs_dispersion)
print 'Mean absolute deviation of X:', MAD
In [5]:
# Variance and standard deviation
print 'Variance of X:', np.var(X)
print 'Standard deviation of X:', np.std(X)
In [6]:
# Semivariance and semideviation
lows = [e for e in X if e <= mu]
semivar = np.sum( (lows - mu) ** 2 ) / len(lows)
print 'Semivariance of X:', semivar
print 'Semideviation of X:', np.sqrt(semivar)
In [7]:
# Target variance
B = 60
lows_B = [e for e in X if e <= B]
semivar_B = sum(map(lambda x: (x - B)**2,lows_B))/len(lows_B)
print 'Target semivariance of X:', semivar_B
print 'Target semideviation of X:', np.sqrt(semivar_B)
In [8]:
att = get_pricing('T', fields='open_price', start_date='2016-01-01', end_date='2017-01-01')
In [9]:
# Rolling mean
variance = att.rolling(window = 30).var()
In [10]:
# Rolling standard deviation
std = att.rolling(window = 15).std()
In [11]:
asset1 = get_pricing('AAPL', fields='open_price', start_date='2016-01-01', end_date='2017-01-01')
asset2 = get_pricing('XLF', fields='open_price', start_date='2016-01-01', end_date='2017-01-01')
cov = np.cov(asset1, asset2)[0,1]
w1 = 0.87
w2 = 1 - w1
v1 = np.var(asset1)
v2 = np.var(asset2)
pvariance = (w1**2)*v1+(w2**2)*v2+(2*w1*w2)*cov
print 'Portfolio variance: ', pvariance
Congratulations on completing the Variance answer key!
As you learn more about writing trading models and the Quantopian platform, enter a daily Quantopian Contest. Your strategy will be evaluated for a cash prize every day.
Start by going through the Writing a Contest Algorithm tutorial.
This presentation is for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation for any security; nor does it constitute an offer to provide investment advisory or other services by Quantopian, Inc. ("Quantopian"). Nothing contained herein constitutes investment advice or offers any opinion with respect to the suitability of any security, and any views expressed herein should not be taken as advice to buy, sell, or hold any security or as an endorsement of any security or company. In preparing the information contained herein, Quantopian, Inc. has not taken into account the investment needs, objectives, and financial circumstances of any particular investor. Any views expressed and data illustrated herein were prepared based upon information, believed to be reliable, available to Quantopian, Inc. at the time of publication. Quantopian makes no guarantees as to their accuracy or completeness. All information is subject to change and may quickly become unreliable for various reasons, including changes in market conditions or economic circumstances.