
    M/PhtN                    D   d Z ddlmZ ddlmZ ddlZddlZddl	m
Z
mZ ddlmZmZ ddlmZ ddlmZ dd	lmZ dd
lmZ  ej        e          Z	 dAdeegef         dededefdZ	 dBdeegef         dedededeeef         f
dZdCdefdZdDdZ 	 	 dCdZ!dEdZ"d Z#dFd Z$	 	 dGd"Z%d# Z&dHd$Z'dId&Z(dJd(Z)dKd)Z*	 	 	 dLd*Z+d+ Z,	 	 dMd-Z-dNd/Z.dOd0Z/dPd2Z0	 	 dQd3Z1	 	 dRd5Z2	 	 dSd7Z3	 	 	 dTd8Z4	 	 	 	 dUd9Z5	 dVd:Z6dWd;Z7	 	 	 dXd<Z8	 	 	 dYd=Z9	 	 dZd>Z:	 	 d[d?Z;dHd@Z<dS )\z
Tests and Confidence Intervals for Binomial Proportions

Created on Fri Mar 01 00:23:07 2013

Author: Josef Perktold
License: BSD-3
    )lzip)CallableN)optimizestats)AllPairsResultsHolderTuple_zstat_generic2)HypothesisTestWarning)Holder)
array_likeTfuncqilowerreturnc                     rt           j        ndt           j        z
  }fd} ||          } | |          }d}|dk    r-|dk     r' ||          } | |          }|dz  }|dk    r|dk     '|dk     r|n|S )a  
    Try hard to find a bound different from eps/1 - eps in proportion_confint

    Parameters
    ----------
    func : callable
        Callable function to use as the objective of the search
    qi : float
        The empirical success rate
    lower : bool
        Whether to fund a lower bound for the left side of the CI

    Returns
    -------
    float
        The coarse bound
          ?c                 (    r| dz  n
| d| z
  dz  z   S )N   r    )vr   s    \/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/stats/proportion.pystepz'_bound_proportion_confint.<locals>.step/   s#    4q1uu1a1}#44       r   
   )
FLOAT_INFOeps)r   r   r   defaultr   xwcnts     `     r   _bound_proportion_confintr#      s    ( !&?jnn3+?G5 5 5 5 5 	RAQA
C
a%%C"HHDGGDGGq a%%C"HH b11g%r      lbubstepsc                     | |          } | |          }|dk     r|n|}|dk     r|n|}t          j        |          t          j        |          k    rt          d          ||z   dz  } | |          }	|	dk     r
|	|k    r|	}|}t          |          D ]V}
t          j        |	          t          j        |          k    r|}|	}n|}||z   dz  } | |          }	|	dk     r
|	|k    r|	}|}W||fS )a9  
    Private function used as a fallback by proportion_confint

    Used when brentq returns a non-conservative bound for the CI

    Parameters
    ----------
    func : callable
        Callable function to use as the objective of the search
    lb : float
        Lower bound
    ub : float
        Upper bound
    steps : int
        Number of steps to use in the bisection

    Returns
    -------
    est : float
        The estimated value.  Will always produce a negative value of func
    func_val : float
        The value of the function at the estimate
    r   zproblem with signs   )npsign
ValueErrorrange)r   r%   r&   r'   upperr   bestbest_ptmpmid_s              r   _bisection_search_conservativer4   <   s    4 DHHEDHHEAII555DAIIbb2G	wu~~''-...
r'QB
$r((CacDjj5\\ 
 
73<<275>>))BEEB2g]d2hh!GG#**DGD=r   皙?normalalphac                 j   t          j        |           ot          j        |          }t          | t          j        t          j        f          }t          | ddd          }t          |ddd          }dt           j        dt          dt           j        fd	}|d
k    r< |t          j	        |          d          } |t          j	        |          d          }||z  }	dz  }
|dk    rNt          j
        |	d|	z
  z  |z            }t          j                            dz            |z  }|	|z
  }|	|z   }nP|d
k    rcdt          dt          dt          t           gt           f         ffd}t          j        ||          }t          j        |j                  }t          j        |j                  }|j        }|D ]\  }}d}|	j        |         }||dz  k    r||z
  }d}d|z
  } |||          }|dk    rd|j        |<   nt-          ||d          }t/          j        |||d          \  }} ||          dk    rad}|||z
  d|z  z  z
  } ||          dk    r.|dk    r(|dz  }|||z
  d|z  z  z
  } ||          dk    r|dk    (t3          |||          \  }}||j        |<   ||k    rd|j        |<   nt-          ||d          }t/          j        |||d          \  }} ||          dk    rad}|||z
  d|z  z  z   } ||          dk    r.|dk    r(|dz  }|||z
  d|z  z  z
  } ||          dk    r|dk    (t3          |||          \  }}||j        |<   |r2|j        |         }d|j        |         z
  |j        |<   d|z
  |j        |<   |j        }n|dk    rt          j                            |
|||z
  dz             }t          j                            |
|dz   ||z
            }t          j        |          dk    r(d|j        |	j        dk    <   d|j        |	j        dk    <   nR|	dk    rdn|}|	dk    rdn|}n<|dk    ret          j                            dz            } || dz  z   }!|| dz  dz  z   |!z  }"t          j
        |"d|"z
  z  |!z            }#| |#z  }|"|z
  }|"|z   }n|dk    ryt          j                            dz            } | dz  }$d|$|z  z   }%|	|$d|z  z  z   |%z  }&| t          j
        |	d|	z
  z  |z  |$d|dz  z  z  z             z  }||%z  }|&|z
  }|&|z   }nR|dd         dk    r1t          j                            dz
  |dz   ||z
  dz             \  }}nt=          d| d          |d v r,t          j        |dd          }t          j        |dd          }|rVt          | t          j                  rt          j        nt          j        }' |'|| j        !          } |'|| j        !          }|rt!          |          t!          |          fS ||fS )"u\	  
    Confidence interval for a binomial proportion

    Parameters
    ----------
    count : {int or float, array_like}
        number of successes, can be pandas Series or DataFrame. Arrays
        must contain integer values if method is "binom_test".
    nobs : {int or float, array_like}
        total number of trials.  Arrays must contain integer values if method
        is "binom_test".
    alpha : float
        Significance level, default 0.05. Must be in (0, 1)
    method : {"normal", "agresti_coull", "beta", "wilson", "binom_test"}
        default: "normal"
        method to use for confidence interval. Supported methods:

         - `normal` : asymptotic normal approximation
         - `agresti_coull` : Agresti-Coull interval
         - `beta` : Clopper-Pearson interval based on Beta distribution
         - `wilson` : Wilson Score interval
         - `jeffreys` : Jeffreys Bayesian Interval
         - `binom_test` : Numerical inversion of binom_test

    Returns
    -------
    ci_low, ci_upp : {float, ndarray, Series DataFrame}
        lower and upper confidence level with coverage (approximately) 1-alpha.
        When a pandas object is returned, then the index is taken from `count`.

    Notes
    -----
    Beta, the Clopper-Pearson exact interval has coverage at least 1-alpha,
    but is in general conservative. Most of the other methods have average
    coverage equal to 1-alpha, but will have smaller coverage in some cases.

    The "beta" and "jeffreys" interval are central, they use alpha/2 in each
    tail, and alpha is not adjusted at the boundaries. In the extreme case
    when `count` is zero or equal to `nobs`, then the coverage will be only
    1 - alpha/2 in the case of "beta".

    The confidence intervals are clipped to be in the [0, 1] interval in the
    case of "normal" and "agresti_coull".

    Method "binom_test" directly inverts the binomial test in scipy.stats.
    which has discrete steps.

    TODO: binom_test intervals raise an exception in small samples if one
       interval bound is close to zero or one.

    References
    ----------
    .. [*] https://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval

    .. [*] Brown, Lawrence D.; Cai, T. Tony; DasGupta, Anirban (2001).
       "Interval Estimation for a Binomial Proportion", Statistical
       Science 16 (2): 101–133. doi:10.1214/ss/1009213286.
    countFN)optionalndimnobsr    namer   c                     t          j        | j        t           j                  r| S |                     t           j        d          }t          j        || k              rt          | d| j                   |S )Nunsafe)castingz4 must have an integral dtype. Found data with dtype )r*   
issubdtypedtypeintegerastypeint64anyr,   )r    r=   ys      r   _checkz"proportion_confint.<locals>._check   s    ="*-- 	HHHRXxH006!q&>> 	 # ## #   r   
binom_test      ?r6   r          @c                 T     t          t          d          r fd}n fd}|S )N	binomtestc                 B    t          j        |           j        z
  S N)p)r   rM   pvaluer   r7   r9   r<   s    r   r   z6proportion_confint.<locals>.func_factory.<locals>.func   s"     ?5$"===DuLLr   c                 8    t          j        |           z
  S rO   )r   rI   rR   s    r   r   z6proportion_confint.<locals>.func_factory.<locals>.func   s      +E42>>>FFr   )hasattrr   )r9   r<   r   r7   s   `` r   func_factoryz(proportion_confint.<locals>.func_factory   sy    uk** GM M M M M M M M
G G G G G G G Kr   r)   Tr           )r   )full_outputr   r   betaagresti_coullwilsong      @   jeffzmethod z is not available)r6   rY   )index) r*   isscalar
isinstancepdSeries	DataFramer   ndarraystrasarraysqrtr   normisfintr   float	broadcastzerosshaper]   flatr#   r   brentqr4   rX   ppfr;   intervalNotImplementedErrorclip)(r9   r<   r7   method	is_scalar	is_pandascount_anobs_arH   q_alpha_2std_distci_lowci_upprU   bcastr]   cnreverse_qr   	lower_bndval_zpowernew_lbr3   	upper_bndnew_ubtempcritnobs_cq_cstd_ccrit2denomcenter	containers(     `                                     r   proportion_confintr   o   sF   v E""8r{4'8'8I529bl";<<I%dCCCGfu4@@@F	"* 	C 	BJ 	 	 	 	 &G,,g66
6**G44	6	BEkGwrQV}v-..z~~eck**T1dd	<			 	3 	8UGUN3K 	 	 	 	 	 	 Wf--%+&&%+&& +	  +	 DAqGB16zzEV<1%%DAvv%(E""5dBdKKK	"/)RT  R 499q==E C)Oq%x#??F$v,,**uzz
!$i1e8'C!C $v,,**uzz <D&"MMFC%(E"Avv%(E""5dBeLLL	"/"iT  R 499q==E IOq%x#??F$v,,**uzz
!$	C1e8'C!C $v,,**uzz <D"fMMFC%(E" .{5)%&U);%;E"%&XE"KEEW+	 X 
6		&72BQ2FGG1fw6FGG76??Q()FK1%()FK1%%''QQvF''QQvFF	?	"	"z~~eck**$'!q3&&0sSy)F233e|tt	8		z~~eck**aEFN"uF
++u4bg#(Of$ufai'@@
 
 
 	$$	v		,,Iw}fw&6&<
 
 ""EF"E"E"EFFF,,,A&&A&& 6!+E29!=!=OBII2<	65556555 ,V}}eFmm++6>r   goodmanc                 l    |dk    s|dk    rt          d          t          j         t                      dk                                     rt          d                                           t                     } z  }|dk    rt          j        	                    d||z  z
  d          }|dz  dz  |z  |z  d|z
  z  z   }dz  |z  |z   t          j        t          j
        |           t          j
        |          g          z   d|z   z  z  j        }n|d	d
         dk    rd fd fd fd fd}d}	 ||	          }
 ||	dz             }|
d|z
  cxk    r|k     s?n |	k    rt          d          |	dz  }	|}
 ||	dz             }|
d|z
  cxk    r|k     :n =d|z
  |
z
  ||
z
  z  }t          j        ||	z  z
  d          }t          j        ||	d|z  z   z  z   d          }t          j        ||g          j        }nt          d|z            |S )u$  
    Confidence intervals for multinomial proportions.

    Parameters
    ----------
    counts : array_like of int, 1-D
        Number of observations in each category.
    alpha : float in (0, 1), optional
        Significance level, defaults to 0.05.
    method : {'goodman', 'sison-glaz'}, optional
        Method to use to compute the confidence intervals; available methods
        are:

         - `goodman`: based on a chi-squared approximation, valid if all
           values in `counts` are greater or equal to 5 [2]_
         - `sison-glaz`: less conservative than `goodman`, but only valid if
           `counts` has 7 or more categories (``len(counts) >= 7``) [3]_

    Returns
    -------
    confint : ndarray, 2-D
        Array of [lower, upper] confidence levels for each category, such that
        overall coverage is (approximately) `1-alpha`.

    Raises
    ------
    ValueError
        If `alpha` is not in `(0, 1)` (bounds excluded), or if the values in
        `counts` are not all positive or null.
    NotImplementedError
        If `method` is not kown.
    Exception
        When ``method == 'sison-glaz'``, if for some reason `c` cannot be
        computed; this signals a bug and should be reported.

    Notes
    -----
    The `goodman` method [2]_ is based on approximating a statistic based on
    the multinomial as a chi-squared random variable. The usual recommendation
    is that this is valid if all the values in `counts` are greater than or
    equal to 5. There is no condition on the number of categories for this
    method.

    The `sison-glaz` method [3]_ approximates the multinomial probabilities,
    and evaluates that with a maximum-likelihood estimator. The first
    approximation is an Edgeworth expansion that converges when the number of
    categories goes to infinity, and the maximum-likelihood estimator converges
    when the number of observations (``sum(counts)``) goes to infinity. In
    their paper, Sison & Glaz demo their method with at least 7 categories, so
    ``len(counts) >= 7`` with all values in `counts` at or above 5 can be used
    as a rule of thumb for the validity of this method. This method is less
    conservative than the `goodman` method (i.e. it will yield confidence
    intervals closer to the desired significance level), but produces
    confidence intervals of uniform width over all categories (except when the
    intervals reach 0 or 1, in which case they are truncated), which makes it
    most useful when proportions are of similar magnitude.

    Aside from the original sources ([1]_, [2]_, and [3]_), the implementation
    uses the formulas (though not the code) presented in [4]_ and [5]_.

    References
    ----------
    .. [1] Levin, Bruce, "A representation for multinomial cumulative
           distribution functions," The Annals of Statistics, Vol. 9, No. 5,
           1981, pp. 1123-1126.

    .. [2] Goodman, L.A., "On simultaneous confidence intervals for multinomial
           proportions," Technometrics, Vol. 7, No. 2, 1965, pp. 247-254.

    .. [3] Sison, Cristina P., and Joseph Glaz, "Simultaneous Confidence
           Intervals and Sample Size Determination for Multinomial
           Proportions," Journal of the American Statistical Association,
           Vol. 90, No. 429, 1995, pp. 366-369.

    .. [4] May, Warren L., and William D. Johnson, "A SAS® macro for
           constructing simultaneous confidence intervals  for multinomial
           proportions," Computer methods and programs in Biomedicine, Vol. 53,
           No. 3, 1997, pp. 153-162.

    .. [5] May, Warren L., and William D. Johnson, "Constructing two-sided
           simultaneous confidence intervals for multinomial proportions for
           small counts in a large number of cells," Journal of Statistical
           Software, Vol. 5, No. 6, 2000, pp. 1-24.
    r   r   z(alpha must be in (0, 1), bounds excludedrB   zcounts must be >= 0r   r)   r[   N   sisonc                     | \  }}t           j                            ||          t           j                            |dz
  |          z
  }|S )zj
            Compute P(b <= Z <= a) where Z ~ Poisson(p) and
            `interval = (b, a)`.
            r   )r   poissoncdf)rq   rP   baprobs        r   poisson_intervalz9multinomial_proportions_confint.<locals>.poisson_interval  sD    
 DAq=$$Q**U]->->q1ua-H-HHDKr   c                     | \  }}||z  d ||z
  dz   |f|           ||z
  |dz
  f|          z
   ||f|          z  z
  z  S )z
            Compute mu_r, the r-th factorial moment of a poisson random
            variable of parameter `p` truncated to `interval = (b, a)`.
            r   r   )rq   rrP   r   r   r   s        r   "truncated_poisson_factorial_momentzKmultinomial_proportions_confint.<locals>.truncated_poisson_factorial_moment  s    
 DAq6Q#3#3QUQYNA#F#F#3#3QUAENA#F#F$G"2"2Aq61"="=#> ? @ @r   c                      fdt          dd          D             \  }}}}|}||z   |dz  z
  }||dd|z  z
  z  z   |z   d|dz  z  z
  d|dz  z  z   }||dd|z  z
  z  z   |dd	|z  z
  d|dz  z  z   z  z   |z   d|dz  z  z
  d|dz  z  z   d|dz  z  z
  }|                                |                                d
z  z  }	|                                d|dz                                  z  z
  |                                dz  z  }
|                                z
  t          j        |                                          z  }t          j        |dz   dz            t          j        dt          j        z            z  }|dz  d|z  z
  }|dz  d|dz  z  z
  dz   }|dz  d|dz  z  z
  d|dz  z  z   dz
  }|d|	|z  dz  z   |
|z  dz  z   |	dz  |z  dz  z   z  }|t          j        |                                          z  S )z
            Compute the Edgeworth expansion term of Sison & Glaz's formula
            (1) (approximated probability for multinomial proportions in a
            given box).
            c              3   x   K   | ]3t          j        fd t                    D                       V  4dS )c                 0    g | ]\  }} ||          S r   r   ).0rq   rP   r   r   s      r   
