Hi guys, This link below is another post that resolved my question refers to the PROC IML coding, at a very beginning stage. https://communities.sas.com/t5/Mathematical-Optimization/from-Proc-IML-to-Proc-OPTMODEL/td-p/20592 However, as following the paper (Brandt et al., 2009) provided in the link, I would like to add a weight constraint in the codes by having a little modification on the codes provided in the previous post. The aim for this step is to restrict each weight to positive given the sum of the weight to 1. The modified part in shown here below. However, the SAS seems does not work to add the constraint on this way. w=mktb+(1/countb)*charb*x`;
w=max(0,w)/sum(max(0,w));
portret=((1+w`*retb)**onemgam)/onemgam; Could you please help me out for the problem? Thank you very much! The full modified codes are below: proc iml;
USE ppp.wgtfracsas;
read all var {date} into by_vars;
unique_rows=uniqueby(by_vars,1,1:nrow(by_vars));
read all var {logsize logbm mom} into char;
read all var {wgtfrac} into mkt;
read all var {ret} into ret;
USE ppp.equalsas;
read all var {stocks} into stocks;
onemgam=1-5;
div = J(nrow(unique_rows),1,1/nrow(unique_rows));
start opt(x) global(mkt, char, ret, stocks, onemgam, unique_rows, by_vars, div);
do i=1 to nrow(unique_rows);
if i=nrow(unique_rows) then index=unique_rows[i]:nrow(by_vars);
else index=unique_rows[i]:unique_rows[i+1]-1;
mktb=mkt[index,];
retb=ret[index,];
charb=char[index,];
stocksb=stocks[i,];
w=mktb+(1/countb)*charb*x`;
w=max(0,w)/sum(max(0,w));
portret=((1+w`*retb)**onemgam)/onemgam;
port=port//portret;
end;
f=port`*div;
return(f);
finish opt;
*initial values;
x0 = j(3,1,1/3);
optn={1 3};
ter={100000};
*run Sharpe Ratio;
call NLPDD(rcsr,xtheta,"opt",x0,optn,) tc=ter ; *This is the non-linear function; *rc is the vector of weights;
thetanames={'sizetheta' 'bmtheta' 'momtheta'};
create theta1 from xtheta[colname=thetanames];
append from xtheta;
quit;
run; The original codes in the previous post that i followed is below. The selected part is the only difference between the original codes and the codes would like to add the constraints. The file " wgtfracsas" and "equalsas" could be downloaded from the previous post. portret=((1+(mktb+(1/stocksb)*charb*x`)`*retb)**onemgam)/onemgam; proc iml;
USE ppp.wgtfracsas;
read all var {date} into by_vars;
unique_rows=uniqueby(by_vars,1,1:nrow(by_vars));
read all var {logsize logbm mom} into char;
read all var {wgtfrac} into mkt;
read all var {ret} into ret;
USE ppp.equalsas;
read all var {stocks} into stocks;
onemgam=1-5;
div = J(nrow(unique_rows),1,1/nrow(unique_rows));
start opt(x) global(mkt, char, ret, stocks, onemgam, unique_rows, by_vars, div);
do i=1 to nrow(unique_rows);
if i=nrow(unique_rows) then index=unique_rows[i]:nrow(by_vars);
else index=unique_rows[i]:unique_rows[i+1]-1;
mktb=mkt[index,];
retb=ret[index,];
charb=char[index,];
stocksb=stocks[i,];
portret=((1+(mktb+(1/stocksb)*charb*x`)`*retb)**onemgam)/onemgam;
port=port//portret;
end;
f=port`*div;
return(f);
finish opt;
*initial values;
x0 = j(3,1,1/3);
optn={1 3};
ter={100000};
*run Sharpe Ratio;
call NLPDD(rcsr,xtheta,"opt",x0,optn,) tc=ter ; *This is the non-linear function; *rc is the vector of weights;
thetanames={'sizetheta' 'bmtheta' 'momtheta'};
create theta1 from xtheta[colname=thetanames];
append from xtheta;
quit;
run;
... View more