I have the following function in Julia which calculates a certain probability.
function p(x, n)
q = floor(Int, (n - 1) / 2)
pr = 0
for i in 1:q
pr += (-1) ^ (i - 1) * binomial(q, q - i) * max(1 - i * x / q, 0) ^ (q - 1)
end
return pr
end
However, it seems that there is a problem when x
is close to 0. The function returns values that are greater than 1 when x
is close to 0. For example, the result of p(.1, 100)
is 1.0225245327404304
but the value should never be greater than 1 since this value is a probability. Is this caused by computation errors? If yes, which part of the function causes this and how could I create a function that calculates the probability correctly?
Any help is much appreciated!