Comments for The Quant MBA https://thequantmba.wordpress.com DZ's blog Sat, 23 Sep 2017 13:17:32 +0000 hourly 1 http://wordpress.com/ Comment on Risk Parity/Risk Budgeting Portfolio in Python by Feng https://thequantmba.wordpress.com/2016/12/14/risk-parityrisk-budgeting-portfolio-in-python/comment-page-1/#comment-64 Sat, 23 Sep 2017 13:17:32 +0000 http://thequantmba.wordpress.com/?p=177#comment-64 I read this article several weeks ago. It is very inspiring and helpful to my research. Thanks for sharing.
I had once been puzzled by the same problem with Hansueli, i.e. the minimize function only runs 1 iteration with certain covariance matrix input. I also tried two ways, i.e. (1)penalizing the cost (2)changing the stopping criterion in the minimize function to 1e-15. It solved the problem in some situation. But there is still problems with certain covariance matrix input.
For example, I have this 9*9 covariance matrix:
V=np.matrix([[3.23822414e-02,2.09586336e-02,1.19953181e-03,-3.42945597e-04,1.44226523e-03,3.18839922e-03,1.28094942e-04,1.09620895e-04,-8.14963119e-05]
,[2.09586336e-02,1.39930713e-01,6.50801720e-03,3.02103328e-03,1.08176923e-02,4.80103744e-02,7.69201371e-04,-1.25520964e-02,-1.88265113e-04]
,[1.19953181e-03,6.50801720e-03,3.69433053e-03,-5.13665176e-04,2.20203633e-03,2.31740623e-03,8.55164862e-05,-7.56440183e-04,-7.81788792e-06]
,[-3.42945597e-04,3.02103328e-03,-5.13665176e-04,1.95207568e-03,-3.27383676e-04,4.38795650e-04,2.68556355e-05,-2.18586766e-04,-2.54360233e-06]
,[1.44226523e-03,1.08176923e-02,2.20203633e-03,-3.27383676e-04,2.96759371e-03,4.80443514e-03,1.93560587e-04,-1.06510489e-03,-2.46200168e-05]
,[3.18839922e-03,4.80103744e-02,2.31740623e-03,4.38795650e-04,4.80443514e-03,2.36495619e-02,9.62021325e-04,-1.86584784e-03,-9.06395586e-05]
,[1.28094942e-04,7.69201371e-04,8.55164862e-05,2.68556355e-05,1.93560587e-04,9.62021325e-04,2.89909625e-04,5.31840917e-04,2.55116425e-06]
,[1.09620895e-04,-1.25520964e-02,-7.56440183e-04,-2.18586766e-04,-1.06510489e-03,-1.86584784e-03,5.31840917e-04,9.29821052e-03,2.14245629e-05]
,[-8.14963119e-05,-1.88265113e-04,-7.81788792e-06,-2.54360233e-06,-2.46200168e-05,-9.06395586e-05,2.55116425e-06,2.14245629e-05,5.78390121e-06]])
With equal risk budgets, the resulting weights in this risk parity portfolio are:
[ 3.96638553e-02 1.31838486e-02 1.15744406e-01 2.23200288e-01
1.00733213e-01 2.98562419e-02 3.71432543e-01 1.06185604e-01
5.36184804e-19]
The weight of the last asset is 0! And the optimization appears not to reach the 1e-15 stopping criterion:
Optimization terminated successfully. (Exit mode 0)
Current function value: 0.000738113620943
Iterations: 21
Function evaluations: 232
Gradient evaluations: 21
The possible reason is that the variance of the last asset, which represents the money market mutual fund, is far more smaller than other assets. This is the usual case in practical market. I am trying to find ways to solve this problem, and to make this algorithm more robust. Do you have any suggestions?
Thank you very much.

