Hedging

Make sure to refer to the video for full explanations!


In [163]:
import numpy as np
from statsmodels import regression
import statsmodels.api as sm
import matplotlib.pyplot as plt

In [164]:
# Get data for the specified period and stocks
start = '2016-01-01'
end = '2017-01-01'
asset = get_pricing('AAPL', fields='price', start_date=start, end_date=end)
benchmark = get_pricing('SPY', fields='price', start_date=start, end_date=end)

In [165]:
asset_ret = asset.pct_change()[1:]
bench_ret = benchmark.pct_change()[1:]

In [166]:
asset_ret.plot()
bench_ret.plot()
plt.legend()


Out[166]:
<matplotlib.legend.Legend at 0x7fbc682c86d0>

Regression for Alpha and Beta Values


In [167]:
plt.scatter(bench_ret,asset_ret,alpha=0.6,s=50)
plt.xlabel('SPY Ret')
plt.ylabel('AAPL Ret')


Out[167]:
<matplotlib.text.Text at 0x7fbc5eca1a10>

In [168]:
AAPL = asset_ret.values
spy = bench_ret.values

In [169]:
# Add a constant (column of 1s for intercept)
spy_constant = sm.add_constant(spy)

# Fit regression to data
model = regression.linear_model.OLS(AAPL,spy_constant).fit()

In [170]:
model.params


Out[170]:
array([  1.67990248e-05,   1.02981370e+00])

In [171]:
alpha , beta = model.params

In [172]:
alpha


Out[172]:
1.6799024809993564e-05

In [173]:
beta


Out[173]:
1.0298136979465768

Plot Alpha and Beta


In [174]:
# Scatter Returns
plt.scatter(bench_ret,asset_ret,alpha=0.6,s=50)

# Fit Line
min_spy = bench_ret.values.min()
max_spy = bench_ret.values.max()

spy_line = np.linspace(min_spy,max_spy,100)
y = spy_line * beta + alpha

plt.plot(spy_line,y,'r')

plt.xlabel('SPY Ret')
plt.ylabel('AAPL Ret')


Out[174]:
<matplotlib.text.Text at 0x7fbc5db40810>

Implementing the Hedge


In [175]:
hedged = -1*beta*bench_ret + asset_ret

In [176]:
hedged.plot(label='AAPL with Hedge')
bench_ret.plot(alpha=0.5)
asset_ret.plot(alpha=0.5)
plt.legend()


Out[176]:
<matplotlib.legend.Legend at 0x7fbc5c08d490>

What happens if there is a big market drop?


In [177]:
hedged.plot(label='AAPL with Hedge')
bench_ret.plot()
asset_ret.plot()
plt.xlim(['2016-06-01','2016-08-01'])
plt.legend()


Out[177]:
<matplotlib.legend.Legend at 0x7fbc5d4cdfd0>

Effects of Hedging


In [178]:
def alpha_beta(benchmark_ret,stock):
    
    benchmark = sm.add_constant(benchmark_ret)
    
    model = regression.linear_model.OLS(stock,benchmark).fit()
    
    return model.params[0], model.params[1]

2016-2017 Alpha and Beta


In [179]:
# Get the alpha and beta estimates over the last year
start = '2016-01-01'
end = '2017-01-01'

asset2016 = get_pricing('AAPL', fields='price', start_date=start, end_date=end)
benchmark2016 = get_pricing('SPY', fields='price', start_date=start, end_date=end)

asset_ret2016 = asset2016.pct_change()[1:]
benchmark_ret2016 = benchmark2016.pct_change()[1:]

aret_val = asset_ret2016.values
bret_val = benchmark_ret2016.values

alpha2016, beta2016 = alpha_beta(bret_val,aret_val)

print('2016 Based Figures')
print('alpha: ' + str(alpha2016))
print('beta: ' + str(beta2016))


2016 Based Figures
alpha: 1.679902481e-05
beta: 1.02981369795

Creating a Portfolio


In [180]:
# Create hedged portfolio and compute alpha and beta
portfolio = -1*beta2016*benchmark_ret2016 + asset_ret2016

alpha, beta = alpha_beta(benchmark_ret2016,portfolio)
print('Portfolio with Alphas and Betas:')
print('alpha: ' + str(alpha))
print('beta: ' + str(beta))


Portfolio with Alphas and Betas:
alpha: 1.679902481e-05
beta: 1.36609473733e-16

In [181]:
# Plot the returns of the portfolio as well as the asset by itself
portfolio.plot(alpha=0.9,label='AAPL with Hedge')
asset_ret2016.plot(alpha=0.5);
benchmark_ret2016.plot(alpha=0.5)
plt.ylabel("Daily Return")
plt.legend();



In [182]:
portfolio.mean()


Out[182]:
1.6799024809993652e-05

In [183]:
asset_ret2016.mean()


Out[183]:
0.000574549568770769

In [184]:
portfolio.std()


Out[184]:
0.012065447808277703

In [185]:
asset_ret2016.std()


Out[185]:
0.014702747344261722

2017 Based Figures


In [186]:
# Get data for a different time frame:
start = '2017-01-01'
end = '2017-08-01'

asset2017 = get_pricing('AAPL', fields='price', start_date=start, end_date=end)
benchmark2017 = get_pricing('SPY', fields='price', start_date=start, end_date=end)

asset_ret2017 = asset2017.pct_change()[1:]
benchmark_ret2017 = benchmark2017.pct_change()[1:]

aret_val = asset_ret2017.values
bret_val = benchmark_ret2017.values

alpha2017, beta2017 = alpha_beta(bret_val,aret_val)

print('2016 Based Figures')
print('alpha: ' + str(alpha2017))
print('beta: ' + str(beta2017))


2016 Based Figures
alpha: 0.000968854542346
beta: 1.26769880296

Creating a Portfolio based off 2016 Beta estimate


In [187]:
# Create hedged portfolio and compute alpha and beta
portfolio = -1*beta2016*benchmark_ret2017 + asset_ret2017

alpha, beta = alpha_beta(benchmark_ret2017,portfolio)
print 'Portfolio with Alphas and Betas Out of Sample:'
print 'alpha: ' + str(alpha)
print 'beta: ' + str(beta)


Portfolio with Alphas and Betas Out of Sample:
alpha: 0.000968854542346
beta: 0.23788510501

In [188]:
# Plot the returns of the portfolio as well as the asset by itself
portfolio.plot(alpha=0.9,label='AAPL with Hedge')
asset_ret2017.plot(alpha=0.5);
benchmark_ret2017.plot(alpha=0.5)
plt.ylabel("Daily Return")
plt.legend();


What are the actual effects? Typically sacrificing average returns for less volatility, but this is also highly dependent on the security:


In [189]:
portfolio.mean()


Out[189]:
0.0011399935632582228

In [190]:
asset_ret2017.mean()


Out[190]:
0.0018808609159293456

In [191]:
portfolio.std()


Out[191]:
0.009066375411652783

In [192]:
asset_ret2017.std()


Out[192]:
0.010484461271996029

Great Job!