<?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: betainv function not working in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/betainv-function-not-working/m-p/461364#M4172</link>
    <description>&lt;P&gt;You might try using the QUANTILE function instead&lt;/P&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;quantile(&lt;/FONT&gt;&lt;FONT color="#800080" face="SAS Monospace" size="2"&gt;'beta'&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt;,&lt;/FONT&gt;&lt;FONT color="#008080" face="SAS Monospace" size="2"&gt;0.57&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt;,alpha,beta);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;There might be an internal precision issue in the betainv function algorithm.&lt;/FONT&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 10 May 2018 16:35:19 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2018-05-10T16:35:19Z</dc:date>
    <item>
      <title>betainv function not working</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/betainv-function-not-working/m-p/461346#M4171</link>
      <description>&lt;P&gt;The following code shows my problem. The betainv() function will not work for a certain range of probabilities. If I run the code through the section below titled "/*works*/", the code runs fine. But if I change the probability from 0.58 to 0.59, it does not work. Excel does the calculation just fine (see pic attached below). And I can't think of any mathematical reason why SAS would be failing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;

mean  = 0.975;
sdev  = 0.10;
alpha = (mean**2)*(1-mean)/(sdev**2)-mean;
beta  = (alpha)*(1-mean)/mean;

/*works*/
output = betainv(0.58,alpha,beta);

/*does not work*/
output = betainv(0.57,alpha,beta);

quit;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The error i receive is that there's an invalid argument to the function.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Like i said, Excel does it just fine. For a range of probabilities 0.001 to 0.999, this is what i'm expecting SAS to give me.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="betainv.JPG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/20436i36BF83242488DFCC/image-size/large?v=v2&amp;amp;px=999" role="button" title="betainv.JPG" alt="betainv.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 10 May 2018 15:07:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/betainv-function-not-working/m-p/461346#M4171</guid>
      <dc:creator>stephenpayne</dc:creator>
      <dc:date>2018-05-10T15:07:34Z</dc:date>
    </item>
    <item>
      <title>Re: betainv function not working</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/betainv-function-not-working/m-p/461364#M4172</link>
      <description>&lt;P&gt;You might try using the QUANTILE function instead&lt;/P&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;quantile(&lt;/FONT&gt;&lt;FONT color="#800080" face="SAS Monospace" size="2"&gt;'beta'&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt;,&lt;/FONT&gt;&lt;FONT color="#008080" face="SAS Monospace" size="2"&gt;0.57&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt;,alpha,beta);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;There might be an internal precision issue in the betainv function algorithm.&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 10 May 2018 16:35:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/betainv-function-not-working/m-p/461364#M4172</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-05-10T16:35:19Z</dc:date>
    </item>
    <item>
      <title>Re: betainv function not working</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/betainv-function-not-working/m-p/461405#M4173</link>
      <description>&lt;P&gt;Thanks for the quick reply. Sadly, the QUANTILE function does not work for certain probabilities either. For example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;

mean  = 0.975;
sdev  = 0.10;
alpha = (mean**2)*(1-mean)/(sdev**2)-mean;
beta  = (alpha)*(1-mean)/mean;

invalid = quantile('beta',0.652,alpha,beta);

quit;

/*I found that 0.652 did not work by including this code and checking the error message:*/

output = j(1000,1,.);
do i = 1 to nrow(output) - 1;
	output[i] = quantile('beta',i/nrow(output),alpha,beta);
end;
print output;

quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Do you think QUANTILE is suffering from the same internal precision issue? If so, any other thoughts on an alternative method? I need a reliable function because the probability parameter will always be uniform random. I do not control it.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would be okay with a function equivalent to Excel's =iferror(calc,value if error), but SAS does not have one to my knowledge. The SAS function coalesce() returns the first non-missing value, but in this case it does not work, because it resolves as error and not missing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again for your help.&lt;/P&gt;</description>
      <pubDate>Thu, 10 May 2018 19:23:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/betainv-function-not-working/m-p/461405#M4173</guid>
      <dc:creator>stephenpayne</dc:creator>
      <dc:date>2018-05-10T19:23:35Z</dc:date>
    </item>
    <item>
      <title>Re: betainv function not working</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/betainv-function-not-working/m-p/461424#M4174</link>
      <description>&lt;P&gt;Quantile worked for your previous example but not this one so I think there may be an issue, possibly related to the precision in BETA. At least I can show not large changes in the value of beta yield invalid or valid:&lt;/P&gt;
