Skip to content

Commit

Permalink
MAINT: stats.pearson3: fix ppf for negative skew (#17055)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdhaber committed Oct 3, 2022
1 parent fa3276a commit 942426a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
4 changes: 3 additions & 1 deletion scipy/stats/_continuous_distns.py
Expand Up @@ -7192,7 +7192,9 @@ def _ppf(self, q, skew):
ans, q, _, mask, invmask, beta, alpha, zeta = (
self._preprocess(q, skew))
ans[mask] = _norm_ppf(q[mask])
ans[invmask] = sc.gammaincinv(alpha, q[invmask])/beta + zeta
q = q[invmask]
q[beta < 0] = 1 - q[beta < 0] # for negative skew; see gh-17050
ans[invmask] = sc.gammaincinv(alpha, q)/beta + zeta
return ans

@_call_super_mom
Expand Down
1 change: 1 addition & 0 deletions scipy/stats/_distr_params.py
Expand Up @@ -87,6 +87,7 @@
['norminvgauss', (1.25, 0.5)],
['pareto', (2.621716532144454,)],
['pearson3', (0.1,)],
['pearson3', (-2,)],
['powerlaw', (1.6591133289905851,)],
['powerlaw', (0.6591133289905851,)],
['powerlognorm', (2.1413923530064087, 0.44639540782048337)],
Expand Down
19 changes: 19 additions & 0 deletions scipy/stats/tests/test_distributions.py
Expand Up @@ -1846,6 +1846,25 @@ def test_return_array_bug_11746(self):
assert_equal(moment, 0)
assert isinstance(moment, np.number)

def test_ppf_bug_17050(self):
# incorrect PPF for negative skews were reported in gh-17050
# Check that this is fixed (even in the array case)
skews = [-3, -1, 0, 0.5]
x_eval = 0.5
res = stats.pearson3.ppf(stats.pearson3.cdf(x_eval, skews), skews)
assert_allclose(res, x_eval)

# Negation of the skew flips the distribution about the origin, so
# the following should hold
skew = np.array([[-0.5], [1.5]])
x = np.linspace(-2, 2)
assert_allclose(stats.pearson3.pdf(x, skew),
stats.pearson3.pdf(-x, -skew))
assert_allclose(stats.pearson3.cdf(x, skew),
stats.pearson3.sf(-x, -skew))
assert_allclose(stats.pearson3.ppf(x, skew),
-stats.pearson3.isf(x, -skew))


class TestKappa4:
def test_cdf_genpareto(self):
Expand Down
4 changes: 2 additions & 2 deletions scipy/stats/tests/test_fit.py
Expand Up @@ -378,8 +378,8 @@ def test_basic_fit(self, dist_name):
dist = getattr(stats, dist_name)
shapes = np.array(dist_data[dist_name])
bounds = np.empty((len(shapes) + 2, 2), dtype=np.float64)
bounds[:-2, 0] = shapes/10**np.sign(shapes)
bounds[:-2, 1] = shapes*10**np.sign(shapes)
bounds[:-2, 0] = shapes/10.**np.sign(shapes)
bounds[:-2, 1] = shapes*10.**np.sign(shapes)
bounds[-2] = (0, 10)
bounds[-1] = (0, 10)
loc = rng.uniform(*bounds[-2])
Expand Down

0 comments on commit 942426a

Please sign in to comment.