问题
I want to maximize the value of q[1](Sharpe Ratio), subject to following constraints in julia.
- 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
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.
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.
回答1:
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
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.
来源:https://stackoverflow.com/questions/42159761/maximise-sharpe-ratio-subject-to-contraints-in-julia