-
Notifications
You must be signed in to change notification settings - Fork 4
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
BUG: density function returns NaN for certain values of the parameters. #80
Comments
Hi @martinjankowiak, thank you for the bug report. Im glad to hear you have found the library useful for sampling. This is definitely unexpected behaviour. From my quick analysis, it appears that the implementation breaks down for sufficiently large values of the scale parameter Line 117 in da31ac8
Here is the output of the line when using the input you provided:
The sum eventually vanishes after many iterations. It doesn't help that there is a division by As a quick workaround, I wrote a function that calculates the logpdf using normal approximation. Since import math
def polyagamma_logpdf_large_h(x, h=1, z=0):
if z == 0:
mean = 0.25 * h
var = 0.041666688 * h
else:
x = math.tanh(0.5 * z)
mean = 0.5 * h * x / z
var = 0.25 * h * (math.sinh(z) - z) * (1 - x * x) / (z ** 3)
return -math.log(math.sqrt(var)) - 0.5 * math.log(2 * math.pi) - 0.5 * (x - mean) ** 2 / var and using it with the input,we get: >>> polyagamma_logpdf_large_h(25.80653973, h=100.0)
-1.7105576873858082
>>> polyagamma_logpdf_large_h(25.10653973, h=100.0)
-1.6338590520155094 Below is the pdf plot of the above normal approximation vs the pdf as it stands in the main branch for large scale parameter values and I will have to investigate the real cause of numerical instability because I am sure it happens with other combinations of ( |
@martinjankowiak I submitted a re-write at #81 that addresses part of the issue and ensures nans are not returned. However the instability still persists for large Do you have suggestions on how to address this? The implementation seems to break down at around x>=20 for very small values of |
@zoj613 thanks for taking a closer look at this! for my present use case i can actually compute the necessary MH ratio without explicitly evaluating the density. so i myself don't have a pressing need for this tricky regime at present. i'm not sure how best to address the remaining instabilities as i haven't stared at the necessary formulae long enough. for what it's worth when i implemented a somewhat rough approximation to PG(1, 0) in pyro i found it helpful to group even and odd terms together. i have no idea if that would be helpful here. i guess one place to look for ideas might be generic references about summing alternating series. |
I considered a saddle point approximation of the density as a viable solution for large values of
When computing the saddle point approximation, we have to solve for |
hello @zoj613 thanks for the great library! i've been able to use it for sampling with great ease. recently i've been looking at using log pdfs to compute MH ratios and the like. i am encountering nans in strange places. for example:
yields
have you encountered anything like this? is the non-log version more stable and better tested?
i'm using
pip install --pre -U polyagamma
and thus version1.3.1
.incidentally, would it be possible to support some sort of precision argument to the pdf methods so that users can make trade offs between speed and accuracy? or otherwise just dictate a fixed number of terms in the series to use? i'm not sure if that can help with parallelization....
The text was updated successfully, but these errors were encountered: