最大化Sharpe比率,但要注意Julia中的矛盾 [英] Maximise Sharpe ratio subject to contraints in Julia
问题描述
我要最大化q [1](Sharpe Ratio)的值,但要遵守julia中的以下约束条件.
I want to maximize the value of q[1](Sharpe Ratio), subject to following constraints in julia.
-
W的
- 值元素为正. (W [i]> 0)
-
W的总和为1.(sum(W [1:5])== 1)
- Value elements of W is positive. ( W[i] >0 )
Sum of values of W is 1. ( sum(W[1:5]) == 1 )
function getSharpeRatio(W,ex_mu,S)
q = ( W'*ex_mu ) / sqrt((W'*S*W))
return q[1]
end
供参考:: W是(5X1)向量,ex_mu是(5x1)向量,S是(5x5)矩阵. 我发现有两个julia库可以使用JuMP和Optim.jl,但是无法按照库的要求转换函数getSharpeRatio.
For Reference :: W is (5X1) vector,ex_mu is (5x1) vector and S is (5x5) matrix. I found out two julia libraries to use JuMP and Optim.jl , but not able to translate the function getSharpeRatio as required by the libraries.
更新:到目前为止,我已经完成了,但是似乎尚未在JuMP库中实现转置 使用JuMP
Update : I have done so far but seems like transpose is not implemented yet in JuMP library using JuMP
function getSharpeRatio(W,ex_mu,S)
return dot(W', ex_mu) / sqrt(dot(W',S*W))
end
items = [1;2;3;4;5]
m = Model()
@variable(m, 0 <= W[items] <= 1)
@constraint(m, sum{ W[item] , item in items} == 1)
@objective(m, Max, getSharpeRatio(W,ex_mu,S))
solve(m)
println(getvalue(W))
关于如何解决此问题的任何建议.
Any suggestions how to go about this.
推荐答案
我相信您的函数需要位于一个数组中.制作一个长度为25 + 5 + 5的向量,并使用getSharpeRatio(v)
开始W,ex_mu,S=translate(v)
函数,该函数使用视图将临时数组取出.
I believe your function needs to be on one array. Make a 25+5+5 length vector, and make getSharpeRatio(v)
start W,ex_mu,S=translate(v)
function which gets your temporary arrays out using views.
translate(v) = (view(v,1:5),view(v,6:10),reshape(view(11:35),5,5))
这不会分配,但这意味着您可以使用新功能
That won't allocate, but it means your new function can be
function getSharpeRatio(v)
W,ex_mu,S=translate(v)
dot(W,ex_mu)/ sqrt(dot(W,S*W))
end
与JuMP/Optim期望的匹配.从那里开始,您需要重新设置约束以匹配此处的索引...尽管我认为我已对此进行了设置(首先使用W进行设置),以便它可以正常工作.
which matches what JuMP/Optim expect. From there, you need to re-do your constraints to match the indices from here... though I think I set this up (with W first) so that way it'll work.
这篇关于最大化Sharpe比率,但要注意Julia中的矛盾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!