## Proc Optmodel to minimize Mean Absolute Error

Using the below code I can calculate absolute error for each month. However, something isn't working when I try to minimize the sum of a created variable, "sae". Effectively, I'm trying to minimize "mae" (which is the average of the "sae" values, excluding the first observation) to find the optimal value for "a".

proc optmodel;
set <str,num> MASTER_DATES;
set MASTER = setof {<i,t> in MASTER_DATES}i;
set NROW = setof {<i,t> in MASTER_DATES}t;

num performance {MASTER_DATES};
num month {MASTER_DATES};

read data example_data1 into MASTER_DATES=[master_nbr nrow] performance month;

num forecast{<i,t> in MASTER_DATES};
num sae{<i,t> in MASTER_DATES};

var a >=0 <=1;

for {<i,t> in MASTER_DATES} do;
if <i,t-1> in MASTER_DATES then
do;
forecast[i,t] = (performance[i,t-1]*a)+((1-a)*forecast[i,t-1]);
sae[i,t] = abs(performance[i,t] - forecast[i,t]);
end;
else
do;
forecast[i,t] = performance[i,t];
sae[i,t] = 0;
end;
end;

min mae = (sum {<i,t> in MASTER_DATES} sae[i,t]) / 10;

solve;
print a;

create data optmodel_outdata from [master_nbr nrow] performance month forecast sae;
quit;

Thanks,

Dan

1 ACCEPTED SOLUTION

Accepted Solutions

## Re: Proc Optmodel to minimize Mean Absolute Error

You have declared forecast and sae as numeric constants and explicitly set their values once, so the objective function is a constant that does not depend on the variable a.  Instead, you can use implicit variables to get the behavior you want:

``````impvar forecast{<i,t> in MASTER_DATES} =
if <i,t-1> in MASTER_DATES then performance[i,t-1]*a+(1-a)*forecast[i,t-1]
else performance[i,t];

impvar sae{<i,t> in MASTER_DATES} = abs(performance[i,t] - forecast[i,t]);
``````
2 REPLIES 2

## Re: Proc Optmodel to minimize Mean Absolute Error

You have declared forecast and sae as numeric constants and explicitly set their values once, so the objective function is a constant that does not depend on the variable a.  Instead, you can use implicit variables to get the behavior you want:

``````impvar forecast{<i,t> in MASTER_DATES} =
if <i,t-1> in MASTER_DATES then performance[i,t-1]*a+(1-a)*forecast[i,t-1]
else performance[i,t];

impvar sae{<i,t> in MASTER_DATES} = abs(performance[i,t] - forecast[i,t]);
``````

## Re: Proc Optmodel to minimize Mean Absolute Error

Thank you Rob for the explanation. This works perfect!
Discussion stats
• 2 replies
• 424 views
• 0 likes
• 2 in conversation