You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
BUG: proportion confint returns nan, negative and larger than 1 #2742
beta/exact confidence interval contains a NaN if the count is all of the same kind,
normal and agresti_coull return proportions that are negative or larger than one.
This might also be the reason for the comment that binom_test inversion fails, it uses normal ci for the bounds of root search, which should fail if probability is negative or larger than 1.
TODO: trim/clip to [0,1], and explicitly set bounds to 0 or 1 for beta in corner case.
import numpy as np
import statsmodels.stats.proportion as smprop
import pandas as pd
confint_methods = ['beta', 'wilson', 'normal', 'agresti_coull', 'jeffrey']
nobs = 30
count_ = 0
confints0 = [smprop.proportion_confint(count_, nobs, alpha=0.05, method=method) for method in confint_methods]
count_ = 1
confints1 = [smprop.proportion_confint(count_, nobs, alpha=0.05, method=method) for method in confint_methods]
count_ = nobs - 1
confintsnm1 = [smprop.proportion_confint(count_, nobs, alpha=0.05, method=method) for method in confint_methods]
count_ = nobs
confintsn = [smprop.proportion_confint(count_, nobs, alpha=0.05, method=method) for method in confint_methods]
pd.DataFrame(np.column_stack((confints0, confints1, confintsnm1, confintsn)), index=confint_methods,
columns=['0 lower', '0 upper', '1 lower', '1 upper', 'n-1 lower', 'n-1 upper', 'n lower', 'n upper'])
I would like to work on this.
In case of 'beta', ci_low and ci_low are calculated using stats.beta function. So, should I implement clipping within the implementation of stats.beta?
I have create a PR #2789 But the travis CI build showing some bugs. I don't think the error is related to my commits because the exact same error details are shown for PR #2779
see my comment in #2789
PR #2779 is not merged because it assumes at least 1-D when it can be scalar.
PR #2789 assumes scalar when it can be vectorized
PR #3058 merged better array handling, so examples will not run in the same way anymore.
correction that is for proportion_ztest
we need a similar change for proportion_conf
AFAICS, no change in nan and out of [0, 1] range
(all normal distribution based approximation will have the possibility of out of range values, unless appropriate endpoint transformation is used for the confidence interval.)
'beta' and 'jeffreys' us beta distribution and will not go outside [0,1]. Those two are similar, except jeffreys add 0.5 and will stay away from the corner case (AFAICS)
i.e. we need to np.clip(..., 0, 1) for all except those two
using numpy array and pandas Series both work. list raises an exception.
if we add np.asarray, then we don't return pandas anymore if pandas was used for the data.
currently:
wilson, normal and agresti_coul return pandas,
beta and jeffreys return array with pandas Seris as input
wilson doesn't return values outside [0, 1] in the example, so it's only 'normal' and 'agresti-coull' that needs clipping
maybe that's too much, but it is a side effect of allowing numpy broadcasting inside the function.
There is no squeeze, so Series and a one column DataFrame are handled in a different way.
I haven't changed the column labels to account for this, the index comes from the count argument.
that is current behavior is, except for labels, like using c, n = map(pd.DataFrame, np.broadcast_arrays(np.asarray(count_), np.asarray(nobs_)))
(or whatever the pandas version might possibly be, if it exists)
Activity
ngaurav commentedon Jan 29, 2016
I would like to work on this.
In case of 'beta', ci_low and ci_low are calculated using stats.beta function. So, should I implement clipping within the implementation of stats.beta?
josef-pkt commentedon Jan 29, 2016
The confidence intervals need clipping, beta needs clipping or special casing of the boundaries.
My example can be used for unit tests to check that the values are not nan and in between 0 and 1.
ngaurav commentedon Jan 30, 2016
I have create a PR #2789 But the travis CI build showing some bugs. I don't think the error is related to my commits because the exact same error details are shown for PR #2779
josef-pkt commentedon Jan 30, 2016
see my comment in #2789
PR #2779 is not merged because it assumes at least 1-D when it can be scalar.
PR #2789 assumes scalar when it can be vectorized
ngaurav commentedon Jan 30, 2016
@josef-pkt Thanks. You not only saved my time, but also provided me with really helpful inputs. It's nice to discuss doubts with you.
josef-pkt commentedon Feb 3, 2017
It's a
type-bug-wrong, but the wrong numbers are not misleadingjosef-pkt commentedon Mar 29, 2018
PR #3058 merged better array handling, so examples will not run in the same way anymore.
correction that is for proportion_ztest
we need a similar change for proportion_conf
AFAICS, no change in nan and out of [0, 1] range
(all normal distribution based approximation will have the possibility of out of range values, unless appropriate endpoint transformation is used for the confidence interval.)
'beta' and 'jeffreys' us beta distribution and will not go outside [0,1]. Those two are similar, except jeffreys add 0.5 and will stay away from the corner case (AFAICS)
i.e. we need to np.clip(..., 0, 1) for all except those two
josef-pkt commentedon Mar 29, 2018
current behavior
asarray is missing:
currently:
wilson, normal and agresti_coul return pandas,
beta and jeffreys return array with pandas Seris as input
wilson doesn't return values outside [0, 1] in the example, so it's only 'normal' and 'agresti-coull' that needs clipping
josef-pkt commentedon Mar 29, 2018
my current WIP version allows pandas broadcasting after np.asarray
count_ is a DataFrame, nobs is a Series
maybe that's too much, but it is a side effect of allowing numpy broadcasting inside the function.
There is no squeeze, so Series and a one column DataFrame are handled in a different way.
I haven't changed the column labels to account for this, the index comes from the
countargument.that is current behavior is, except for labels, like using
c, n = map(pd.DataFrame, np.broadcast_arrays(np.asarray(count_), np.asarray(nobs_)))(or whatever the pandas version might possibly be, if it exists)
proportion_confint is an elementwise function
josef-pkt commentedon Mar 29, 2018
unit tests, the current unit tests fail if we impose [0,1] range
It looks like the R library for the unit test has one value larger than 1.
i.e. 1.0314784 for upper for 'normal' or 'asymptotic'
josef-pkt commentedon Mar 29, 2018
The two corner cases in R binom
BUG/ENH: proportion_confint: impose bounds, support asarray and pandas …