Dear all, I want to compute the skewness of portfolio. By reading loads of literature, I found that the formula of computing the skewness of portfolio is as following: skewness_p=ω' M3 (ω⊗ω) where M3=E[(r-μ) (r-μ)'⊗(r-μ)']={aijk} the ⊗ denote the kronecker product, M3 is the co-skewness matrix, and r and u are the return matrix and average return matrix, respectively. However, I don't know how to decompose the expectation of the co-skewness matrix M3, thus I found a alternative way to calculate the M3, that's: The co-skewness matrix of M3 of dimensions (N,N2) can be represented by N Aijk matrixes (N,N) such that: M3=[ A1jk A1jk ... ANjk ] where j,k=1,…,N as well as for an index i. The individual elements of the matrix can be obtained as: the capital K denote the element in the TxN return matrix, T is the number of observations and N is the number of assets. According to the formula, I write down the following code: proc iml; T=j(81,100,.); /* This is my return matrix, for example*/ W=j(1,100,.); /* This is my weights matrix, for example*/ call randgen(T,'normal'); call randgen(W,'normal'); n=ncol(T); r=nrow(T); M=j(n,n,.); do i=1 to n; S=j(n,n,.); do j=1 to n; do k=1 to n; u=0; do y=1 to r-1; u=u+((T[y,i]-T[:,i])*(T[y,j]-T[:,j])*(T[y,k]-T[:,k])); end; s[j,k]=u/(y-1); end; end; M=M||S; end; M3=M[,(n+1):(n*n+n)]; skew=W*M3*(W`@W`); print skew ; quit; My problem comes out...There are 4 loops in this piece of code, and the speed is extremely low! I made a few simulation to approximate the time elapse. If I have 20 assets(n=20) in my portfolio, it will cost me 8 seconds, if I have 100 assets in my portfolio, it will cost me around 4 minutes. If I have 200 asset in my portfolio, it costs me 21 minutes! For the number of assets equal to 300, I run approximately 60 minutes and it still hasn't finished, so I have to abort it...The problem becomes unmanageable, I have 185,000 portfolios need to be evaluated, and around 17000 of them have more than 200 assets(n>=200). Within these 17000 portfolio, there are even more than 1200 portfolios with more than 1000 assets! This problem really makes me desperate! Does anyone know how can I improve this code? I reaaaaally reaaaally appreciate!!!
... View more