<listcomp>zPmultinomial_proportions_confint.<locals>.edgeworth.<locals>.<genexpr>.<listcomp>  sB     G G G+x =<Xq!LL G G Gr   N)r*   arrayzip)r   r   counts	intervalsr   s    @r   	<genexpr>zEmultinomial_proportions_confint.<locals>.edgeworth.<locals>.<genexpr>  s       * *   G G G G G/29f/E/EG G G H H* * * * * *r   r   r   r)         r[         g      ?   -      H   )r-   sumr*   rf   exppi)r   mu_r1mu_r2mu_r3mu_r4mumu2mu3mu4g1g2r    phiH3H4H6fr   r   r   s   `                r   	edgeworthz2multinomial_proportions_confint.<locals>.edgeworth  s_   * * * * * * q!* * *&E5%
 B"*rQw&C%1q2v:..3a"'kAAaKOC5ABJ//ARK!bAg+567B!G$&'"'k245aK@C
 SWWYY#--B''))a3!8.."2"222cggii1nDB RVVXX!3!33A&16A&&RU););;Ca!a%Ba!a1f*$q(Ba"qAv+%Q!V3b8Bq27Q;b25a"r8IIJArwswwyy))))r   c                 J   t          j        t          j        t          j        fdt	          |           D                                 t          j         |                     z   t          j        t
          j                                                z
            S )z
            Compute approximated probability for Multinomial(n, proportions)
            to be in `intervals` (Sison & Glaz's formula (1)).
            c                 .    g | ]\  }} ||          S r   r   )r   rq   rP   r   s      r   r   z^multinomial_proportions_confint.<locals>.approximated_multinomial_interval.<locals>.<listcomp>  s@     L L L#0Ha  0/!<< L L Lr   )r*   r   r   logr   r   r   _pmf)r   r   r   r   r   s    r   !approximated_multinomial_intervalzJmultinomial_proportions_confint.<locals>.approximated_multinomial_interval  s    
 6rv L L L L47	64J4JL L L M M N Nyy++,,- u}))!Q//001  r   c                 6       fdD                       S )zm
            Compute interval coverage for a given `c` (Sison & Glaz's
            formula (7)).
            c                 p    g | ]2}t          j        |z
  d           t          j        |z             f3S r   )r*   maximumminimum)r   r9   r   r   s     r   r   z?multinomial_proportions_confint.<locals>.nu.<locals>.<listcomp>  sN     & & & *UQY**BJuqy!,D,DE & & &r   r   )r   r   r   r   s   `r   nuz+multinomial_proportions_confint.<locals>.nu  sF    
 54& & & & &$& & &' ' 'r   r   zHCouldn't find a value for `c` that solves nu(c) <= 1 - alpha < nu(c + 1)zmethod "%s" is not available)r,   r*   r   rj   rF   r   lenr   chi2rp   rf   T	Exceptionr   r   rr   )r   r7   rt   kproportionsr   deltaregionr   r   nucnucp1gci_lowerci_upperr   r   r   r   r   s   `              @@@@@r   multinomial_proportions_confintr   5  s   j zzUaZZCDDDXfE***F
 0.///

AFA1*Kz~~a%!)mQ//	QU[0471{?KLq5;&-8rwu~~-rwu~~>??@q>#$% 	 
w			 	 		@ 	@ 	@ 	@ 	@	* 	* 	* 	* 	* 	* 	*D
	 
	 
	 
	 
	 
	 
	 
		' 	' 	' 	' 	' 	' 	' bee1q5		1u9--------1uu !H I I IFACBq1uIIE 1u9--------- Y_-:kAE1155:kQQY!O;Q??8X.//1!"@6"IJJJMr   c                     | }|dk    r2|d|z
  z  |t           j                            |dz            z  dz  z  }nt          d          |S )a  
    Find sample size to get desired confidence interval length

    Parameters
    ----------
    proportion : float in (0, 1)
        proportion or quantile
    half_length : float in (0, 1)
        desired half length of the confidence interval
    alpha : float in (0, 1)
        significance level, default 0.05,
        coverage of the two-sided interval is (approximately) ``1 - alpha``
    method : str in ['normal']
        method to use for confidence interval,
        currently only normal approximation

    Returns
    -------
    n : float
        sample size to get the desired half length of the confidence interval

    Notes
    -----
    this is mainly to store the formula.
    possible application: number of replications in bootstrap samples

    r6   r   rK   r)   zonly "normal" is available)r   rg   rh   rr   )
proportionhalf_lengthr7   rt   ry   r   s         r   samplesize_confint_proportionr     sX    : 
B!b&M[5:>>%"*+E+EEII!">???Hr   c                     |dk    rt          d          dt          j        t          j        |                     t          j        t          j        |                    z
  z  }|S )a]  
    Effect size for a test comparing two proportions

    for use in power function

    Parameters
    ----------
    prop1, prop2 : float or array_like
        The proportion value(s).

    Returns
    -------
    es : float or ndarray
        effect size for (transformed) prop1 - prop2

    Notes
    -----
    only method='normal' is implemented to match pwr.p2.test
    see http://www.statmethods.net/stats/power.html

    Effect size for `normal` is defined as ::

        2 * (arcsin(sqrt(prop1)) - arcsin(sqrt(prop2)))

    I think other conversions to normality can be used, but I need to check.

    Examples
    --------
    >>> import statsmodels.api as sm
    >>> sm.stats.proportion_effectsize(0.5, 0.4)
    0.20135792079033088
    >>> sm.stats.proportion_effectsize([0.3, 0.4, 0.5], 0.4)
    array([-0.21015893,  0.        ,  0.20135792])

    r6   zonly "normal" is implementedr)   )r,   r*   arcsinrf   )prop1prop2rt   ess       r   proportion_effectsizer   "  sW    H 7888	
bi''")BGENN*C*CC	DBIr   c                 <    t          j        | d| z
  z  |z            S )ae  
    Standard error for the estimate of a proportion

    This is just ``np.sqrt(p * (1. - p) / nobs)``

    Parameters
    ----------
    prop : array_like
        proportion
    nobs : int, array_like
        number of observations

    Returns
    -------
    std : array_like
        standard error for a proportion of nobs independent observations
    r   r*   rf   )propr<   s     r   std_propr   M  s"    $ 7429%,---r   r   c                 N    t          j        | d| z
  z  |d|z
  z  |z  z             S )Nr   r   )p1p2ratios      r   _std_diff_propr   b  s,    72R=2R=5#88999r   rg   c                    t          |
t                    s|
|
f}
t          j                            |          }| t          j        |          |z  z   }|t          j        |          |z  z
  }|s|dk    rPt          j        ||	z  d|z  z             }t          j        ||	z  d|z  z
            }|dk    r|dz  |	z  }|dz  |	z  }t          j	        ||k              rddl
}|                    dt                     t          j        |          }||z
  |
d         dz  |	z  z
  |z  }||z
  |
d         dz  |	z  z   |z  }|dk    r@t          j                            |          t          j                            |          z
  }nL|dk    rFt          j                            ||	|          t          j                            |dz
  |	|          z
  }|||||ffS )	a  
    Generic statistical power function for normal based equivalence test

    This includes options to adjust the normal approximation and can use
    the binomial to evaluate the probability of the rejection region

    see power_ztost_prob for a description of the options
    binomrJ   rg   r   r   Nzno overlap, power is zeror   )r_   tupler   rg   rh   r*   rf   ceiltruncrF   warningswarnr   r   r   )mean_lowvar_lowmean_uppvar_uppmean_altvar_altr7   discreter|   r<   
continuitycritval_continuityr   k_lowk_uppr   std_altz_lowz_uppr   s                       r   _power_ztostr
  f  s    j%(( . *-
:>>%  Drww''$..Erww''$..E &47??s-?'??@@.@(@@AA6>>bL4'EBJ%E 
veem J13HIIIggGX
1 3d ::gEEX
1 3d ::gEEv~~
u%%
u(=(==	h77[__U1WdH==>5%...r   c                 ~    t          | |d|          }t          | |d|          }t          j        ||          ||fS )a  
    Exact TOST test for one proportion using binomial distribution

    Parameters
    ----------
    count : {int, array_like}
        the number of successes in nobs trials.
    nobs : int
        the number of trials or observations.
    low, upp : floats
        lower and upper limit of equivalence region

    Returns
    -------
    pvalue : float
        p-value of equivalence test
    pval_low, pval_upp : floats
        p-values of lower and upper one-sided tests

    larger)alternativer   smaller)rI   r*   r   )r9   r<   lowupptt1tt2s         r   
binom_tostr    sI    , UDhS
A
A
AC
UDic
B
B
BC:c3c**r   c                     t           j                            |||           dz   }t           j                            |||          dz
  }||fS )a  
    Rejection region for binomial TOST

    The interval includes the end points,
    `reject` if and only if `r_low <= x <= r_upp`.

    The interval might be empty with `r_upp < r_low`.

    Parameters
    ----------
    low, upp : floats
        lower and upper limit of equivalence region
    nobs : int
        the number of trials or observations.

    Returns
    -------
    x_low, x_upp : float
        lower and upper bound of rejection region

    r   )r   r   rh   rp   )r  r  r<   r7   x_lowx_upps         r   binom_tost_reject_intervalr    sF    , KOOE4--1EKOOE4--1E%<r   	two-sidedc                     |dv rd}|dz  }|dv r%t           j                            |||           dz
  }nd}|dv r%t           j                            |||           dz   }n|}t	          |          t	          |          fS )a  
    Rejection region for binomial test for one sample proportion

    The interval includes the end points of the rejection region.

    Parameters
    ----------
    value : float
        proportion under the Null hypothesis
    nobs : int
        the number of trials or observations.

    Returns
    -------
    x_low, x_upp : int
        lower and upper bound of rejection region
    2sr  r  r)   )r  r  r   r   )r  r  )r   r   rp   rh   ri   )valuer<   r7   r  r  r  s         r   binom_test_reject_intervalr    s    $ )))	'''tU33a7&&&tU33a7u::s5zz!!r   rJ   c                    t          j        |dk              st          j        |dk               rt          d          |dv rE	 t          j        | ||          j        }n# t          $ r t          j        | ||          }Y nbw xY w|dv r%t          j        	                    | dz
  ||          }n5|dv r"t          j        
                    | ||          }nt          d	          |S )
aV  
    Perform a test that the probability of success is p.

    This is an exact, two-sided test of the null hypothesis
    that the probability of success in a Bernoulli experiment
    is `p`.

    Parameters
    ----------
    count : {int, array_like}
        the number of successes in nobs trials.
    nobs : int
        the number of trials or observations.
    prop : float, optional
        The probability of success under the null hypothesis,
        `0 <= prop <= 1`. The default value is `prop = 0.5`
    alternative : str in ['two-sided', 'smaller', 'larger']
        alternative hypothesis, which can be two-sided or either one of the
        one-sided tests.

    Returns
    -------
    p-value : float
        The p-value of the hypothesis test

    Notes
    -----
    This uses scipy.stats.binom_test for the two-sided alternative.
    r   rV   zp must be in range [0,1]r  )r   rP   )lr  r   )sr  zAalternative not recognized
should be two-sided, larger or smaller)r*   rF   r,   r   rM   rQ   AttributeErrorrI   r   sfr   )r9   r<   r   r  pvals        r   rI   rI     s   > 
vdSj 5RVD3J// 53444)))	;?5DD999@DD 	; 	; 	;#ETT:::DDD	; 
	'	'{~~eAgtT22	(	(	({udD11 B C C 	CKs   A" "!BBc                     |d| |z   z  }t          | |||          \  }}t          j                            |||          t          j                            |dz
  ||          z
  }|S )NrJ   )r7   r   )r  r   r   r   )r  r  r<   p_altr7   r  r  r   s           r   power_binom_tostr&    si    }sSy!-c3EJJJLE5[__UD%00[__U1WdE::;ELr   c
                 .   | }
t          | |          dz  }|}t          ||          dz  }|}t          ||          dz  }|t          ||          dz  x}}t          |
|||||||||||	          }t          j        |d         d          |dd         fS )a  
    Power of proportions equivalence test based on normal distribution

    Parameters
    ----------
    low, upp : floats
        lower and upper limit of equivalence region
    nobs : int
        number of observations
    p_alt : float in (0,1)
        proportion under the alternative
    alpha : float in (0,1)
        significance level of the test
    dist : str in ['norm', 'binom']
        This defines the distribution to evaluate the power of the test. The
        critical values of the TOST test are always based on the normal
        approximation, but the distribution for the power can be either the
        normal (default) or the binomial (exact) distribution.
    variance_prop : None or float in (0,1)
        If this is None, then the variances for the two one sided tests are
        based on the proportions equal to the equivalence limits.
        If variance_prop is given, then it is used to calculate the variance
        for the TOST statistics. If this is based on an sample, then the
        estimated proportion can be used.
    discrete : bool
        If true, then the critical values of the rejection region are converted
        to integers. If dist is "binom", this is automatically assumed.
        If discrete is false, then the TOST critical values are used as
        floating point numbers, and the power is calculated based on the
        rejection region that is not discretized.
    continuity : bool or float
        adjust the rejection region for the normal power probability. This has
        and effect only if ``dist='norm'``
    critval_continuity : bool or float
        If this is non-zero, then the critical values of the tost rejection
        region are adjusted before converting to integers. This affects both
        distributions, ``dist='norm'`` and ``dist='binom'``.

    Returns
    -------
    power : float
        statistical power of the equivalence test.
    (k_low, k_upp, z_low, z_upp) : tuple of floats
        critical limits in intermediate steps
        temporary return, will be changed

    Notes
    -----
    In small samples the power for the ``discrete`` version, has a sawtooth
    pattern as a function of the number of observations. As a consequence,
    small changes in the number of observations or in the normal approximation
    can have a large effect on the power.

    ``continuity`` and ``critval_continuity`` are added to match some results
    of PASS, and are mainly to investigate the sensitivity of the ztost power
    to small changes in the rejection region. From my interpretation of the
    equations in the SAS manual, both are zero in SAS.

    works vectorized

    **verification:**

    The ``dist='binom'`` results match PASS,
    The ``dist='norm'`` results look reasonable, but no benchmark is available.

    References
    ----------
    SAS Manual: Chapter 68: The Power Procedure, Computational Resources
    PASS Chapter 110: Equivalence Tests for One Proportion.

    r)   N)r7   r  r|   r<   r  r  r   r   )r   r
  r*   r   )r  r  r<   r%  r7   r|   variance_propr  r  r  r   r   r   r   r   r  r   s                    r   power_ztost_propr)  %  s    T HsD!!1$GHsD!!1$GHud##Q&G $]D991<<'7Hgxxd&;MO O OE :eAh""E!""I--r   c                    t          j        |           } t          j        | j        t           j                  }t          j        | |          } t          j        | || z
  f          }|                    d          |                    d          dddf         z  dz  |                                z  }|j        d         }|||fS )a  
    Create a k by 2 contingency table for proportion

    helper function for proportions_chisquare

    Parameters
    ----------
    count : {int, array_like}
        the number of successes in nobs trials.
    nobs : int
        the number of trials or observations.

    Returns
    -------
    table : ndarray
        (k, 2) contingency table

    Notes
    -----
    recent scipy has more elaborate contingency table functions

    r   r   r   Nr   )r*   re   promote_typesrB   float64column_stackr   rm   )r9   r<   dttableexpectedn_rowss         r   _table_proportionr2  }  s    . JuE		%+rz	2	2BJuB'''EOUD5L122Eyy||eiill111d733b8599;;FH[^F(F""r   Fc                 b   t          j        |           } t          j        |          }|j        dk    r|t          j        |           z  }| dz  |z  }t          j        |          }||dk    rt	          d          d}|dk    r||z
  }n,|dk    r|d         |d         z
  |z
  }nd}t          |          t          j        |           dz  t          j        |          z  }	t          j        d|z            }
|r|}	|	d|	z
  z  |
z  }t          j        |          }ddlm	}  ||||          S )	a
  
    Test for proportions based on normal (z) test

    Parameters
    ----------
    count : {int, array_like}
        the number of successes in nobs trials. If this is array_like, then
        the assumption is that this represents the number of successes for
        each independent sample
    nobs : {int, array_like}
        the number of trials or observations, with the same length as
        count.
    value : float, array_like or None, optional
        This is the value of the null hypothesis equal to the proportion in the
        case of a one sample test. In the case of a two-sample test, the
        null hypothesis is that prop[0] - prop[1] = value, where prop is the
        proportion in the two samples. If not provided value = 0 and the null
        is prop[0] = prop[1]
    alternative : str in ['two-sided', 'smaller', 'larger']
        The alternative hypothesis can be either two-sided or one of the one-
        sided tests, smaller means that the alternative hypothesis is
        ``prop < value`` and larger means ``prop > value``. In the two sample
        test, smaller means that the alternative hypothesis is ``p1 < p2`` and
        larger means ``p1 > p2`` where ``p1`` is the proportion of the first
        sample and ``p2`` of the second one.
    prop_var : False or float in (0, 1)
        If prop_var is false, then the variance of the proportion estimate is
        calculated based on the sample proportion. Alternatively, a proportion
        can be specified to calculate this variance. Common use case is to
        use the proportion under the Null hypothesis to specify the variance
        of the proportion estimate.

    Returns
    -------
    zstat : float
        test statistic for the z-test
    p-value : float
        p-value for the z-test

    Examples
    --------
    >>> count = 5
    >>> nobs = 83
    >>> value = .05
    >>> stat, pval = proportions_ztest(count, nobs, value)
    >>> print('{0:0.3f}'.format(pval))
    0.695

    >>> import numpy as np
    >>> from statsmodels.stats.proportion import proportions_ztest
    >>> count = np.array([5, 12])
    >>> nobs = np.array([83, 99])
    >>> stat, pval = proportions_ztest(count, nobs)
    >>> print('{0:0.3f}'.format(pval))
    0.159

    Notes
    -----
    This uses a simple normal test for proportions. It should be the same as
    running the mean z-test on the data encoded 1 for event and 0 for no event
    so that the sum corresponds to the count.

    In the one and two sample cases with two-sided alternative, this test
    produces the same p-value as ``proportions_chisquare``, since the
    chisquare is the distribution of the square of a standard normal
    distribution.
    r   r   Nz*value must be provided for a 1-sample testr   r)   z-more than two samples are not implemented yetr	   )
r*   re   size	ones_liker,   rr   r   rf   statsmodels.stats.weightstatsr
   )r9   r<   r  r  prop_varr   k_samplediffmsgp_pooled	nobs_factvar_std_diffr
   s                 r   proportions_ztestr?    sM   R JuE:dDyA~~bl5)))2:Dwt}}H}q==IJJJ1}}e|	QAwa 5(=!#&&&ve}}r!BF4LL0HrDy!!I q8|$y0Dwt}}H======?4;777r   samplec                    |dk    r|}|}n4|dk    rdx}}n)|dk    rd||z   z  x}}nt          j        |          r|x}}t          | |d||          }t          | |d||          }t          j        |d	         |d	                   ||fS )
aY  
    Equivalence test based on normal distribution

    Parameters
    ----------
    count : {int, array_like}
        the number of successes in nobs trials. If this is array_like, then
        the assumption is that this represents the number of successes for
        each independent sample
    nobs : int
        the number of trials or observations, with the same length as
        count.
    low, upp : float
        equivalence interval low < prop1 - prop2 < upp
    prop_var : str or float in (0, 1)
        prop_var determines which proportion is used for the calculation
        of the standard deviation of the proportion estimate
        The available options for string are 'sample' (default), 'null' and
        'limits'. If prop_var is a float, then it is used directly.

    Returns
    -------
    pvalue : float
        pvalue of the non-equivalence test
    t1, pv1 : tuple of floats
        test statistic and pvalue for lower threshold test
    t2, pv2 : tuple of floats
        test statistic and pvalue for upper threshold test

    Notes
    -----
    checked only for 1 sample case

    limitsr@  FnullrJ   r  )r  r7  r  r  r   )r*   isrealr?  r   )	r9   r<   r  r  r7  prop_var_lowprop_var_uppr  r  s	            r   proportions_ztostrG    s    F 8	X		&++||	V		&)S3Y&77||	8		 /&..|
E4X%1> > >C
E4Y%1> > >C:c!fc!f%%sC00r   c                 .   t          j        |          }t          | |          \  }}}|%t          j        ||z  |d|z
  z  f          }|dz
  }n|}t	          j        |                                |                                |          \  }}||||ffS )a  
    Test for proportions based on chisquare test

    Parameters
    ----------
    count : {int, array_like}
        the number of successes in nobs trials. If this is array_like, then
        the assumption is that this represents the number of successes for
        each independent sample
    nobs : int
        the number of trials or observations, with the same length as
        count.
    value : None or float or array_like

    Returns
    -------
    chi2stat : float
        test statistic for the chisquare test
    p-value : float
        p-value for the chisquare test
    (table, expected)
        table is a (k, 2) contingency table, ``expected`` is the corresponding
        table of counts that are expected under independence with given
        margins

    Notes
    -----
    Recent version of scipy.stats have a chisquare test for independence in
    contingency tables.

    This function provides a similar interface to chisquare tests as
    ``prop.test`` in R, however without the option for Yates continuity
    correction.

    count can be the count for the number of events for a single proportion,
    or the counts for several independent proportions. If value is given, then
    all proportions are jointly tested against this value. If value is not
    given and count and nobs are not scalar, then the null hypothesis is
    that all samples have the same proportion.

    Nr   )ddof)r*   
atleast_1dr2  r-  r   	chisquareravel)	r9   r<   r  r/  r0  r1  rI  chi2statr#  s	            r   proportions_chisquarerN  9  s    T =D/t<<E8V?D5L$!e)2D#EFFz _U[[]]HNN4D4D*.0 0 0NHdTE8,,,r   hsc                      t          t          j        t                     d           } fd|D             }t	          |||          S )a  
    Chisquare test of proportions for all pairs of k samples

    Performs a chisquare test for proportions for all pairwise comparisons.
    The alternative is two-sided

    Parameters
    ----------
    count : {int, array_like}
        the number of successes in nobs trials.
    nobs : int
        the number of trials or observations.
    multitest_method : str
        This chooses the method for the multiple testing p-value correction,
        that is used as default in the results.
        It can be any method that is available in  ``multipletesting``.
        The default is Holm-Sidak 'hs'.

    Returns
    -------
    result : AllPairsResults instance
        The returned results instance has several statistics, such as p-values,
        attached, and additional methods for using a non-default
        ``multitest_method``.

    Notes
    -----
    Yates continuity correction is not available.
    r   c           	          g | ]>}t          t          |                   t          |                             d          ?S r   rN  listr   pairr9   r<   s     r   r   z2proportions_chisquare_allpairs.<locals>.<listcomp>  sN     & & & #5d#4d4::6FGGJ & & &r   multitest_method)r   r*   triu_indicesr   r   )r9   r<   rX  	all_pairspvalss   ``   r   proportions_chisquare_allpairsr\  q  se    > boc%jj!445I& & & & &$& & &E5)>NOOOOr   c                      ||dvrt           d t          dt                               D             } fd|D             }t          |||          S )a  
    Chisquare test of proportions for pairs of k samples compared to control

    Performs a chisquare test for proportions for pairwise comparisons with a
    control (Dunnet's test). The control is assumed to be the first element
    of ``count`` and ``nobs``. The alternative is two-sided, larger or
    smaller.

    Parameters
    ----------
    count : {int, array_like}
        the number of successes in nobs trials.
    nobs : int
        the number of trials or observations.
    multitest_method : str
        This chooses the method for the multiple testing p-value correction,
        that is used as default in the results.
        It can be any method that is available in  ``multipletesting``.
        The default is Holm-Sidak 'hs'.
    alternative : str in ['two-sided', 'smaller', 'larger']
        alternative hypothesis, which can be two-sided or either one of the
        one-sided tests.

    Returns
    -------
    result : AllPairsResults instance
        The returned results instance has several statistics, such as p-values,
        attached, and additional methods for using a non-default
        ``multitest_method``.


    Notes
    -----
    Yates continuity correction is not available.

    ``value`` and ``alternative`` options are not yet implemented.

    Nr  r  c                     g | ]}d |fS r   r   )r   r   s     r   r   z6proportions_chisquare_pairscontrol.<locals>.<listcomp>  s    666A!Q666r   r   c           	          g | ]>}t          t          |                   t          |                             d          ?S rR  rS  rU  s     r   r   z6proportions_chisquare_pairscontrol.<locals>.<listcomp>  sW     & & &  #5d#4d4::6F % %%&( & & &r   rW  )rr   r-   r   r   )r9   r<   r  rX  r  rZ  r[  s   ``     r   "proportions_chisquare_pairscontrolra    s    P 	{2EEE!!66q#e**!5!5666I& & & & & %& & &E 5)>NOOOOr   r9  c           	      b   dddd}|                                 dk    rd}|||         }|                                 }|                    d          rd	}| |z  }	||z  }
|	|
z
  }|d	k    rd
nd}|dk    rL|dv r| |z   |d|z  z   }}||z   |d|z  z   }}||z  }||z  }||z
  }|d
|z
  z  |z  |d
|z
  z  |z  z   }t          j                            |dz            }|t          j        |          z  }||z
  }||z   }n.|                    d          r~t          | |d|          \  }}t          ||d|          \  }}t          j        |	|z
  dz  ||
z
  dz  z             }t          j        |
|z
  dz  ||	z
  dz  z             }||z
  }||z   }n|dk    rt          | ||||||          \  }}nzt          d          |dk    r|dv r|dk    rdnd}| |z   ||z   }}||z   ||z   }}||z  }||z  }||z  } d
|z  d
|z  z
  d
|z  z   d
|z  z
  }t          j                            |dz            }|t          j        |          z  }!t          j
        t          j        |           |!z
            }t          j
        t          j        |           |!z             }n|dk    r!t          | |||||          }"|"j        \  }}nht          d          |dk    rC|dv r|dv r#t          | |||dd          d         }#|#\  }}}}n$|dk    rdnd}| |z   |d|z  z   }}||z   |d|z  z   }}||z  }||z  }|d
|z
  z  |z  d
|z
  z  }$d
|z  d
||z
  z  z   d
|z  z   d
||z
  z  z   }t          j                            |dz            }|t          j        |          z  }!t          j
        t          j        |$          |!z
            }t          j
        t          j        |$          |!z             }n>|dk    rt          | ||||||          \  }}nt          d          t          d          ||fS )u
  
    Confidence intervals for comparing two independent proportions.

    This assumes that we have two independent binomial samples.

    Parameters
    ----------
    count1, nobs1 : float
        Count and sample size for first sample.
    count2, nobs2 : float
        Count and sample size for the second sample.
    method : str
        Method for computing confidence interval. If method is None, then a
        default method is used. The default might change as more methods are
        added.

        diff:
         - 'wald',
         - 'agresti-caffo'
         - 'newcomb' (default)
         - 'score'

        ratio:
         - 'log'
         - 'log-adjusted' (default)
         - 'score'

        odds-ratio:
         - 'logit'
         - 'logit-adjusted' (default)
         - 'score'

    compare : string in ['diff', 'ratio' 'odds-ratio']
        If compare is diff, then the confidence interval is for diff = p1 - p2.
        If compare is ratio, then the confidence interval is for the risk ratio
        defined by ratio = p1 / p2.
        If compare is odds-ratio, then the confidence interval is for the
        odds-ratio defined by or = p1 / (1 - p1) / (p2 / (1 - p2).
    alpha : float
        Significance level for the confidence interval, default is 0.05.
        The nominal coverage probability is 1 - alpha.

    Returns
    -------
    low, upp

    See Also
    --------
    test_proportions_2indep
    tost_proportions_2indep

    Notes
    -----
    Status: experimental, API and defaults might still change.
        more ``methods`` will be added.

    References
    ----------
    .. [1] Fagerland, Morten W., Stian Lydersen, and Petter Laake. 2015.
       “Recommended Confidence Intervals for Two Independent Binomial
       Proportions.” Statistical Methods in Medical Research 24 (2): 224–54.
       https://doi.org/10.1177/0962280211415469.
    .. [2] Koopman, P. A. R. 1984. “Confidence Intervals for the Ratio of Two
       Binomial Proportions.” Biometrics 40 (2): 513–17.
       https://doi.org/10.2307/2531405.
    .. [3] Miettinen, Olli, and Markku Nurminen. "Comparative analysis of two
       rates." Statistics in medicine 4, no. 2 (1985): 213-226.
    .. [4] Newcombe, Robert G. 1998. “Interval Estimation for the Difference
       between Independent Proportions: Comparison of Eleven Methods.”
       Statistics in Medicine 17 (8): 873–90.
       https://doi.org/10.1002/(SICI)1097-0258(19980430)17:8<873::AID-
       SIM779>3.0.CO;2-I.
    .. [5] Newcombe, Robert G., and Markku M. Nurminen. 2011. “In Defence of
       Score Intervals for Proportions and Their Differences.” Communications
       in Statistics - Theory and Methods 40 (7): 1271–82.
       https://doi.org/10.1080/03610920903576580.
    newcomblog-adjustedlogit-adjustedr9  r   
odds-ratioorrg  Nagragresti-caffor   r   r9  waldrj  r)   rZ   )rt   r7   score)comparer7   
correctionmethod not recognizedr   r   rd  rJ   )r7   ro  logitre  logit-smoothedrt  Fshrink_factorreturn_corrzcompare not recognized)r   
startswithr   rg   rh   r*   rf   r   _score_confint_inversionr,   r   r   _confint_riskratio_koopmanconfint_shrink_prob)%count1nobs1count2nobs2rt   rn  r7   ro  method_defaultr   r   r9  addonecount1_nobs1_count2_nobs2_p1_p2_diff_varzd_waldr  r  low1upp1low2upp2d_lowd_uppaddhalfratio_d_logresadjustedodds_ratio_s%                                        r   confint_proportions_2indepr    sr   ^ (-$46 6N }}$~(\\^^F ! 	%B	%B7DO++QQF&...$vouq6z/AVG$vouq6z/AVGF"CF"C#IES/F*SAG_v-EEC
uqy))A%F&.C&.CCy)) 	6+FE3;5J J JJD$+FE3;5J J JJD$GR$YNdRi!^;<<EGR$YNdRi!^;<<E,C,CCw/vu8?u;EG G GHC
 4555	G		,,,#~55cc1G$w.VG$w.VGF"CF"C3YFw;!f*,q7{:QZGC
uqy))A$E&%/00C&%/00CCw,VUFE388BD D DC {HC 4555	L	 	 BBB+++'vu67UL L LLMO3;0&& "(+;!;!;##"(7"2EAK4G"(7"2EAK4GF"CF"CS/C/1s7;Kw;fw&6!77w;!"fw&6!78C
uqy))A$E&,,u455C&,,u455CCw/vu8?u;EG G GHC
 4555 12228Or   r)   c                 4   t          d | |||fD                       }|rt          d          t          j        | |z   || z
  |z   |z
  g          }t          j        ||g          }||z   }	||dddf         z  |	dz  z  }
||
z  }|rB|d         |d                                         |d         |d                                         fS | |d         z   ||d                                         z   ||d         z   ||d                                         z   f|
fS )	a  
    Shrink observed counts towards independence

    Helper function for 'logit-smoothed' inference for the odds-ratio of two
    independent proportions.

    Parameters
    ----------
    count1, nobs1 : float or int
        count and sample size for first sample
    count2, nobs2 : float or int
        count and sample size for the second sample
    shrink_factor : float
        This corresponds to the number of observations that are added in total
        proportional to the probabilities under independence.
    return_corr : bool
        If true, then only the correction term is returned
        If false, then the corrected counts, i.e. original counts plus
        correction term, are returned.

    Returns
    -------
    count1_corr, nobs1_corr, count2_corr, nobs2_corr : float
        correction or corrected counts
    prob_indep :
        TODO/Warning : this will change most likely
        probabilities under independence, only returned if return_corr is
        false.

    c              3   F   K   | ]}t          j        |          d k    V  dS )r   N)r*   r4  )r   is     r   r   z_shrink_prob.<locals>.<genexpr>  s.      LLRWQZZ!^LLLLLLr   zfunction is not vectorizedNr)   )r   r   r   )r   r   r   )rF   r,   r*   r   r   )r~  r  r  r  rw  rx  
vectorizednobs_colnobs_rowr<   
prob_indepcorrs               r   r}  r}    s7   @ LLfeVU-KLLLLLJ 75666x&%&.5*@6*IJKKHx''H5=DXaaag..$'9J:%D HT
DGKKMM4:tAw{{}}EEd#UT!W[[]]%:d#UT!W[[]]%:<=GH 	Hr   c	           
         |dk    rdnd}	||	}||z   }
| |z   }| |z  }||z  }||	k    r||
z  x}}||}}|}|dk    r|}|dk    r|
}|d|z  z   |z  |
z
  |z
  }||z  |
z
  d|z  z
  |z  |z   }||z  d|z
  z  }|d|z  z  dz  ||z  d|dz  z  z  z
  |d|z  z  z   }t          j        |          t          j        |d|z  z  dz  |d|z  z  z
            z  }t           j        t          j        ||dz  z            z   dz  }d|z  t          j        |          z  |d|z  z  z
  }||z   }|d|z
  z  |z  |d|z
  z  |z  z   }|r||
|
dz
  z  z  }||z
  |z
  }ng|dk    r|}|dk    rH|
|z  }||z  | z   |z   ||z  z    }|}| t          j        |dz  d	|z  |z  z
            z
  d|z  z  }||z  }|d|z
  z  |z  |dz  |z  d|z
  z  |z  z   }|r||
|
dz
  z  z  }|||z  z
  }n|d
v r|} | dk    rW|| dz
  z  }|| z  |z   || dz
  z  z
  }| }| t          j        |dz  d	|z  |z  z
            z   d|z  z  }|| z  d|| dz
  z  z   z  }d}!t          j        ||!d|!z
            }t          j        ||!d|!z
            }d|d|z
  z  |z  z  d|d|z
  z  |z  z  z   }|r||
|
dz
  z  z  }||z
  |d|z
  z  z  ||z
  |d|z
  z  z  z
  }t          |t          j        |          |          \  }"}#|rt          |"|#|d||||          }$|$S |"|#fS )a  
    Score test for two independent proportions

    This uses the constrained estimate of the proportions to compute
    the variance under the Null hypothesis.

    Parameters
    ----------
    count1, nobs1 :
        count and sample size for first sample
    count2, nobs2 :
        count and sample size for the second sample
    value : float
        diff, ratio or odds-ratio under the null hypothesis. If value is None,
        then equality of proportions under the Null is assumed,
        i.e. value=0 for 'diff' or value=1 for either rate or odds-ratio.
    compare : string in ['diff', 'ratio' 'odds-ratio']
        If compare is diff, then the confidence interval is for diff = p1 - p2.
        If compare is ratio, then the confidence interval is for the risk ratio
        defined by ratio = p1 / p2.
        If compare is odds-ratio, then the confidence interval is for the
        odds-ratio defined by or = p1 / (1 - p1) / (p2 / (1 - p2)
    return_results : bool
        If true, then a results instance with extra information is returned,
        otherwise a tuple with statistic and pvalue is returned.

    Returns
    -------
    results : results instance or tuple
        If return_results is True, then a results instance with the
        information in attributes is returned.
        If return_results is False, then only ``statistic`` and ``pvalue``
        are returned.

        statistic : float
            test statistic asymptotically normal distributed N(0, 1)
        pvalue : float
            p-value based on normal distribution
        other attributes :
            additional information about the hypothesis test

    Notes
    -----
    Status: experimental, the type or extra information in the return might
    change.

    r9  r   r   Nr)   r   r   r   r[   )rh  rg  g|=r  rm  )	statisticrQ   rn  rt   variancer  
prop1_null
prop2_null)	r*   r+   rf   r   arccoscosrs   r
   r   )%r~  r  r  r  r  rn  r  ro  return_resultsvalue_defaultr<   r9   r   r   prop0r   count0nobs0p0r9  tmp3tmp2tmp1tmp0qrP   r   r  	diff_statr   r   r   oratior   r  rQ   r  s%                                        r   score_test_proportions_2indepr    s   f !F**AAM}5=DVOE	%B	%B $EEF	B&199DAI%-4u<DTMD(1v:5=EDD=AH-D!d(#a'$+T1W*EET"#A

RWda$h&7!%;%)QX%6&7 8 8 8A1q!t8,,,1AEBF1II%D(99EDLEq5y!E)EQY,?%,GG 	%44!8$$C"Wt^			G		A::uA%-&(506E>ABAAR"'!Q$Q"2333A>EEMEE	"U*ax%1u9-56 	%44!8$$C %"*_			(	(	(Q;;!$A&&1*)==AAR"'!Q$Q"2333A>EFNa%6A:*>&>?E sAG,,sAG,,EQY'%/0EQY'%/01 	%44!8$$C5jUa%i%895jUa%i%89:	 (	273<<4?A A AIv  !I!'")!(#&&1%*%*   
&  r   c
                    dddd}
|                                 dk    rd}||
|         }|                                 }|                    d          rd}|
|d	k    rd
nd}t          t          j        | |||g          \  } }}}| |z  }||z  }||z
  }||z  }|d|z
  z  |z  d|z
  z  }d}|d	k    r|dv rh|dk    rdnd
}| |z   |d|z  z   }}||z   |d|z  z   }}||z  }||z  }||z
  |z
  }|d|z
  z  |z  |d|z
  z  |z  z   }|t          j        |          z  }d}ni|                    d          rd}t          |          |dk    r/t          | ||||||||		  	        }|	du r|dd         \  }}d}d}nt          d          |dk    r|dv r|dk    rdnd
}| |z   ||z   }}||z   ||z   }}||z  }||z  }||z  }d|z  d|z  z
  d|z  z   d|z  z
  }t          j	        |          t          j	        |          z
  }|t          j        |          z  }d}nm|dk    r/t          | ||||||||		  	        }|	du r|dd         \  }}d}d}n8t          d          |dk    r|dv r|dv r#t          | |||dd          d
         } | \  }}}}n$|dk    rdnd
}| |z   |d|z  z   }}||z   |d|z  z   }}||z  }||z  }|d|z
  z  |z  d|z
  z  }!d|z  d||z
  z  z   d|z  z   d||z
  z  z   }t          j	        |!          t          j	        |          z
  }|t          j        |          z  }d}nX|dk    r.t          | ||||||||		  	        }|	du r|dd         \  }}d}d}n$t          d|z            t          d|z            |dk    r)|'t          |t          j        |          |          \  }}|	r:|t          ||||||||||
  
        }n||_        ||_        ||_        ||_        |S ||fS )a  
    Hypothesis test for comparing two independent proportions

    This assumes that we have two independent binomial samples.

    The Null and alternative hypothesis are

    for compare = 'diff'

    - H0: prop1 - prop2 - value = 0
    - H1: prop1 - prop2 - value != 0  if alternative = 'two-sided'
    - H1: prop1 - prop2 - value > 0   if alternative = 'larger'
    - H1: prop1 - prop2 - value < 0   if alternative = 'smaller'

    for compare = 'ratio'

    - H0: prop1 / prop2 - value = 0
    - H1: prop1 / prop2 - value != 0  if alternative = 'two-sided'
    - H1: prop1 / prop2 - value > 0   if alternative = 'larger'
    - H1: prop1 / prop2 - value < 0   if alternative = 'smaller'

    for compare = 'odds-ratio'

    - H0: or - value = 0
    - H1: or - value != 0  if alternative = 'two-sided'
    - H1: or - value > 0   if alternative = 'larger'
    - H1: or - value < 0   if alternative = 'smaller'

    where odds-ratio or = prop1 / (1 - prop1) / (prop2 / (1 - prop2))

    Parameters
    ----------
    count1 : int
        Count for first sample.
    nobs1 : int
        Sample size for first sample.
    count2 : int
        Count for the second sample.
    nobs2 : int
        Sample size for the second sample.
    value : float
        Value of the difference, risk ratio or odds ratio of 2 independent
        proportions under the null hypothesis.
        Default is equal proportions, 0 for diff and 1 for risk-ratio and for
        odds-ratio.
    method : string
        Method for computing the hypothesis test. If method is None, then a
        default method is used. The default might change as more methods are
        added.

        diff:

        - 'wald',
        - 'agresti-caffo'
        - 'score' if correction is True, then this uses the degrees of freedom
           correction ``nobs / (nobs - 1)`` as in Miettinen Nurminen 1985

        ratio:

        - 'log': wald test using log transformation
        - 'log-adjusted': wald test using log transformation,
           adds 0.5 to counts
        - 'score': if correction is True, then this uses the degrees of freedom
           correction ``nobs / (nobs - 1)`` as in Miettinen Nurminen 1985

        odds-ratio:

        - 'logit': wald test using logit transformation
        - 'logit-adjusted': wald test using logit transformation,
           adds 0.5 to counts
        - 'logit-smoothed': wald test using logit transformation, biases
           cell counts towards independence by adding two observations in
           total.
        - 'score' if correction is True, then this uses the degrees of freedom
           correction ``nobs / (nobs - 1)`` as in Miettinen Nurminen 1985

    compare : {'diff', 'ratio' 'odds-ratio'}
        If compare is `diff`, then the hypothesis test is for the risk
        difference diff = p1 - p2.
        If compare is `ratio`, then the hypothesis test is for the
        risk ratio defined by ratio = p1 / p2.
        If compare is `odds-ratio`, then the hypothesis test is for the
        odds-ratio defined by or = p1 / (1 - p1) / (p2 / (1 - p2)
    alternative : {'two-sided', 'smaller', 'larger'}
        alternative hypothesis, which can be two-sided or either one of the
        one-sided tests.
    correction : bool
        If correction is True (default), then the Miettinen and Nurminen
        small sample correction to the variance nobs / (nobs - 1) is used.
        Applies only if method='score'.
    return_results : bool
        If true, then a results instance with extra information is returned,
        otherwise a tuple with statistic and pvalue is returned.

    Returns
    -------
    results : results instance or tuple
        If return_results is True, then a results instance with the
        information in attributes is returned.
        If return_results is False, then only ``statistic`` and ``pvalue``
        are returned.

        statistic : float
            test statistic asymptotically normal distributed N(0, 1)
        pvalue : float
            p-value based on normal distribution
        other attributes :
            additional information about the hypothesis test

    See Also
    --------
    tost_proportions_2indep
    confint_proportions_2indep

    Notes
    -----
    Status: experimental, API and defaults might still change.
        More ``methods`` will be added.

    The current default methods are

    - 'diff': 'agresti-caffo',
    - 'ratio': 'log-adjusted',
    - 'odds-ratio': 'logit-adjusted'

    rj  rd  re  rf  rh  rg  Nri  r9  r   r   rk  r)   r6   rc  z)newcomb not available for hypothesis testrm  )r  rn  r  ro  r  Frp  r   rq  rJ   rr  ru  rv  zmethod "%s" not recognizedzcompare "%s" not recognizedr  )
r  rQ   rn  rt   r9  r   
odds_ratior  r  r  )r   ry  mapr*   re   rf   rr   r  r,   r   r}  r
   r   r9  r   r  r  )"r~  r  r  r  r  rt   rn  r  ro  r  r  r   r   r9  r   r  r  r  r  r  r  r  r  r  r  r  r  distrr:  rQ   r  r  r  r  s"                                     r   test_proportions_2indepr  O  s   D .-$46 6N }}$~(\\^^F ! }&&A#&rz(.vu'E$G $G FE65 
%B	%B7DGEq2v#q2v.J
C&... O33QQF$vouq6z/AVG$vouq6z/AVGF"CF"Cc	E)IS/F*SAG_v-EEC!BGCLL0IEEy)) 	6=C%c***w/vu6;W<G;E?M	O O OC
 &&$'G!	6E II4555	G		,,,#~55cc1G$w.VG$w.VGF"CF"C3YFw;!f*,q7{:QZGCv6I!BGCLL0IEEw/vu6;W<G;E?M	O O OC
 &&$'G!	6EII 4555	L	 	 BBB+++'vu67UL L LLMO3;0&& "(+;!;!;##"(7"2EAK4G"(7"2EAK4GF"CF"CS/C/1s7;Kw;fw&6!77w;!"fw&6!78C {++bfUmm;I!BGCLL0IEEw/vu6;W<G;E?M	O O OC
 &&$'G!	6EII9FBCCC 6@AAAY2+Irws||8CE E E	6  !;	%+&-%+#'$))3'**5$)
  
  
 CC CHCI'CNCI
&  r   c	                 j   t          | ||||||d|d
  
        }	t          | ||||||d|d
  
        }
t          j        |	j        |
j        k     t                    }t          j        ||	j        |
j        g          }t          j        ||	j        |
j        g          }t          |||||	|
d          }|S )a  
    Equivalence test based on two one-sided `test_proportions_2indep`

    This assumes that we have two independent binomial samples.

    The Null and alternative hypothesis for equivalence testing are

    for compare = 'diff'

    - H0: prop1 - prop2 <= low or upp <= prop1 - prop2
    - H1: low < prop1 - prop2 < upp

    for compare = 'ratio'

    - H0: prop1 / prop2 <= low or upp <= prop1 / prop2
    - H1: low < prop1 / prop2 < upp


    for compare = 'odds-ratio'

    - H0: or <= low or upp <= or
    - H1: low < or < upp

    where odds-ratio or = prop1 / (1 - prop1) / (prop2 / (1 - prop2))

    Parameters
    ----------
    count1, nobs1 :
        count and sample size for first sample
    count2, nobs2 :
        count and sample size for the second sample
    low, upp :
        equivalence margin for diff, risk ratio or odds ratio
    method : string
        method for computing the hypothesis test. If method is None, then a
        default method is used. The default might change as more methods are
        added.

        diff:
         - 'wald',
         - 'agresti-caffo'
         - 'score' if correction is True, then this uses the degrees of freedom
           correction ``nobs / (nobs - 1)`` as in Miettinen Nurminen 1985.

        ratio:
         - 'log': wald test using log transformation
         - 'log-adjusted': wald test using log transformation,
            adds 0.5 to counts
         - 'score' if correction is True, then this uses the degrees of freedom
           correction ``nobs / (nobs - 1)`` as in Miettinen Nurminen 1985.

        odds-ratio:
         - 'logit': wald test using logit transformation
         - 'logit-adjusted': : wald test using logit transformation,
            adds 0.5 to counts
         - 'logit-smoothed': : wald test using logit transformation, biases
            cell counts towards independence by adding two observations in
            total.
         - 'score' if correction is True, then this uses the degrees of freedom
            correction ``nobs / (nobs - 1)`` as in Miettinen Nurminen 1985

    compare : string in ['diff', 'ratio' 'odds-ratio']
        If compare is `diff`, then the hypothesis test is for
        diff = p1 - p2.
        If compare is `ratio`, then the hypothesis test is for the
        risk ratio defined by ratio = p1 / p2.
        If compare is `odds-ratio`, then the hypothesis test is for the
        odds-ratio defined by or = p1 / (1 - p1) / (p2 / (1 - p2).
    correction : bool
        If correction is True (default), then the Miettinen and Nurminen
        small sample correction to the variance nobs / (nobs - 1) is used.
        Applies only if method='score'.

    Returns
    -------
    pvalue : float
        p-value is the max of the pvalues of the two one-sided tests
    t1 : test results
        results instance for one-sided hypothesis at the lower margin
    t1 : test results
        results instance for one-sided hypothesis at the upper margin

    See Also
    --------
    test_proportions_2indep
    confint_proportions_2indep

    Notes
    -----
    Status: experimental, API and defaults might still change.

    The TOST equivalence test delegates to `test_proportions_2indep` and has
    the same method and comparison options.

    r  T)r  rt   rn  r  ro  r  r  z.Equivalence test for 2 independent proportions)r  rQ   rn  rt   results_largerresults_smallertitle)r  r*   re   rQ   ri   chooser  r   )r~  r  r  r  r  r  rt   rn  ro  r  r  idx_maxr  rQ   r  s                  r   tost_proportions_2indepr  j  s    D "&%c)/.6-715	7 7 7C
 "&%c)/.7-715	7 7 7C jcj0#66G	'CM3=#ABBIYwSZ 899F
	#%#%(&)L  C Jr   c                     |dk    rd}t          |          |}|| z   }|||z  z   d|z   z  }||}
}	|}|| z   }t          |	|
|          }t          |||          }|||fS )z
    Compute standard error under null and alternative for 2 proportions

    helper function for power and sample size computation

    r   z7non-zero diff under null, value, is not yet implementedr   )r   )rr   r   )r9  r   r   r7   r  r:  
nobs_ratior   r;  p1_vnullp2_vnullp2_altp1_altstd_nullr  s                  r   _std_2prop_powerr    s     zzG!#&&&J	dB R%ZAI.H!8hHFd]Fh
CCCHVV:>>>GXw&&r   c           
          ddl m} t          | ||||          \  }	}
} || |||
||          }|rt          ||	|
||||z  ||          }|S |S )aq  
    Power for ztest that two independent proportions are equal

    This assumes that the variance is based on the pooled proportion
    under the null and the non-pooled variance under the alternative

    Parameters
    ----------
    diff : float
        difference between proportion 1 and 2 under the alternative
    prop2 : float
        proportion for the reference case, prop2, proportions for the
        first case will be computed using p2 and diff
        p1 = p2 + diff
    nobs1 : float or int
        number of observations in sample 1
    ratio : float
        sample size ratio, nobs2 = ratio * nobs1
    alpha : float in interval (0,1)
        Significance level, e.g. 0.05, is the probability of a type I
        error, that is wrong rejections if the Null Hypothesis is true.
    value : float
        currently only `value=0`, i.e. equality testing, is supported
    alternative : string, 'two-sided' (default), 'larger', 'smaller'
        Alternative hypothesis whether the power is calculated for a
        two-sided (default) or one sided test. The one-sided test can be
        either 'larger', 'smaller'.
    return_results : bool
        If true, then a results instance with extra information is returned,
        otherwise only the computed power is returned.

    Returns
    -------
    results : results instance or float
        If return_results is True, then a results instance with the
        information in attributes is returned.
        If return_results is False, then only the power is returned.

        power : float
            Power of the test, e.g. 0.8, is one minus the probability of a
            type II error. Power is the probability that the test correctly
            rejects the Null Hypothesis if the Alternative Hypothesis is true.

        Other attributes in results instance include :

        p_pooled
            pooled proportion, used for std_null
        std_null
            standard error of difference under the null hypothesis (without
            sqrt(nobs1))
        std_alt
            standard error of difference under the alternative hypothesis
            (without sqrt(nobs1))
    r   )normal_power_hetr   r7   r  )r  std_alternativer  )r   r;  r  r  r  r  r  r7   )statsmodels.stats.powerr  r  r   )r9  r   r  r   r7   r  r  r  r  r;  r  r  pow_r  s                 r   power_proportions_2indepr    s    t 988888"24e9>e#M #M #MHh D%,3(35 5 5D  4&&$  5= %    
r   c                 t    ddl m} |dv r|dz  }t          | ||||          \  }}	}
 || |||	|
          }|S )a  
    Required sample size assuming normal distribution based on one tail

    This uses an explicit computation for the sample size that is required
    to achieve a given power corresponding to the appropriate tails of the
    normal distribution. This ignores the far tail in a two-sided test
    which is negligible in the common case when alternative and null are
    far apart.

    Parameters
    ----------
    diff : float
        Difference between proportion 1 and 2 under the alternative
    prop2 : float
        proportion for the reference case, prop2, proportions for the
        first case will be computing using p2 and diff
        p1 = p2 + diff
    power : float
        Power for which sample size is computed.
    ratio : float
        Sample size ratio, nobs2 = ratio * nobs1
    alpha : float in interval (0,1)
        Significance level, e.g. 0.05, is the probability of a type I
        error, that is wrong rejections if the Null Hypothesis is true.
    value : float
        Currently only `value=0`, i.e. equality testing, is supported
    alternative : string, 'two-sided' (default), 'larger', 'smaller'
        Alternative hypothesis whether the power is calculated for a
        two-sided (default) or one sided test. In the case of a one-sided
        alternative, it is assumed that the test is in the appropriate tail.

    Returns
    -------
    nobs1 : float
        Number of observations in sample 1.
    r   )normal_sample_size_one_tailr^  r)   r  )r  r  )r  r  r  )r9  r   r   r   r7   r  r  r  r3   r  r  r<   s               r   %samplesize_proportions_2indep_onetailr  T  s    P DCCCCC)))	+D%u27uF F FAx '&tUEH7>@ @ @DKr   c                      fd}t           ddd	  	        }dddd	}	t           |	         
          }
|
d         t          j        |
d                   dz  z   }|
d         t          j        |
d                   dz  z
  }dk    r|j        }t          |d          }ndk    r|j        }|dz  }dk    r|j        }t          j	        |||          }t          j	        |||          }||fS )a  
    Compute score confidence interval by inverting score test

    Parameters
    ----------
    count1, nobs1 :
        Count and sample size for first sample.
    count2, nobs2 :
        Count and sample size for the second sample.
    compare : string in ['diff', 'ratio' 'odds-ratio']
        If compare is `diff`, then the confidence interval is for
        diff = p1 - p2.
        If compare is `ratio`, then the confidence interval is for the
        risk ratio defined by ratio = p1 / p2.
        If compare is `odds-ratio`, then the confidence interval is for the
        odds-ratio defined by or = p1 / (1 - p1) / (p2 / (1 - p2).
    alpha : float in interval (0,1)
        Significance level, e.g. 0.05, is the probability of a type I
        error, that is wrong rejections if the Null Hypothesis is true.
    correction : bool
        If correction is True (default), then the Miettinen and Nurminen
        small sample correction to the variance nobs / (nobs - 1) is used.
        Applies only if method='score'.

    Returns
    -------
    low : float
        Lower confidence bound.
    upp : float
        Upper confidence bound.
    c                 H    t          | dd	  	        }|j        z
  S )Nrm  r  r  rn  rt   ro  r  )r  rQ   )	r   r   r7   rn  ro  r~  r  r  r  s	     r   r   z&_score_confint_inversion.<locals>.func  s;    #FE65*+WW/90;= = = x%r   r   rm  r  r  rl  r   rs  rf  )rt   rn  r7   r   rJ   g      ?r9  gwJ?r   r)   rg  )
r  r  r*   absr9  minr   r  r   ro   )r~  r  r  r  rn  r7   ro  r   rt0
use_methodrci0r&   r%   paramr  r  s   ```````         r   rz  rz    sl   D                      "&%()77-7.9; ; ;C !5HHJ%feVU-7-@.5UD D DD
 
