[SciPy-User] optimize.brentq and step function

josef.pktd@gmai... josef.pktd@gmai...
Thu Feb 28 21:44:37 CST 2013


brentq documentation says "f must be a continuous function"

I forgot that I have a step function and tried brentq and it worked.
Is this an accident or a feature?

the underlying function has a ceil and floor in it to convert the
argument to integer.

example:
trying to invert stats.binom_test to get a confidence interval

>>> n_rep
10000
>>> for count in np.arange(455, 460, 0.5): count, stats.binom_test(count, n_rep, p=0.05)
...
(455.0, 0.038914401175656158)
(455.5, 0.038914401175656158)
(456.0, 0.043471737392089219)
(456.5, 0.043471737392089219)
(457.0, 0.048471945345671313)
(457.5, 0.048471945345671313)
(458.0, 0.053946450095604122)
(458.5, 0.053946450095604122)
(459.0, 0.059927554205846612)
(459.5, 0.059927554205846612)

>>> def func(qi):
...     return stats.binom_test(qi * n_rep, n_rep, p=0.05) - 0.05
...


>>> qi = optimize.brentq(func, 0.01, 0.05)
>>> (qi * n_rep)
457.99999999456901
>>> stats.binom_test(np.floor(qi * n_rep), n_rep, p=0.05)
0.048471945345671313
>>> stats.binom_test((qi * n_rep), n_rep, p=0.05)
0.048471945345671313
>>> stats.binom_test(np.floor(qi * n_rep)+1, n_rep, p=0.05)
0.053946450095604122

It looks like I get an integer next to the step root.

Josef


More information about the SciPy-User mailing list