0
$\begingroup$

I am looking to run weight portfolio simulations, but I would like to fix the weight of one asset and set lower/upper bound limits for the remaining assets.

I have only been using Python for a couple of months.

Below is the standard code I found to run simulated asset weights. It works great; but I want to see how I could add weight constraints. Namely, fixing the weight of one asset and setting lower/upper bounds on the rest.

The data is uploaded via excel in the usual way.

Code below:

df=pd.read_excel('data.xlsx', sheet_name='data1')

dRtns = df.pct_change()

number_of_portfolios=10000
number_of_assets=len(df.columns)

portfolio_returns = []
portfolio_risk = []
sharpe_ratio_port = []
portfolio_weights = []

for portfolio in range (number_of_portfolios):
    weights = np.random.random_sample(number_of_assets)
    weights=4*weights/np.sum(weights)   
    annualise_return=np.sum((dRtns.mean()*weights)*252)
    portfolio_returns.append(annualise_return)
    matrix_cov_port=(dRtns.cov())*252
    portfolio_variance=np.dot(weights.T,np.dot(matrix_cov_port,weights))
    portfolio_std=np.sqrt(portfolio_variance)
    portfolio_risk.append(portfolio_std)
    sharpe_ratio=((annualise_return)/portfolio_std)
    sharpe_ratio_port.append(sharpe_ratio)
    portfolio_weights.append(weights)
    
portfolio_risk=np.array(portfolio_risk)
portfolio_returns=np.array(portfolio_returns)
sharpe_ratio_port=np.array(sharpe_ratio_port)

Can anyone help?

Thank you, H

$\endgroup$
2
  • $\begingroup$ Caution: the weights you are generating are not uniformly distributed (maybe the factor of 4 in the 11th line is an attempt to fix this?). Here is an example of a correct method to generate (unconstrained) random weights. quant.stackexchange.com/questions/45897/… $\endgroup$
    – nbbo2
    Mar 7, 2022 at 14:28
  • $\begingroup$ Hi - in fact the 4 is not meant to be there. I was playing around with the code. i used: weights=weights/np.sum(weights). Thank you, H $\endgroup$
    – Hercules
    Mar 8, 2022 at 13:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.