-
Notifications
You must be signed in to change notification settings - Fork 2k
Bug in GammaDistribution.ProbabilityDensityFunction #304
Comments
Hi Burkhardus, Thank you a lot for the detailed report! The problem is happening because the Gamma distribution is too close to being generate. In the init method of the GammaDistribution, the distribution normalization constant was being computed for the non-logarithm case using direct multiplication of really large numbers, which was bringing Infinity into the calculations and leading to a NaN at the end. The same problem will happen in the PDF function. A possible workaround for this problem is to use LogProbabilityDensityFunction instead of ProbabilityDensityFunction, then apply Math.Exp() to the generated values. I haven't tested this yet, but it should work. In any case, I will commit a fix in the next few minutes. Regards, |
Hello Cesar, have you tested if the Probability Density Function is delivering exactly the right results, on the values of my dataset2? I am expecting to get the same results like shown in the picture of my report using the online tool of keisan.casio.com which creates the PDF with exactly the same k and theta parameters, that your fitting function found and which are verified by another external tool. See here: I am asking, because I could not finish the test successfully. In my test, the y values are all the way zero. I dont know how the guys at the web site of keisan.casio.com are doing their calculations (maybe at higher accuracy and bigger data types, so they can deal with very small and very large values), could you please do the same in your library? |
Hi Burkhardus, Thanks for the remarks! This is the reason the framework offers the LogProbabilityDensity(x) and similar functions. While exp(-600) cannot be represented using System.Double, its logarithm can. In this case, the solution is to keep the data as logarithms until the very end when you need to plot it. When you finally have to plot, first convert the samples to the log-space of your plot coordinates (which will put their exponential version into the normal range of System.Double) and then finally convert them with Math.Exp only after they have been normalized. This is the traditional approach used in many libraries, including R and Python (that's the reason they include methods for computing log-probabilities in methods such as R's pgamma and Scipy's logpdf. I will work out an example on how this can be done, but unfortunately I can't do it right now. But if you can, please try to keep the values as logarithms until the time you actually need to use them. If you have to compare the values against others, first see if it is possible to transform the others to the log-space instead of the ones you have into normal space. Regards, |
Hi Burkhardus, So, checking for the example you posted, it is not even necessary to go much further in the log route. Please consider the following example. It will give you the needle at 1 that you mentioned, reproducing the values at the Casio website:
Regards, |
Hi Cesar, yes after reading your pre-previous comment, I found it yesterday night, that I only need to use the log probability function and apply the exp function to its results. I wonder why it's so easy! Why not hiding this all from the user and always do this internally inside the framework? Thank you anyway for your efforts, I was not clear about this log probability function approach also in other languages like the mentioned "R" which is common in the field of statistics. |
On the given dataset2, there is no density function calculated. Please have a look at my test program.
External fit test to show that the parameter fitted by Accord are right for both data sets.
The Probability Density Function (PDF) produces valid results on Dataset 1.
The PDF produces invalid results on Dataset 2.
The PDF from inside with invalid result on the given k and theta parameter.
Here is why the calculation is not working:
But on external website it is possible to calculate a density function with the same k and theta parameter:
The text was updated successfully, but these errors were encountered: