<?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: My poisson probabilities are not summing up to 1 in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/My-poisson-probabilities-are-not-summing-up-to-1/m-p/520364#M141082</link>
    <description>&lt;P&gt;Well, a brief look at the code, and I can see a lot which can be narrowed down.&amp;nbsp; Then first set of loops for instance, can be replaced with;&lt;/P&gt;
&lt;PRE&gt;do i = 1 to 11;
  n[i] = i + 14;
  sf[i] = i + 44;
  v[i] = i + 54;
  t[i] = i + 24;
  f[i] = i + 39;
  a[i] = i - 1;
  w[i] = i + 6;
end;
&lt;/PRE&gt;
&lt;P&gt;The if could be resolved to:&lt;/P&gt;
&lt;PRE&gt;select(county);
  when (20) do;
    ...;
    end;
  when (60) do;
    ...;
    end;
  otherwise probability_fp=pdf(...);
end;&lt;/PRE&gt;
&lt;P&gt;The do loop blocks within the if statements are repeating.&amp;nbsp; Put your values in another array, then do the loop once, e.g:&lt;/P&gt;
&lt;PRE&gt;array vals{5,2} (15,25,
                          55,65,
                           ...);        /* First column is lower, second upper */
array p_res{5,2} 8.;         /* Array of results for p based

select(county);
  when (20) do;
    p=0;
    do i=1 to 6;
      p{i,1}=p{i,1}+((n{i}-vals{i,1})/5)*pdf...;
    end;
    do i=1 to 7;
      p{i,1}=p{i,1}+((vals{i,2}-n{i})/5)*pdf...;
    end;
   end;
...
&lt;/PRE&gt;
&lt;P&gt;Do note however that a simple change to your data structure will simplify your coding a lot.&amp;nbsp; Have one row per set of tests, so all v is on one row, all n on another etc.&amp;nbsp; This will simplify down to be one array, for each observation.&amp;nbsp; Also note I have nothing to work with in terms of test data in the form of a datastep, or what its supposed to do, so just generaralising.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't know about he stat question.&lt;/P&gt;</description>
    <pubDate>Tue, 11 Dec 2018 13:56:19 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2018-12-11T13:56:19Z</dc:date>
    <item>
      <title>My poisson probabilities are not summing up to 1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/My-poisson-probabilities-are-not-summing-up-to-1/m-p/520359#M141080</link>
      <description>&lt;P&gt;I have written a procedure that uses a specific estimate to compute Poisson probabilities for values between 0 to 80.&lt;/P&gt;&lt;P&gt;But at specific values (5, 12, 20, 30, 45, 50, 60), the procedure for computing the probability is a little different as in the code.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA xxxx2;&lt;BR /&gt;&amp;nbsp; SET final_sim_data;&lt;BR /&gt;&amp;nbsp; estimate=3.99793;&lt;BR /&gt;&amp;nbsp; IF x1~=1 or x2~=1 or x3~=1 then DELETE;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data pred; 
set xxxx2;
  array v[11];
  array n[11];
  array sf[11];
  array t[11];
  array f[11];
  array a[11];
  array w[11];
  do i = 1 to 11;
	n[i] = i + 14;
   end;
  do i = 1 to 11;
	sf[i] = i + 44;
   end;
  do i = 1 to 11;
     v[i] = i + 54;
   end;
  do i = 1 to 11;
     t[i] = i + 24;
   end;
  do i = 1 to 11;
     f[i] = i + 39;
   end;
  do i = 1 to 11;
     a[i] = i - 1;
   end;
  do i = 1 to 11;
     w[i] = i + 6;
   end;
do count_y= 0 to 80;
if  count_y ~= 5 and count_y ~=12 and count_y ~=20 and count_y ~=30 and count_y ~=45 and count_y ~=50 and count_y ~=60 then 
probability_FP= pdf("poisson",count_y,exp(estimate));
else if count_y= 20 then do;
    p = 0;
     do i = 1 to 6;
      p = p + ((n[i]-15)/5)*(pdf("poisson",n[i],exp(estimate)));
     end;
     do i = 7 to 11;
      p = p + ((25-n[i])/5)*(pdf("poisson",n[i],exp(estimate)));
     end;
     probability_FP=p;
    end;
else if count_y= 60 then do;
     sum = 0;
     do i = 1 to 6;
      sum = sum + ((v[i]-55)/5)*(pdf("poisson",v[i],exp(estimate)));
     end;
     do i = 7 to 11;
      sum = sum + ((65-v[i])/5)*(pdf("poisson",v[i],exp(estimate)));
     end;
     probability_FP=sum;
  end;
else if count_y= 30 then do;
    th = 0;
     do i = 1 to 6;
      th = th + ((t[i]-25)/5)*(pdf("poisson",t[i],exp(estimate)));
     end;
     do i = 7 to 11;
      th = th + ((35-t[i])/5)*(pdf("poisson",t[i],exp(estimate)));
     end;
     probability_FP=th;
    end;
else if count_y= 50 then do;
    sd = 0;
     do i = 1 to 6;
      sd = sd + ((sf[i]-45)/5)*(pdf("poisson",sf[i],exp(estimate)));
     end;
     do i = 7 to 11;
      sd = sd + ((55-sf[i])/5)*(pdf("poisson",sf[i],exp(estimate)));
     end;
     probability_FP=sd;
    end;
else if count_y= 12 then do;
    bb = 0;
     do i = 1 to 6;
      bb = bb + ((w[i]-7)/5)*(pdf("poisson",w[i],exp(estimate)));
     end;
     do i = 7 to 11;
      bb = bb + ((17-w[i])/5)*(pdf("poisson",w[i],exp(estimate)));
     end;
     probability_FP=bb;
    end;
else if count_y= 5 then do;
    aa = 0;
     do i = 1 to 6;
      aa = aa + ((a[i]-0)/5)*(pdf("poisson",a[i],exp(estimate)));
     end;
     do i = 7 to 11;
      aa = aa + ((10-a[i])/5)*(pdf("poisson",a[i],exp(estimate)));
     end;
     probability_FP=aa;
    end;
else if count_y= 45 then do;
    fr = 0;
     do i = 1 to 6;
      fr = fr + ((f[i]-40)/5)*(pdf("poisson",f[i],exp(estimate)));
     end;
     do i = 7 to 11;
      fr = fr + ((50-f[i])/5)*(pdf("poisson",f[i],exp(estimate)));
     end;
     probability_FP=fr;
    end;
output; 
end;
keep count_y probability_FP;
run;



/********************/
proc means data=pred;
class count_y;
output out=means mean(probability_FP)=;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1. I am sure the code is so manual (I mean it could be shortened) especially because I am averaging the same value in the last proc code. Is there a way I can shorten everything so that I avoid the proc means code?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2. After getting the probabilities for count_y = 0,1,2...80. I notice that the probabilities are not summing to 1. What could be the problem, and Is there a way out.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Dec 2018 13:44:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/My-poisson-probabilities-are-not-summing-up-to-1/m-p/520359#M141080</guid>
      <dc:creator>john1111</dc:creator>
      <dc:date>2018-12-11T13:44:07Z</dc:date>
    </item>
    <item>
      <title>Re: My poisson probabilities are not summing up to 1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/My-poisson-probabilities-are-not-summing-up-to-1/m-p/520364#M141082</link>
      <description>&lt;P&gt;Well, a brief look at the code, and I can see a lot which can be narrowed down.&amp;nbsp; Then first set of loops for instance, can be replaced with;&lt;/P&gt;
