<?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 sample from a posterior distribution in Statistical Procedures</title>
    <link>https://communities.sas.com/t5/Statistical-Procedures/How-to-sample-from-a-posterior-distribution/m-p/820496#M40579</link>
    <description>&lt;P&gt;To mimic the R script, use KSharp's idea but use&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;p_grid=T( do(0,1, 1/(&amp;amp;Size.-1)) );&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;With this change, you can skip the PROC SORT step.&amp;nbsp; I also inserted a histogram for the sample:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let size=1000;
proc iml;
call randseed(123);
p_grid=T( do(0,1, 1/(&amp;amp;Size.-1)) ); 
prob_data=pdf('binomial', 6, p_grid,9);

prob_p=j(&amp;amp;size.,1);
posterior= prob_data # prob_p;
posterior=posterior/sum(posterior);
call series (p_grid, posterior);

samples = sample(p_grid, 1e4,'replace',posterior) ;
call histogram(samples);
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 27 Jun 2022 11:06:16 GMT</pubDate>
    <dc:creator>Rick_SAS</dc:creator>
    <dc:date>2022-06-27T11:06:16Z</dc:date>
    <item>
      <title>How to sample from a posterior distribution</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/How-to-sample-from-a-posterior-distribution/m-p/820434#M40573</link>
      <description>&lt;P&gt;I am working through the book "Statistical Rethinking" by Richard McElreath. The following is a toy example from the book that uses grid approximation to get a posterior distribution then samples from the posterior.&amp;nbsp;&lt;EM&gt;My question is what is an equivalent way to obtain the same posterior and&amp;nbsp; similar sample in SAS?&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since I am more familiar with SAS I am doing this for understanding and fun.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The set up is we toss a globe and catch it 9 times. The tip of our right index finger will land on either land (L) or water (W). We observe L= 3 and W= 6. We are trying to determine the proportion of the globe that is water using a bayesian approach.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The R code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;# create a grid of possible parameter values (proportion of globe that is W)
p_grid &amp;lt;- seq(from= 0, to= 1, length.out= 1000)
p_grid[1000]

# Set prior
prob_p &amp;lt;- rep(1, l)

# Liklihood function for grid of possible parameter values
prob_data &amp;lt;- dbinom(6, size = 9, p_grid)

# combine prior with liklihood to get posterior
posterior &amp;lt;- prob_data * prob_p
sum(posterior)

#Standard posterior
posterior &amp;lt;- posterior/sum(posterior)

# Sample from posterior
samples &amp;lt;- sample(p_grid, prob = posterior, size = 1e4, replace = TRUE)

#plot samples
plot(samples)
plot(p_grid, posterior)&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is what the resulting posterior distribution looks like from the R script.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="supp_0-1656247249542.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/72749i3721291A936FD424/image-size/medium?v=v2&amp;amp;px=400" role="button" title="supp_0-1656247249542.png" alt="supp_0-1656247249542.png" /&gt;&lt;/span&gt;&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 my attempt to recreate the process in SAS. The tricky part seems to be is how to sample (or run simulations) based on the posterior. I used PROC SURVEYSELECT.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/**
Bayes practice
**/

data _test1;
	p_grid= 0;											/** Set initial parameter value **/
	
	do i= 1 to 1000;
		
/*		p_grid = rand('uniform') ;	*/					/** Alternative approach is to just randomly generate a bunch of parameter values **/
		prob_p = 1 ; 									/** Set a prior **/
		prob_data = pdf('binomial', 6, p_grid, 9); 		/** Liklihood function, liklihood of data given parameter value **/
		posterior = prob_data * prob_p;
		output;
		p_grid + .001;									/** update p_grid **/
	end;
&lt;BR /&gt;drop i;
run;


proc sql;
	select sum(posterior) into: sum_posterior from _test1;         /* Sum posterior to standardize */

	create table _test2 as
		select *, posterior/(&amp;amp;sum_posterior.) as posterior_s from _test1 order by p_grid;
quit;

# create macro to sample from dataset using standard posterior as the size. 
%macro sample(i);
	proc surveyselect data= _test2 method= pps_wr out= _sample1 n= 1 outhits noprint;
		size posterior_s;
	run;

	proc append base= all_simulations data= _sample1; run;
%mend sample;

proc delete data= all_simulations; run;

data _null_;

	%let sim_size = 10000;
	do i = 1 to &amp;amp;sim_size;
		call execute("%sample("||i||");");
	end;
run; 

