I want to maximise the expected return of a portfolio of securities, subject to a maximum level of risk. The following code does this.
I would now like to fix the first security weight to be something like 0.3 and then rerun the optimisation. How do I pass this information through CVXPY?
import yfinance as yf
import numpy as np
import cvxpy as cp
tickers = ['AAPL', 'GOOGL', 'MSFT', 'AMZN']
start_date = '2018-01-01'
end_date = '2023-10-31'
data = yf.download(tickers, start_date, end_date)['Adj Close']
returns = np.log(data / data.shift(1)).dropna()
mean_returns = returns.mean()
cov_matrix = returns.cov()
n_assets = len(tickers)
weights = cp.Variable(n_assets)
returns = mean_returns.values
portfolio_return = returns @ weights
portfolio_risk = cp.quad_form(weights, cov_matrix.values)
objective = cp.Maximize(portfolio_return)
constraints = [weights <= 1, weights >= 0, portfolio_risk <= 0.0001]
problem = cp.Problem(objective, constraints)
problem.solve()
print(weights.value)