-
Notifications
You must be signed in to change notification settings - Fork 892
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Distributions.StudentT.Density returns NaN for large degrees of freedom #44
Comments
Thanks for reporting. Looks like we should go logarithmic on large DoF. In the meantime, a quick workaround would be to use |
I changed the computation for the ratios of the Gamma functions. This should fix the problem. (Problem arises because Gamma(339)=infinity and so the output becomes infinity). Try
|
Thanks a lot for the quick fix. I wonder, how does this compare numeric stability and accuracy-wise with just computing the gamma ratio in logarithmic space, i.e. replace
with
? Also note that DoF > 0.0, so the special casing for zero-division and zero-gamma in the fix should not actually be needed. |
Thanks for pointing out. I have looked at it again and the recurrence implementation is not as good as the logarithmic space one, so please ignore it and sorry about that. I have looked at the values of the density for the above parameters (Location=1 and Scale=1). At Dof=10000, they differ at an order of 1e-13, and at Dof=100000, they differ at about 1e-11. I was thinking about the differences of large numbers when using Math.Exp(SpecialFunctions.GammaLn((_dof + 1.0) / 2.0) - SpecialFunctions.GammaLn(_dof / 2.0)) Looking at the logarithm of Gamma functions, at 1e10, it is about 220258509288.811, so at this level, you still retain accuracy up to 1e-3 and the value of the density differs from the Gaussian density at the order of 1e-7. So this does not seem to be too much of an issue. On the other hand, the recurrence implementation has accumulative error and is slower than the computation of gamma ration in logarithmic space. So please ignore the recurrence implementation. The logarithmic space implementation do start to differ from the Gaussian limit at about Dof=1e11 and at Dof=1e15 gives a result that is rather wrong. While it is rather unlikely that such large Dof will be encountered, it may be a good idea to use a cutoff that is smaller than positive infinity (e.g. 1e10?) where the Gaussian density will be used when Dof is greater than the cut off? |
Thanks for the thorough analysis! Yes, switching to Gaussian density for large DoF would be very reasonable. |
Distributions.StudentT.Density returns NaN for large degrees of freedom. For examples
The StudentT distribution is well defined for all degrees of freedom, so this is a bug. However, for large degrees of freedom it converges to a normal distribution, so there's a workaround:
http://numerics.mathdotnet.com/api/MathNet.Numerics.Distributions/StudentT.htm
The text was updated successfully, but these errors were encountered: