I am trying to implement the Sharpe's return-based style analysis on Python. The problem is formulated as follows:

min Var(M-(c1a1 + c2a2 + c3a3 + c4a4))
subject to c1 + c2 + c3 + c4 = 1
           c1 >=0, c2 >= 0, c3 >= 0, c4 >= 0
where M = monthly or daily return of an investor's portfolio
      a1, a2, a3, a4 = monthly or daily return of an index
      and c1, c2, c4, c4 are the optimization decision variables.

Of course, the objective function (Variance) makes the problem nonlinear. I am trying to use Scipy to implement this, but I cannot find a good example of quadratic/nonlinear optimization similar to this problem.

What Python library should I use to do this? The example above has only 4 indices, but I want to make it more general and flexible as to handle many indices.

I would also very appreciate it if someone could show a Python code for the optimization problem above.

Thank you!!!

  • $\begingroup$ try google searching cvxopt $\endgroup$
    – Attack68
    Jun 20 '18 at 19:19
  • $\begingroup$ Attack68, for personal reason, I cannot install cvxopt. Is it possible to use Scipy to solve this? $\endgroup$
    – Jun Jang
    Jun 20 '18 at 19:30
  • $\begingroup$ yes just can use scipy.optimize.minimize and pass your own functions and constraints. You will want to use the 'SLSQP' method. With 4 variables you wont need to bother about calculating jacobian functions but if you had a lot more varianbles it would work much faster if you provide a function for calculating the derivatives of your objective function and the constraints (which aren't actually that complicated) $\endgroup$
    – Attack68
    Jun 20 '18 at 19:38
  • $\begingroup$ Thank you very much. So I am actually having a hard time writing the objective function, because it is variance.. $\endgroup$
    – Jun Jang
    Jun 20 '18 at 19:39
  • $\begingroup$ you might be interested in this: stackoverflow.com/questions/44515880/…. Note also that $Var(aX-bY) = a^2Var(X) + b^2Var(Y) - 2ab Cov(X,Y)$ so you need to know all of the variances and covariances of your portfolio and indexes $\endgroup$
    – Attack68
    Jun 20 '18 at 19:46

You could restate your problem as:

$$ \min_x \quad Var \left (-\sum_{i=0}^n x_iR_i \right) $$ $$ \text{s.t.} \quad x_0 = -1, \quad \sum_{i=1}^n x_i = 1, \quad \text{non-negativity of }x_1:x_n $$ where $R_i$ are the expected returns of asset $i$ and $x_i$ are your solution variables.

The objective function can be expressed as: $$ \min_x \quad - \sum_{i,j} x_i x_j Cov(R_i, R_j)= - \mathbf{x^TQx} $$ where $\mathbf{x}=[x_0, ..., x_n]^T$ and $\mathbf{Q}$ is the covariance matrix of returns.

  • $\begingroup$ Thank you very much for your reply. So the first objective function (Var(- sum of xR)) is the same as -X^TQX? The second objective function seems pretty easy to calculate (just matrix multiplications). $\endgroup$
    – Jun Jang
    Jun 20 '18 at 20:07
  • $\begingroup$ yes it is. and is why i suggested cvxopt since that is a library optimised for quadratic programs, and this is a (matrix) quadratic equation. You look at covariance in wikipedia for the properties of expanding linear sums. $\endgroup$
    – Attack68
    Jun 20 '18 at 20:14
  • $\begingroup$ Oh i see. this is great. Thank you so much! By the way, do you have experience with scipy optimization? $\endgroup$
    – Jun Jang
    Jun 20 '18 at 20:15
  • $\begingroup$ i do but give it a go first, and use the link I posted above which shows some python code with scipy. $\endgroup$
    – Attack68
    Jun 20 '18 at 20:16
  • $\begingroup$ My only problem is that I do not know how to declare the decision variables (X = [x_0, x_1, ..., x_n]^T) using Scipy.. Once I can do that, then I can just do the matrix multiplications, right? $\endgroup$
    – Jun Jang
    Jun 20 '18 at 20:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.