I have a dataframe $n$ by $m$ representing $m$ timeseries of returns (each column is a different time series) with total $n$ number of observations, I want to find weight vector of length $m$ such that the sharpe ratio of the resulting time series is maximized (defined as average of column / std of column)

I tried using cvxpy to accomplish this, but I am getting a DCP rules error. Is there a way to do this in cvxpy? If not, what about cvxopt? My suspicion is that I have not formulated it in a convex way. I can change the problem to maximize return subject to the standard deviation be below a certain threshold. But I dont want to set an arbitrary threshold

import pandas as pd
import cvxpy as cp
import numpy as np

df = pd.DataFrame([[0.01, -0.005],
                   [-0.005, -0.005],
                   [0.02, 0.01],
                   [0.01, -0.005],
                   [-0.03, 0.0025],
                   [0.01, -0.005],
                   [0.01, 0.001],], columns=["a","b"])
m = len(df.columns)
n = len(df)
A = np.array(df.values)
x = cp.Variable(m)
objective = cp.Maximize(cp.sum(A@x) / cp.sum_squares(A@x - cp.sum(A@x) / n))
constraints = [sum(x)==1, x<=np.array([1]*m), x>=np.array([0]*m)]
prob = cp.Problem(objective, constraints)



Your Answer

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