<?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 construct correlation matrix from correlation vectors in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/construct-correlation-matrix-from-correlation-vectors/m-p/225848#M2313</link>
    <description>&lt;P&gt;I have read the Dr. Rick's post about how to extract nontrivial correlation from a correlation matrix.&amp;nbsp;&lt;A href="http://blogs.sas.com/content/iml/2012/08/16/extract-the-lower-triangular-elements-of-a-matrix.html" target="_blank"&gt;http://blogs.sas.com/content/iml/2012/08/16/extract-the-lower-triangular-elements-of-a-matrix.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My question is that I want to construct a correlation matrix from a vector only containing nontrivial corrleations. The vector only contains those correlations that are not 1. But I want to construct a correlation matrix whose diagonal elements are all 1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 16 Sep 2015 14:44:56 GMT</pubDate>
    <dc:creator>Hanyu</dc:creator>
    <dc:date>2015-09-16T14:44:56Z</dc:date>
    <item>
      <title>construct correlation matrix from correlation vectors</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/construct-correlation-matrix-from-correlation-vectors/m-p/225848#M2313</link>
      <description>&lt;P&gt;I have read the Dr. Rick's post about how to extract nontrivial correlation from a correlation matrix.&amp;nbsp;&lt;A href="http://blogs.sas.com/content/iml/2012/08/16/extract-the-lower-triangular-elements-of-a-matrix.html" target="_blank"&gt;http://blogs.sas.com/content/iml/2012/08/16/extract-the-lower-triangular-elements-of-a-matrix.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My question is that I want to construct a correlation matrix from a vector only containing nontrivial corrleations. The vector only contains those correlations that are not 1. But I want to construct a correlation matrix whose diagonal elements are all 1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Sep 2015 14:44:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/construct-correlation-matrix-from-correlation-vectors/m-p/225848#M2313</guid>
      <dc:creator>Hanyu</dc:creator>
      <dc:date>2015-09-16T14:44:56Z</dc:date>
    </item>
    <item>
      <title>Re: construct correlation matrix from correlation vectors</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/construct-correlation-matrix-from-correlation-vectors/m-p/225874#M2315</link>
      <description>&lt;P&gt;I'd use the ROW and COL functions to specify the upper triangular and lower triangular portions of the matrix:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
/* vector contains n*(n-1)/2 upper triangular corr */
v = {0.6 0.5 0.4
         0.3 0.2
             0.1};
k = ncol(v);
n = (sqrt(1 + 8*k) + 1)/2;   /* dimension of full matrix */
print n;