&lt;PRE&gt;do i = 1 to 11;
  n[i] = i + 14;
  sf[i] = i + 44;
  v[i] = i + 54;
  t[i] = i + 24;
  f[i] = i + 39;
  a[i] = i - 1;
  w[i] = i + 6;
end;
&lt;/PRE&gt;
&lt;P&gt;The if could be resolved to:&lt;/P&gt;
&lt;PRE&gt;select(county);
  when (20) do;
    ...;
    end;
  when (60) do;
    ...;
    end;
  otherwise probability_fp=pdf(...);
end;&lt;/PRE&gt;
&lt;P&gt;The do loop blocks within the if statements are repeating.&amp;nbsp; Put your values in another array, then do the loop once, e.g:&lt;/P&gt;
&lt;PRE&gt;array vals{5,2} (15,25,
                          55,65,
                           ...);        /* First column is lower, second upper */
array p_res{5,2} 8.;         /* Array of results for p based

select(county);
  when (20) do;
    p=0;
    do i=1 to 6;
      p{i,1}=p{i,1}+((n{i}-vals{i,1})/5)*pdf...;
    end;
    do i=1 to 7;
      p{i,1}=p{i,1}+((vals{i,2}-n{i})/5)*pdf...;
    end;
   end;
...
&lt;/PRE&gt;
&lt;P&gt;Do note however that a simple change to your data structure will simplify your coding a lot.&amp;nbsp; Have one row per set of tests, so all v is on one row, all n on another etc.&amp;nbsp; This will simplify down to be one array, for each observation.&amp;nbsp; Also note I have nothing to work with in terms of test data in the form of a datastep, or what its supposed to do, so just generaralising.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't know about he stat question.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Dec 2018 13:56:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/My-poisson-probabilities-are-not-summing-up-to-1/m-p/520364#M141082</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-12-11T13:56:19Z</dc:date>
    </item>
    <item>
      <title>Re: My poisson probabilities are not summing up to 1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/My-poisson-probabilities-are-not-summing-up-to-1/m-p/520391#M141096</link>
      <description>&lt;P&gt;I just realized that there is no need for setting any data and therefore there is no need for the data;&lt;/P&gt;&lt;P&gt;I have also set a specific estimate value as below;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data pred; 
  array v[11];
  array n[11];
  array sf[11];
  array t[11];
  array f[11];
  array a[11];
  array w[11];
  do i = 1 to 11;
	n[i] = i + 14;
   end;
  do i = 1 to 11;
	sf[i] = i + 44;
   end;
  do i = 1 to 11;
     v[i] = i + 54;
   end;
  do i = 1 to 11;
     t[i] = i + 24;
   end;
  do i = 1 to 11;
     f[i] = i + 39;
   end;
  do i = 1 to 11;
     a[i] = i - 1;
   end;
  do i = 1 to 11;
     w[i] = i + 6;
   end;
do count_y= 0 to 80;
if  count_y ~= 5 and count_y ~=12 and count_y ~=20 and count_y ~=30 and count_y ~=45 and count_y ~=50 and count_y ~=60 then 
probability_FP= pdf("poisson",count_y,exp(3.99793));
else if count_y= 20 then do;
    p = 0;
     do i = 1 to 6;
      p = p + ((n[i]-15)/5)*(pdf("poisson",n[i],exp(3.99793)));
     end;
     do i = 7 to 11;
      p = p + ((25-n[i])/5)*(pdf("poisson",n[i],exp(3.99793)));
     end;
     probability_FP=p;
    end;
else if count_y= 60 then do;
     sum = 0;
     do i = 1 to 6;
      sum = sum + ((v[i]-55)/5)*(pdf("poisson",v[i],exp(3.99793)));
     end;
     do i = 7 to 11;
      sum = sum + ((65-v[i])/5)*(pdf("poisson",v[i],exp(3.99793)));
     end;
     probability_FP=sum;
  end;
else if count_y= 30 then do;
    th = 0;
     do i = 1 to 6;
      th = th + ((t[i]-25)/5)*(pdf("poisson",t[i],exp(3.99793)));
     end;
     do i = 7 to 11;
      th = th + ((35-t[i])/5)*(pdf("poisson",t[i],exp(3.99793)));
     end;
     probability_FP=th;
    end;
else if count_y= 50 then do;
    sd = 0;
     do i = 1 to 6;
      sd = sd + ((sf[i]-45)/5)*(pdf("poisson",sf[i],exp(3.99793)));
     end;
     do i = 7 to 11;
      sd = sd + ((55-sf[i])/5)*(pdf("poisson",sf[i],exp(3.99793)));
     end;
     probability_FP=sd;
    end;
else if count_y= 12 then do;
    bb = 0;
     do i = 1 to 6;
      bb = bb + ((w[i]-7)/5)*(pdf("poisson",w[i],exp(3.99793)));
     end;
     do i = 7 to 11;
      bb = bb + ((17-w[i])/5)*(pdf("poisson",w[i],exp(3.99793)));
     end;
     probability_FP=bb;
    end;
else if count_y= 5 then do;
    aa = 0;
     do i = 1 to 6;
      aa = aa + ((a[i]-0)/5)*(pdf("poisson",a[i],exp(3.99793)));
     end;
     do i = 7 to 11;
      aa = aa + ((10-a[i])/5)*(pdf("poisson",a[i],exp(3.99793)));
     end;
     probability_FP=aa;
    end;
else if count_y= 45 then do;
    fr = 0;
     do i = 1 to 6;
      fr = fr + ((f[i]-40)/5)*(pdf("poisson",f[i],exp(3.99793)));
     end;
     do i = 7 to 11;
      fr = fr + ((50-f[i])/5)*(pdf("poisson",f[i],exp(3.99793)));
     end;
     probability_FP=fr;
    end;
output; 
end;
keep count_y probability_FP;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;So the idea is that I should get probabilities from 0 to 80 but at points (5,12,20,30,45,50,60) the probability is computed as below;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As an example for 20 (estimate in the code is the theta below);&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="tdsfggesrt.PNG" style="width: 403px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/25555i0030582A0A7B478D/image-size/large?v=v2&amp;amp;px=999" role="button" title="tdsfggesrt.PNG" alt="tdsfggesrt.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I applied the same idea to&amp;nbsp;&lt;SPAN&gt;5,12,30,45,50,60 as shown in the code&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Dec 2018 15:12:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/My-poisson-probabilities-are-not-summing-up-to-1/m-p/520391#M141096</guid>
      <dc:creator>john1111</dc:creator>
      <dc:date>2018-12-11T15:12:08Z</dc:date>
    </item>
    <item>
      <title>Re: My poisson probabilities are not summing up to 1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/My-poisson-probabilities-are-not-summing-up-to-1/m-p/520437#M141121</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/164166"&gt;@john1111&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You received good advice from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt;&amp;nbsp;as to how you could simplify your code. It turns out that dataset PRED (the new version without redundant duplicate observations) can be created with this much shorter code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data pred; 
set xxxx2(obs=1 keep=estimate);
do count_y=0 to 80;
  if count_y in (5, 12, 20, 30, 45, 50, 60) then do;
    probability_FP=0;
    do i=-4 to 4;
      probability_FP+(5-abs(i))/5*pdf("poisson",count_y+i,exp(estimate));
    end;
  end;
  else probability_FP=pdf("poisson",count_y,exp(estimate));
  output; 
end;
keep count_y probability_FP;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that I do use the SET statement from your first approach (in order to avoid hardcoding the value of ESTIMATE), but enhanced with the appropriate dataset options so that only &lt;SPAN&gt;variable ESTIMATE from&amp;nbsp;&lt;/SPAN&gt;&lt;EM&gt;one&lt;/EM&gt; observation&amp;nbsp;of&amp;nbsp;dataset XXXX2 is used.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now it's fairly obvious why the probabilities are not summing up to 1: For the "exceptional" COUNT_Y values (5, 12, etc.) the probabilities that would have been used otherwise (see the case &lt;FONT face="courier new,courier"&gt;i=0&lt;/FONT&gt; in my code!) are substantially increased by eight additional terms (see cases &lt;FONT face="courier new,courier"&gt;i=-4, ..., -1, 1, ..., 4&lt;/FONT&gt;). For example,&amp;nbsp;for COUNT_Y=50 the calculation amounts to:&lt;/P&gt;
&lt;PRE&gt;data _null_;
estimate=3.99793;
x&lt;FONT color="#FF0000"&gt;+0.2*pdf("poisson",46,exp(estimate))
 +0.4*pdf("poisson",47,exp(estimate))
 +0.6*pdf("poisson",48,exp(estimate))
 +0.8*pdf("poisson",49,exp(estimate))&lt;/FONT&gt;
 +&lt;STRONG&gt;&lt;FONT color="#008000"&gt;1.0*pdf("poisson",50,exp(estimate))&lt;/FONT&gt;&lt;/STRONG&gt;
 &lt;FONT color="#FF0000"&gt;+0.8*pdf("poisson",51,exp(estimate))
 +0.6*pdf("poisson",52,exp(estimate))
 +0.4*pdf("poisson",53,exp(estimate))
 +0.2*pdf("poisson",54,exp(estimate))&lt;/FONT&gt;;
put x=;
run;&lt;/PRE&gt;
&lt;P&gt;The result is approximately 0.2266, which replaces&amp;nbsp;&lt;FONT face="courier new,courier"&gt;pdf("poisson",50,exp(&lt;SPAN&gt;3.99793&lt;/SPAN&gt;))&lt;/FONT&gt;=0.0465... in the overall sum, which would be&amp;nbsp;&lt;FONT face="courier new,courier"&gt;cdf('poisson',80,exp(3.99793))=&lt;/FONT&gt;0.999532... &lt;EM&gt;without&lt;/EM&gt; the exceptions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Look at the bar plot created by the code below to see what happened:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sgplot data=pred;
vbar count_y / response=probability_FP;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 11 Dec 2018 16:52:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/My-poisson-probabilities-are-not-summing-up-to-1/m-p/520437#M141121</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-12-11T16:52:03Z</dc:date>
    </item>
  </channel>
</rss>

