问题描述
我有下面的代码,在其中我试图找到有效前沿的最大sharpe_ratio解决方案。我已经修改了一些我以前用来解决背包问题的代码作为一个 mip。我正在使用 cvxpy 模块。当我运行代码时,首先我会收到重复的警告。然后它失败并出现错误:
“ufunc 的循环不支持没有可调用 sqrt 方法的 MulExpression 类型的参数 0”
我已经包含了下面的代码。我还包括警告和错误。下面我已经包含了示例数据。任何人都可以看到问题可能是什么并建议如何解决它?
代码:
selection = cvxpy.Variable(len(weights),boolean=True)
# The sum of the weights should be less than or equal to P
weight_constraint = weights * selection <= current_account
def portfolio_performance(weights,mean_returns,cov_matrix,period):
print('returns')
returns = np.sum(weights*mean_returns.T)*period
print('std')
std = np.sqrt(np.dot(weights.T,np.dot(cov_matrix,weights))) * np.sqrt(period)
return std,returns
def sharpe_ratio(weights,risk_free_rate,period):
p_var,p_ret = portfolio_performance(weights,period)
return (p_ret - risk_free_rate) / p_var
df=extended_prices
mean_returns = df.mean()
cov_matrix = df.cov()
risk_free_rate = 0.0
# Our total utility is the sum of the item utilities
# total_utility = utilities * selection
total_utility = sharpe_ratio(weights=selection,mean_returns=mean_returns,cov_matrix=cov_matrix,risk_free_rate=risk_free_rate,period=df.shape[0])
# We tell cvxpy that we want to maximize total utility
# subject to weight_constraint. All constraints in
# cvxpy must be passed as a list
knapsack_problem = cvxpy.Problem(cvxpy.Maximize(total_utility),[weight_constraint])
# Solving the problem
knapsack_problem.solve(solver=cvxpy.GLPK_MI)
警告:
UserWarning:
This use of ``*`` has resulted in matrix multiplication.
Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1.
Use ``*`` for matrix-scalar and vector-scalar multiplication.
Use ``@`` for matrix-matrix and matrix-vector multiplication.
Use ``multiply`` for elementwise multiplication.
错误:
AttributeError Traceback (most recent call last)
AttributeError: 'MulExpression' object has no attribute 'sqrt'
The above exception was the direct cause of the following exception:
TypeError Traceback (most recent call last)
<ipython-input-122-35839b05103e> in <module>
49 cov_matrix=cov_matrix,50 risk_free_rate=risk_free_rate,---> 51 period=df.shape[0])
52
53
<ipython-input-122-35839b05103e> in sharpe_ratio(weights,period)
26
27 def sharpe_ratio(weights,period):
---> 28 p_var,period)
29 return (p_ret - risk_free_rate) / p_var
30
<ipython-input-122-35839b05103e> in portfolio_performance(weights,period)
22 # returns = 5
23 print('std')
---> 24 std = np.sqrt(np.dot(weights.T,weights))) * np.sqrt(period)
25 return std,returns
26
TypeError: loop of ufunc does not support argument 0 of type MulExpression which has no callable sqrt method
权重:
[171.35499572753906,108.7699966430664,104.56999969482422,271.70001220703125,253.1499938964844,79.58999633789062,479.6099853515625,92.48999786376952,152.24000549316406,20.6299991607666,159.17999267578125,184.97999572753903,41.2599983215332,61.889997482299805,82.5199966430664,103.14999580383301,123.77999496459961,144.4099941253662,165.0399932861328,185.6699924468994]
样本数据:
print(df[:50])
MMM ABT ABBV ABMD ACN ATVI \
0 175.869995 107.610001 103.809998 264.899994 248.520004 76.190002
1 175.714996 107.355003 103.889999 265.589996 248.429993 76.690002
2 176.135696 107.330002 104.010002 265.720001 249.085007 76.870003
3 175.979996 107.415001 104.080002 265.910004 248.845001 77.010002
4 176.350006 107.500000 103.849998 264.160004 249.603195 76.900002
5 176.240005 107.245003 103.830002 262.910004 249.520004 76.629997
6 176.700699 106.940002 103.839996 262.954987 249.100006 76.110001
7 176.154999 106.820000 103.846497 262.595001 249.380005 76.339996
8 176.550003 107.029999 103.955002 264.269989 249.645004 76.379997
9 176.360001 106.809998 103.879997 263.559998 250.220001 76.430000
10 176.804993 106.839996 103.824997 263.179993 250.139999 76.190002
11 176.955002 106.699997 103.919998 NaN 249.520004 76.125000
12 177.130005 106.684998 103.914597 263.209991 249.904999 76.019997
13 176.860001 106.394997 103.769997 262.945007 249.869995 75.974998
14 176.630005 106.500000 103.800003 262.850006 249.509995 76.070000
15 176.580002 106.570000 103.930000 262.549988 249.770004 76.264000
16 176.570007 106.605003 103.870003 262.399994 249.970001 76.059998
17 176.690002 106.599998 103.860001 261.804993 249.410004 76.040001
18 176.520004 106.790001 103.930000 261.929993 249.750000 76.074997
19 176.845001 106.610001 103.959999 262.070007 249.279999 75.980003
20 176.660004 106.599998 103.955002 261.575012 248.865005 76.029999
21 176.550003 106.699997 103.870003 262.505005 248.850006 76.089996
22 176.500000 106.660004 103.769997 NaN 248.940002 76.110001
23 176.309998 106.669998 103.680000 262.200012 248.699997 76.250000
24 176.315994 106.724998 103.690002 262.200012 249.020004 76.449997
25 176.315002 106.690002 103.660004 NaN 249.065002 76.297600
26 176.279999 106.510002 103.698997 NaN 248.809998 76.360001
27 176.424606 106.543198 103.690002 262.595001 248.940002 76.339996
28 176.660004 106.612900 103.739998 NaN 248.931793 76.279999
29 176.722794 106.500000 103.660004 263.070007 249.149994 76.339996
30 176.570007 106.419998 103.680000 263.690002 249.289993 76.535004
31 176.686996 106.459999 103.794998 264.470001 249.330002 76.500000
32 176.350006 106.433998 103.760002 264.260010 249.384995 76.559998
33 176.369995 106.699997 103.629997 NaN 249.509995 76.519997
34 176.160004 106.540001 103.709999 263.910004 249.369995 76.459999
35 176.037598 106.332001 103.669998 NaN 249.050003 76.485001
36 175.919998 106.379997 103.614998 264.209991 249.410004 76.480003
37 175.899994 106.239998 103.730003 264.209991 249.429993 76.440002
38 176.490005 106.160004 103.892700 264.200012 249.910004 76.290001
39 176.509995 106.260002 103.949997 264.584991 249.970001 76.385002
40 176.289993 106.459999 103.809998 264.559998 249.839996 76.440002
41 176.619995 106.519997 103.910004 NaN 249.919998 76.320000
42 176.350006 106.410004 103.830002 264.609985 250.009995 76.290001
43 176.350006 106.425003 103.860001 264.540009 249.985001 76.260002
44 176.270004 106.339996 103.870003 264.785004 249.860001 76.199898
45 176.179993 106.360001 103.860001 264.500000 249.899994 76.106300
46 176.205002 106.360001 103.834999 264.179993 249.759995 76.139999
47 176.119995 106.389999 103.820000 264.519989 249.645004 76.120003
48 176.251999 106.195000 103.800003 264.670013 249.740005 76.137802
49 176.300003 106.239998 103.750000 264.839996 249.589996 76.114998
ADBE AMD AAP AES ATVI_2 AMD_2 \
0 467.489990 86.769997 150.009995 21.125000 152.380005 173.539993
1 470.070007 87.209999 149.005005 21.004999 153.380005 174.419998
2 469.512085 87.220001 148.613098 21.000000 153.740005 174.440002
3 468.850006 87.070000 148.529999 21.020000 154.020004 174.139999
4 470.535004 87.550003 148.865005 21.075001 153.800003 175.100006
5 470.410004 87.550102 148.830002 21.010000 153.259995 175.100204
6 468.819885 87.129997 147.990005 20.995001 152.220001 174.259995
7 469.279999 86.714897 148.070007 20.940001 152.679993 173.429794
8 469.070007 87.078102 148.529999 20.990000 152.759995 174.156204
9 469.459991 86.820000 148.369995 20.955000 152.860001 173.639999
10 468.929993 86.739899 148.250000 21.010000 152.380005 173.479797
11 466.700012 86.419998 148.190002 20.973499 152.250000 172.839996
12 466.329987 86.339996 148.339996 21.040001 152.039993 172.679993
13 467.734985 86.519997 148.550003 20.940001 151.949997 173.039993
14 466.630005 86.110001 148.270004 20.940001 152.139999 172.220001
15 467.480011 86.180000 147.669998 20.870001 152.528000 172.360001
16 467.075012 85.889999 147.539993 20.840000 152.119995 171.779999
17 467.070007 85.980003 147.384995 20.834999 152.080002 171.960007
18 468.390015 86.199898 147.479996 20.875000 152.149994 172.399796
19 467.600006 86.309998 147.570999 20.915001 151.960007 172.619995
20 468.369995 86.520103 147.804993 20.889999 152.059998 173.040207
21 467.644989 86.431999 147.850006 20.934999 152.179993 172.863998
22 467.480011 86.363297 148.335007 20.934999 152.220001 172.726593
23 467.750000 86.410004 148.735001 20.969999 152.500000 172.820007
24 468.375000 86.529999 148.682999 21.019899 152.899994 173.059998
25 467.540009 86.211700 148.360001 21.075001 152.595200 172.423401
26 466.230011 86.075600 148.735001 21.080000 152.720001 172.151199
27 466.200012 86.139397 148.820007 21.063299 152.679993 172.278793
28 466.359985 86.121696 149.179993 21.098000 152.559998 172.243393
29 466.440002 86.184998 149.414993 21.084999 152.679993 172.369995
30 467.220001 86.260002 149.639999 21.090000 153.070007 172.520004
31 466.920013 86.309998 149.770004 21.129999 153.000000 172.619995
32 467.579987 86.364998 149.365005 21.155001 153.119995 172.729996
33 467.890015 86.514397 149.580002 21.115000 153.039993 173.028793
34 468.174988 86.565002 149.595001 21.075001 152.919998 173.130005
35 467.820007 86.540001 149.330002 21.059999 152.970001 173.080002
36 467.589996 86.660004 149.330002 21.049999 152.960007 173.320007
37 467.530090 86.620003 149.990005 21.030001 152.880005 173.240005
38 467.070007 86.940002 149.460007 21.030001 152.580002 173.880005
39 466.899994 87.050003 149.740005 20.950100 152.770004 174.100006
40 466.660004 87.124397 149.559998 20.915001 152.880005 174.248795
41 466.899994 87.203400 149.589996 20.969999 152.639999 174.406799
42 468.149994 87.279999 149.710007 20.969999 152.580002 174.559998
43 469.116211 87.264999 149.839996 20.995001 152.520004 174.529999
44 469.210785 87.235001 149.850006 20.920000 152.399796 174.470001
45 469.052795 87.370003 149.839996 20.910000 152.212601 174.740005
46 469.279999 87.309998 150.100006 20.940001 152.279999 174.619995
47 468.655304 87.080002 150.014999 20.959999 152.240005 174.160004
48 468.570007 87.040001 150.220001 20.959999 152.275604 174.080002
49 469.649994 87.085098 150.270004 20.940001 152.229996 174.170197
AES_2 AES_3 AES_4 AES_5 AES_6 AES_7 \
0 42.250000 63.375000 84.500000 105.625000 126.750000 147.875000
1 42.009998 63.014997 84.019997 105.024996 126.029995 147.034994
2 42.000000 63.000000 84.000000 105.000000 126.000000 147.000000
3 42.040001 63.060001 84.080002 105.100002 126.120003 147.140003
4 42.150002 63.225002 84.300003 105.375004 126.450005 147.525005
5 42.020000 63.030001 84.040001 105.050001 126.060001 147.070002
6 41.990002 62.985003 83.980003 104.975004 125.970005 146.965006
7 41.880001 62.820002 83.760002 104.700003 125.640003 146.580004
8 41.980000 62.969999 83.959999 104.949999 125.939999 146.929998
9 41.910000 62.865000 83.820000 104.775000 125.730000 146.684999
10 42.020000 63.030001 84.040001 105.050001 126.060001 147.070002
11 41.946999 62.920498 83.893997 104.867496 125.840996 146.814495
12 42.080002 63.120003 84.160004 105.200005 126.240005 147.280006
13 41.880001 62.820002 83.760002 104.700003 125.640003 146.580004
14 41.880001 62.820002 83.760002 104.700003 125.640003 146.580004
15 41.740002 62.610003 83.480003 104.350004 125.220005 146.090006
16 41.680000 62.520000 83.360001 104.200001 125.040001 145.880001
17 41.669998 62.504997 83.339996 104.174995 125.009995 145.844994
18 41.750000 62.625000 83.500000 104.375000 125.250000 146.125000
19 41.830002 62.745003 83.660004 104.575005 125.490005 146.405006
20 41.779999 62.669998 83.559998 104.449997 125.339996 146.229996
21 41.869999 62.804998 83.739998 104.674997 125.609997 146.544996
22 41.869999 62.804998 83.739998 104.674997 125.609997 146.544996
23 41.939999 62.909998 83.879997 104.849997 125.819996 146.789995
24 42.039799 63.059698 84.079597 105.099497 126.119396 147.139296
25 42.150002 63.225002 84.300003 105.375004 126.450005 147.525005
26 42.160000 63.240000 84.320000 105.400000 126.480000 147.559999
27 42.126598 63.189898 84.253197 105.316496 126.379795 147.443094
28 42.195999 63.293999 84.391998 105.489998 126.587997 147.685997
29 42.169998 63.254997 84.339996 105.424995 126.509995 147.594994
30 42.180000 63.270000 84.360001 105.450001 126.540001 147.630001
31 42.259998 63.389997 84.519997 105.649996 126.779995 147.909994
32 42.310001 63.465002 84.620003 105.775003 126.930004 148.085005
33 42.230000 63.344999 84.459999 105.574999 126.689999 147.804998
34 42.150002 63.225002 84.300003 105.375004 126.450005 147.525005
35 42.119999 63.179998 84.239998 105.299997 126.359997 147.419996
36 42.099998 63.149998 84.199997 105.249996 126.299995 147.349995
37 42.060001 63.090002 84.120003 105.150003 126.180004 147.210005
38 42.060001 63.090002 84.120003 105.150003 126.180004 147.210005
39 41.900200 62.850300 83.800400 104.750500 125.700600 146.650700
40 41.830002 62.745003 83.660004 104.575005 125.490005 146.405006
41 41.939999 62.909998 83.879997 104.849997 125.819996 146.789995
42 41.939999 62.909998 83.879997 104.849997 125.819996 146.789995
43 41.990002 62.985003 83.980003 104.975004 125.970005 146.965006
44 41.840000 62.760000 83.680000 104.600000 125.520000 146.440001
45 41.820000 62.730000 83.639999 104.549999 125.459999 146.369999
46 41.880001 62.820002 83.760002 104.700003 125.640003 146.580004
47 41.919998 62.879997 83.839996 104.799995 125.759995 146.719994
48 41.919998 62.879997 83.839996 104.799995 125.759995 146.719994
49 41.880001 62.820002 83.760002 104.700003 125.640003 146.580004
AES_8 AES_9
0 169.000000 190.125000
1 168.039993 189.044992
2 168.000000 189.000000
3 168.160004 189.180004
4 168.600006 189.675007
5 168.080002 189.090002
6 167.960007 188.955008
7 167.520004 188.460005
8 167.919998 188.909998
9 167.639999 188.594999
10 168.080002 189.090002
11 167.787994 188.761494
12 168.320007 189.360008
13 167.520004 188.460005
14 167.520004 188.460005
15 166.960007 187.830008
16 166.720001 187.560001
17 166.679993 187.514992
18 167.000000 187.875000
19 167.320007 188.235008
20 167.119995 188.009995
21 167.479996 188.414995
22 167.479996 188.414995
23 167.759995 188.729994
24 168.159195 189.179094
25 168.600006 189.675007
26 168.639999 189.719999
27 168.506393 189.569693
28 168.783997 189.881996
29 168.679993 189.764992
30 168.720001 189.810001
31 169.039993 190.169992
32 169.240005 190.395006
33 168.919998 190.034998
34 168.600006 189.675007
35 168.479996 189.539995
36 168.399994 189.449993
37 168.240005 189.270006
38 168.240005 189.270006
39 167.600800 188.550900
40 167.320007 188.235008
41 167.759995 188.729994
42 167.759995 188.729994
43 167.960007 188.955008
44 167.360001 188.280001
45 167.279999 188.189999
46 167.520004 188.460005
47 167.679993 188.639992
48 167.679993 188.639992
49 167.520004 188.460005
我尝试更新我的原始目标函数,如下所示,使其符合 dcp,但我现在收到以下错误。谁能指出我做错了什么?
更新:
nonzero_constraint=selection.T*(mean_returns)>=1
total_utility = selection.Tcov_matrixselection
knapsack_problem = cvxpy.Problem(cvxpy.Minimize(total_utility),[weight_constraint,nonzero_constraint])
解决问题
knapsack_problem.solve(solver=cvxpy.GLPK_MI)
错误:
DCPError Traceback(最后一次调用) 在 76 77#解决问题 ---> 78 knapsack_problem.solve(solver=cvxpy.GLPK_MI)
~/anaconda3/envs/pyopft/lib/python3.6/site-packages/cvxpy/problems/problem.py in solve(self,*args,**kwargs) 394 其他: 第395话 --> 396 返回solve_func(self,**kwargs) 397 第398话
~/anaconda3/envs/pyopft/lib/python3.6/site-packages/cvxpy/problems/problem.py in _solve(self,solver,warm_start,verbose,gp,qcp,requires_grad,enforce_dpp,**kwargs ) 749 750 数据,solution_chain,inverse_data = self.get_problem_data( --> 751 求解器、gp、enforce_dpp) 第752话 第 753 章
~/anaconda3/envs/pyopft/lib/python3.6/site-packages/cvxpy/problems/problem.py in get_problem_data(self,enforce_dpp) 第498话 第499话 --> 500 求解器=求解器,gp=gp,enforce_dpp=enforce_dpp) 501 self._cache.key = 键 第502话
~/anaconda3/envs/pyopft/lib/python3.6/site-packages/cvxpy/problems/problem.py in _construct_chain(self,enforce_dpp) 第 657 章 第658话 --> 659 enforce_dpp=enforce_dpp) 660 661 def_invalidate_cache(self):
~/anaconda3/envs/pyopft/lib/python3.6/site-packages/cvxpy/reductions/solvers/solving_chain.py inconstruct_solving_chain(problem,Candidate,enforce_dpp) 149 如果 len(problem.variables()) == 0: 150 return SolvingChain(reductions=[ConstantSolver()]) --> 151 减少 = _reductions_for_problem_class(问题,候选人,gp) 152 第153话
~/anaconda3/envs/pyopft/lib/python3.6/site-packages/cvxpy/reductions/solvers/solving_chain.py in _reductions_for_problem_class(问题,候选人,gp)
88“考虑使用qcp=True
调用solve()。”)
第89话
---> 90 "问题不符合 DCP 规则。具体:\n" + append)
91 elif gp 而不是问题.is_dgp():
92 append = build_non_disciplined_error_msg(问题,'DGP')
DCPError:问题不符合 DCP 规则。具体来说: 目标不是 DCP。它的以下子表达式不是: var23093344 @ [[ 3.84942037e+00 -1.44475820e+00 -3.07897522e-01 -3.83725155e+00 -6.97027884e-01 -1.98708303e+00 -2.56525048e+00 -4.56423916e+00 -1.28337594e+00 2.83267804e-01 -3.97416605e+00 -9.12847832e+00 5.66535607e-01 8.49803411e-01 1.13307121e+00 1.41633902e+00 1.69960682e+00 1.98287463e+00 2.26614243e+00 2.54941023e+00] [-1.44475820e+00 9.03419342e-01 3.95196397e-01 1.83672079e+00 6.48748093e-01 1.01702327e+00 2.01661555e+00 1.99196571e+00 5.52363255e-01 -9.01794890e-02 2.03404654e+00 3.98393142e+00 -1.80358978e-01 -2.70538467e-01 -3.60717956e-01 -4.50897445e-01 -5.41076934e-01 -6.31256423e-01 -7.21435912e-01 -8.11615401e-01] [-3.07897522e-01 3.95196397e-01 3.33613139e-01 7.09458137e-01 5.11557701e-01 4.31630655e-01 1.51539538e+00 7.18197672e-01 2.25679149e-01 -1.70698178e-02 8.63261311e-01 1.43639534e+00 -3.41396357e-02 -5.12094535e-02 -6.82792714e-02 -8.53490892e-02 -1.02418907e-01 -1.19488725e-01 -1.36558543e-01 -1.53628361e-01] [-3.83725155e+00 1.83672079e+00 7.09458137e-01 1.04386637e+01 -1.10101961e+00 3.26227765e+00 8.38200553e+00 5.25645683e+00 2.66755640e-01 -3.75571048e-01 6.52455530e+00 1.05129137e+01 -7.51142096e-01 -1.12671314e+00 -1.50228419e+00 -1.87785524e+00 -2.25342629e+00 -2.62899734e+00 -3.00456838e+00 -3.38013943e+00] [-6.97027884e-01 6.48748093e-01 5.11557701e-01 -1.10101961e+00 2.81383991e+00 2.58908121e-01 2.00101825e+00 9.04831781e-01 1.34931224e+00 5.39865451e-02 5.17816242e-01 1.80966356e+00 1.07973090e-01 1.61959635e-01 2.15946180e-01 2.69932725e-01 3.23919270e-01 3.77905816e-01 4.31892361e-01 4.85878906e-01] [-1.98708303e+00 1.01702327e+00 4.31630655e-01 3.26227765e+00 2.58908121e-01 1.51587371e+00 3.27026890e+00 2.86060880e+00 5.57662873e-01 -1.74534952e-01 3.03174743e+00 5.72121760e+00 -3.49069904e-01 -5.23604856e-01 -6.98139808e-01 -8.72674759e-01 -1.04720971e+00 -1.22174466e+00 -1.39627962e+00 -1.57081457e+00] [-2.56525048e+00 2.01661555e+00 1.51539538e+00 8.38200553e+00 2.00101825e+00 3.27026890e+00 1.61762017e+01 4.57673878e+00 1.06989491e+00 -2.07397238e-01 6.54053779e+00 9.15347755e+00 -4.14794475e-01 -6.22191713e-01 -8.29588950e-01 -1.03698619e+00 -1.24438343e+00 -1.45178066e+00 -1.65917790e+00 -1.86657514e+00] [-4.56423916e+00 1.99196571e+00 7.18197672e-01 5.25645683e+00 9.04831781e-01 2.86060880e+00 4.57673878e+00 6.79844270e+00 1.59813819e+00 -4.13211396e-01 5.72121760e+00 1.35968854e+01 -8.26422792e-01 -1.23963419e+00 -1.65284558e+00 -2.06605698e+00 -2.47926838e+00 -2.89247977e+00 -3.30569117e+00 -3.71890256e+00] [-1.28337594e+00 5.52363255e-01 2.25679149e-01 2.66755640e-01 1.34931224e+00 5.57662873e-01 1.06989491e+00 1.59813819e+00 1.49212902e+00 -4.43315724e-02 1.11532575e+00 3.19627637e+00 -8.86631448e-02 -1.32994717e-01 -1.77326290e-01 -2.21657862e-01 -2.65989434e-01 -3.10321007e-01 -3.54652579e-01 -3.98984151e-01] [ 2.83267804e-01 -9.01794890e-02 -1.70698178e-02 -3.75571048e-01 5.39865451e-02 -1.74534952e-01 -2.07397238e-01 -4.13211396e-01 -4.43315724e-02 3.57022766e-02 -3.49069904e-01 -8.26422792e-01 7.14045531e-02 1.07106830e-01 1.42809106e-01 1.78511383e-01 2.14213659e-01 2.49915936e-01 2.85618212e-01 3.21320489e-01] [-3.97416605e+00 2.03404654e+00 8.63261311e-01 6.52455530e+00 5.17816242e-01 3.03174743e+00 6.54053779e+00 5.72121760e+00 1.11532575e+00 -3.49069904e-01 6.06349486e+00 1.14424352e+01 -6.98139808e-01 -1.04720971e+00 -1.39627962e+00 -1.74534952e+00 -2.09441942e+00 -2.44348933e+00 -2.79255923e+00 -3.14162913e+00] [-9.12847832e+00 3.98393142e+00 1.43639534e+00 1.05129137e+01 1.80966356e+00 5.72121760e+00 9.15347755e+00 1.35968854e+01 3.19627637e+00 -8.26422792e-01 1.14424352e+01 2.71937708e+01 -1.65284558e+00 -2.47926838e+00 -3.30569117e+00 -4.13211396e+00 -4.95853675e+00 -5.78495954e+00 -6.61138234e+00 -7.43780513e+00] [ 5.66535607e-01 -1.80358978e-01 -3.41396357e-02 -7.51142096e-01 1.07973090e-01 -3.49069904e-01 -4.14794475e-01 -8.26422792e-01 -8.86631448e-02 7.14045531e-02 -6.98139808e-01 -1.65284558e+00 1.42809106e-01 2.14213659e-01 2.85618212e-01 3.57022766e-01 4.28427319e-01 4.99831872e-01 5.71236425e-01 6.42640978e-01] [ 8.49803411e-01 -2.70538467e-01 -5.12094535e-02 -1.12671314e+00 1.61959635e-01 -5.23604856e-01 -6.22191713e-01 -1.23963419e+00 -1.32994717e-01 1.07106830e-01 -1.04720971e+00 -2.47926838e+00 2.14213659e-01 3.21320489e-01 4.28427319e-01 5.35534148e-01 6.42640978e-01 7.49747808e-01 8.56854637e-01 9.63961467e-01] [ 1.13307121e+00 -3.60717956e-01 -6.82792714e-02 -1.50228419e+00 2.15946180e-01 -6.98139808e-01 -8.29588950e-01 -1.65284558e+00 -1.77326290e-01 1.42809106e-01 -1.39627962e+00 -3.30569117e+00 2.85618212e-01 4.28427319e-01 5.71236425e-01 7.14045531e-01 8.56854637e-01 9.99663743e-01 1.14247285e+00 1.28528196e+00] [ 1.41633902e+00 -4.50897445e-01 -8.53490892e-02 -1.87785524e+00 2.69932725e-01 -8.72674759e-01 -1.03698619e+00 -2.06605698e+00 -2.21657862e-01 1.78511383e-01 -1.74534952e+00 -4.13211396e+00 3.57022766e-01 5.35534148e-01 7.14045531e-01 8.92556914e-01 1.07106830e+00 1.24957968e+00 1.42809106e+00 1.60660244e+00] [ 1.69960682e+00 -5.41076934e-01 -1.02418907e-01 -2.25342629e+00 3.23919270e-01 -1.04720971e+00 -1.24438343e+00 -2.47926838e+00 -2.65989434e-01 2.14213659e-01 -2.09441942e+00 -4.95853675e+00 4.28427319e-01 6.42640978e-01 8.56854637e-01 1.07106830e+00 1.28528196e+00 1.49949562e+00 1.71370927e+00 1.92792293e+00] [ 1.98287463e+00 -6.31256423e-01 -1.19488725e-01 -2.62899734e+00 3.77905816e-01 -1.22174466e+00 -1.45178066e+00 -2.89247977e+00 -3.10321007e-01 2.49915936e-01 -2.44348933e+00 -5.78495954e+00 4.99831872e-01 7.49747808e-01 9.99663743e-01 1.24957968e+00 1.49949562e+00 1.74941155e+00 1.99932749e+00 2.24924342e+00] [ 2.26614243e+00 -7.21435912e-01 -1.36558543e-01 -3.00456838e+00 4.31892361e-01 -1.39627962e+00 -1.65917790e+00 -3.30569117e+00 -3.54652579e-01 2.85618212e-01 -2.79255923e+00 -6.61138234e+00 5.71236425e-01 8.56854637e-01 1.14247285e+00 1.42809106e+00 1.71370927e+00 1.99932749e+00 2.28494570e+00 2.57056391e+00] [ 2.54941023e+00 -8.11615401e-01 -1.53628361e-01 -3.38013943e+00 4.85878906e-01 -1.57081457e+00 -1.86657514e+00 -3.71890256e+00 -3.98984151e-01 3.21320489e-01 -3.14162913e+00 -7.43780513e+00 6.42640978e-01 9.63961467e-01 1.28528196e+00 1.60660244e+00 1.92792293e+00 2.24924342e+00 2.57056391e+00 2.89188440e+00]] @ var23093344
解决方法
你不能只在 cvxpy 对象上使用非 cvxpy 函数!
有些运算符重载 (Docs) 和 sum / (np.sum)
有它的用途,但一般来说:不要!
查找 cvxpy 的大量构建块:Atomic Functions
import cvxpy as cvx
import numpy as np
selection = cvx.Variable(2)
np.sqrt(selection)
# AttributeError: 'Variable' object has no attribute 'sqrt'
#
# The above exception was the direct cause of the following exception:
#
# Traceback (most recent call last):
# File "so_cvxpy_ufunc.py",line 5,in <module>
# np.sqrt(selection)
# TypeError: loop of ufunc does not support argument 0 of type Variable which has no
# callable sqrt method
相比:
import cvxpy as cvx
selection = cvx.Variable(2)
cvx.sqrt(selection)
# OK
提示
您会遇到其他问题(解决上述问题还不够),例如缺少 DCP 兼容性!我强烈建议首先对 cvxpy 进行更基本/正式/理论的研究,以了解它的作用以及它是如何做到的,以推断它的功能和局限性。我只是这么说,因为人们经常在同一个问题上循环 5 个问题。