Notebooks and Code for Advanced Portfolio Construction and Analysis with Python

(c) Vijay Vaidyanathan 2019

These files should be in the same level where you have your data folder.

Please send any comments, bugs (or bug fixes!) to vijay@OptimalAM.com and I'll incorporate in the next version.

This is the beta test version v03 19_01_2021

The amendents in v03 with respect to v02 are:

  1. lab_203: to give the width of the rolling window we have to use the number of months. In the previous version there was the number of days written as string but it was not working.

ax = ind_cw.rolling('1825D').apply(erk.sharpe_ratio, raw=True, kwargs={"riskfree_rate":0.03, "periods_per_year":12}).mean(axis=1)["1945":].plot(figsize=(12,5), label="CW", legend=True) ind_ew.rolling('1825D').apply(erk.sharpe_ratio, raw=True, kwargs={"riskfree_rate":0.03, "periods_per_year":12}).mean(axis=1)["1945":].plot(ax=ax, label="EW", legend=True)

====

#=========================
#=========================== #=========================

====

ax = ind_cw.rolling(60).apply(erk.sharpe_ratio, raw=True, kwargs={"riskfree_rate":0.03, "periods_per_year":12}).mean(axis=1)["1945":].plot(figsize=(12,5), label="CW", legend=True) ind_ew.rolling(60).apply(erk.sharpe_ratio, raw=True, kwargs={"riskfree_rate":0.03, "periods_per_year":12}).mean(axis=1)["1945":].plot(ax=ax, label="EW", legend=True)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

  1. lab_204: in the weight_cw fuction it was used the less recent information in the rolling window to compute the weights of the portfolio in the back test. Now the function takes the most recent information available.

def weight_cw(r, cap_weights, **kwargs): """ Returns the weights of the CW portfolio based on the time series of capweights """ return cap_weights.loc[r.index[0]]

====

#=========================
#=========================== #=========================

====

def weight_cw(r, cap_weights, **kwargs): """ Returns the weights of the CW portfolio based on the time series of capweights """ return cap_weights.loc[r.index[1]]

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

def weight_cw(r, cap_weights, **kwargs): """ Returns the weights of the CW portfolio based on the time series of capweights """ w = cap_weights.loc[r.index[0]] return w/w.sum()

====

#=========================
#=========================== #=========================

====

def weight_cw(r, cap_weights, **kwargs): """ Returns the weights of the CW portfolio based on the time series of capweights """ w = cap_weights.loc[r.index[1]] return w/w.sum()