Truncated chi-square returning NaNs

I’m trying to sample from a truncated chi-square distribution, defined over [0, a], by using the following line of code.

rand(Truncated(Chisq(n), 0, a))

This does alright most of the time, but sometimes it returns NaNs, for example with n = 1595 and a = 250. Interestingly enough, it returns valid values for smaller values of a (e.g. a = 5).

Is there anyway around this behavior? What is causing it?

1 Like