在计算简单的最大夏普比率投资组合权重时,我在 PortfolioAnalytics 中遇到了一个问题:
Error in max_sr_opt(R = R, constraints = constraints, moments = moments, :
Objective function failed with message
Error in optimize(f = sharpe_obj_fun, R = R, constraints = constraints, :
'xmin' not less than 'xmax'
请在下面找到可重现的示例代码:
library(PortfolioAnalytics)
returnsText <- '
"date","NEAR","MINT"
"2014-07-01",0.000161110006774212,-9.86193284648884e-05
"2014-07-02",-0.000218949044995531,0.000197258111483523
"2014-07-03",-0.000378267533203247,9.86096047428386e-05
"2014-07-07",0,-9.85998818474609e-05
"2014-07-08",-0.000199163513616485,9.86096047428386e-05
"2014-07-09",0.000197211155088084,0.000197199763695144
"2014-07-10",0.000201155547844056,0
"2014-07-11",0.000203106333835912,0.000197160883615322
"2014-07-14",-0.000203065090029986,0
"2014-07-15",0.000509757069411476,0.000169524935917709
"2014-07-16",-0.000111452544867996,-0.00016949620208484
"2014-07-17",-0.000597133756883639,0.000394244036637348
"2014-07-18",0.000597490538653656,-0.000492610837184437
"2014-07-21",0,9.85707246621281e-05
"2014-07-22",-0.000199044586359554,0.000295683027205396
"2014-07-23",-3.78259998102815e-05,-0.000394127499825259
"2014-07-24",3.78274306707116e-05,0
"2014-07-25",-0.000597252636787449,-0.000197141449324145
"2014-07-28",0.000398406373049998,0.000295770482607871
"2014-07-29",0.00019912385541021,0.000295683027205396
"2014-07-30",0,-0.000197063750457582
"2014-07-31",0,0.000502611610304848
"2014-08-01",0.00054833764653095,0.000197122017793872
"2014-08-04",-0.000199123854866534,-0.000689791091990632
"2014-08-05",-0.000300736903335763,0.000493048023763265
"2014-08-06",-9.76194805905051e-05,0
"2014-08-07",0.000203227733818867,0
"2014-08-08",0.000195218346190407,-0.000394244037766889
"2014-08-11",-0.000131447918101602,0.000197199763714462
"2014-08-12",0.000131465198928149,0.000197160883634862
"2014-08-13",0.00019916351307292,0.000197122017793872
"2014-08-14",-1.99123714428318e-06,0
"2014-08-15",0.000199124251370142,-0.000492707922539193
"2014-08-18",-0.000197093764112588,9.85901608789863e-05
"2014-08-19",-0.000199123854866534,0
"2014-08-20",-0.000199163513073031,9.85804418176528e-05
"2014-08-21",0,0.000394282897597575
"2014-08-22",2.78884462789897e-05,-0.000394127499864894
"2014-08-25",-0.000426282932069477,-9.85707246720091e-05
"2014-08-26",0.000398565165061537,0.000394321766179928
"2014-08-27",0.000199203187079666,9.85415847256643e-05
"2014-08-28",-1.19498111355032e-05,-0.000295595624626332
"2014-08-29",0.000211115847003063,0.000413956238783486
"2014-09-02",-0.000876005574944494,-0.000492902208474866
"2014-09-03",0.000516553649691165,0.000197258111695131
"2014-09-04",0.00038073671596095,0.000295828814544263
"2014-09-05",-0.00029889409268935,3.5488959045038e-05
"2014-09-08",-0.000597966911962122,-3.5487699623471e-05
"2014-09-09",0.000398883126886407,-9.85804419127989e-05
"2014-09-10",5.58213729657808e-05,-0.000197180321948931
"2014-09-11",-0.000454520083608245,9.86096048480878e-05
"2014-09-12",0,9.85998819527101e-05
"2014-09-15",0,0.000394360642808511
"2014-09-16",-1.99441482195972e-06,0.000197102591354748
"2014-09-17",0.000201436380012687,-0.000183269286988752
"2014-09-18",-0.000398803588875118,0.000183302880776948
"2014-09-19",0.000797925393260623,-0.000197063749579174
"2014-09-22",-0.000797289215949459,-9.85512962219381e-05
"2014-09-23",0,0.000295683027522475
"2014-09-24",0.000797925393260623,-0.000394127500247143
"2014-09-25",-0.000199322303987337,0.000492853622747313
"2014-09-26",0.000398724082577573,-9.85221677600734e-05
"2014-09-29",0.00019928258471813,-9.85318742451335e-05
"2014-09-30",-6.37577209552376e-05,-0.000650374458294301
"2014-10-01",7.00981129100331e-05,0.000986679822432501
"2014-10-02",0.000388755980016198,-9.85707241716316e-05
"2014-10-03",-0.000189320340130417,-0.000690063092486648
"2014-10-06",-0.000346820809271176,0.000493242577261688
"2014-10-07",0.000346941135676682,0.000394399526516764
"2014-10-08",0.000201315527737389,-9.85610089417888e-05
"2014-10-09",3.38779711579473e-05,-0.000492853623035749
"2014-10-10",-0.000235145012875138,-0.000197238658130816
"2014-10-13",0.000199322305363792,0.000197277568893872
"2014-10-14",0.000398565165606435,0.000197238659219723
"2014-10-15",0.00119521912191933,0.000394399526516764
"2014-10-16",-0.00099482689953001,-0.000591366055827103
"2014-10-17",-0.000328619796307605,-0.000690335305635781
"2014-10-20",-0.000468188112956458,0.000789499655275971
"2014-10-21",0.00139525613096603,9.86096042523421e-05
"2014-10-22",-0.000199044587183228,-0.000492999408962524
"2014-10-23",-0.000597252637073331,-0.000394594061155717
"2014-10-24",0.000398406374703786,0.000394749827093088
"2014-10-27",0,0
"2014-10-28",-0.000388291518670814,0
"2014-10-29",0.000189241143167429,-0.000394594061155717
"2014-10-30",-0.000231029674647831,0.00029606237059232
"2014-10-31",-0.000167336011627328,-1.97316490654353e-05
"2014-11-03",-0.000122952778378926,0
"2014-11-04",0.000201396213098226,0
"2014-11-05",0.000199362042857754,-9.87166832120412e-05
"2014-11-06",-0.000199322305555638,-9.87264291576473e-05
"2014-11-07",-0.000199362040665285,0.000394944708111522
"2014-11-10",0.000199401793813836,9.86971960028615e-05
"2014-11-11",-0.000199362040665285,0
"2014-11-12",0,-0.000234876146634422
"2014-11-13",0.000187437687632297,0.000333641968091047
"2014-11-14",-0.000187402561329542,-0.000197355436238444
"2014-11-17",-6.38085750730433e-05,0.000394788787279277
"2014-11-18",0.000662056203570183,-0.000197316495844313
"2014-11-19",-0.000398565165992459,0.000197355437327573
"2014-11-20",0.000199362042857754,-0.000118389897071025
"2014-11-21",-0.000398644608919363,-0.000177605873436515
"2014-11-24",0,0.000157899931938488
"2014-11-25",0.000598205383634198,-0.000157875003486185
"2014-11-26",-0.000334794738732325,9.86874569168794e-05
"2014-11-28",0.000334906863788342,0.000197355437327573
"2014-12-01",0.000486389000651677,-9.87166831345476e-05
"2014-12-02",-0.000396652177151791,-9.87264290801537e-05
"2014-12-03",-0.000199401794614751,0
"2014-12-04",0,0.000296208530850661
"2014-12-05",-0.000598324690861629,-0.000493534695564235
"2014-12-08",0.000199560965873813,0
"2014-12-09",0,0.000296267035399866
"2014-12-10",-0.000251396648437763,0.000208727416499466
"2014-12-11",-0.00034725409780012,-0.000691562932129686
"2014-12-12",0.00016170892356282,0.000790904597333109
"2014-12-15",0.00036528331564889,-0.00138298923224633
"2014-12-16",-0.000127702709234789,0.000494608763774496
"2014-12-17",-0.000556775094721962,-9.88728487232304e-05
"2014-12-18",-0.000241603287466252,-0.000395530505390096
"2014-12-19",-0.000998601957253209,-0.000593530518055996
"2014-12-22",0.000399840063971757,0.000593883005743923
"2014-12-23",0.000399680255792623,-0.000197843506382078
"2014-12-24",3.72952466447529e-05,-0.000296823983180428
"2014-12-26",0,0.000197941408847546
"2014-12-29",0.000799520286848265,0.000949930734458038
"2014-12-30",0.000419412822907139,-0.000494608764139093
"2014-12-31",-0.00141742029524627,0.000197941409211477
"2015-01-02",0,9.89511180820024e-05
"2015-01-05",0.000599760094678548,9.89413277270312e-05
"2015-01-06",0.000965034964814926,-0.000197863078618354
"2015-01-07",-0.000864300783189664,0.000395804472327788
"2015-01-08",0.00089901108807422,-0.00108803165110805
"2015-01-09",0.000199600798706845,0.000495098524277537
"2015-01-12",0,-0.000197941409211366
"2015-01-13",0,0.000593941793309849
"2015-01-14",0.000399121930165558,-0.000296794617927421
"2015-01-15",-0.000398962695404403,9.89609103747568e-05
"2015-01-16",0.000399121930165558,-0.000692657826573684
"2015-01-20",-0.00099740674179738,0.00108921675341067
"2015-01-21",0.000796725239052831,-0.000791295746260245
"2015-01-22",-0.000995612520915179,0.000296970896654924
"2015-01-23",0.000599161175268037,0
"2015-01-26",0,0.000197921820749514
"2015-01-27",-0.000598802396120535,0
"2015-01-28",0.00032354703512727,0.000191946175833735
"2015-01-29",-0.000119793477545094,-9.29869996078025e-05
"2015-01-30",0.000539135227258791,0.000613375543934369
"2015-02-02",0.000305924098682908,-0.000395726156791842
"2015-02-03",0.000399281293311349,-0.000197941409533775
"2015-02-04",-0.000261424864042237,0.000296970897138538
"2015-02-05",6.18800764327077e-05,-9.89609105357392e-05
"2015-02-06",-0.000698602795423664,-0.0010886777513458
"2015-02-09",9.9870169238736e-05,0.00108986426232005
"2015-02-10",0.000399440782543614,0.000197941408444091
"2015-02-11",0.000199640647751131,-0.00029685335364027
"2015-02-12",0,0.0003959220026577
"2015-02-13",-0.000199600799318245,-9.89413278882356e-05
"2015-02-17",-0.000588939907798625,0.000197902236486414
"2015-02-18",0.000389528671218109,0.000296794618410701
"2015-02-19",0.000798722044007327,0.000296706557501025
"2015-02-20",-0.000458898644046579,-0.000148309274416336
"2015-02-23",0.000259496576516804,-4.94437577732709e-05
"2015-02-24",0.000199560966790413,0.000197784810317403
"2015-02-25",0.000399042298123486,-0.000296618548832561
"2015-02-26",-0.000199441562347635,0.000494510929168523
"2015-02-27",0.000199481347219033,-0.000237247924962269
"2015-03-02",-0.00101557638576522,-9.89315392826429e-05
"2015-03-03",0.000999000999640121,9.89413277003859e-05
"2015-03-04",0.000199600798092892,0
"2015-03-05",-8.78068262490928e-05,0.000296794618936502
"2015-03-06",-0.000327308723116593,-9.89021856461214e-05
"2015-03-09",0.000397291231692742,0.000989119682560435
"2015-03-10",-0.000161647285065403,9.88142302440487e-05
"2015-03-11",0.000578830761837512,-0.000592826796305901
"2015-03-12",-0.000398962696368299,-0.000197726149099076
"2015-03-13",0.000199560965565171,0.000395530504921915
"2015-03-16",0,-0.000247108827041043
"2015-03-17",0,0.000247169904906253
"2015-03-18",-0.000199521148931958,0.000395374123483005
"2015-03-19",0,-0.00069163126217564
"2015-03-20",-9.57892632084123e-05,9.88728494233371e-05
"2015-03-23",-0.000103781643523648,0.000790904597484321
"2015-03-24",0.000399201596186005,-0.000197569890164728
"2015-03-25",0,-9.88044669567589e-05
"2015-03-26",0.000399042297863694,-0.000494071145785369
"2015-03-27",-0.000199441563312086,0.000395452298198373
"2015-03-30",0.000199481348184039,0.000197647989834904
"2015-03-31",0.000199441563312197,-4.94022334783795e-05
"2015-04-01",-0.00011058823470167,0.000405342613665427
"2015-04-02",-0.000199560966330781,-9.88239946962954e-05
"2015-04-06",0.000399201597718113,0.000197667523686773
"2015-04-07",-0.000199521149697346,-0.000494071146310504
"2015-04-08",0,0.00019673751829985
"2015-04-09",0.000798243863135983,-0.000295542448883968
"2015-04-10",-0.000199401792886245,0.000296618548151217
"2015-04-13",-0.000199441564076919,0.00069190471536329
"2015-04-14",0.000797925393610566,-0.000197550375806266
"2015-04-15",-0.000364759816298466,0.000296384113416481
"2015-04-16",-3.38971572640956e-05,9.87654323292286e-05
"2015-04-17",-0.000598205383029238,-0.000493778392323452
"2015-04-20",0.000598563446905231,9.88044661922594e-05
"2015-04-21",0,-0.000395178818250796
"2015-04-22",-0.000199401792886245,9.98220990706677e-05
"2015-04-23",0,0.000493131245507206
"2015-04-24",0.000199441561891334,9.87751868171127e-05
"2015-04-27",-0.000199401792886245,-0.000197530863572326
"2015-04-28",0.000398883125968252,-9.87849454047174e-05
"2015-04-29",-0.000713716108378337,-0.000197589408582277
"2015-04-30",0.000714225862881612,0.0005928853747037
"2015-05-01",0.000282037480494379,-0.000988142292275507
"2015-05-04",-0.000199441562950597,0.000494559842176834
"2015-05-05",0.000359066425643206,0.000197726148371213
"2015-05-06",-0.000159527796745107,0
"2015-05-07",0.000199441562950486,0.000197687061556096
"2015-05-08",0,0.000395295978211863
"2015-05-11",9.97008969727542e-05,-0.000493924726307049
"2015-05-12",9.96909598782736e-05,0.000296501285100392
"2015-05-13",-0.000797448167551118,0.000790435728391659
"2015-05-14",-0.000399042297140717,0
"2015-05-15",0.000399201595462362,-0.000276434001625692
"2015-05-18",0,7.90029824320193e-05
"2015-05-19",-7.98084581170588e-05,0
"2015-05-20",0.000478888976833902,0.000789967413455273
"2015-05-21",0,-0.000690675875202995
"2015-05-22",0,9.87361770221629e-05
"2015-05-26",0.000199441562950486,0
"2015-05-27",0.000199401796128873,-9.87264291519852e-05
"2015-05-28",-0.000223285487700342,0.000394944707003964
"2015-05-29",2.39287876717675e-05,0.000138176075921148
"2015-06-01",0.000503649054035327,-0.000197491852779952
"2015-06-02",-0.000398883127633032,-0.000395061729201562
"2015-06-03",9.97605752641739e-05,-9.88044656212717e-05
"2015-06-04",-9.97506240845158e-05,0.000395256916718267
"2015-06-05",0,-0.000197550374664623
"2015-06-08",-0.000798084597746107,0.000493973522400326
"2015-06-09",0.000996405751629847,-0.000197491852779952
"2015-06-10",1.99481651663724e-06,9.87654317581299e-05
"2015-06-11",-0.000199481347597286,0
"2015-06-12",0.000199521148344761,-0.000395022713528315
"2015-06-15",-0.000598444044974777,0.000296384113874115
"2015-06-16",0.000598802394702114,0.00019753086351626
"2015-06-17",-0.000199481347597286,-0.00039498370664437
"2015-06-18",-0.00059856344721787,0
"2015-06-19",0.000798562585937024,0.000395139780419385
"2015-06-22",0.000199481349780317,-0.00039498370664437
"2015-06-23",-0.00059832469254073,-9.87849448337297e-05
"2015-06-24",-0.000399121929956281,0
"2015-06-25",-0.000199640648122834,-9.87947053479044e-05
"2015-06-26",0.000998402555793199,0.000197608932327231
"2015-06-29",-0.000173548771514542,9.87849448337297e-05
"2015-06-30",0.00037309487145154,-0.00018767285625676
'
returns <- as.xts(read.zoo(file=textConnection(returnsText), index.column="date", read=read.csv))
portfolioSpec <- portfolio.spec(assets=colnames(returns))
portfolioSpec <- add.constraint(portfolio=portfolioSpec, type="full_investment", enabled=TRUE)
portfolioSpec <- add.constraint(portfolio=portfolioSpec, type="box", min=0.0, max=1.0, enabled=TRUE)
portfolioSpec <- add.objective(portfolio=portfolioSpec, type="return", name="mean", enabled=TRUE, multiplier=-1)
portfolioSpec <- add.objective(portfolio=portfolioSpec, type="risk", name="StdDev", enabled=TRUE, multiplier=1, risk_aversion=1e-9)
maxSROpt <- optimize.portfolio(R=returns,
portfolio=portfolioSpec,
optimize_method="ROI",
maxSR=TRUE,
trace=TRUE)
当我运行此代码时,R 控制台中的输出是:
> library(PortfolioAnalytics)
Loading required package: zoo
Attaching package: ‘zoo’
The following objects are masked from ‘package:base’:
as.Date, as.Date.numeric
Loading required package: xts
Loading required package: foreach
Loading required package: PerformanceAnalytics
Attaching package: ‘PerformanceAnalytics’
The following object is masked from ‘package:graphics’:
legend
>
> returnsText <- '
+ "date","NEAR","MINT"
+ "2014-07-01",0.000161110006774212,-9.86193284648884e-05
+ "2014-07-02",-0.000218949044995531,0.000197258111483523
+ "2014-07-03",-0.000378267533203247,9.86096047428386e-05
+ "2014-07-07",0,-9.85998818474609e-05
...
+ "2015-06-25",-0.000199640648122834,-9.87947053479044e-05
+ "2015-06-26",0.000998402555793199,0.000197608932327231
+ "2015-06-29",-0.000173548771514542,9.87849448337297e-05
+ "2015-06-30",0.00037309487145154,-0.00018767285625676
+ '
>
> returns <- as.xts(read.zoo(file=textConnection(returnsText), index.column="date", read=read.csv))
>
> portfolioSpec <- portfolio.spec(assets=colnames(returns))
> portfolioSpec <- add.constraint(portfolio=portfolioSpec, type="full_investment", enabled=TRUE)
> portfolioSpec <- add.constraint(portfolio=portfolioSpec, type="box", min=0.0, max=1.0, enabled=TRUE)
> portfolioSpec <- add.objective(portfolio=portfolioSpec, type="return", name="mean", enabled=TRUE, multiplier=-1)
> portfolioSpec <- add.objective(portfolio=portfolioSpec, type="risk", name="StdDev", enabled=TRUE, multiplier=1, risk_aversion=1e-9)
>
> maxSROpt <- optimize.portfolio(R=returns,
+ portfolio=portfolioSpec,
+ optimize_method="ROI",
+ maxSR=TRUE,
+ trace=TRUE)
Registered S3 method overwritten by 'ROI':
method from
print.constraint PortfolioAnalytics
Error in max_sr_opt(R = R, constraints = constraints, moments = moments, :
Objective function failed with message
Error in optimize(f = sharpe_obj_fun, R = R, constraints = constraints, :
'xmin' not less than 'xmax'
使用纯 ROI,如下面的 R 控制台输出所示,我使用相同的返回对象:
> library("ROI")
> library("ROI.plugin.glpk")
>
> sharpe_objective <- function(r_mat, rf = 0){
+ N <- NCOL(r_mat)
+ S <- NROW(r_mat)
+ mu <- colMeans(r_mat)
+ Amat <- rbind(c(mu - rf, 0),
+ c(rep(0, N), 1),
+ c(rep(1, N), -1))
+ var.names <- c(paste0("y_sharpe_aux", seq_len(N)), "kappa_sharpe")
+
+ constraint <- L_constraint(L = Amat, dir = c("==", ">", "=="),
+ rhs = c(1, 0, 0), names = var.names)
+
+ mat <- matrix(0, ncol = N + 1, nrow = N + 1)
+ mat[1:N, 1:N] <- 2 * cov(r_mat)
+ mat[N + 1, N + 1] <- 1e-04
+
+ objective <- Q_objective(Q = - mat, L = c(rep(0, N), 0))
+
+ list(objective = objective, constraint = constraint)
+ }
>
> N <- NCOL(returns)
> tmp <-sharpe_objective(returns)
>
> lp <- OP(maximum=TRUE)
> objective(lp) <- tmp$objective
>
> mat <- cbind(diag(N), 1)
> shortsell_constraint <- L_constraint(mat, dir = rep(">=", N), rhs = rep(0, N))
>
> constraints(lp) <- rbind(tmp$constraint, shortsell_constraint)
>
> bounds(lp) <- V_bound(li = seq_len(N +1), lb = c(rep(-Inf, N), 0))
>
> (sol <- ROI_solve(lp, solver = "quadprog"))
Optimal solution found.
The objective value is: -9.586494e+04
>
> sol_sharpe <- solution(sol)
> x_opt <- round(sol_sharpe[1:2]/sol_sharpe["kappa_sharpe"], 3)
> names(x_opt) <- colnames(returns)
> x_opt
NEAR MINT
0.585 0.415
我在 Github PortfolioAnalyrics 包上打开了一个问题https://github.com/braverock/PortfolioAnalytics/issues/26并询问了https://stat.ethz.ch/pipermail/r-sig-finance/2020q3/014982.html在 R-SIG-Finance 中,但没有收到任何答复。任何解决方案或解决方法的想法都值得赞赏。
编辑:我想出了问题所在。在功能max_sr_opt
(https://github.com/braverock/PortfolioAnalytics/blob/master/R/optFUN.R)
# This function uses optimize() to find the target return value that
# results in the maximum sharpe ratio (mean / sd).
# returns the target return value
max_sr_opt <- function(R, constraints, moments, lambda_hhi, conc_groups, solver, control){
# create a copy of the moments that can be modified
tmp_moments <- moments
# Find the maximum return
max_ret <- maxret_opt(R=R, moments=moments, constraints=constraints,
target=NA, solver="glpk", control=control)
max_mean <- as.numeric(-max_ret$out)
# Find the minimum return
tmp_moments$mean <- -1 * moments$mean
min_ret <- maxret_opt(R=R, moments=tmp_moments, constraints=constraints,
target=NA, solver="glpk", control=control)
min_mean <- as.numeric(min_ret$out)
# use optimize() to find the target return value that maximizes sharpe ratio
opt <- try(optimize(f=sharpe_obj_fun, R=R, constraints=constraints,
solver=solver, lambda_hhi=lambda_hhi,
conc_groups=conc_groups, moments=moments, control=control,
lower=min_mean, upper=max_mean,
maximum=TRUE, tol=.Machine$double.eps),
silent=TRUE)
if(inherits(opt, "try-error")){
stop(paste("Objective function failed with message\n", opt))
return(NULL)
}
return(opt$maximum)
}
实际发生的情况是,对于这个特定示例max_mean
,min_mean
它们是相等/相同的,当然调用optimize()
失败。
我在调用之前尝试了增加max_mean
和减少min_mean
一点,optimize()
但是解决方案非常不稳定,并且取决于我增加和减少了多少,max_mean
并且min_mean
:
if (max_mean == min_mean) {
epsilon <- 0.01
max_mean <- max_mean*(1.0 + epsilon)
min_mean <- min_mean*(1.0 - epsilon)
}
这是不同值的结果epsilon
:
# epsilon <- 0.01
#> extractWeights(maxSROpt)
#NEAR MINT
#NA NA
# epsilon <- 0.001
#> extractWeights(maxSROpt)
#NEAR MINT
#0.4452578 0.5547422
# epsilon <- 0.0001
#> extractWeights(maxSROpt)
#NEAR MINT
#0.8923873 0.1076127
# epsilon <- 0.00001
#> extractWeights(maxSROpt)
#NEAR MINT
#0.98926189 0.01073811
# epsilon <- 0.000001
#> extractWeights(maxSROpt)
#NEAR MINT
#0.998951981 0.001048019
# epsilon <- 0.0000001
#> extractWeights(maxSROpt)
#NEAR MINT
#9.999111e-01 8.890042e-05
有什么想法或更好的方法来解决这个问题或找到解决方法吗?