<?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: Trouble generating random matrix from Bernoulli Distribution in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Trouble-generating-random-matrix-from-Bernoulli-Distribution/m-p/273206#M2796</link>
    <description>&lt;P&gt;I think there are three problems that I can see&amp;nbsp;with your code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Did you intend the second loop to run from 7?&amp;nbsp; If&amp;nbsp;n_months is less than 7 then the second do loop will not iterate at all.&lt;/LI&gt;
&lt;LI&gt;As the statement 'forecast=j(n_farms,n_months,.)' is inside the nested loops, it will set all the elements to missing on every iteration, so it&amp;nbsp;needs to be&amp;nbsp;moved&amp;nbsp;outside of the loops.&lt;/LI&gt;
&lt;LI&gt;The first parameter in CALL RANDGEN should be the name of matrix which is to be entirely filled with random numbers in one call.&amp;nbsp;&amp;nbsp;It will not work when you&amp;nbsp;try to&amp;nbsp;set&amp;nbsp;individual elements.&amp;nbsp; Make&amp;nbsp;a single call to RANDGEN at the end of the code.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Putting&amp;nbsp;all of this&amp;nbsp;together the code will look something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do i=1 to n_farms;
  do j=1 to n_months;
    &amp;lt; code to set each element of the matrix bernoulli_p here &amp;gt;;
  end;
end;

call randseed(1729);
forecast=j(n_farms,n_months,.);
call randgen(forecast,'Bernoulli',bernoulli_p);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 26 May 2016 07:06:28 GMT</pubDate>
    <dc:creator>IanWakeling</dc:creator>
    <dc:date>2016-05-26T07:06:28Z</dc:date>
    <item>
      <title>Trouble generating random matrix from Bernoulli Distribution</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Trouble-generating-random-matrix-from-Bernoulli-Distribution/m-p/273135#M2794</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am struggling to generate a matrix, let's call it "forecast", such that each element forecast[i,j] is drawn from a different Bernoulli distribution with probability corresponding to another matrix element,&amp;nbsp;say bernoulli_p[i,j].&amp;nbsp;The log displays&amp;nbsp;no errors, but when I print the forecast matrix, it only has blank values.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the relevant part of the code I have been using:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;do i=1 to n_farms;&lt;BR /&gt;do j=7 to n_months;&lt;BR /&gt;if avc[i,j]&amp;gt;0 then stoch_avc[i,j]=(avc[i,j]+u[i,j]);&lt;BR /&gt;&lt;BR /&gt;if avc[i,j]=0 then exit_z[i,j]=.;&lt;BR /&gt;exit_z[i,j]=-2.929548+FALL[i,j]*(-0.027052)+Winter[i,j]*0.61746+spring[i,j]*(-0.075117)+&lt;BR /&gt;PRICE[i,j-1]*(-0.078526)+PRICE[i,j-2]*(-0.004963)+PRICE[i,j-3]*0.117736+PRICE[i,j-4]*(-0.042244)+PRICE[i,j-5]*0.001874+PRICE[i,j-6]*(-0.069283)+&lt;BR /&gt;AVC[i,j-1]*(-0.552019)+AVC[i,j-2]*(-0.017645)+AVC[i,j-3]*0.615848+AVC[i,j-4]*(-0.173533)+AVC[i,j-5]*0.174981+AVC[i,j-6]*0.043233;&lt;BR /&gt;&lt;BR /&gt;if exit_z[i,j]^=. then bernoulli_p[i,j]=cdf('normal',exit_z[i,j]);&lt;BR /&gt;if exit_z[i,j]=. then bernoulli_p[i,j]=.;&lt;BR /&gt;&lt;BR /&gt;call randseed(datetime());&lt;BR /&gt;forecast=j(n_farms,n_months,.);&lt;BR /&gt;call randgen(forecast[i,j],"bernoulli",bernoulli_p[i,j]);&lt;BR /&gt;&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 25 May 2016 20:55:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Trouble-generating-random-matrix-from-Bernoulli-Distribution/m-p/273135#M2794</guid>
      <dc:creator>NCSU_2016</dc:creator>
      <dc:date>2016-05-25T20:55:49Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble generating random matrix from Bernoulli Distribution</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Trouble-generating-random-matrix-from-Bernoulli-Distribution/m-p/273183#M2795</link>
      <description>&lt;P&gt;Can you post the data about matrix&amp;nbsp;&lt;SPAN&gt;forecast[i,j] and&amp;nbsp;bernoulli_p[i,j] ? &amp;nbsp;and the output matrix you want ?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 26 May 2016 03:09:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Trouble-generating-random-matrix-from-Bernoulli-Distribution/m-p/273183#M2795</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-05-26T03:09:38Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble generating random matrix from Bernoulli Distribution</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Trouble-generating-random-matrix-from-Bernoulli-Distribution/m-p/273206#M2796</link>
      <description>&lt;P&gt;I think there are three problems that I can see&amp;nbsp;with your code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Did you intend the second loop to run from 7?&amp;nbsp; If&amp;nbsp;n_months is less than 7 then the second do loop will not iterate at all.&lt;/LI&gt;
&lt;LI&gt;As the statement 'forecast=j(n_farms,n_months,.)' is inside the nested loops, it will set all the elements to missing on every iteration, so it&amp;nbsp;needs to be&amp;nbsp;moved&amp;nbsp;outside of the loops.&lt;/LI&gt;
&lt;LI&gt;The first parameter in CALL RANDGEN should be the name of matrix which is to be entirely filled with random numbers in one call.&amp;nbsp;&amp;nbsp;It will not work when you&amp;nbsp;try to&amp;nbsp;set&amp;nbsp;individual elements.&amp;nbsp; Make&amp;nbsp;a single call to RANDGEN at the end of the code.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Putting&amp;nbsp;all of this&amp;nbsp;together the code will look something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do i=1 to n_farms;
  do j=1 to n_months;
    &amp;lt; code to set each element of the matrix bernoulli_p here &amp;gt;;
  end;
end;

call randseed(1729);
forecast=j(n_farms,n_months,.);
call randgen(forecast,'Bernoulli',bernoulli_p);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 May 2016 07:06:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Trouble-generating-random-matrix-from-Bernoulli-Distribution/m-p/273206#M2796</guid>
      <dc:creator>IanWakeling</dc:creator>
      <dc:date>2016-05-26T07:06:28Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble generating random matrix from Bernoulli Distribution</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Trouble-generating-random-matrix-from-Bernoulli-Distribution/m-p/273232#M2797</link>
      <description>&lt;P&gt;For more information about Ian's excellent suggestion, see the article &lt;A href="http://blogs.sas.com/content/iml/2013/01/16/generate-binary-outcomes-with-varying-probability.html" target="_self"&gt;"Generate binary outcomes with varying probability."&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 26 May 2016 11:36:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Trouble-generating-random-matrix-from-Bernoulli-Distribution/m-p/273232#M2797</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2016-05-26T11:36:55Z</dc:date>
    </item>
  </channel>
</rss>

