<?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: draw samples from distribution whose parameters are matrices in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/draw-samples-from-distribution-whose-parameters-are-matrices/m-p/375813#M3604</link>
    <description>&lt;P&gt;This seems to be &lt;A href="https://stackoverflow.com/questions/45070056/working-with-matrices-and-samples-using-proc-iml" target="_self"&gt;crossposted to StackOverflow.&lt;/A&gt;&amp;nbsp;Please only post in one place unless you are not getting a satisfactory response. &amp;nbsp;For&amp;nbsp;completeness, here is the solution for data from the Sashelp.Cars data. The randomly generated C matrix is very close to the C_hat matrix for the data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
use sashelp.cars;
read all var {weight wheelbase enginesize horsepower} into X;
read all var {mpg_city mpg_highway} into Z;
close;

*normal equations and covariance;
xpx = x`*x;
invxpx = inv(xpx);
C_hat = invxpx*(x`*z);
r = z-x*c_hat;
S = r`*r;

*draw sigma;
call randseed(4321);
DF = nrow(X)-ncol(X)-2;
W = RandWishart(1, DF, inv(S));  /* 1 x (p*p) row vector */
sigma = shape(W, sqrt(ncol(W))); /* reshape to p x p matrix */
*draw vec(c);
vec_c_hat = colvec(c_hat`);      /* stack columns of c_hat */
vec_c = RandNormal(1, vec_c_hat, sigma@invxpx);
c = shapecol(vec_c, nrow(C_hat), ncol(C_hat));   /* reshape w/ SHAPECOL */
print C_hat, c;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 13 Jul 2017 19:30:41 GMT</pubDate>
    <dc:creator>Rick_SAS</dc:creator>
    <dc:date>2017-07-13T19:30:41Z</dc:date>
    <item>
      <title>draw samples from distribution whose parameters are matrices</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/draw-samples-from-distribution-whose-parameters-are-matrices/m-p/375206#M3595</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;I would like to draw samples from some distribution with parameters in matrix form. For example, a normal distribution N(mu,sigma) takes to parameters mu and sigma which are numbers. However, my mu and sigma are now matrices. &amp;nbsp;In particular, the distribution of interest is:&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/10276i142045B9A40D3E86/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="Capture.PNG" title="Capture.PNG" /&gt;&lt;/P&gt;&lt;P&gt;where S is a 4x4 matrix, X is a 100x4 matrix.&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2017 05:08:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/draw-samples-from-distribution-whose-parameters-are-matrices/m-p/375206#M3595</guid>
      <dc:creator>somebody</dc:creator>
      <dc:date>2017-07-12T05:08:18Z</dc:date>
    </item>
    <item>
      <title>Re: draw samples from distribution whose parameters are matrices</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/draw-samples-from-distribution-whose-parameters-are-matrices/m-p/375248#M3601</link>
      <description>&lt;P&gt;The RANDNORMAL function in SAS/IML can handle this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can generate samples from the multivariate normal distribution in SAS following the article by Rick Wicklin&amp;nbsp;here&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.sas.com/content/iml/2013/04/10/generate-multiple-mvn-samples.html" target="_blank"&gt;http://blogs.sas.com/content/iml/2013/04/10/generate-multiple-mvn-samples.html&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2017 09:35:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/draw-samples-from-distribution-whose-parameters-are-matrices/m-p/375248#M3601</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2017-07-12T09:35:41Z</dc:date>
    </item>
    <item>
      <title>Re: draw samples from distribution whose parameters are matrices</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/draw-samples-from-distribution-whose-parameters-are-matrices/m-p/375249#M3602</link>
      <description>&lt;P&gt;You can use &lt;A href="http://support.sas.com/documentation/cdl/en/imlug/68150/HTML/default/viewer.htm#imlug_langref_sect371.htm" target="_self"&gt;the RANDWISHART function in SAS/IML&lt;/A&gt; to draw samples (matrices) from the Wishart distribution.&lt;/P&gt;
&lt;P&gt;In addition to the documentation, you can read the article &lt;A href="http://blogs.sas.com/content/iml/2014/11/26/the-wishart-distribution.html" target="_self"&gt;"The Wishart distribution: Covariance matrices for multivariate normal data,"&lt;/A&gt; which provides a discussion and example as well as how to .reshape the output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If necessary, you can also &lt;A href="http://blogs.sas.com/content/iml/2011/01/12/sampling-from-the-multivariate-normal-distribution.html" target="_self"&gt;use the RANDNORMAL function&lt;/A&gt; to sample from a&amp;nbsp;multivariate normal distribution.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2017 09:36:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/draw-samples-from-distribution-whose-parameters-are-matrices/m-p/375249#M3602</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2017-07-12T09:36:35Z</dc:date>
    </item>
    <item>
      <title>Re: draw samples from distribution whose parameters are matrices</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/draw-samples-from-distribution-whose-parameters-are-matrices/m-p/375813#M3604</link>
      <description>&lt;P&gt;This seems to be &lt;A href="https://stackoverflow.com/questions/45070056/working-with-matrices-and-samples-using-proc-iml" target="_self"&gt;crossposted to StackOverflow.&lt;/A&gt;&amp;nbsp;Please only post in one place unless you are not getting a satisfactory response. &amp;nbsp;For&amp;nbsp;completeness, here is the solution for data from the Sashelp.Cars data. The randomly generated C matrix is very close to the C_hat matrix for the data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
use sashelp.cars;
read all var {weight wheelbase enginesize horsepower} into X;
read all var {mpg_city mpg_highway} into Z;
close;

*normal equations and covariance;
xpx = x`*x;
invxpx = inv(xpx);
C_hat = invxpx*(x`*z);
r = z-x*c_hat;
S = r`*r;

*draw sigma;
call randseed(4321);
DF = nrow(X)-ncol(X)-2;
W = RandWishart(1, DF, inv(S));  /* 1 x (p*p) row vector */
sigma = shape(W, sqrt(ncol(W))); /* reshape to p x p matrix */
*draw vec(c);
vec_c_hat = colvec(c_hat`);      /* stack columns of c_hat */
vec_c = RandNormal(1, vec_c_hat, sigma@invxpx);
c = shapecol(vec_c, nrow(C_hat), ncol(C_hat));   /* reshape w/ SHAPECOL */
print C_hat, c;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 13 Jul 2017 19:30:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/draw-samples-from-distribution-whose-parameters-are-matrices/m-p/375813#M3604</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2017-07-13T19:30:41Z</dc:date>
    </item>
    <item>
      <title>Re: draw samples from distribution whose parameters are matrices</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/draw-samples-from-distribution-whose-parameters-are-matrices/m-p/376827#M3610</link>
      <description>&lt;P&gt;Thanks &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt;&amp;nbsp;for pointing out my error in the Stackoverflow&amp;nbsp;post.&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jul 2017 00:34:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/draw-samples-from-distribution-whose-parameters-are-matrices/m-p/376827#M3610</guid>
      <dc:creator>somebody</dc:creator>
      <dc:date>2017-07-18T00:34:28Z</dc:date>
    </item>
  </channel>
</rss>