And when I enter different initial values, it gives totally different results. So strange that the algorithm is sensitive to initial values.
case 1.
w0=np.ones(9)*1.0/9
Optimization terminated successfully. (Exit mode 0)
Current function value: 0.000738113620943
Iterations: 21
Function evaluations: 232
Gradient evaluations: 21
The resulting weights:
[[ 3.96638553e-02 1.31838486e-02 1.15744406e-01 2.23200288e-01
1.00733213e-01 2.98562419e-02 3.71432543e-01 1.06185604e-01
5.36184804e-19]]

case 2.
w0=[0.05217992,0.04176691,0.08060158,0.03545008,0.00784548,0.00609901,0.01657915,0.07723674,0.68224114]
Optimization terminated successfully. (Exit mode 0)
Current function value: 1.00601342965e-15
Iterations: 33
Function evaluations: 363
Gradient evaluations: 33
The resulting weights:
[[ 0.00787686 0.00260464 0.020088 0.03888814 0.01903772 0.0061617
0.05590877 0.01845764 0.83097653]]

]]>
Comment on Risk Parity/Risk Budgeting Portfolio in Python by Feng https://thequantmba.wordpress.com/2016/12/14/risk-parityrisk-budgeting-portfolio-in-python/comment-page-1/#comment-63 Sat, 23 Sep 2017 13:06:25 +0000 http://thequantmba.wordpress.com/?p=177#comment-63

Risk Parity/Risk Budgeting Portfolio in Python

]]>
Comment on Risk Parity/Risk Budgeting Portfolio in Python by Feng https://thequantmba.wordpress.com/2016/12/14/risk-parityrisk-budgeting-portfolio-in-python/comment-page-1/#comment-61 Tue, 19 Sep 2017 09:27:21 +0000 http://thequantmba.wordpress.com/?p=177#comment-61 And when I enter different initial values, it gives totally different results. So strange that the algorithm is sensitive to initial values.
case 1.
w0=np.ones(9)*1.0/9
Optimization terminated successfully. (Exit mode 0)
Current function value: 0.000738113620943
Iterations: 21
Function evaluations: 232
Gradient evaluations: 21
The resulting weights:
[[ 3.96638553e-02 1.31838486e-02 1.15744406e-01 2.23200288e-01
1.00733213e-01 2.98562419e-02 3.71432543e-01 1.06185604e-01
5.36184804e-19]]