a26$q'??S(	(B	a26$q'??T)	)B& W	G			
a, /$r
*
*C
/$E
*
*C8Or   c                 f   || ||f\  }}}}	||z   }
||	z   }t           j                            |dz            dz  }|r|||dz
  z  z  }|||z  |z  |	||z   z  |z  z   z  }| ||	z  |
z  d|z  |z  |z  z   |	||z   d|z  z   z  |z  z   z  }d|z  |	z  |z  |
z  ||z  |z  |z  z   ||	z  |
z  |z  z   }|	 |z  |z  |
z  }t          j        t          j        ||||g                    }|dd         ddd         }d|	|z
  d|z
  z  ||	z   ||z  z
  z  z
  |z  }t                      }||_        ||_        |S )z
    Score confidence interval for ratio or proportions, Koopman/Nam

    signature not consistent with other functions

    When correction is True, then the small sample correction nobs / (nobs - 1)
    by Miettinen/Nurminen is used.
    r)   r   N)	r   rg   rh   r*   sortrootsr   r|  _p_roots)r~  r  r  r  r7   ro  x0x1n0n1r    r   r  a1a2a3a4p_roots_p_rootscir  s                        r   r{  r{    s    VUE1NBB
RA
RA
uqy!!1$A 	Q!a%[ 
rAv{R27^a//	0Bb1q1urzB.rBwR7G1H11LL	MB	
R"r	A	Bb 0	027Q;?	BBR!	BwrxRR 01122Hrrl44R4 G rBw1w;'27Q[+@A
AW	LB
((CCKCLJr   c           	         t          j        |           \  }}}}t          j        |           }||z  ||z  }}||z   |z  }	||z   |z  }
d||z  z
  }t          j                            |dz            dz  }||
