1

I'm a bit of a beginner and in the process of moving an algorithm that works with minimum variance optimization from scipy.minimize.optimize (which didn't perform properly) to CVXPY.

R are the expected returns, C the coveriances and rf the risk-free rate. w are the optimal weights and r various means along the Efficient Frontier for which the weights are calculated.

When I run the code below I get:

ValueError: setting an array element with a sequence.

I believe var is at fault here, but I don't know how else to structure it. Insight much appreciated. On top of that, the rest of the code could have additional errors so if you spot any please do point them out!

def solve_frontier(R, C, rf, context):
        frontier_mean, frontier_var, frontier_weights = [], [], []
        n = len(R)
        w = cvx.Variable(n)
        r = cvx.Parameter(sign='positive')
        mean_1 = sum(R*w)  
        var = dot(dot(w, C), w)
        penalty = (1/100)*abs(mean_1-r)

        prob = cvx.Problem(cvx.Minimize(var + penalty),
                       [sum(w)-context.allowableMargin == 0])

        r_vals = linspace(max(min(R), rf), max(R), num=20)
        for i in range(20):
            r.value = r_vals[i]
            prob.solve()
            frontier_mean.append(r)
            frontier_var.append(compute_var(prob.value, C))
            frontier_weights.append(prob.value) 
            print "status:", prob.status
        return array(frontier_mean), array(frontier_var), frontier_weights
4
  • The error should tell you exactly the line where the problem occurs. Please get that information. Also if you read up on minimum complete verifiable questions and improve your question, it will probably get a better response in addition to being more helpful for future users. Commented Sep 12, 2015 at 8:13
  • I'm using an ipython notebook and it doesn't actually say which line's problematic. I'll try and make it mcv. Commented Sep 12, 2015 at 8:40
  • This part looks suspicious to me [sum(w)-context.allowableMargin == 0]. Do you really intend to pass a single boolean in a list as an argument? If not what do you intend? Also, are you saying that the code above prints the value error that you mentioned? If so, that Problem class is strange for hiding an unhandled error and you will want to focus on what is going on in that class. Commented Sep 12, 2015 at 9:05
  • allowableMargin is set to either 1 or more depending on arbitrary conditions; the constraint implicitly ensures the weights sum to 1 for non-leveraged trading or higher if leverage is in play. If I debug manually I can see the error thrown at the definition of "var"; could this be an NaN problem? Commented Sep 12, 2015 at 9:22

1 Answer 1

1

The problem was in frontier_mean.append(r), which should have been frontier_mean.append(r.value).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.