&lt;P&gt;I have the first digits of alpha in beta for reference in the code. Then loop beta through a small range around that:&lt;/P&gt;
&lt;PRE&gt;data example;
   mean  = 0.975;
   sdev  = 0.10;
   alpha = (mean**2)*(1-mean)/(sdev**2)-mean;
   beta  = (alpha)*(1-mean)/mean;
   /*alpha=1.4015625 beta=0.0359375*/
   do b = 0.03593 to 0.0360 by .000001;
      invalid = quantile('beta',0.652,alpha,b);
      put b= f7.6 +1 invalid= f17.16;
   end;
run;&lt;/PRE&gt;
&lt;P&gt;I am going to go out on a limb and guess that an exponential somewhere runs out of significant digits.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 May 2018 15:02:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/betainv-function-not-working/m-p/461424#M4174</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-05-11T15:02:07Z</dc:date>
    </item>
    <item>
      <title>Re: betainv function not working</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/betainv-function-not-working/m-p/461510#M4175</link>
      <description>&lt;P&gt;I'm sorry you are having this problem. I am not sure what is going wrong so I have passed it on to the developer.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For the (alpha, beta) parameters in your problem, the beta PDF is highly singular at&amp;nbsp; x=1. In particular, the PDF at x=1 is undefined.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;The CDF is very, very, steep as&amp;nbsp;x --&amp;gt; 1.&amp;nbsp; Here is the graph:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
mean  = 0.975;  sdev  = 0.10;
alpha = (mean**2)*(1-mean)/(sdev**2)-mean;
beta  = (alpha)*(1-mean)/mean;
print alpha beta;

x = do(0, 1, 0.0001);
p = cdf("beta", x, alpha, beta);
title "Graph of CDF Function";&lt;BR /&gt;call series(x, p);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I suspect the reason that you can't compute the quantile for a large number such as 0.7 or 0.9 is that&amp;nbsp;the x value for those&amp;nbsp;probabilities is too close to 1. The root-finding algorithm to find the quantile is failing for these extreme parameter&amp;nbsp;values. Let's&amp;nbsp;use the&amp;nbsp;CDF function to compute the probabilities&amp;nbsp;for which the quantile is of the form&amp;nbsp;(1 - epsilon) for epsilon = 10^{-k}, k = 10, 11, ..., 16:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;g = 10##(-10 : -16);
p = cdf("beta", 1 - g, alpha, beta);
lbls = putn(g, "BEST5.");
print p[L="1 - 10^{-k}" F=8.6 c=lbls];&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In standard double-precision arithmetic, the smallest number that you can add/subtract from unity is about 2.2E-16 (this is called "machine epsilon"). You can see that after about p=0.7, the quantile function will not be able to compute x because it will be too close to 1.&amp;nbsp; In practice, this problem occurs for even smaller values of p because finding the quantile is a root-finding problem which typically involves trying to bracket the root (and thus work very close to 1).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Presumably,&amp;nbsp;you want to find a way to work around this issue.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. If you want to &lt;STRONG&gt;graph&lt;/STRONG&gt; the quantile function, just reverse the horizontal and vertical axes:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;title "Graph of Quantile Function";
call series(p, x);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;2. If you need to compute quantiles for other reasons, you can exploit a symmetry in the Beta distribution which enables you to always evalute&amp;nbsp;x values that are near 0 instead of x values near 1. The symmetry is&amp;nbsp;&lt;/P&gt;
&lt;P&gt;cdf("beta", x, alpha, beta) = 1 - cdf("beta", 1-x, beta, alpha)&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In terms of the quantile function, the x that satisfies&amp;nbsp;x = QUANTILE("beta", x, alpha, beta) also satisfies&lt;/P&gt;
&lt;P&gt;x = 1 - QUANTILE("beta", 1-p, beta, alpha)&lt;/P&gt;
&lt;P&gt;In this way you can always evaluate values of x &amp;lt;= 0.5. For example, if you are iterating over a sequence of probability values, you can branch according to whether the probability is less than or greater than 0.5, like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;p = do(0.01, 0.99, 0.01);
x = j(1, ncol(p), .);
do i = 1 to ncol(p);
   if p[i] &amp;lt;= 0.5 then 
      x[i] = quantile("beta", p[i], alpha, beta);
   else 
      x[i] = 1 - quantile("beta", 1-p[i], beta, alpha);
end;
call series(p, x);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 11 May 2018 10:16:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/betainv-function-not-working/m-p/461510#M4175</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2018-05-11T10:16:55Z</dc:date>
    </item>
  </channel>
</rss>

