[
https://issues.apache.org/jira/browse/STATISTICS-25?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17433133#comment-17433133
]
Alex Herbert commented on STATISTICS-25:
----------------------------------------
This ticket was raised as the CDF of the t-distribution was incorrect at large
degrees of freedom. However it also relates to the pdf. I computed the relative
error between the t-distribution and the normal distribution using:
{noformat}
relative error = |a - b| / max(|a|, |b|)
{noformat}
The following shows output from degrees of freedom ranging from 1e3 to 1e18:
||DF||z||error cdf(z)||error pdf(z)||
|1.0e+03|-3.000|0.024185|0.015281|
|1.0e+03|-2.000|0.0059008|0.0017418|
|1.0e+03|-1.000|0.00076180|0.00049979|
|1.0e+03|-0.500|0.00017823|0.00035930|
|1.0e+03|-0.125|2.7898e-05|0.00025772|
|1.0e+03|-0.025|5.0892e-06|0.00025028|
|1.0e+04|-3.000|0.0024579|0.0015478|
|1.0e+04|-2.000|0.00059298|0.00017492|
|1.0e+04|-1.000|7.6249e-05|4.9998e-05|
|1.0e+04|-0.500|1.7829e-05|3.5937e-05|
|1.0e+04|-0.125|2.7902e-06|2.5775e-05|
|1.0e+04|-0.025|5.0898e-07|2.5031e-05|
|1.0e+05|-3.000|0.00024619|0.00015498|
|1.0e+05|-2.000|5.9327e-05|1.7499e-05|
|1.0e+05|-1.000|7.6256e-06|5.0000e-06|
|1.0e+05|-0.500|1.7829e-06|3.5937e-06|
|1.0e+05|-0.125|2.7902e-07|2.5775e-06|
|1.0e+05|-0.025|5.0898e-08|2.5031e-06|
|1.0e+06|-3.000|2.4623e-05|1.5500e-05|
|1.0e+06|-2.000|5.9327e-06|1.7502e-06|
|1.0e+06|-1.000|7.6257e-07|4.9979e-07|
|1.0e+06|-0.500|1.7829e-07|3.5917e-07|
|1.0e+06|-0.125|2.7902e-08|2.5754e-07|
|1.0e+06|-0.025|5.0899e-09|2.5011e-07|
|1.0e+07|-3.000|2.4598e-06|1.5608e-06|
|1.0e+07|-2.000|5.9079e-07|1.8583e-07|
|1.0e+07|-1.000|7.6257e-08|3.9168e-08|
|1.0e+07|-0.500|1.7829e-08|2.5106e-08|
|1.0e+07|-0.125|2.7902e-09|1.4943e-08|
|1.0e+07|-0.025|5.0899e-10|1.4199e-08|
|1.0e+08|-3.000|2.3811e-07|5.3925e-08|
|1.0e+08|-2.000|1.2208e-08|8.3575e-08|
|1.0e+08|-1.000|7.6257e-09|1.0608e-07|
|1.0e+08|-0.500|1.7829e-09|1.0467e-07|
|1.0e+08|-0.125|2.7902e-10|1.0365e-07|
|1.0e+08|-0.025|5.0898e-11|1.0358e-07|
|1.0e+09|-3.000|2.4630e-07|8.6585e-07|
|1.0e+09|-2.000|5.3129e-07|8.5210e-07|
|1.0e+09|-1.000|7.6257e-10|8.4985e-07|
|1.0e+09|-0.500|1.7829e-10|8.4999e-07|
|1.0e+09|-0.125|2.7902e-11|8.5009e-07|
|1.0e+09|-0.025|5.0897e-12|8.5010e-07|
|1.0e+10|-3.000|1.0595e-06|6.7777e-06|
|1.0e+10|-2.000|3.1133e-06|6.7791e-06|
|1.0e+10|-1.000|7.6258e-11|6.7793e-06|
|1.0e+10|-0.500|1.7830e-11|6.7793e-06|
|1.0e+10|-0.125|2.7906e-12|6.7793e-06|
|1.0e+10|-0.025|5.0897e-13|6.7793e-06|
|1.0e+11|-3.000|8.0541e-06|0.00023733|
|1.0e+11|-2.000|2.4682e-05|0.00023733|
|1.0e+11|-1.000|7.6251e-12|0.00023733|
|1.0e+11|-0.500|1.7828e-12|0.00023733|
|1.0e+11|-0.125|2.7912e-13|0.00023733|
|1.0e+11|-0.025|5.0863e-14|0.00023733|
|1.0e+12|-3.000|7.3514e-05|0.0029322|
|1.0e+12|-2.000|0.00029632|0.0029322|
|1.0e+12|-1.000|7.6310e-13|0.0029322|
|1.0e+12|-0.500|1.7884e-13|0.0029322|
|1.0e+12|-0.125|2.8109e-14|0.0029322|
|1.0e+12|-0.025|5.0977e-15|0.0029322|
|1.0e+13|-3.000|0.00043581|0.018390|
|1.0e+13|-2.000|0.0065069|0.018390|
|1.0e+13|-1.000|7.8724e-14|0.018390|
|1.0e+13|-0.500|1.8531e-14|0.018390|
|1.0e+13|-0.125|3.0822e-15|0.018390|
|1.0e+13|-0.025|5.6641e-16|0.018390|
|1.0e+14|-3.000|0.014899|0.077863|
|1.0e+14|-2.000|0.048815|0.077863|
|1.0e+14|-1.000|3.1490e-15|0.077863|
|1.0e+14|-0.500|3.5983e-15|0.077863|
|1.0e+14|-0.125|3.6986e-16|0.077863|
|1.0e+14|-0.025|0.0000|0.077863|
|1.0e+15|-3.000|0.011126|0.60106|
|1.0e+15|-2.000|0.027197|0.60106|
|1.0e+15|-1.000|5.9481e-15|0.60106|
|1.0e+15|-0.500|2.8787e-15|0.60106|
|1.0e+15|-0.125|6.1643e-16|0.60106|
|1.0e+15|-0.025|1.1328e-16|0.60106|
|1.0e+16|-3.000|0.47494|1.0000|
|1.0e+16|-2.000|0.27731|1.0000|
|1.0e+16|-1.000|7.6975e-15|1.0000|
|1.0e+16|-0.500|8.9959e-16|1.0000|
|1.0e+16|-0.125|2.4657e-16|1.0000|
|1.0e+16|-0.025|0.0000|1.0000|
|1.0e+17|-3.000|0.65558|0.60106|
|1.0e+17|-2.000|0.95450|0.60106|
|1.0e+17|-1.000|0.68269|0.60106|
|1.0e+17|-0.500|0.38292|0.60106|
|1.0e+17|-0.125|0.099476|0.60106|
|1.0e+17|-0.025|0.019945|0.60106|
|1.0e+18|-3.000|0.99730|0.60106|
|1.0e+18|-2.000|0.95450|0.60106|
|1.0e+18|-1.000|0.68269|0.60106|
|1.0e+18|-0.500|0.38292|0.60106|
|1.0e+18|-0.125|0.099476|0.60106|
|1.0e+18|-0.025|0.019945|0.60106|
The functions should get closer as df increases. This happens until about 1e6
to 1e7. This is the threshold previously chosen for the switch to use the
normal for the CDF. After this the relative error fluctuates, particularly for
the pdf. Then at about 1e11 degrees of freedom the density error increases.
Here is the variance for the different DF:
{noformat}
var = df / (df -2)
df=1.0e+03 var=1.002004008016032
df=1.0e+04 var=1.0002000400080016
df=1.0e+05 var=1.000020000400008
df=1.0e+06 var=1.000002000004
df=1.0e+07 var=1.00000020000004
df=1.0e+08 var=1.0000000200000003
df=1.0e+09 var=1.000000002
df=1.0e+10 var=1.0000000002
df=1.0e+11 var=1.00000000002
df=1.0e+12 var=1.000000000002
df=1.0e+13 var=1.0000000000002
df=1.0e+14 var=1.00000000000002
df=1.0e+15 var=1.000000000000002
df=1.0e+16 var=1.0000000000000002
df=1.0e+17 var=1.0
df=1.0e+18 var=1.0
{noformat}
I have implemented a switch to the normal distribution when the variance
computes as 1. In this case the degrees of freedom are effectively infinity and
the t-distribution matches the normal distribution. This threshold for a 53-bit
mantissa of a double is at 2^55 = 3.6e16.
However it may be sensible to switch to a normal approximation at a lower level
due to instability in the computation of the pdf and cdf. This requires further
investigation. It may be possible to improve the computations to support
degrees of freedom closer to the machine limit imposed by a double.
Changes in commit:
481e922f44e72c4b5c4d4d991326077ad87a51d7
> T Distribution Inverse Cumulative Probability Function gives the Wrong Answer
> -----------------------------------------------------------------------------
>
> Key: STATISTICS-25
> URL: https://issues.apache.org/jira/browse/STATISTICS-25
> Project: Apache Commons Statistics
> Issue Type: Bug
> Reporter: Andreas Stefik
> Priority: Major
>
> Hi There,
> Given code like this:
>
> import org.apache.commons.math3.analysis.UnivariateFunction;
> import org.apache.commons.math3.analysis.solvers.BrentSolver;
> import org.apache.commons.math3.distribution.TDistribution;
> public class Main {
> public static void main(String[] args) {
> double df = 1E38;
> double t = 0.975;
> TDistribution dist = new TDistribution(df);
>
> double prob = dist.inverseCumulativeProbability(1.0 - t);
>
> System.out.println("Prob: " + prob);
> }
> }
>
> It is possible I am misunderstanding, but that seems equivalent to:
>
> scipy.stats.t.cdf(1.0 - 0.975, 1e38)
>
> In Python. They give different answers. Python gives 0.509972518193, which
> seems correct, whereas Apache Commons gives Prob: -6.462184036284304E-10.
> That's a huge difference.
> My hunch is that as you get closer to infinity it begins to fail, but I
> haven't checked carefully. For calls with much smaller degrees of freedom,
> you get answers that are basically the same as Python or online calculators.
>
--
This message was sent by Atlassian Jira
(v8.3.4#803005)