One_period_mean variance

Hi,I am trying one period mean variance but I got solver error:
min_var = AMPL()
min_var.eval(
r"“”

 set A ordered;
 
 param risk_aversion;
 param S{A, A};
 param mu{A};
 param R_min;
 param R_max;
 param Var_min;
 param Var_max;
 param lb default 0;
 param ub default 1;
 var w{A} >= lb <= ub;
        
 maximize risk_adjusted_return:
     risk_aversion*((sum {i in A} mu[i] * w[i]- R_min)/(R_max-R_min))+
     (1-risk_aversion)*((sum {i in A, j in A} w[i] * S[i, j] * w[j]- Var_min)/(Var_max - Var_min));
 s.t. portfolio_weights:
 sum {i in A} w[i] = 1;  
 

"""

)

min_var.set[“A”] = stocks
min_var.param[“S”] = risk_models.risk_matrix(X, method=“sample_cov”)
min_var.option[“solver”] = “gurobi”
min_var.param[“mu”] = expected_returns.mean_historical_return(X)
min_var.param[“risk_aversion”] = lr_predictions
min_var.param[“R_min”]=R_min
min_var.param[“R_max”]=R_max
min_var.param[“Var_min”]=Var_min
min_var.param[“Var_max”]=Var_max
min_var.solve()
min_var.get_data(“w”).to_pandas().plot.barh()
then


risk_aversion=0.36340402

AMPL is reporting an error when the solve command is processing your objective function:

maximize risk_adjusted_return:
     risk_aversion*((sum {i in A} mu[i] * w[i]- R_min)/(R_max-R_min))+
     (1-risk_aversion)*((sum {i in A, j in A} w[i] * S[i, j] * w[j]- Var_min)/(Var_max - Var_min));

The message can't compute 0.363404/0 indicates that a division by zero is occurring when AMPL tries to evaluate the coefficients in your objective. Add the following statement just before min_var.solve() to check the values that AMPL has for the two expressions that the objective is dividing by:

min_var.eval( "display R_max-R_min, Var_max-Var_min;" )

What output do you see? If R_max-R_min or Var_max-Var_min is zero, then you can check your Python program to determine the cause. Otherwise, you can get more help by posting your entire program, including the data.


R_max=R_min,how can I solve the issue?

Are you expecting R_max to be greater than R_min? Both R_max and R_min are params of the model, so if you are expecting R_max > R_min but AMPL is seeing R_max = R_min, then you should look for an error in your Python program.

Or, do you actually want your model to work when R_max = R_min? Then you cannot use an objective function that divides by R_max – R_min. You will have to reformulate the objective so that it does not divide by R_max – R_min. I cannot tell you what changes to make, however, since I am not familiar with the application that you are modeling.

R_max and R_min corresponding return values of variance min and variance max models,they have to be in the final model,I will do scaling,It is better when R_max=R_min solver still works,but how,model gives me like that.

I suggest that you concentrate on answering the following question:

When R_max = R_min, what expression do you want to use for the objective function?

If you can answer this question, then it should be possible to tell you how to write an AMPL model that works when R_max = R_min and also when R_max > R_min.

When R_{max} = R_{min}, you cannot use this expression for your objective, because it becomes

{\rm max} \,\,\, \lambda \frac{R-R_{min}}{0} - (1-\lambda) \frac{G-G_{min}}{G_{max}-G_{min}}

For the case when R_{max} = R_{min}, you need a different expression that does not have 0 in the denominator of a fraction.