A = J(n,n,0);          /* zero matrix */
r = row(A);
c = col(A);
upperTri = loc(r &amp;lt; c); /* upper tri indices in row major order */
A[upperTri] = v;       /* copy elements */
A = A + A`;            /* make symmetric */
diag = loc(r = c);     /* diagonal elements */
A[diag] = 1;           /* put 1 on diagonal */

print A;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you are using an old version of SAS/IML that does not support the ROW and COL functions, see the blog post &lt;A href="http://blogs.sas.com/content/iml/2012/09/17/filling-the-lower-and-upper-triangular-portions-of-a-matrix.html" target="_self"&gt;"Filling the lower and upper triangular portions of a matrix."&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Sep 2015 15:48:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/construct-correlation-matrix-from-correlation-vectors/m-p/225874#M2315</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2015-09-16T15:48:46Z</dc:date>
    </item>
    <item>
      <title>Re: construct correlation matrix from correlation vectors</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/construct-correlation-matrix-from-correlation-vectors/m-p/226735#M2318</link>
      <description>&lt;P&gt;Hey Rick,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My memory of matrix algebra is non-functional this morning.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I see the code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token function"&gt;n&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;sqrt&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;+&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;8&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt;k&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;+&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;/&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;2&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;   &lt;SPAN class="token comment"&gt;/* dimension of full matrix */&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;and wanted to generalize it, but that '8' seems a bit arbitrary, especially when I am going after a 4x4, such that k=4. &amp;nbsp;Then sqrt(33) is a non-integer value. &amp;nbsp;Is there something in the blog post to help me out here?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Steve Denham&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Sep 2015 13:44:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/construct-correlation-matrix-from-correlation-vectors/m-p/226735#M2318</guid>
      <dc:creator>SteveDenham</dc:creator>
      <dc:date>2015-09-22T13:44:34Z</dc:date>
    </item>
    <item>
      <title>Re: construct correlation matrix from correlation vectors</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/construct-correlation-matrix-from-correlation-vectors/m-p/226748#M2324</link>
      <description>&lt;P&gt;Hi Steve,&lt;/P&gt;&lt;P&gt;It's not matrix algebra that you need to recall. The formula is derived from the quadratic formula. The formula is valid for any size matrix.&amp;nbsp; The '8' is not arbitrary.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There are k = n(n-1)/2&amp;nbsp;elements in the upper triangular portion of an nxn matrix.For this problem, a vector with k elements is specified, so we need to discover n.&amp;nbsp; The formula is equivalent to&lt;/P&gt;&lt;P&gt;n**2 - n -2k = 0&lt;/P&gt;&lt;P&gt;which by the quadratic formula has the solutions&lt;/P&gt;&lt;P&gt;n = ( 1 +/- sqrt(1 + 8k) ) / 2.&lt;/P&gt;&lt;P&gt;We discard the negative solution and are left with&lt;/P&gt;&lt;P&gt;n = (1 + sqrt(1 + 8*k)) / 2&lt;/P&gt;&lt;P&gt;as the formula that gives n when k is properly specified.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So as you see, the '8' comes from the "-4ac" portion of the quadratic formula. Here a=1 and c=-2k.&amp;nbsp; The "trick" that you missed is that k cannot be arbitrary. The formula is only valid when k is the number of elements in the upper/lower triangular portion of&amp;nbsp;an nxn matrix for some n.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Sep 2015 13:46:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/construct-correlation-matrix-from-correlation-vectors/m-p/226748#M2324</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2015-09-23T13:46:28Z</dc:date>
    </item>
    <item>
      <title>Re: construct correlation matrix from correlation vectors</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/construct-correlation-matrix-from-correlation-vectors/m-p/226960#M2332</link>
      <description>&lt;P&gt;Fooled by a picture.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That's what happened. &amp;nbsp;k=ncol(v), but the upper triangular "picture" made me think 3 columns in v, so everything solved in my head. (Plug in k=3, and out comes a 3x3 matrix)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But in fact there are 6 columns in that upper triangular "picture", and so long as k is a triangular number everything works.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Steve Denham&lt;/P&gt;</description>
      <pubDate>Wed, 23 Sep 2015 19:11:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/construct-correlation-matrix-from-correlation-vectors/m-p/226960#M2332</guid>
      <dc:creator>SteveDenham</dc:creator>
      <dc:date>2015-09-23T19:11:21Z</dc:date>
    </item>
    <item>
      <title>Re: construct correlation matrix from correlation vectors</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/construct-correlation-matrix-from-correlation-vectors/m-p/229307#M2380</link>
      <description>&lt;P&gt;proc iml;&lt;BR /&gt;start corr_mat(corr,p);/*p is the number of variables which is the dimension of the objective p*p correlation matrix*/&lt;/P&gt;
&lt;P&gt;/*corr is the vector containing all nontrivial correlations in order*/&lt;/P&gt;
&lt;P&gt;w=p:2;&lt;BR /&gt;v=(1||cusum(w)+1);&lt;BR /&gt;do i=1 to ncol(v);&lt;BR /&gt;s=v[i];&lt;BR /&gt;corr=insert(corr,1,0,s);&lt;BR /&gt;end;&lt;BR /&gt;corr_mat=sqrvech(corr);&lt;BR /&gt;return(corr_mat);&lt;BR /&gt;finish;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you Rick! Would the above code also works? Suppose we know how many variables we want, say p=7 variables, and we have the correlation vectors. Do you think this solution is correct?&lt;/P&gt;</description>
      <pubDate>Fri, 09 Oct 2015 15:01:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/construct-correlation-matrix-from-correlation-vectors/m-p/229307#M2380</guid>
      <dc:creator>Hanyu</dc:creator>
      <dc:date>2015-10-09T15:01:42Z</dc:date>
    </item>
    <item>
      <title>Re: construct correlation matrix from correlation vectors</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/construct-correlation-matrix-from-correlation-vectors/m-p/229320#M2381</link>
      <description>&lt;P&gt;Yes, I think it is correct.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, calling the INSERT function multiple times in a loop is not very efficient because it allocates a new matrix (one element greater than before) and copies over the existing elements.&amp;nbsp; You end up copying the array p times.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As a general rule, it is inefficient to dynamically grow an array insdie of a loop. See the article &lt;A href="http://blogs.sas.com/content/iml/2015/02/16/friends-dont-let-friends-concatenate-results-inside-a-loop.html" target="_self"&gt;"Friends don't let friends concatenate results inside a loop," &lt;/A&gt;which describes a situation that is&amp;nbsp;similar to your approach.&lt;/P&gt;</description>
      <pubDate>Fri, 09 Oct 2015 15:36:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/construct-correlation-matrix-from-correlation-vectors/m-p/229320#M2381</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2015-10-09T15:36:03Z</dc:date>
    </item>
    <item>
      <title>Re: construct correlation matrix from correlation vectors</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/construct-correlation-matrix-from-correlation-vectors/m-p/230540#M2404</link>
      <description>&lt;P&gt;I just published a blog post that explains how to &lt;A href="http://blogs.sas.com/content/iml/2015/10/19/corr-upper-tri.html" target="_self"&gt;create a correlation matrix from the uppse triangular elements.&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Oct 2015 09:45:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/construct-correlation-matrix-from-correlation-vectors/m-p/230540#M2404</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2015-10-19T09:45:56Z</dc:date>
    </item>
  </channel>
</rss>

