# Help for minimize CVaR Problem

I want to model minimize CVaR for portfolio,I could not solve correctly I could not write correctly second constraint : s.t. sample_excess {i in 1…samples}: -w[i]*r[i] - nu <= excess[i];hereI need matrix multiplication.
My problem:

I tried as:
ampl = AMPL()
ampl.eval(
r"“”
set A ordered;
param samples > 0;
param mu{A};
param r{1…samples};#constraint
param lb default 0;
param ub default 1;
var w{A} >= lb <= ub;
var nu;
var excess{1…samples} >= 0;
# minimize CVaR
minimize obj: nu + (1.0 / ((1 - alpha) * samples)) * sum{i in 1…samples} excess[i];
s.t. portfolio_weights:
sum {i in A} w[i] = 1;
s.t. no_short {i in A}: w[i] >= lb;
s.t. sample_excess {i in 1…samples}: -w[i]*r[i] - nu <= excess[i];
s.t. greater {i in 1…samples}:
excess[i]>=0;
“”"
)
ampl.set[“A”] = tickers
ampl.param[“samples”] = len(weekly_returns)
ampl.param[‘r’]=weekly_returns.values
ampl.param[“mu”] = mean_return
ampl.option[“solver”] = SOLVER
ampl.param[“alpha”] = 0.95#
ampl.solve()

I tried different settings,but does not work I did not understand how to solve? param samples and se TIME are equal actually?
ampl = AMPL()
ampl.eval(
r"“”
set ASSETS;
set TIME;
param samples > 0; # number of samples
param alpha >= 0, <= 1; # value at risk
param weekly_returns{TIME, ASSETS};
var u{1…samples} >= 0; # Decision variables ut for each time period

``````var nu;
param w{TIME};  # Vector parameter w for each time period
# minimize CVaR
minimize obj: nu + (1.0 / ((1 - alpha) * samples)) * sum{i in 1..samples} u[i];
s.t. portfolio_weights:
sum{j in ASSETS}w[j] = 1;
s.t. no_short {j in ASSETS}: w[j] >= 0;
s.t. sample_excess  {i in 1..samples}{t in TIME}:sum{j in ASSETS} w[j]*weekly_returns[t,j] - nu <= u[i];
s.t. greater {TIME}:
u[i]>=0;
"""
``````

)
ampl.set[“ASSETS”] = list(weekly_returns.columns)
ampl.set[“TIME”] = len(weekly_returns)#number of samples
ampl.param[“samples”] =len(weekly_returns)

ampl.param[‘r’]=weekly_returns
ampl.param[“mu”] = mean_return
ampl.param[“alpha”] = 0.95#
ampl.option[“solver”] = SOLVER

ampl.solve()
weights_df = ampl.var[“w”].to_pandas()
weights_df

I still could not solve

Here is my work with syntax issue,I so close the solution I change the return indexing with numbers but it starts from 0,but samples starts from 1,maybe this is the issue ,but still syntax error:
TEZ_CVAR.zip (18.4 KB)

I solved the minimization issue as follows:

``````ampl = AMPL()
ampl.eval(
r"""
set A;
set TIME;
param samples > 0;             # number of samples
param weekly_returns{t in TIME,j in A};
param alpha >= 0, <= 1;        # value at risk
param mu{A};
param lb default 0;
param ub default 1;
var w{A} >= lb <= ub;
var nu;
var excess{t in TIME} >= 0;
# minimize CVaR
minimize obj: nu + (1.0 / ((1 - alpha) * samples)) * sum{t in TIME} excess[t];
s.t. portfolio_weights:
sum {j in A} w[j] = 1;
s.t. no_short {j in A}: w[j] >= lb;
s.t. sample_excess {t in TIME}: excess[t]>= lb;
# relation between excess and profit of each sample
s.t. sample_excess_ {t in TIME}: -sum{j in A}(w[j] * (weekly_returns[t, j])) - nu <= excess[t];
"""
)
ampl.set["A"] = list(weekly_returns.columns)
ampl.set["TIME"] = weekly_returns.index
ampl.param["samples"] =len(weekly_returns)
ampl.param["weekly_returns"] = weekly_returns
#ampl.param['r']=weekly_returns
ampl.param["mu"] = mean_return
ampl.param["alpha"] = 0.95#
ampl.option["solver"] = SOLVER
ampl.solve()
``````

Then I want solve maximization return than evaluate corresponding CVaR:

``````max_ret = AMPL()
max_ret.eval(
r"""
set A ordered;
set TIME;
param samples > 0;             # number of samples
param mu{A};
param alpha >= 0, <= 1;        # value at risk
param lb default 0;
param ub default 1;
var w{A} >= lb <= ub;
var excess{t in TIME} >= 0;
var nu;
maximize portfolio_return:
sum {i in A} mu[i] * w[i];
s.t. portfolio_weights:
sum {i in A} w[i] =  1;
s.t. no_short {i in A}: w[i] >= lb;
"""
)
max_ret.set["A"] = list(weekly_returns.columns)
max_ret.set["TIME"] = weekly_returns.index
max_ret.param["mu"] = mean_return
max_ret.param["alpha"] = 0.95#
max_ret.param["samples"] =len(weekly_returns)
max_ret.option["solver"] = SOLVER
max_ret.solve()
``````
``````G_max.append(max_ret.get_value("nu + (1.0 / ((1 - alpha) * samples)) * sum{t in TIME} excess[t]"))#
``````

Here G_max=CVaR,How Ampl calculated it as 0 value? I think G_max calculation is wrong,help please,I want to solve maximum return optimization then calculate corresponding CVaR value @fdabrandao @4er