0

I've done a Python implementation of Markowitz portfolio optimization. The minimization function would ensure the risk is minimized while solving the optimizer to generate certain percentage return based on list of tickers. It also takes into account that sum of weights should be equal 1. Most of the time I generate the results are infeasible. What could be the reason?

import pandas as pd
import numpy as np
from helper_functions.get_data import download_data
from scipy.optimize import minimize

def optimizer(tickers, start_date, end_date, required_return=0.02):

    df = download_data(tickers, start_date, end_date)
    
    # Calculate daily returns
    returns = df['Close'].pct_change()

    # Calculate mean returns and covariance
    mean_returns = returns.mean()
    cov_matrix = returns.cov()

    # Number of portfolio assets
    num_assets = len(mean_returns)

    # Initialize weights
    weights_initial = num_assets * [1. / num_assets,]

    # Constraints
    constraints = ({'type': 'eq', 'fun': lambda x: np.sum(x) - 1}, 
                {'type': 'eq', 'fun': lambda x: np.sum(x*mean_returns) - required_return})

    # Bounds
    bounds = tuple((0,1) for asset in range(num_assets))

    # Objective function
    def objective(weights):
        return np.dot(weights.T, np.dot(cov_matrix, weights))

    # Optimize
    result = minimize(objective, weights_initial, method='SLSQP', bounds=bounds, constraints=constraints)

    # Return optimization results
    return result
Lopez
  • 461
  • 5
  • 19
  • In the past I've found this kind of constrained optimization works better if you reduce it to N-1 weights, and find the weight of the last item in the portfolio from the first N-1 weights. See this example: https://stackoverflow.com/a/76124870/530160 – Nick ODell Jun 13 '23 at 20:59
  • The eq constraint on returns seems to imply that the solver should prefer a solution with returns of 0.02 to a solution with returns of 0.03. Is that intended? – Nick ODell Jun 13 '23 at 21:02
  • Expected return should be 0.02 i.e. 2%. Yes, that is intended. I've constructed it based on my understanding of Markowitz optimization so either there is something wrong with implementation/understanding or it is true there isn't any feasible solution. – Lopez Jun 13 '23 at 21:06
  • [Wikipedia says](https://en.wikipedia.org/wiki/Markowitz_model#Methodology) `From the portfolios that have the same risk level, an investor will prefer the portfolio with higher rate of return.`, which would imply that a return of 0.03 would be better than a return of 0.02. – Nick ODell Jun 13 '23 at 21:13
  • Given the constraint if the optimizer fails to find an optimal solution with 0.02 return how would it find optimal solution at 0.03? Although I do agree that return constraint should be `>=0.02` instead of `==0.02` – Lopez Jun 13 '23 at 21:19
  • It is quite possible that you cannot reach your wanted return if none of the instruments has a return as good as you want. – Erwin Kalvelagen Jun 14 '23 at 15:44
  • I agree I just need confirmation that my implementation is correct – Lopez Jun 14 '23 at 17:14
  • I don't see any problems in the implementation. Unfortunately, no-one else can test the implementation, because there is no data to test with. You might get better answers if you make the problem [reproducible](https://stackoverflow.com/help/minimal-reproducible-example). As it is, it is hard to say if the solver cannot find a solution because there is no solution, or if it cannot find a solution because of some technical problem. – Nick ODell Jun 16 '23 at 00:35

0 Answers0