Monte Carlo Portfolio Simulation and the Need for Weight Constraints
Summary
The document presents a Python example that simulates portfolios by drawing random asset weights, scaling them to a fixed total, and calculating annualized return, volatility, and a return-to-risk ratio from historical returns and covariance. It asks how to adapt this procedure to hold one asset’s weight fixed while imposing lower and upper bounds on the others.
The code illustrates basic Monte Carlo portfolio construction and performance calculations, but it does not provide a solution to the requested constraints. Its random-weight generation only enforces a total weight; it does not guarantee a fixed holding or asset-specific bounds. The scaling factor shown also makes the total allocation differ from a conventional fully invested portfolio. No results, comparison with an optimizer, or discussion of transaction costs and estimation error are supplied, so the example is best read as an introductory question rather than a validated allocation method.
Key ideas
- The example estimates portfolio return and volatility from historical asset returns and their covariance matrix.
- It samples random asset weights and rescales them to a chosen aggregate allocation.
- The presented simulation does not enforce a fixed weight for one asset or bounds for the others.
- A portfolio constraint must be built into the weight generation or handled by a constrained optimization method.
- The example reports no performance results or assessment of estimation uncertainty.
Tags
Full text
# Optimising a portfolio with weight constraints in Python
# Optimising a portfolio with weight constraints in Python
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, HShown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.