statsmodels/statsmodels — pull request #723 by josef-pkt (2013-03-23 03:02:01+00:00)
Title: Rootfinding for Power

This adds ``brentq_expanding`` as a rootfinder, and connects it to the power calculations in stats.

**Status:**
looks like it works, but need some more robustness checks.
The test suite for power passes, but it needed some help.
Also, currently I deactivated using fsolve as backup, and exceptions in
``brentq_expanding`` for not finding a solution, are not caught.

I hoped the new solver would find the correct interval for brentq in all cases, but that didn't work. I prepared the ``brentq_expanding`` for some range of NaN's returned by the function, but not enough.

The way it works now is to define some bounds for the valid range, for example minimum sample size required for the test (so it doesn't return nan or inf).

**Problems for roots in power:**

non-monotonic symmetric function:
F-test uses sqrt of effects size which is then squared, so power only depends on absolute value, and the power as function of effect size is U-shaped around zero. I think I worked around that successfully in the solver (kind of).

invalid range of function :
``brentq_expanding`` can handle automatically functions that are only defined on the postive or negative real line, but only if the threshold is around zero (by default).
for nobs the threshold is larger, the lower bound is 2 or larger (as in FTestAnova)

some power functions are not defined if ratio=0, and we get zero division warning and inf if evaluated at 0. This also breaks automatic rootfinding and needs a bound away from zero.

some weird cases:
I thought power is alwasy increasing in nobs, but it looks like that's not always the case. I had to drop specifying that it is increasing.

The advantage is still that it works without assumption on any curvature (besides monotonicity), besides the current problems with fsolve in recent scipy, fsolve can also make huge steps out of the valid range if the derivative is very small.
(for example t-distribution: df has little effect if nobs is large)