z  |dz  z   |
z  }d|z  |	z  |
z  ||z  z    }||	z  |dz  z   |	z  }|dz  ||
z  dz  dz  z
  }d|z  |z  }|dz  d|z  |z  z   |dz  |	|
z  d|z  |z  z
  z  dz  z   }d|z  |z  }|dz  ||	z  dz  dz  z
  }t          j        t          j        |||||g                    }|                                |	                                g}t                      }||_        |	|
f|_        ||_        |S )a  
    Confidence interval for marginal risk ratio for matched pairs

    need full table

             success fail  marginal
    success    x11    x10  x1.
    fail       x01    x00  x0.
    marginal   x.1    x.0   n

    The confidence interval is for the ratio p1 / p0 where
    p1 = x1. / n and
    p0 - x.1 / n
    Todo: rename p1 to pa and p2 to pb, so we have a, b for treatment and
    0, 1 for success/failure

    current namings follow Nam 2009

    status
    testing:
    compared to example in Nam 2009
    internal polynomial coefficients in calculation correspond at around
        4 decimals
    confidence interval agrees only at 2 decimals

    r   r)   )r*   rL  r   r   rg   rh   r  r  r  maxr   r|  rP   r  )r/  r7   x11x10x01x00r   p10p01r   r  q00z2r   r   g3a0r  r  r  r  r  r  r  s                           r   _confint_riskratio_paired_namr    s   6 %Cc3
