简体   繁体   中英

ValueError in CVXPY minization function (Minimum Variance Optimization)

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

问题出在frontier_mean.append(r) ,应该是frontier_mean.append(r.value)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM