<?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: How to correct for non-positive matrix while generating multivariate random numbers in SAS in Statistical Procedures</title>
    <link>https://communities.sas.com/t5/Statistical-Procedures/How-to-correct-for-non-positive-matrix-while-generating/m-p/79491#M3797</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Rick,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;thanks the correlation is matrix is acceptable, it will be of great help if I could get the code you used in obtaining the correlation matrix. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As I need to document it as well.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;C. Wood.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 26 Nov 2012 16:03:18 GMT</pubDate>
    <dc:creator>Tcook</dc:creator>
    <dc:date>2012-11-26T16:03:18Z</dc:date>
    <item>
      <title>How to correct for non-positive matrix while generating multivariate random numbers in SAS</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/How-to-correct-for-non-positive-matrix-while-generating/m-p/79485#M3791</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am trying to generate 300 random sample of a trivariate normal distribution with mean (-2,4,0) and variance co-variance matrix of ( 1 0.99 0.35, 0.99 1 0.80, 0.35 0.80 1 ).&lt;/P&gt;&lt;P&gt;I found two different codes online and tried using them but got an Error message that my matrix is not positive definite&amp;nbsp; hence, failure in generating the random sample. Please below are the two different codes I used.&lt;/P&gt;&lt;P&gt;Please I would appreciate if anyone could help sort this out.&amp;nbsp; Thanks in anticipation of a favorable and swift responses.&lt;/P&gt;&lt;P&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; CODE ONE&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;/P&gt;&lt;P&gt; proc iml ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; A = {1.0 0.99 0.35, 0.99 1.0 0.80, 0.35 0.80 1.0} ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; call eigen(DIAG, P, A);&amp;nbsp; &lt;/P&gt;&lt;P&gt; print A P DIAG ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; s1 = sqrt(DIAG[1]);&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp; s2 = sqrt(DIAG[2]);&lt;/P&gt;&lt;P&gt;&amp;nbsp; s3 = sqrt(DIAG[3]);&lt;/P&gt;&lt;P&gt;&amp;nbsp; seed = 20000222 ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; e1 = {1, 0, 0} ; e2 = {0, 1, 0} ; e3 = {0, 0, 1} ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; V = j(300, 3, 0) ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; do i = 1 to 300 ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; y1 = -2 + s1 * rannor(seed) ; &lt;/P&gt;&lt;P&gt;&amp;nbsp; y2 = 4 + s2 * rannor(seed) ;&amp;nbsp; /*&amp;nbsp; y1 ~ N(-2, s1), ys ~ N(4, s2). and y3 ~ N(0, s3)&amp;nbsp;&amp;nbsp;&amp;nbsp; */&lt;/P&gt;&lt;P&gt;&amp;nbsp; y3 = s3 * rannor(seed) ;&amp;nbsp; /*&amp;nbsp; Note that the y's have covariance */&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; V[i, 1] = y1 ;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /*&amp;nbsp; Create an n x 2 matrix of the y's */&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; V[i, 2] = y2 ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; v[i, 3] = y3 ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; end ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; W = V * P` ;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp; varnames = 'y1':'y2':'y3' ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; create yobs&amp;nbsp; from V [colname = varnames] ; &lt;/P&gt;&lt;P&gt;&amp;nbsp; varnames = 'x1':'x2':'x3' ;&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; &lt;/P&gt;&lt;P&gt;&amp;nbsp; create trans from W [colname = varnames] ; &lt;/P&gt;&lt;P&gt;&amp;nbsp; append from W ;&lt;/P&gt;&lt;P&gt; quit ;&lt;/P&gt;&lt;P&gt; run ;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;/////////////// CODE TWO //////////////////////////////&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;/* Generate the multivariate normal data in SAS/IML */&lt;/P&gt;&lt;P&gt;/* non-macro version */&lt;/P&gt;&lt;P&gt;data work1r; /* data for the parameter for the multivariate normal data */&lt;/P&gt;&lt;P&gt;input r1 r2 r3 means vars;&lt;/P&gt;&lt;P&gt;cards;&lt;/P&gt;&lt;P&gt;1.0 0.99 0.35 -2 1&lt;/P&gt;&lt;P&gt;0.99 1.0 0.80 4 1&lt;/P&gt;&lt;P&gt;0.35 0.80 1.0 0 1&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;proc iml;&lt;/P&gt;&lt;P&gt;use work1;&lt;/P&gt;&lt;P&gt;read all var {k1 k2 k3} into R;&lt;/P&gt;&lt;P&gt;read all var {means} into mu;&lt;/P&gt;&lt;P&gt;read all var {vars} into sigma;&lt;/P&gt;&lt;P&gt;p = ncol(R);&lt;/P&gt;&lt;P&gt;diag_sig = diag( sigma );&lt;/P&gt;&lt;P&gt;DRD = diag_sig * R * diag_sig`;&lt;/P&gt;&lt;P&gt;U = half(DRD);&lt;/P&gt;&lt;P&gt;do i = 1 to 300;&lt;/P&gt;&lt;P&gt;z = rannor( j(p,1,1234));&lt;/P&gt;&lt;P&gt;y = mu + U` * z;&lt;/P&gt;&lt;P&gt;yprime = y`;&lt;/P&gt;&lt;P&gt;yall = yall // yprime;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;varnames = { y1 y2 y3 };&lt;/P&gt;&lt;P&gt;create work2 from yall (|colname = varnames|);&lt;/P&gt;&lt;P&gt;append from yall;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;proc print data=work2;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 25 Nov 2012 20:03:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/How-to-correct-for-non-positive-matrix-while-generating/m-p/79485#M3791</guid>
      <dc:creator>Tcook</dc:creator>
      <dc:date>2012-11-25T20:03:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to correct for non-positive matrix while generating multivariate random numbers in SAS</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/How-to-correct-for-non-positive-matrix-while-generating/m-p/79486#M3792</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You can generate mv normal data in SAS/IML by using the RandNormal function. See &lt;A href="http://blogs.sas.com/content/iml/2011/01/12/sampling-from-the-multivariate-normal-distribution/" title="http://blogs.sas.com/content/iml/2011/01/12/sampling-from-the-multivariate-normal-distribution/"&gt; Sampling from the multivariate normal distribution - The DO Loop&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As you've discovered, you must have a valid covariance matrix in order to generate the data. The reason why your matrix is not a valid covariance matrix, and some reasons why this occurs, are discussed in this article: &lt;A href="http://blogs.sas.com/content/iml/2012/09/12/when-is-a-correlation-matrix-not-a-correlation-matrix/" title="http://blogs.sas.com/content/iml/2012/09/12/when-is-a-correlation-matrix-not-a-correlation-matrix/"&gt; When is a correlation matrix not a correlation matrix? - The DO Loop&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt; What to DO about it? That's hard.&amp;nbsp; If you computed the covariance by using pairwise correlations, go back and recompute it by using listwise deletion of missing values.&amp;nbsp; If the correlations are estimated and you don't have the original data, you can try shrinkage methods or projection methods to obtain a nearby matrix that is a valid correlation matrix.&amp;nbsp; For example, the nearest correlation matrix (in the Frobenius norm) to your matrix is approximately&lt;/P&gt;&lt;P&gt;A={ 1.0&amp;nbsp; 0.9&amp;nbsp; 0.4,&lt;/P&gt;&lt;P&gt; 0.9&amp;nbsp; 1.0&amp;nbsp; 0.75,&lt;/P&gt;&lt;P&gt; 0.4&amp;nbsp; 0.75&amp;nbsp; 1.0};&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 26 Nov 2012 02:03:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/How-to-correct-for-non-positive-matrix-while-generating/m-p/79486#M3792</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2012-11-26T02:03:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to correct for non-positive matrix while generating multivariate random numbers in SAS</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/How-to-correct-for-non-positive-matrix-while-generating/m-p/79487#M3793</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks for your response. It is quite help but I still have some questions:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If I understand correctly, if for me to generate a random sample using the correlation matrix&amp;nbsp; MUST also have the standard deviation?&lt;/P&gt;&lt;P&gt;In my case all I have is the correlation matrix and mean, please what sas code/procedure can I use in generation the nearest correlation matrix based on the Frobenius norm?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks in anticipation of your response.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 26 Nov 2012 13:06:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/How-to-correct-for-non-positive-matrix-while-generating/m-p/79487#M3793</guid>
      <dc:creator>Tcook</dc:creator>
      <dc:date>2012-11-26T13:06:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to correct for non-positive matrix while generating multivariate random numbers in SAS</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/How-to-correct-for-non-positive-matrix-while-generating/m-p/79488#M3794</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'll try to find time to write a blog post on this topic in the next week.&lt;/P&gt;&lt;P&gt;Rick&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 26 Nov 2012 14:37:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/How-to-correct-for-non-positive-matrix-while-generating/m-p/79488#M3794</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2012-11-26T14:37:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to correct for non-positive matrix while generating multivariate random numbers in SAS</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/How-to-correct-for-non-positive-matrix-while-generating/m-p/79489#M3795</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Rick,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;That will be appreciated.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However, I am seriously looking for a solution to my problem am working on a project and need to continue with the main aspect of the project which depends on this. Is there anyway you&amp;nbsp; could be of help with the correct code to generate the random sample given that I have only the MEAN and a Correlation matrix that is not positive definite?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt; Meanwhile I was able to generate an eigenvalue that is positive using R-Code I tried generating a random sampling using your code below (&lt;/P&gt;&lt;P&gt;proc iml;&lt;/P&gt;&lt;P&gt;load module=RndOrthog; /* load or define modules here */&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;start RndMatWithEigenval(lambda);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; n = ncol(lambda); /* assume lambda is row vector */&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; Q = RndOrthog(n);&lt;/P&gt;&lt;P&gt;return( Q`*diag(lambda)*Q );&lt;/P&gt;&lt;P&gt;finish;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;lambda = {2.4 0.6 0};&lt;/P&gt;&lt;P&gt;call randseed(1234);&lt;/P&gt;&lt;P&gt;S1 = RndMatWithEigenval(lambda); /* eigenvalues are lambda */&lt;/P&gt;&lt;P&gt;eval = eigval(S1);&lt;/P&gt;&lt;P&gt;print eval;&lt;/P&gt;&lt;P&gt;print S1; ) but I got an error message which has to do with the line &lt;/P&gt;&lt;P&gt;load module=RndOrthog; /* load or define modules here */. how do I sort this out? &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks in anticipation of favorable response.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;C. Wood.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 26 Nov 2012 14:56:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/How-to-correct-for-non-positive-matrix-while-generating/m-p/79489#M3795</guid>
      <dc:creator>Tcook</dc:creator>
      <dc:date>2012-11-26T14:56:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to correct for non-positive matrix while generating multivariate random numbers in SAS</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/How-to-correct-for-non-positive-matrix-while-generating/m-p/79490#M3796</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I've already given you a correlation matrix that is close to your estimate. If that correlation matrix is acceptable, just call the RANDNORMAL function:&lt;/P&gt;&lt;P&gt;N = 1000; /* number of obs to generate */&lt;/P&gt;&lt;P&gt;mean = {-2,4,0};&lt;/P&gt;&lt;P&gt;x = RandNormal(N, mean, A);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;See the &lt;A href="http://support.sas.com/documentation/cdl/en/imlug/65547/HTML/default/viewer.htm#imlug_modlib_sect022.htm"&gt;RandNormal doc &lt;/A&gt;for details.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 26 Nov 2012 15:04:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/How-to-correct-for-non-positive-matrix-while-generating/m-p/79490#M3796</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2012-11-26T15:04:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to correct for non-positive matrix while generating multivariate random numbers in SAS</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/How-to-correct-for-non-positive-matrix-while-generating/m-p/79491#M3797</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Rick,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;thanks the correlation is matrix is acceptable, it will be of great help if I could get the code you used in obtaining the correlation matrix. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As I need to document it as well.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;C. Wood.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 26 Nov 2012 16:03:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/How-to-correct-for-non-positive-matrix-while-generating/m-p/79491#M3797</guid>
      <dc:creator>Tcook</dc:creator>
      <dc:date>2012-11-26T16:03:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to correct for non-positive matrix while generating multivariate random numbers in SAS</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/How-to-correct-for-non-positive-matrix-while-generating/m-p/79492#M3798</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Here is a description of how to get a correlation matrix when your estimate is not positive definite:&lt;/P&gt;&lt;P&gt;&lt;A href="http://blogs.sas.com/content/iml/2012/11/28/computing-the-nearest-correlation-matrix/" title="http://blogs.sas.com/content/iml/2012/11/28/computing-the-nearest-correlation-matrix/"&gt; Computing the nearest correlation matrix - The DO Loop&lt;/A&gt; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Nov 2012 20:42:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/How-to-correct-for-non-positive-matrix-while-generating/m-p/79492#M3798</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2012-11-28T20:42:43Z</dc:date>
    </item>
  </channel>
</rss>

