<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Computing the Co-Skewness matrix of portfolio in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Computing-the-Co-Skewness-matrix-of-portfolio/m-p/130116#M1012</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The inner loop is just the sum of the elementwise product of the centered ith, jth, and kth columns (not including the last row):&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;M3 = j(n, n*n);&lt;/P&gt;&lt;P&gt;begCol = 1;&lt;/P&gt;&lt;P&gt;endCol = n;&lt;/P&gt;&lt;P&gt;mean = mean(T);&lt;/P&gt;&lt;P&gt;idx = 1:(r-1);&lt;/P&gt;&lt;P&gt;do i=1 to n;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; Ti = T[idx, i] - mean&lt;I&gt;;&lt;/I&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; S=j(n,n,.);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; do j=1 to n;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Tj = T[idx, j] - mean&lt;J&gt;;&lt;/J&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; do k=1 to n;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Tk = T[idx, k] - mean&lt;K&gt;;&lt;/K&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; s[j,k]= sum( Ti#Tj#Tk ) / (r-1);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp; M3[,begCol:endCol]=S;&lt;/P&gt;&lt;P&gt;&amp;nbsp; begCol=endCol+1;&lt;/P&gt;&lt;P&gt;&amp;nbsp; endCol=endCol+n;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;skew=W*M3*(&lt;A href="mailto:W`@W"&gt;W`@W&lt;/A&gt;`);&lt;/P&gt;&lt;P&gt;print skew ;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 06 May 2013 13:33:21 GMT</pubDate>
    <dc:creator>Rick_SAS</dc:creator>
    <dc:date>2013-05-06T13:33:21Z</dc:date>
    <item>
      <title>Computing the Co-Skewness matrix of portfolio</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Computing-the-Co-Skewness-matrix-of-portfolio/m-p/130114#M1010</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Dear all,&lt;/P&gt;&lt;P&gt;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:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="text-align: left;"&gt;skewness_p=&lt;STRONG&gt;ω&lt;/STRONG&gt;' M3 (&lt;STRONG&gt;ω&lt;/STRONG&gt;&lt;SPAN style="font-family: 'Cambria Math','serif';"&gt;⊗&lt;/SPAN&gt;&lt;STRONG&gt;ω&lt;/STRONG&gt;)&lt;/P&gt;&lt;P style="text-align: left;"&gt;where &lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;M3=E[(&lt;STRONG&gt;r-μ&lt;/STRONG&gt;) &lt;STRONG&gt;(r-μ&lt;/STRONG&gt;)'&lt;/SPAN&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em; font-family: 'Cambria Math', serif;"&gt;⊗&lt;/SPAN&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;(&lt;STRONG&gt;r-μ&lt;/STRONG&gt;)']=&lt;/SPAN&gt;&lt;SPAN class="mo" id="MathJax-Span-811" style="line-height: 1.5em; padding: 0px 0px 0px 5.1px; color: #333333; font-size: 18px; font-family: MathJax_Main;"&gt;{&lt;/SPAN&gt;&lt;SPAN class="msubsup" id="MathJax-Span-812" style="line-height: 1.5em; color: #333333; font-family: Merriweather, serif; font-size: 18px;"&gt;&lt;SPAN class="mi" id="MathJax-Span-813" style="font-family: MathJax_Math; font-style: italic;"&gt;a&lt;SPAN class="texatom" id="MathJax-Span-814"&gt;&lt;SPAN class="mrow" id="MathJax-Span-815"&gt;&lt;SPAN class="mi" id="MathJax-Span-816" style="font-size: 13px;"&gt;i&lt;SPAN class="mi" id="MathJax-Span-817"&gt;j&lt;/SPAN&gt;&lt;SPAN class="mi" id="MathJax-Span-818"&gt;k&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN class="mo" id="MathJax-Span-819" style="line-height: 1.5em; color: #333333; font-size: 18px; font-family: MathJax_Main;"&gt;}&lt;/SPAN&gt;&lt;SPAN class="mtext" id="MathJax-Span-820" style="line-height: 1.5em; color: #333333; font-size: 18px; font-family: MathJax_Main;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P style="text-align: left;"&gt;&lt;/P&gt;&lt;P style="text-align: left;"&gt;the &lt;SPAN style="font-family: 'Cambria Math', serif;"&gt;⊗&lt;/SPAN&gt; denote the kronecker product, M3 is the co-skewness matrix, and &lt;STRONG&gt;r&lt;/STRONG&gt; and &lt;STRONG&gt;u&lt;/STRONG&gt; are the return matrix and average return matrix, respectively.&lt;/P&gt;&lt;P style="text-align: left;"&gt;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:&lt;/P&gt;&lt;P style="text-align: left;"&gt;&lt;/P&gt;&lt;P style="margin-bottom: 0.0001pt;"&gt;&lt;SPAN style="font-size: 10.0pt; color: #333333;"&gt;The co-skewness matrix of M3 of dimensions (N,N2) can be represented by N Aijk matrixes (N,N) such that:&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN style="text-align: center; font-size: 10pt; line-height: 1.5em; color: #333333;"&gt;M&lt;/SPAN&gt;&lt;SPAN style="text-align: center; font-size: 10pt; line-height: 1.5em; color: #333333;"&gt;3=[ A1jk A1jk ... ANjk ]&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10.0pt; color: #333333;"&gt;where j,k=1,…,N as well as for an index i. The individual elements of the matrix can be obtained as:&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="text-align: left;"&gt;&lt;/P&gt;&lt;P style="text-align: left;"&gt;&lt;IMG alt="Capture.JPG" class="jive-image-thumbnail jive-image" height="67" src="https://communities.sas.com/legacyfs/online/3525_Capture.JPG" style="width: 403.2641509433962px; height: 67px;" width="403" /&gt;&lt;/P&gt;&lt;P style="text-align: left;"&gt;the capital K denote the element in the TxN return matrix, T is the number of observations and N is the number of assets. &lt;/P&gt;&lt;P style="text-align: left;"&gt;According to the formula, I write down the following code:&lt;/P&gt;&lt;P style="text-align: left;"&gt;&lt;/P&gt;&lt;P&gt;proc iml;&lt;/P&gt;&lt;P&gt;T=j(81,100,.); &lt;SPAN style="color: #ff9900;"&gt;/* This is my return matrix, for example*/&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;W=j(1,100,.); &lt;SPAN style="color: #ff9900;"&gt;/* This is my weights matrix, for example*/&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;call randgen(T,'normal');&lt;/P&gt;&lt;P&gt;call randgen(W,'normal');&lt;/P&gt;&lt;P&gt;n=ncol(T);&lt;/P&gt;&lt;P&gt;r=nrow(T);&lt;/P&gt;&lt;P&gt;M=j(n,n,.);&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #ff0000;"&gt;do i=1 to n;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; S=j(n,n,.);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #ff0000;"&gt;&amp;nbsp; do j=1 to n;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #ff0000;"&gt; do k=1 to n;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; u=0;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #ff0000;"&gt;do y=1 to r-1;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; u=u+((T[y,i]-T[:,i])*(T[y,j]-T[:,j])*(T[y,k]-T[:,k]));&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; s[j,k]=u/(y-1);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp; M=M||S;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;M3=M[,(n+1):(n*n+n)];&lt;/P&gt;&lt;P&gt;skew=W*M3*(W`@W`);&lt;/P&gt;&lt;P&gt;print skew ;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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&amp;nbsp; be evaluated, and around 17000 of them have more than 200 assets(n&amp;gt;=200). Within these 17000 portfolio, there are even more than 1200 portfolios with more than 1000 assets!&amp;nbsp; This problem really makes me desperate! Does anyone know how can I improve this code? I reaaaaally reaaaally appreciate!!!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 05 May 2013 21:40:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Computing-the-Co-Skewness-matrix-of-portfolio/m-p/130114#M1010</guid>
      <dc:creator>MichaelGong</dc:creator>
      <dc:date>2013-05-05T21:40:43Z</dc:date>
    </item>
    <item>
      <title>Re: Computing the Co-Skewness matrix of portfolio</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Computing-the-Co-Skewness-matrix-of-portfolio/m-p/130115#M1011</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I see that you are hedging your bets by cross-posting this question to multiple forums: &lt;A href="http://www.mathworks.com/matlabcentral/answers/74743-computing-the-co-skewness-matrix-of-portfolio" title="http://www.mathworks.com/matlabcentral/answers/74743-computing-the-co-skewness-matrix-of-portfolio"&gt;Computing the Co-Skewness matrix of portfolio - MATLAB Answers - MATLAB Central&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm pretty sure that the four loops are vectorizable, but that is not the only performance issue here. You are also concatenating inside a loop, which is inefficient.See &lt;A href="http://blogs.sas.com/content/iml/2011/06/20/pre-allocate-arrays-to-improve-efficiency/" title="http://blogs.sas.com/content/iml/2011/06/20/pre-allocate-arrays-to-improve-efficiency/"&gt; Pre-allocate arrays to improve efficiency - The DO Loop&lt;/A&gt;.&amp;nbsp; So while you contemplate how to vectorize the summation computations, start by allocating the M3 matrix outside of the loop and then filling each column within the loop:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;M3 = j(n, n*n);&lt;BR /&gt;begCol = 1;&lt;BR /&gt;endCol = n;&lt;BR /&gt;do i=1 to n;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; S=j(n,n,.);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; do j=1 to n;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; do k=1 to n;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; u=0;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; do y=1 to r-1;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; u=u+((T[y,i]-T[:,i])*(T[y,j]-T[:,j])*(T[y,k]-T[:,k]));&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; s[j,k]=u/(y-1);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;BR /&gt;&amp;nbsp; M3[,begCol:endCol]=S;&lt;BR /&gt;&amp;nbsp; begCol=endCol+1;&lt;BR /&gt;&amp;nbsp; endCol=endCol+n;&lt;BR /&gt;end;&lt;BR /&gt;skew=W*M3*(&lt;A _jive_internal="true" href="/mailto:W`@W/"&gt;W`@W&lt;/A&gt;`);&lt;BR /&gt;print skew ;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 06 May 2013 13:27:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Computing-the-Co-Skewness-matrix-of-portfolio/m-p/130115#M1011</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2013-05-06T13:27:41Z</dc:date>
    </item>
    <item>
      <title>Re: Computing the Co-Skewness matrix of portfolio</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Computing-the-Co-Skewness-matrix-of-portfolio/m-p/130116#M1012</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The inner loop is just the sum of the elementwise product of the centered ith, jth, and kth columns (not including the last row):&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;M3 = j(n, n*n);&lt;/P&gt;&lt;P&gt;begCol = 1;&lt;/P&gt;&lt;P&gt;endCol = n;&lt;/P&gt;&lt;P&gt;mean = mean(T);&lt;/P&gt;&lt;P&gt;idx = 1:(r-1);&lt;/P&gt;&lt;P&gt;do i=1 to n;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; Ti = T[idx, i] - mean&lt;I&gt;;&lt;/I&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; S=j(n,n,.);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; do j=1 to n;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Tj = T[idx, j] - mean&lt;J&gt;;&lt;/J&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; do k=1 to n;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Tk = T[idx, k] - mean&lt;K&gt;;&lt;/K&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; s[j,k]= sum( Ti#Tj#Tk ) / (r-1);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp; M3[,begCol:endCol]=S;&lt;/P&gt;&lt;P&gt;&amp;nbsp; begCol=endCol+1;&lt;/P&gt;&lt;P&gt;&amp;nbsp; endCol=endCol+n;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;skew=W*M3*(&lt;A href="mailto:W`@W"&gt;W`@W&lt;/A&gt;`);&lt;/P&gt;&lt;P&gt;print skew ;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 06 May 2013 13:33:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Computing-the-Co-Skewness-matrix-of-portfolio/m-p/130116#M1012</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2013-05-06T13:33:21Z</dc:date>
    </item>
    <item>
      <title>Re: Computing the Co-Skewness matrix of portfolio</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Computing-the-Co-Skewness-matrix-of-portfolio/m-p/130117#M1013</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Reaaaally thank you dear Rick Wicklin!!!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Vectorization is really difficult. I read tons of papers and literatures, and I still haven't found a clear decomposition of the expectation of M3. I also searched many researcher's method to compute the M3, all of them do it element by element... The key is that they only evaluate a portfolio of 100 to 300 assets. My situation is that I have 17000 portfolios need to be evaluated, within which around 1000 portfolios have more 1000 assets..the problem really become unmanageable.. Really thank you! Now I write down the following code, and it shows that for a portfolio with 300 assets (n=300), the calculation time is 30 seconds, which is a tremendous improvement!&amp;nbsp; and I also find that actually many elements in the M3 matrix are same, so I add a IF-THEN clause to avoid redundant calculation; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc iml;&lt;/P&gt;&lt;P&gt;T=j(81,300,.);&lt;/P&gt;&lt;P&gt;W=j(1,300,1/300);&lt;/P&gt;&lt;P&gt;call randgen(T,'normal');&lt;/P&gt;&lt;P&gt;n=ncol(T);&lt;/P&gt;&lt;P&gt;r=nrow(T);&lt;/P&gt;&lt;P&gt;M3 = j(n, n*n);&lt;/P&gt;&lt;P&gt;begCol = 1;&lt;/P&gt;&lt;P&gt;endCol = n;&lt;/P&gt;&lt;P&gt;mean = mean(T);&lt;/P&gt;&lt;P&gt;idx = 1:(r-1);&lt;/P&gt;&lt;P&gt;do i=1 to n;&lt;/P&gt;&lt;P&gt;&amp;nbsp; Ti = T[idx,i] - mean&lt;I&gt;;&lt;/I&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; S=j(n,n,.);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; do j=1 to n;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Tj = T[idx,j] - mean&lt;J&gt;;&lt;/J&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; do k=1 to n;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if j&amp;lt;=k then do;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Tk = T[idx,k] - mean&lt;K&gt;;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/K&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; s[j,k]= sum( Ti#Tj#Tk )/(r-1);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else do;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; s[j,k]=s[k,j];&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp; M3[,begCol:endCol]=S;&lt;/P&gt;&lt;P&gt;&amp;nbsp; begCol=endCol+1;&lt;/P&gt;&lt;P&gt;&amp;nbsp; endCol=endCol+n;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;skew=W*M3*(W`@W`);&lt;/P&gt;&lt;P&gt;print skew;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I will keep searching the method to vectorize. But if unfortunately I can't find it out and I have to use the code above, is there still any space for improving the efficiency ?&amp;nbsp; Last but not least, Reallly thank you!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 06 May 2013 14:19:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Computing-the-Co-Skewness-matrix-of-portfolio/m-p/130117#M1013</guid>
      <dc:creator>MichaelGong</dc:creator>
      <dc:date>2013-05-06T14:19:27Z</dc:date>
    </item>
    <item>
      <title>Re: Computing the Co-Skewness matrix of portfolio</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Computing-the-Co-Skewness-matrix-of-portfolio/m-p/130118#M1014</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I think the key is to think of the "centered" matrix X = T[1:(r-1),] - mean(T);&lt;/P&gt;&lt;P&gt;In terms of that matrix, the double loops over j and k become essentially an X`X computation, except that you have to include the ith column as a weight matrix.&amp;nbsp; That means that you can replace the j and k loops by X`*diag(X[, i])*X / (r-1);&lt;/P&gt;&lt;P&gt;The resulting code ought to be lightening fast.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 06 May 2013 14:29:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Computing-the-Co-Skewness-matrix-of-portfolio/m-p/130118#M1014</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2013-05-06T14:29:47Z</dc:date>
    </item>
    <item>
      <title>Re: Computing the Co-Skewness matrix of portfolio</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Computing-the-Co-Skewness-matrix-of-portfolio/m-p/130119#M1015</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Really really thank you!! Rick Wicklin, I follow your suggestion and I made a tremendous improvement!! &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 07 May 2013 09:32:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Computing-the-Co-Skewness-matrix-of-portfolio/m-p/130119#M1015</guid>
      <dc:creator>MichaelGong</dc:creator>
      <dc:date>2013-05-07T09:32:10Z</dc:date>
    </item>
    <item>
      <title>Re: Computing the Co-Skewness matrix of portfolio</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Computing-the-Co-Skewness-matrix-of-portfolio/m-p/130120#M1016</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Great!&amp;nbsp; The vectorizing code and pre-allocating matrices that are filled within a loop are two of the many performance tips that I discuss in my book &lt;EM&gt;&lt;A href="http://support.sas.com/publishing/authors/wicklin.html"&gt;Statistical Programming withSAS/IML Software&lt;/A&gt;&lt;/EM&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 07 May 2013 10:17:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Computing-the-Co-Skewness-matrix-of-portfolio/m-p/130120#M1016</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2013-05-07T10:17:38Z</dc:date>
    </item>
    <item>
      <title>Re: Computing the Co-Skewness matrix of portfolio</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Computing-the-Co-Skewness-matrix-of-portfolio/m-p/130121#M1017</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;For more on how to vectorize loops in a matrix language, see&lt;/P&gt;&lt;P&gt;&lt;A href="http://blogs.sas.com/content/iml/2013/05/15/vectorize-computations/" title="http://blogs.sas.com/content/iml/2013/05/15/vectorize-computations/"&gt; How to vectorize computations in a matrix language - The DO Loop&lt;/A&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 21 May 2013 14:37:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Computing-the-Co-Skewness-matrix-of-portfolio/m-p/130121#M1017</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2013-05-21T14:37:10Z</dc:date>
    </item>
    <item>
      <title>Re: Computing the Co-Skewness matrix of portfolio</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Computing-the-Co-Skewness-matrix-of-portfolio/m-p/300069#M3012</link>
      <description>&lt;P&gt;Hello dear,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for the help you have given to me to compute the skewness-co skewenss matrix.&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, my problem&amp;nbsp;is to maximize&amp;nbsp;the&amp;nbsp;skewness&amp;nbsp;of&amp;nbsp;portfolio of assets under constraints&amp;nbsp;(), I calculated&amp;nbsp;the matrix&amp;nbsp;of&amp;nbsp;skewness co-skewness of assets returns.&amp;nbsp;But&amp;nbsp;I can not&amp;nbsp;express&amp;nbsp;the objective function&amp;nbsp;as it contains&amp;nbsp;a&amp;nbsp;triple&amp;nbsp;sum.&amp;nbsp;it&amp;nbsp;is&amp;nbsp;having the following forms.&lt;/P&gt;&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/5011i903DA19B7C3BF676/image-size/original?v=v2&amp;amp;px=-1" border="0" alt="sskk.png" title="sskk.png" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Where wi are &lt;STRONG&gt;&lt;U&gt;decision variables&lt;/U&gt;&lt;/STRONG&gt; (vector of optimal weights to invest in each asset).&lt;/P&gt;&lt;P&gt;and sijk are assets (i,j,k)coskewness&lt;/P&gt;&lt;P&gt;W is the vector of weghts&amp;nbsp;&lt;BR /&gt;Could you help me please!&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance&lt;/P&gt;</description>
      <pubDate>Thu, 22 Sep 2016 10:13:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Computing-the-Co-Skewness-matrix-of-portfolio/m-p/300069#M3012</guid>
      <dc:creator>JANET1987</dc:creator>
      <dc:date>2016-09-22T10:13:17Z</dc:date>
    </item>
    <item>
      <title>Re: Computing the Co-Skewness matrix of portfolio</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Computing-the-Co-Skewness-matrix-of-portfolio/m-p/300084#M3015</link>
      <description>&lt;P&gt;This thread was answered and closed in 2013. If you have a related question about optimization, please open a new thread. &amp;nbsp;If your question is related to this one, you can link to this thread.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please include your attempt at solving the problem. References, desired output, and sample data are&amp;nbsp;also useful.&lt;/P&gt;</description>
      <pubDate>Thu, 22 Sep 2016 11:50:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Computing-the-Co-Skewness-matrix-of-portfolio/m-p/300084#M3015</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2016-09-22T11:50:53Z</dc:date>
    </item>
    <item>
      <title>Re: Computing the Co-Skewness matrix of portfolio</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Computing-the-Co-Skewness-matrix-of-portfolio/m-p/300086#M3016</link>
      <description>&lt;P&gt;HI,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much! I will do so!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Sep 2016 11:58:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Computing-the-Co-Skewness-matrix-of-portfolio/m-p/300086#M3016</guid>
      <dc:creator>JANET1987</dc:creator>
      <dc:date>2016-09-22T11:58:08Z</dc:date>
    </item>
  </channel>
</rss>

