martinbiel / StochasticPrograms.jl

Julia package for formulating and analyzing stochastic recourse models.
MIT License
75 stars 25 forks source link

[Question] Portfolio Optimization with CVaR #47

Closed amirmm11 closed 1 year ago

amirmm11 commented 1 year ago

I am trying to see how I can model Portfolio Optimization with CVaR within the standard forms in StochasticPrograms.jl. I'd appreciate it if you could create a code snippet.

odow commented 1 year ago

I don't think @martinbiel has added risk. But you can model CVaR as a risk-neutral stochastic program using the primal formulation of CVaR.

I haven't tested, but something like this should work:

# CVaR_α[Z] = min ζ + 1 / α * E[(Z - ζ)₊]
# CVaR_α[Z] = min ζ + E[1 / α * (Z - ζ)₊]
# Make ζ a first-stage decision, then mimimize in expectation
@stochastic_model simple_model begin
    @stage 1 begin
        @decision(simple_model, x₁ >= 40)
        @decision(simple_model, x₂ >= 20)
        @decision(simple_model, ζ)  # VaR quantile
        @objective(simple_model, Min, 100*x₁ + 150*x₂ + ζ)  # Add ζ
        @constraint(simple_model, x₁ + x₂ <= 120)
    end
    @stage 2 begin
        @uncertain q₁ q₂ d₁ d₂
        @recourse(simple_model, 0 <= y₁ <= d₁)
        @recourse(simple_model, 0 <= y₂ <= d₂)
        @constraint(simple_model, 6*y₁ + 10*y₂ <= 60*x₁)
        @constraint(simple_model, 8*y₁ + 5*y₂ <= 80*x₂)
        # Model z₊ = (Z - ζ)₊
        @recourse(simple_model, z₊ >= 0)
        @constraint(simple_model, z₊ >= (-q₁*y₁ - q₂*y₂) - ζ)
        # The second stage objective: 1 / α * (Z - ζ)₊
        @objective(simple_model, Min, 1 / 0.5 * z₊)
    end
end
amirmm11 commented 1 year ago

Thank you @odow

amirmm11 commented 1 year ago

@odow, what about if CVaR should be less than a value and I don't want to minimize CVaR? ζ + E[1 / α * (Z - ζ)₊] <= CVaR_max

odow commented 1 year ago

Ah! Now this is a lot trickier.

You typically can't solve it using decomposition, so you've have to manually code the extensive two-stage formulation and add that constraint to the extensive form.