uAQwaC
)qB
)qB
cAg+C			"	"A	%B b&26/R	BEBJOb3h&	'B
b&26/R	B	Q"r'A+!	!B	
R"B	QR"	r1uR!c'C-(?@1D	DB	
R"B	Q"r'A+!	!BgbhBB34455G ++--	'B
((CCKFCECLJr   )T)r$   )r5   r6   )r5   r   )r6   rR  )r5   Trg   Nr   r   )r5   )r5   r  )rJ   r  )Nr5   )r5   rg   NTr   r   )Nr  F)r@  )N)rO  )NrO  r  )Nr9  r5   T)r)   T)Nr9  r  TT)NNr9  r  TT)Nr9  T)r   r5   r   )r   r5   r   r  T)r   r5   r   r  )r9  r5   T)r5   T)=__doc__statsmodels.compat.pythonr   typingr   numpyr*   pandasr`   scipyr   r   statsmodels.stats.baser   r   r6  r
   statsmodels.tools.sm_exceptionsr   statsmodels.tools.testingr   statsmodels.tools.validationr   finforj   r   boolr#   ri   r   r4   r   r   r   r   r   r   r
  r  r  r  rI   r&  r)  r2  r?  rG  rN  r\  ra  r  r}  r  r  r  r  r  r  rz  r{  r  r   r   r   <module>r	     s    + * * * * *               ! ! ! ! ! ! ! ! ? ? ? ? ? ? ? ? 9 9 9 9 9 9 A A A A A A , , , , , , 3 3 3 3 3 3RXe__
 >B &  &
E7E>
" &(- &6: &
 &  &  &  &H HJ0 0
E7E>
"0(-0380AD0
5%<0 0 0 0fC C% C C C CLD D D DN BF)1# # # #L( ( ( (V. . .*: : : :
 ?C23*/ */ */ */Z+ + +6   6" " " "D. . . .b    >DCD()U. U. U. U.p# # #@ <G$e8 e8 e8 e8P11 11 11 11h5- 5- 5- 5-p"P "P "P "PJ ;?BM0P 0P 0P 0Pf EIFJy y y yx >?!,H ,H ,H ,H^ GK>IBFX! X! X! X!v AE17@D+/X! X! X! X!x EI{ { { {|' ' ' '4 AE2=,0O O O Od EF<=6A2 2 2 2j DJ48G G G GT DH*." " " "J8 8 8 8 8 8r   