11-06-2012 07:44 AM
I think my problem is quite easy for someone who knows PROC IML, so I believe this is the right discussion forum. If someone could suggest a solution, I would be very appreciative. I am familiar with base SAS and its database programming aspects, but I am new to PROC IML and matrix/ vector multiplication.
My problem is this: Given the returns, variances, and weights of n individual assets, I want to combine these assets into a single weighted-average portfolio and then calculate the variance and return of the portfolio. The problem is I have thousands of portfolios to create and each portfolio has a different number of assets, many with only 1 or 2 assets, others with 10 assets. The portfolio variance calculation changes depending on n. Using normal base SAS and linear combinations makes the problem complicated, but using matrix/vector multiplication makes the solution to the problem very easy and flexible since:
E(Rp) = w' * R where E(Rp) = expected return on the portfolio; R is the column vector of asset returns; w is the col vector of weights and w' is the transpose of this vector
Var(Rp) = w' * ( V * V' ) * w where V(Rp) = the variance of the portfolio; V is col vector of asset variances and V' is the transpose.
(NOTE: I am assuming a correlation coeffecient of 1 between the returns on all asset)
My data to construct these vectors comes in the following form: I have three BY variables (dealnum, cusip6, date), I know the number of assets in each portfolio (wt_N), I am given the weight of each asset in the portfolio (wt -- the weights of the portfolio add up to 1), I have variance for the same asset (var10) and the return for this asset (ret10). So referring to the data below, Porfolio 1 is consists of Obs 1, 2; Portfolio 2 is obs 3 only; Portfolio 3 is of obs 4 to 17; Portfolio 4 is of obs 18, 19; Porfolio 5 is of obs 20; and Porfolio 6 is of obs 21, 22, etc (up to n=4000 portfolios). For each of the these portfolios the vectors w, R and V are different, but using matrix/vector multiplication should make this problem REALLY EASY!! :smileygrin:
Could someone show me how this could be done in PROC IML (or otherwise)? I attach a more complete sample of data in case you want it.
Many thanks in advance,
11-06-2012 11:10 AM
Here are a few tips.
1) Read about the UNIQUE-LOC technique, to get the main ideas of how to do something like this The UNIQUE-LOC trick: A real treat! - The DO Loop
2) Because you have three BY-group variables, I recommend implementing the efficiency scheme here: An efficient alternative to the UNIQUE-LOC technique - The DO Loop
3) If the data are too large to fit into memory, use a variation on this approach BY-group processing in SAS/IML - The DO Loop
4) Develop and debug on a small set of sample data. When it is working, scale up to the real data.
11-08-2012 04:27 AM
Thanks very much for your very helpful tips. They helped me focus my learning of PROC IML on my concrete problem, which has now been resolved thanks to the tips you gave. Reducing my three BY variables to just one by pre-sorting the data set before PROC IML helped resolve many problems.
Thanks again! I really appreciate your help.