case 2.
w0=[0.05217992,0.04176691,0.08060158,0.03545008,0.00784548,0.00609901,0.01657915,0.07723674,0.68224114]
Optimization terminated successfully. (Exit mode 0)
Current function value: 1.00601342965e-15
Iterations: 33
Function evaluations: 363
Gradient evaluations: 33
The resulting weights:
[[ 0.00787686 0.00260464 0.020088 0.03888814 0.01903772 0.0061617
0.05590877 0.01845764 0.83097653]]

]]>
Comment on Risk Parity/Risk Budgeting Portfolio in Python by Feng https://thequantmba.wordpress.com/2016/12/14/risk-parityrisk-budgeting-portfolio-in-python/comment-page-1/#comment-60 Tue, 19 Sep 2017 06:21:55 +0000 http://thequantmba.wordpress.com/?p=177#comment-60 I read this article several weeks ago. It is very inspiring and helpful to my research. Thanks for sharing.
I had once been puzzled by the same problem with Hansueli, i.e. the minimize function only runs 1 iteration with certain covariance matrix input. I also tried two ways, i.e. (1)penalizing the cost (2)changing the stopping criterion in the minimize function to 1e-15. It solved the problem in some situation. But there is still problems with certain covariance matrix input.
For example, I have this 9*9 covariance matrix:
V=np.matrix([[3.23822414e-02,2.09586336e-02,1.19953181e-03,-3.42945597e-04,1.44226523e-03,3.18839922e-03,1.28094942e-04,1.09620895e-04,-8.14963119e-05]
,[2.09586336e-02,1.39930713e-01,6.50801720e-03,3.02103328e-03,1.08176923e-02,4.80103744e-02,7.69201371e-04,-1.25520964e-02,-1.88265113e-04]
,[1.19953181e-03,6.50801720e-03,3.69433053e-03,-5.13665176e-04,2.20203633e-03,2.31740623e-03,8.55164862e-05,-7.56440183e-04,-7.81788792e-06]
,[-3.42945597e-04,3.02103328e-03,-5.13665176e-04,1.95207568e-03,-3.27383676e-04,4.38795650e-04,2.68556355e-05,-2.18586766e-04,-2.54360233e-06]
,[1.44226523e-03,1.08176923e-02,2.20203633e-03,-3.27383676e-04,2.96759371e-03,4.80443514e-03,1.93560587e-04,-1.06510489e-03,-2.46200168e-05]
,[3.18839922e-03,4.80103744e-02,2.31740623e-03,4.38795650e-04,4.80443514e-03,2.36495619e-02,9.62021325e-04,-1.86584784e-03,-9.06395586e-05]
,[1.28094942e-04,7.69201371e-04,8.55164862e-05,2.68556355e-05,1.93560587e-04,9.62021325e-04,2.89909625e-04,5.31840917e-04,2.55116425e-06]
,[1.09620895e-04,-1.25520964e-02,-7.56440183e-04,-2.18586766e-04,-1.06510489e-03,-1.86584784e-03,5.31840917e-04,9.29821052e-03,2.14245629e-05]
,[-8.14963119e-05,-1.88265113e-04,-7.81788792e-06,-2.54360233e-06,-2.46200168e-05,-9.06395586e-05,2.55116425e-06,2.14245629e-05,5.78390121e-06]])
With equal risk budgets, the resulting weights in this risk parity portfolio are:
[ 3.96638553e-02 1.31838486e-02 1.15744406e-01 2.23200288e-01
1.00733213e-01 2.98562419e-02 3.71432543e-01 1.06185604e-01
5.36184804e-19]
The weight of the last asset is 0! And the optimization appears not to reach the 1e-15 stopping criterion:
Optimization terminated successfully. (Exit mode 0)
Current function value: 0.000738113620943
Iterations: 21
Function evaluations: 232
Gradient evaluations: 21
The possible reason is that the variance of the last asset, which represents the money market mutual fund, is far more smaller than other assets. This is the usual case in practical market. I am trying to find ways to solve this problem, and to make this algorithm more robust. Do you have any suggestions?
Thank you very much.

]]>
Comment on Risk Parity/Risk Budgeting Portfolio in Python by Hansueli Müller https://thequantmba.wordpress.com/2016/12/14/risk-parityrisk-budgeting-portfolio-in-python/comment-page-1/#comment-31 Mon, 03 Jul 2017 10:27:08 +0000 http://thequantmba.wordpress.com/?p=177#comment-31 Hi DZ,
This helped, thanks for the hint. It is very useful for me as I am not yet very experienced in the world of python optimizers.
Best Regards

Hansueli

]]>
Comment on Risk Parity/Risk Budgeting Portfolio in Python by thequantmba https://thequantmba.wordpress.com/2016/12/14/risk-parityrisk-budgeting-portfolio-in-python/comment-page-1/#comment-30 Fri, 30 Jun 2017 14:43:12 +0000 http://thequantmba.wordpress.com/?p=177#comment-30 In reply to Hansueli Müller.

Hi Hansueli,
Thanks for verifying, I just checked with my code, it seems the minimize function only runs 1 iteration in this case which is not enough to find an optimal solution. It’s caused by the precision goal settings for the minimize function, which is defaulted at 1e-6, and your problem start at 1e-8.

There are two ways to modify this:

Change the risk_budget_objective function by penalising the cost and it should work. (e.g. I multiplied J*1000)
def risk_budget_objective(x,pars):
# calculate portfolio risk
V = pars[0]# covariance table
x_t = pars[1] # risk target in percent of portfolio risk
sig_p = np.sqrt(calculate_portfolio_var(x,V)) # portfolio sigma
risk_target = np.asmatrix(np.multiply(sig_p,x_t))
asset_RC = calculate_risk_contribution(x,V)
J = sum(np.square(asset_RC-risk_target.T))[0,0] *1000 # sum of squared error
return J

or you can change the ftol : float (Precision goal for the value of f in the stopping criterion) in the minimise function to something smaller (in this case I use 1e-12)
res= minimize(risk_budget_objective, w0, args=[V,x_t], method=’SLSQP’,constraints=cons, options={‘disp’: True, ‘ftol’: 1e-12})

By doing so, you would potentially run more iteration to get more precision. (a trade off)

]]>
Comment on Risk Parity/Risk Budgeting Portfolio in Python by Hansueli Müller https://thequantmba.wordpress.com/2016/12/14/risk-parityrisk-budgeting-portfolio-in-python/comment-page-1/#comment-29 Thu, 29 Jun 2017 09:34:56 +0000 http://thequantmba.wordpress.com/?p=177#comment-29 Great article – it helps me a lot solving my problem in python. However, when i run your code, I always end up with an equal weighted portfolio. Any hint you can give me? I operate with
w0 = [1/3] * 3
x_t = [1 /3] * 3
and
V=
[[ 0.00021555 0.00041903 0.0003287 ]
[ 0.00041903 0.00216102 0.00106898]
[ 0.0003287 0.00106898 0.00140555]]

any hints?

]]>
Comment on Max Diversification Portfolio in Python by Harry Tan https://thequantmba.wordpress.com/2017/06/06/max-diversification-in-python/comment-page-1/#comment-28 Mon, 19 Jun 2017 20:57:46 +0000 http://thequantmba.wordpress.com/?p=474#comment-28 This is a good post.

]]>
Comment on Principal Component Analysis of Equity Returns in Python by thequantmba https://thequantmba.wordpress.com/2017/01/24/principal-component-analysis-of-equity-returns-in-python/comment-page-1/#comment-19 Tue, 14 Mar 2017 17:26:00 +0000 http://thequantmba.wordpress.com/?p=371#comment-19 In reply to Jose Artes.

Hi Jose,
Thanks for your comment, good point. sorry about the confusion, with SKlearn, we don’t actually need to compute the covariance matrix to calculate the PCs, I was playing around with the linalg.eig(cov) and forgot to delete the stock_cov variables.

sklearn_pca = sklearnPCA(n_components=5)
pc = sklearn_pca.fit_transform(stocks_ret)

is equivalant to eigen decomposition using the numpy library

x = stocks_ret.values
xm = x – x.mean(axis=0)
cov = np.cov(xm.T) # where you have to compute the covariance matrix
evals, evecs = np.linalg.eig(cov)
xm.dot(evecs) # equivalent to sklearn_pca.fit_transform(stocks_ret)

# to get your first component (sklearn_pca.components_[0,:])
# note: evecs may not be sorted from highest evals to lowest evals, in this case the highest evals just happen to be the first evals)
evecs[:,0] # your first component (negative of sklearn_pca.components_[0,:])

]]>
Comment on Principal Component Analysis of Equity Returns in Python by Jose Artes https://thequantmba.wordpress.com/2017/01/24/principal-component-analysis-of-equity-returns-in-python/comment-page-1/#comment-17 Mon, 13 Mar 2017 19:16:40 +0000 http://thequantmba.wordpress.com/?p=371#comment-17 Sorry, but why do you compute the covariance matrix of the returns and then you completely neglect it afterwards, despite stating in your steps that you apply the PCA to it? Wouldn’t you have to compute pca=sklearn_PCA(n_components=20).fit(stocks_cov), for example, and then do principal_components=pca.transform(stock_ret) instead of doing sklearn_pca = sklearnPCA(n_components=20)?

]]>