Skip to content

BUG: proportion confint returns nan, negative and larger than 1 #2742

Closed

Description

@josef-pkt

partially duplicate of #2609

see https://github.com/josef-pkt/misc/blob/proportion_one/notebooks/proportion_one_power.ipynb
(not yet committed and pushed)
example copied below

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'])

Activity

  1. added this to the 0.8 milestone on Dec 23, 2015
  2. ngaurav commented on Jan 29, 2016

    @ngaurav

    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?

  3. josef-pkt commented on Jan 29, 2016

    @josef-pkt
    MemberAuthor

    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.

  4. ngaurav commented on Jan 30, 2016

    @ngaurav

    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

  5. josef-pkt commented on Jan 30, 2016

    @josef-pkt
    MemberAuthor

    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

  6. ngaurav commented on Jan 30, 2016

    @ngaurav

    @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.

  7. added
    type-bug-wrong
    on Sep 29, 2016
  8. josef-pkt commented on Feb 3, 2017

    @josef-pkt
    MemberAuthor

    It's a type-bug-wrong, but the wrong numbers are not misleading

  9. josef-pkt commented on Mar 29, 2018

    @josef-pkt
    MemberAuthor

    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

  10. josef-pkt commented on Mar 29, 2018

    @josef-pkt
    MemberAuthor

    current behavior

    asarray is missing:

    • 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

  11. josef-pkt commented on Mar 29, 2018

    @josef-pkt
    MemberAuthor

    my current WIP version allows pandas broadcasting after np.asarray

    count_ is a DataFrame, nobs is a Series

    >>> count_.shape
    (3, 1)
    >>> nobs_.shape
    (3,)
    >>> np.asarray(count_) / np.asarray(nobs_)
    array([[ 0.        ,  0.        ,  0.        ],
           [ 0.1       ,  0.1       ,  0.03333333],
           [ 1.        ,  1.        ,  0.33333333]])
    

    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.

    >>> type(count_)
    <class 'pandas.core.frame.DataFrame'>
    >>> type(nobs_)
    <class 'pandas.core.series.Series'>
    >>> for method in confint_methods:
    ...     print(method)
    ...     r = smprop.proportion_confint(count_, nobs_, alpha=0.05, method=method)
    ...     print(r)   
    ... 
    beta
    (          0         1         2
    0  0.000000  0.000000  0.000000
    1  0.002529  0.002529  0.000844
    2  0.691503  0.691503  0.172874,           
    0         1         2
    0  0.308497  0.308497  0.115703
    1  0.445016  0.445016  0.172169
    2  1.000000  1.000000  0.528120)
    wilson
    (          0         1         2
    0  0.000000  0.000000  0.000000
    1  0.017876  0.017876  0.005909
    2  0.722467  0.722467  0.192305,           
    0         1         2
    0  0.277533  0.277533  0.113513
    1  0.404150  0.404150  0.166704
    2  1.000000  1.000000  0.512199)
    normal
    (     0    1         2
    0  0.0  0.0  0.000000
    1  0.0  0.0  0.000000
    2  1.0  1.0  0.164646,          
     0         1         2
    0  0.000000  0.000000  0.000000
    1  0.285939  0.285939  0.097567
    2  1.000000  1.000000  0.502020)
    agresti_coull
    (          0         1         2
    0  0.000000  0.000000  0.000000
    1  0.000000  0.000000  0.000000
    2  0.679113  0.679113  0.191316,           
    0         1         2
    0  0.320887  0.320887  0.134712
    1  0.425968  0.425968  0.180918
    2  1.000000  1.000000  0.513188)
    jeffrey
    (          0         1         2
    0  0.000048  0.000048  0.000016
    1  0.011012  0.011012  0.003620
    2  0.782804  0.782804  0.185979,           
    0         1         2
    0  0.217196  0.217196  0.079678
    1  0.381315  0.381315  0.145418
    2  0.999952  0.999952  0.511148)
    

    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

  12. josef-pkt commented on Mar 29, 2018

    @josef-pkt
    MemberAuthor

    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'

    > library(binom)
    > bci = binom.confint(x = c(18), n = 20, tol = 1e-8)
    > bci
              method  x  n      mean     lower     upper
    1  agresti-coull 18 20 0.9000000 0.6867561 0.9843438
    2     asymptotic 18 20 0.9000000 0.7685216 1.0314784
    3          bayes 18 20 0.8809524 0.7460094 0.9907234
    4        cloglog 18 20 0.9000000 0.6560307 0.9740102
    5          exact 18 20 0.9000000 0.6830173 0.9876515
    6          logit 18 20 0.9000000 0.6761980 0.9748664
    7         probit 18 20 0.9000000 0.7027685 0.9788585
    8        profile 18 20 0.9000000 0.7220529 0.9823182
    9            lrt 18 20 0.9000000 0.7220576 0.9826399
    10     prop.test 18 20 0.9000000 0.6687224 0.9824874
    11        wilson 18 20 0.9000000 0.6989664 0.9721335
    
  13. josef-pkt commented on Mar 29, 2018

    @josef-pkt
    MemberAuthor

    The two corner cases in R binom

    > bci = binom.confint(x = c(20), n = 20, tol = 1e-8)
    > bci
              method  x  n      mean     lower    upper
    1  agresti-coull 20 20 1.0000000 0.8101904 1.028684
    2     asymptotic 20 20 1.0000000 1.0000000 1.000000
    3          bayes 20 20 0.9761905 0.9095236 1.000000
    4        cloglog 20 20 1.0000000 0.8315665 1.000000
    5          exact 20 20 1.0000000 0.8315665 1.000000
    6          logit 20 20 1.0000000 0.8315665 1.000000
    7         probit 20 20 1.0000000 0.8315665 1.000000
    8        profile 20 20 1.0000000 0.8497732 1.000000
    9            lrt 20 20 1.0000000 0.9084309 1.000000
    10     prop.test 20 20 1.0000000 0.7995467 1.000000
    11        wilson 20 20 1.0000000 0.8388748 1.000000
    
    > bci = binom.confint(x = c(0), n = 20, tol = 1e-8)
    > bci
              method x  n       mean         lower      upper
    1  agresti-coull 0 20 0.00000000 -2.868440e-02 0.18980956
    2     asymptotic 0 20 0.00000000  0.000000e+00 0.00000000
    3          bayes 0 20 0.02380952  0.000000e+00 0.09047643
    4        cloglog 0 20 0.00000000  0.000000e+00 0.16843347
    5          exact 0 20 0.00000000  0.000000e+00 0.16843347
    6          logit 0 20 0.00000000  0.000000e+00 0.16843347
    7         probit 0 20 0.00000000  0.000000e+00 0.16843347
    8        profile 0 20 0.00000000  0.000000e+00 0.15022677
    9            lrt 0 20 0.00000000  0.000000e+00 0.09156912
    10     prop.test 0 20 0.00000000  0.000000e+00 0.20045335
    11        wilson 0 20 0.00000000  1.164173e-17 0.16112516
    
  14. added a commit that references this issue on Mar 29, 2018
    1dfdf52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    comp-statsprio-hightype-bugtype-bug-wrong

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions