Hi,
suppose that I have the following stock return data for 3 stocks:
data returns; input abc def zzz; datalines; 10 2 5 3 -5 7 -1 -7 6 1 3 -2 ; run;
I used the following code to calculate the efficient frontier, i.e, for each level of risk (variance) to find the optimal return and the weights of the stocks that correspond to each risk-return combination:
Proc IML; use returns; read all var _num_ into RMAT[colname=varNames]; Print RMAT; f = 1; g = 2; n = 4; /* number of rows*/ k = 3; /* number of columns*/ RAVG = j(f,k,0); RAVG=RMAT[:,]; /* to calculate column average returns, a 1 x k matrix*/ Print RAVG; xbar = REPEAT(RAVG,n,f); /* to create an n x k matrix*/ Print xbar; RMX = j(n,k,0); RMX = RMAT-xbar; /* to calculate RMX*/ Print RMX; V = j(k,k,0); V=RMX`*RMX /(n-1); /* to calculate covariance matrix*/ Print V; VINV =j(k,k,0); VINV = INV(V); /* to take inverse of V*/ Print VINV; P = 0.05; M={1,.05}; /* to define M and required return (must change)*/ Print M; DIG = j(1,k,1); R = j(g,k,0); R =DIG//RAVG; /* to concatenate (combine) DIG and RAVG to a 2 x k matrix*/ Print R; H =j(2,2,0); H = R*inv(V)*R`; /* to calculate H*/ Print H; HINV = j(2,2,0); HINV = INV(H); /* to take inverse of H*/ PRINT HINV; Do until (P<=0); W = j(1,k,0); W = VINV*R`*HINV*M; /* to calculate portfolio weights*/ Print W; PVAR = j(1,1,0); PVAR = W`*V*W; /* to calculate portfolio variance given a return and weights*/ Print PVAR; P = P-.005; M = M - {0, .005}; end; quit;
Basically the core of the code is done, what I need are 2 special additions:
1) I would like to create a table which will contain the information in the following way:
Weight abc
Abc1
Abc2
Etc.
Weight def
Def1
Def2
Weight zzz
Zzz1
Zzz2
return
Ret1
Ret2
variance
Var1
Var2
2) I would like to add the constraint of no short selling, i.e, the weights should all be greater or equal to 0. I just don't know how to add constraints into Proc IML
Thank you very much!!
... View more