Commit b427e83
BUG: fix negative samples generated by Wald distribution (#29609)
In numpy.random, when generating Wald samples, if the mean and scale
have a large discrepancy, the current implementation suffers from
catastrophic cancellation. An explicit example of this has been added
to test_generator_mt19937.py. The key line implicated in distributions.c:
`X = mean + mu_2l * (Y - sqrt(4 * scale * Y + Y * Y));`
which has numerical issues when Y >> scale.
I have replaced this with the equivalent rationalized form:
```
d = Y + sqrt(Y) * sqrt(Y + 4 * scale);
X = mean - (2 * mean * Y) / d;
```
And now the test passes.1 parent 4491392 commit b427e83
2 files changed
Lines changed: 8 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
845 | 845 | | |
846 | 846 | | |
847 | 847 | | |
848 | | - | |
| 848 | + | |
849 | 849 | | |
850 | | - | |
851 | 850 | | |
852 | 851 | | |
853 | | - | |
| 852 | + | |
| 853 | + | |
854 | 854 | | |
855 | 855 | | |
856 | 856 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1858 | 1858 | | |
1859 | 1859 | | |
1860 | 1860 | | |
| 1861 | + | |
| 1862 | + | |
| 1863 | + | |
| 1864 | + | |
| 1865 | + | |
1861 | 1866 | | |
1862 | 1867 | | |
1863 | 1868 | | |
| |||
0 commit comments