# Useful Libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

"""
Explanation: Exercises: Variance - Answer Key
By Christopher van Hoecke, Maxwell Margenot, and Delaney Mackenzie
Lecture Link :
https://www.quantopian.com/lectures/variance
IMPORTANT NOTE:
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:

www.quantopian.com/lectures
github.com/quantopian/research_public
End of explanation
"""


X = np.random.randint(100, size = 100)

"""
Explanation: Data:
End of explanation
"""


# Range of X

print 'Range of X: %s' %(np.ptp(X))

# 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

# Variance and standard deviation

print 'Variance of X:', np.var(X)
print 'Standard deviation of X:', np.std(X)

# 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)

# 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)

"""
Explanation: Exercise 1:
Using the skills aquired in the lecture series, find the following parameters of the list X above:
- Range
- Mean Absolute Deviation
- Variance and Standard Deviation
- Semivariance and Semideviation
- Target variance (with B = 60)
End of explanation
"""


att = get_pricing('T', fields='open_price', start_date='2016-01-01', end_date='2017-01-01')

# Rolling mean
variance = att.rolling(window = 30).var()

# Rolling standard deviation
std = att.rolling(window = 15).std()

"""
Explanation: Exercise 2:
Using the skills aquired in the lecture series, find the following parameters of prices for AT&T stock over a year:
- 30 days rolling variance 
- 15 days rolling Standard Deviation
End of explanation
"""


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

"""
Explanation: Exercise 3 :
The portfolio variance is calculated as
$$\text{VAR}p = \text{VAR}{s1} (w_1^2) + \text{VAR}{s2}(w_2^2) + \text{COV}{S_1, S_2} (2 w_1 w_2)$$
Where $w_1$ and $w_2$ are the weights of $S_1$ and $S_2$.
Find values of $w_1$ and $w_2$ to have a portfolio variance of 50.
End of explanation
"""


