-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ENH: override halflogistic sf
and isf
#18002
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new _sf
works great, but for _isf
, it looks like we have to play whack-a-mole on the relative error. Here's a plot of the relative error of the original version of isf
and the version in this pull request. The pull request fixes the large errors near 0, but it has significantly worse relative error than the default for inputs near 1. If you zoom in, you can see that a good threshold for switching between the versions is right in the middle, p=0.5.
Note: the script uses mpsci.distribution.half_logistic
, a very recent addition to mpsci
.
script that generates the plot
import numpy as np
from scipy import stats
from scipy.special import logit
from mpmath import mp
from mpsci.distributions import half_logistic
import matplotlib.pyplot as plt
p = np.linspace(1e-5, 1, 10000, endpoint=False)
# x1 = stats.halflogistic.isf(p)
x1 = 2*np.arctanh(1 - p) # Same as stats.halflogistic.isf(p) in main.
x2 = -logit(0.5*p)
mp.dps = 100
x = np.array([float(half_logistic.invsf(t)) for t in p])
plt.plot(p, np.abs(x1 - x)/x, alpha=0.35, label='original')
plt.plot(p, np.abs(x2 - x)/x, alpha=0.35, label='pull request')
plt.title('half-logistic isf relative error')
plt.xlabel('p')
plt.grid()
plt.legend(framealpha=1, shadow=True)
plt.ylim(-1e-15, 4e-14)
plt.show()
# from mpmath import mp | ||
# mp.dps = 50 | ||
# def sf_mpmath(x): | ||
# mp.dps = 50 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Delete this--you already set dps
two lines above.
Thanks for the suggestions @WarrenWeckesser , the relative error of Relative error using the new formulation
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. I'll make a tiny tweak to a few lines of commented code and merge.
Reference issue
#17832
What does this implement/fix?
Overrides survival function and inverse survival function for the halflogistic distribution. Using a little algebra (or Wolfram Alpha for convenience) we get:
$SF(x)=1-CDF(x)=1-\tanh(x/2)=2/(1+e^x)=2\text{expit}(-x)$
$ISF(q)=-\text{logit}(q/2)$
Additional information
The current survival function fails at about$x=38$ .
Inverse survival function: greatly improved range for small values.