| Return vol $$ \sigma_{1} $$ | Return vol $$ \sigma_{2} $$ | Return correlation $$ \rho_{12} $$ | Portfolio volatility |
|---|---|---|---|
| $3 | $4 | 1.0 | $$ \sqrt{3^2 + 4^2 + 2 \cdot 3 \cdot 4 \cdot 1} = 3 + 4 = |
| $3 | $4 | -1.0 | $$ \sqrt{3^2 + 4^2 + 2 \cdot 3 \cdot 4 \cdot (-1)} = 4 - 3 = |
| $3 | $4 | 0.5 | $$ \sqrt{3^2 + 4^2 + 2 \cdot 3 \cdot 4 \cdot 0.5} = \sqrt{37} = |
| $3 | $4 | -0.5 | $$ \sqrt{3^2 + 4^2 + 2 \cdot 3 \cdot 4 \ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [ | |
| { | |
| "quote": "When I use a word, it means just what I choose it to mean-neither more nor less.", | |
| "author": "Humpty Dumpty" | |
| }, | |
| { | |
| "quote": "You know the world is going crazy when the best rapper is a white guy, the best golfer is a black guy, the tallest guy in the NBA is Chinese, the Swiss hold the America's Cup, France is accusing the U.S. of arrogance, Germany doesn't want to go to war, and the three most powerful men in America are named 'Bush', 'Dick', and 'Colon.'", | |
| "author": "Chris Rock" | |
| }, | |
| { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # number of stocks | |
| n = 1000 | |
| # random historical mean returns for each stock | |
| mu = np.random.normal(0.1, 0.2, n) | |
| # number of factors | |
| m = 10 | |
| # factor covariance matrix - random symmetrical matrix | |
| SigmaFactor = np.random.randn(m, m)/4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| vol_limit = cp.Parameter(nonneg=True) | |
| prob = cp.Problem(cp.Maximize(ret), | |
| [cp.norm1(w) <= 1.5, # 1-norm <= 1.5, i.e. gross exposure < 150% | |
| cp.sum(w) == 1, | |
| vol <= vol_limit] | |
| ) | |
| # define function so we can solve many in parallel | |
| def solve_vl(vl_val): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| vol_limit = cp.Parameter(nonneg=True) | |
| prob = cp.Problem(cp.Maximize(ret), | |
| [cp.sum(w) == 1, | |
| w >= 0, | |
| vol <= vol_limit # new constraint: vol <= vol_limit parameter | |
| ] | |
| ) | |
| # define helper function |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Solve max return portfolio (corner solution) | |
| prob = cp.Problem(cp.Maximize(ret), # maximize return | |
| [cp.sum(w) == 1, | |
| w >= 0] | |
| ) | |
| prob.solve() | |
| wts = [float('%0.4f' % v) for v in w.value] | |
| maxretvol = vol.value |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import cvxpy as cp | |
| # compute covariance matrix (df being the dataframe of historical returns) | |
| Sigma = np.cov(df.transpose()) | |
| # number of assets | |
| n = Sigma.shape[0] | |
| # average returns | |
| mu = df.mean().values | |
| # asset SDs | |
| asset_vols = np.sqrt(Sigma.diagonal()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pandas_datareader as pdr | |
| # load gold data from FRED API & save copy locally to CSV file | |
| series = ['GOLDAMGBD228NLBM'] | |
| gold_download = pdr.data.DataReader(series, | |
| 'fred', | |
| start='1968-12-31') | |
| # convert daily to annual | |
| gold_download = gold_download.resample('A').last().reset_index() | |
| gold_download.set_index(pd.DatetimeIndex(gold_download['DATE']).year, inplace=True) | |
| gold_download['return'] = gold_download['GOLDAMGBD228NLBM'].pct_change() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| data_xls = 'http://www.stern.nyu.edu/~adamodar/pc/datasets/histretSP.xls' | |
| data_sheet = "Returns by year" | |
| # these will change as rows get added on Damodaran website | |
| skiprows = range(17) | |
| skipfooter = 10 | |
| download_df = pd.read_excel('http://www.stern.nyu.edu/~adamodar/pc/datasets/histretSP.xls', | |
| sheet_name=data_sheet, | |
| skiprows=skiprows, | |
| skipfooter=skipfooter) | |
| download_df = download_df.set_index('Year') |
| ML Algo | Search algo | CV Error (RMSE in $) | Time mm::ss |
|---|---|---|---|
| Linear Regression | -- | 18192 | 0:01 |
| ElasticNet | ElasticNetCV (Grid Search) | 17964 | 0:02 |
| ElasticNet | GridSearchCV | 17964 | 0:05 |
NewerOlder