<jupyter_start><jupyter_text>Efficient Frontier Part 2<jupyter_code>%load_ext autoreload
%autoreload 2
%matplotlib inline
import edhec_risk_kit as erk

ind = erk.get_ind_returns()
er = erk.annualize_rets(ind['1996':'2000'], 12)
cov = ind['1996':'2000'].cov()
def portfolio_return(weights, returns):
    '''
    Weights -> Returns
    '''
    return weights.T @ returns

def portfolio_vol(weights, covmat):
    '''
    Weights -> Vol
    '''
    return (weights.T @ covmat @ weights) ** 0.5
l = ['Food', 'Beer', 'Smoke', 'Coal']
er[l]
cov.loc[l, l]
import numpy as np
weights = np.repeat(1/4, 4)
weights
erk.portfolio_return(weights, er[l])
erk.portfolio_vol(weights, cov.loc[l, l])<jupyter_output><empty_output><jupyter_text>2 Asset Frontier<jupyter_code>l = ['Games', 'Fin']
n_points = 20
weights = [np.array([w, 1-w]) for w in np.linspace(0, 1, n_points)]
weights
import pandas as pd
rets = [erk.portfolio_return(w, er[l]) for w in weights]
vols = [erk.portfolio_vol(w, cov.loc[l, l]) for w in weights]
ef = pd.DataFrame({'R': rets, 'Vol': vols})
ef.plot.scatter(x='Vol', y='R')
l = ['Fin', 'Beer']
erk.plot_ef2(25, er[l], cov.loc[l, l], style='.')<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup