So in Julia I have those 2 functions: portfoliomean(weights; rmean = r_mean) =…
- User
So in Julia I have those 2 functions: portfolio_mean(weights; r_mean = r_mean) = sum(weights .* r_mean') portfolio_var(weights; covar = covar) = weights'covarweights and I want to optimize their maximum from equation: portfolio_mean(x)/portfolio_var(x) given the following constraints: @constraint(portfolio, sum(x) == 1) @constraint(portfolio, portfolio_mean(x) >= r_min) @constraint(portfolio, portfolio_var(x) <= var_max); and variable: @variable(portfolio, x[1:size(rates_of_return)[2]] >= 0)
- Assistant
You can use the JuMP package in Julia to solve this optimization problem. First, install the package by running
import Pkg; Pkg.add("JuMP"). You will also need to choose a solver for the optimization. For example, the Ipopt package (to install it, runPkg.add("Ipopt")).Here's the complete code to define and solve the optimization problem:
using JuMP using Ipopt # Sample data (replace with your own values) r_mean = [0.1, 0.2, 0.15] covar = [0.005, -0.010, 0.004, -0.010, 0.040, -0.002, 0.004, -0.002, 0.023] covar = reshape(covar, 3, 3) r_min = 0.12 var_max = 0.015 # Your functions portfolio_mean(weights; r_mean = r_mean) = sum(weights .* r_mean') portfolio_var(weights; covar = covar) = weights' * covar * weights # Optimization model model = Model(Ipopt.Optimizer) @variable(model, x[1:size(r_mean)[1]] >= 0) @constraint(model, sum(x) == 1) @constraint(model, portfolio_mean(x) >= r_min) @constraint(model, portfolio_var(x) <= var_max) @objective(model, Max, portfolio_mean(x) / portfolio_var(x)) # Solve the optimization problem optimize!(model) # Output the solution println("Optimal weights: ", value.(x))