Ok, I used the implementation in the example and it is gives the right answer:
And then I realized I made a stupid mistake. I used tt.sqr
instead of tt.sqrt
. Gosh. The results I got with my implementation are the same. Good!
I actually prefer my implementation, beacuse it is straight from the standard notation, that one can find on Wikipedia. Therefore one can apply this method on a range of different distributions (in fact, now I will try to apply it on a inverse normal distribution)
Anyway, I got interested in the implementation you suggested. I do not understand the code in the switch statement
return tt.switch(
tt.lt(z, -1.0),
tt.log(tt.erfcx(-z / tt.sqrt(2.)) / 2.) - tt.sqr(z) / 2.,
tt.log1p(-tt.erfc(z / tt.sqrt(2.)) / 2.)
)
from Wikipedia: let z=\frac{x-\mu}{\sigma}, then the CDF F(x)=\Phi(z)=0.5\left[ 1 + erf\left( \frac{z}{\sqrt{2}} \right) \right] = 0.5 * erfc\left( -\frac{z}{\sqrt{2}}\right). In code that should be the last line of the switch statement:
tt.log1p(-tt.erfc(z / tt.sqrt(2.)) / 2.)
I do not understand why we are using a switch statement based on the value of z though. And what the second line of the switch statement means
tt.log(tt.erfcx(-z / tt.sqrt(2.)) / 2.) - tt.sqr(z) / 2.