proc sql;	
	create table _sim_sum1 as
		select p_grid, sum(numberhits) as freq, sum(numberhits) / &amp;amp;sim_size. as proportion from all_simulations group by p_grid;

	select sum(proportion) as sum_proportion from _sim_sum1;
quit;


proc sgplot data= _sim_sum1;
	series x= p_grid y= proportion;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here is the resulting distribution from the plot: (It doesn't look right)&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="supp_1-1656247750962.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/72750i2553674D9E2AE4D5/image-size/medium?v=v2&amp;amp;px=400" role="button" title="supp_1-1656247750962.png" alt="supp_1-1656247750962.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I acknowledge I may be way off base with this approach. I am trying to understand this material and would appreciate someone showing me a proper way to do this in SAS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 26 Jun 2022 12:58:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/How-to-sample-from-a-posterior-distribution/m-p/820434#M40573</guid>
      <dc:creator>supp</dc:creator>
      <dc:date>2022-06-26T12:58:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to sample from a posterior distribution</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/How-to-sample-from-a-posterior-distribution/m-p/820454#M40576</link>
      <description>&lt;P&gt;In SAS, if you want to do some data simulation by&lt;SPAN&gt;&amp;nbsp;bayesian method, you could try PROC MCMC .&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Or try to use SAS/IML ,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt;&amp;nbsp; could help you something .&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Jun 2022 03:30:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/How-to-sample-from-a-posterior-distribution/m-p/820454#M40576</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-06-27T03:30:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to sample from a posterior distribution</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/How-to-sample-from-a-posterior-distribution/m-p/820490#M40578</link>
      <description>&lt;P&gt;You want this ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let size=1000;
proc iml;
call randseed(123);
p_grid=randfun(&amp;amp;size.,'uniform');
prob_data=pdf('binomial', 6,p_grid,9);

prob_p=j(&amp;amp;size.,1);
posterior= prob_data # prob_p;
posterior=posterior/sum(posterior);

samples = sample(p_grid, 1e4,'replace',posterior) ;

create want var{ p_grid posterior };
append;
close;
quit;

proc sort data=want; by p_grid;run;
proc sgplot data=want;
 series x=p_grid y=posterior ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1656324449326.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/72754iF64976F7C0983B99/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1656324449326.png" alt="Ksharp_0-1656324449326.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Jun 2022 10:07:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/How-to-sample-from-a-posterior-distribution/m-p/820490#M40578</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-06-27T10:07:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to sample from a posterior distribution</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/How-to-sample-from-a-posterior-distribution/m-p/820496#M40579</link>
      <description>&lt;P&gt;To mimic the R script, use KSharp's idea but use&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;p_grid=T( do(0,1, 1/(&amp;amp;Size.-1)) );&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;With this change, you can skip the PROC SORT step.&amp;nbsp; I also inserted a histogram for the sample:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let size=1000;
proc iml;
call randseed(123);
p_grid=T( do(0,1, 1/(&amp;amp;Size.-1)) ); 
prob_data=pdf('binomial', 6, p_grid,9);

prob_p=j(&amp;amp;size.,1);
posterior= prob_data # prob_p;
posterior=posterior/sum(posterior);
call series (p_grid, posterior);

samples = sample(p_grid, 1e4,'replace',posterior) ;
call histogram(samples);
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 27 Jun 2022 11:06:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/How-to-sample-from-a-posterior-distribution/m-p/820496#M40579</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2022-06-27T11:06:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to sample from a posterior distribution</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/How-to-sample-from-a-posterior-distribution/m-p/820500#M40580</link>
      <description>Thanks for the reply. When I try running PROC IML I get a message suggesting my shop does not have a license for it. It seems PROC IML lets us approach the problem very similar to R using vectors.</description>
      <pubDate>Mon, 27 Jun 2022 11:21:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/How-to-sample-from-a-posterior-distribution/m-p/820500#M40580</guid>
      <dc:creator>supp</dc:creator>
      <dc:date>2022-06-27T11:21:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to sample from a posterior distribution</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/How-to-sample-from-a-posterior-distribution/m-p/820509#M40581</link>
      <description>&lt;P&gt;Yes. The IML language is similar to R in many ways. Both are high-level languages that support matrix-vector computations, a large library of computational routines, and enable you to extend the language by defining&amp;nbsp;your own functions.&amp;nbsp; As this example shows, you can often translate an R program to PROC IML and vice versa.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The IML language is also syntactically similar to MATLAB.&lt;/P&gt;</description>
      <pubDate>Mon, 27 Jun 2022 12:03:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/How-to-sample-from-a-posterior-distribution/m-p/820509#M40581</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2022-06-27T12:03:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to sample from a posterior distribution</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/How-to-sample-from-a-posterior-distribution/m-p/820517#M40583</link>
      <description>&lt;P&gt;I spent some time summarizing the results from the SAS method. I now think the approach I used and results are valid. I still think there is a more elegant way to achieve the same results.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here are some summaries I made from sampling the posterior.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
/** Add a cumulative proportion column to find confidence intervals **/
data _sample3;
	set _sample2;
	
		retain cumulative_prop;
		if _n_ = 1 then cumulative_prop = proportion;
		else cumulative_prop = cumulative_prop + proportion;
run;



proc sgplot data= _sample3;
	series x= p_grid y= proportion;
run;


proc sql;
/** Add up posterior probability where p &amp;lt; 0.5 */
	select sum(posterior_s) as sum_standard_post from _sample3 where p_grid &amp;lt; 0.5;
	select sum(proportion) as sum_proportion from _sample3 where p_grid &amp;lt; 0.5;

/** What proportion of water is compatible with greater than 0.5 and less than .75 probability? **/
	select sum(proportion) as sum_proportion from _sample3 where 0.5 &amp;lt; p_grid &amp;lt; 0.75;

/** What proportion of water is compatible with greater less than 80% probability? **/
	select max(p_grid) as p_grid from _sample3 where cumulative_prop &amp;lt; .8;

/** What proportion of water is compatible with middle 80% probability? **/
	select min(p_grid) as lower_parameter, max(p_grid) as upper_parameter from _sample3 where 0.1 &amp;lt; cumulative_prop &amp;lt; .9;

/** Point estimate of highest probabilty  **/
	select p_grid as point_estimate, proportion, cumulative_prop from _sample3 having max(proportion) = proportion;

quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/**
Proportion of water less than .5
	Posterior --&amp;gt; 0.166 probability
	Sample --&amp;gt; 0.178 probability

Proportion of water greater than .5 and less than .75
	sample --&amp;gt; .596 probability

Lower 80% posterior probablity when proprotion of water is .76

Middle 80% posterior probablity when proprotion of water is between .445 and .812

point estimate for most likely proportion of water --&amp;gt; .625 (about .4% probability)
**/&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 27 Jun 2022 12:38:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/How-to-sample-from-a-posterior-distribution/m-p/820517#M40583</guid>
      <dc:creator>supp</dc:creator>
      <dc:date>2022-06-27T12:38:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to sample from a posterior distribution</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/How-to-sample-from-a-posterior-distribution/m-p/820525#M40584</link>
      <description>I looked into PROC MCMC. I can't figure out what the MODEL statement would be. Maybe this is too simple of an example?</description>
      <pubDate>Mon, 27 Jun 2022 13:21:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/How-to-sample-from-a-posterior-distribution/m-p/820525#M40584</guid>
      <dc:creator>supp</dc:creator>
      <dc:date>2022-06-27T13:21:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to sample from a posterior distribution</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/How-to-sample-from-a-posterior-distribution/m-p/820674#M40585</link>
      <description>&lt;P&gt;Here are some sas base code if you don't have iml module.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let size=1000;
data have;
call streaminit(123);
 do p_grid=0 to 1 by 1/(&amp;amp;Size.-1);
   prob_data=pdf('binomial', 6, p_grid,9);
   output;
 end;
run;
proc sql;
create table have2 as
 select *,prob_data/sum(prob_data) as posterior
  from have;
quit;
proc sgplot data=have2;
series x=p_grid y=posterior;
run;


proc surveyselect data=have2 out=want seed=123 sampsize=10000 method=pps_wr outhits;
size posterior;
run;
proc sgplot data=want;
histogram p_grid;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Jun 2022 12:11:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/How-to-sample-from-a-posterior-distribution/m-p/820674#M40585</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-06-28T12:11:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to sample from a posterior distribution</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/How-to-sample-from-a-posterior-distribution/m-p/820892#M40607</link>
      <description>Thanks &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;, the code you provided gets a posterior distribution without using PROC IML. It is also a little cleaner than my code in the original post.</description>
      <pubDate>Wed, 29 Jun 2022 12:04:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/How-to-sample-from-a-posterior-distribution/m-p/820892#M40607</guid>
      <dc:creator>supp</dc:creator>
      <dc:date>2022-06-29T12:04:28Z</dc:date>
    </item>
  </channel>
</rss>

