ó
áp7]c           @  si  d  Z  d d l m Z d d l m Z d d l Z d d l m Z m	 Z	 d d l
 m Z d d d  Z d d	 d
  Z d d d  Z d d  Z d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ e   j Z e   j Z e   j Z d S(   sD  Statistical power, solving for nobs, ... - trial version

Created on Sat Jan 12 21:48:06 2013

Author: Josef Perktold

Example
roundtrip - root with respect to all variables

       calculated, desired
nobs   33.367204205 33.367204205
effect 0.5 0.5
alpha  0.05 0.05
power   0.8 0.8


TODO:
refactoring
 - rename beta -> power,    beta (type 2 error is beta = 1-power)  DONE
 - I think the current implementation can handle any kinds of extra keywords
   (except for maybe raising meaningful exceptions
 - streamline code, I think internally classes can be merged
   how to extend to k-sample tests?
   user interface for different tests that map to the same (internal) test class
 - sequence of arguments might be inconsistent,
   arg and/or kwds so python checks what's required and what can be None.
 - templating for docstrings ?


i˙˙˙˙(   t   print_function(   t	   iteritemsN(   t   statst   optimize(   t   brentq_expandings	   two-sidedc   
      C  sE  |  } | d
 k r | d } n  | d k r8 | d } n% | d k rM | } n t d d   d	 } | d k rĐ t j j | |  } t j t j |   r¨ t j } qĐ t j	 j
 | | | t j |   } n  | d k rAt j j | |  }	 t j t j |	   rt j } qA| t j	 j |	 | | t j |   7} n  | S(   s   Calculate power of a ttest
    i   s	   two-sidedt   2sg       @t   smallert   largers,   alternative has to be 'two-sided', 'larger' s   or 'smaller'i    N(   s	   two-sidedR   (   R   R   (   s	   two-sidedR   R   (   s	   two-sidedR   R   (   t   Nonet
   ValueErrorR   t   tt   isft   npt   anyt   isnant   nant   nctt   _sft   sqrtt   ppft   _cdf(
   t   effect_sizet   nobst   alphat   dft   alternativet   dt   alpha_t   pow_t   crit_uppt   crit_low(    (    s6   lib/python2.7/site-packages/statsmodels/stats/power.pyt   ttest_power'   s*    	
(,g      đ?c   	      C  sâ   |  } | d	 k r | d } n% | d
 k r4 | } n t  d d   d } | d k r t j j |  } t j j | | t j |  |  } n  | d k rŢ t j j |  } | t j j | | t j |  |  7} n  | S(   s<   Calculate power of a normal distributed test statistic

    s	   two-sidedR   g       @R   R   s,   alternative has to be 'two-sided', 'larger' s   or 'smaller'i    (   s	   two-sidedR   (   R   R   (   s	   two-sidedR   R   (   s	   two-sidedR   R   (	   R	   R   t   normR   t   sfR   R   R   t   cdf(	   R   R   R   R   t   sigmaR   R   R   t   crit(    (    s6   lib/python2.7/site-packages/statsmodels/stats/power.pyt   normal_powerI   s    	
*.i   c   	      C  sS   | | } | d } t  j j | | |  } t  j j | | | |  d |  } | S(   sľ   power for ftest for one way anova with k equal sized groups

    nobs total sample size, sum over all groups

    should be general nobs observations, k_groups restrictions ???
    i   i   (   R   t   fR   t   ncfR!   (	   R   R   R   t   k_groupsR   t   df_numt   df_denomR$   R   (    (    s6   lib/python2.7/site-packages/statsmodels/stats/power.pyt   ftest_anova_power`   s
    

#i   c         C  sM   |  d | | | } t  j j | | |  } t  j j | | | |  } | S(   sŃ  Calculate the power of a F-test.

    Parameters
    ----------
    effect_size : float
        standardized effect size, mean divided by the standard deviation.
        effect size has to be positive.
    df_num : int or float
        numerator degrees of freedom.
    df_denom : int or float
        denominator degrees of freedom.
    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.
    ncc : int
        degrees of freedom correction for non-centrality parameter.
        see Notes

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

    Notes
    -----

    sample size is given implicitly by df_num

    set ncc=0 to match t-test, or f-test in LikelihoodModelResults.
    ncc=1 matches the non-centrality parameter in R::pwr::pwr.f2.test

    ftest_power with ncc=0 should also be correct for f_test in regression
    models, with df_num and d_denom as defined there. (not verified yet)
    i   (   R   R&   R   R'   R!   (   R   R)   R*   R   t   ncct   ncR$   R   (    (    s6   lib/python2.7/site-packages/statsmodels/stats/power.pyt   ftest_powerm   s    %t   Powerc           B  sP   e  Z d  Z d   Z d   Z d   Z d   Z d d d d d d d d  Z RS(	   s[   Statistical Power calculations, Base Class

    so far this could all be class methods
    c         K  s8  |  j  j |  t d d d d d d d d d	 d d
 d d d d d  |  _ d d l m } | t  |  _ x6 d d	 d d g D]" } t d d d d  |  j | <q~ Wx- d g D]" } t d d d d  |  j | <qŽ Wx- d
 g D]" } t d d d d  |  j | <qŢ Wx- d g D]" } t d d d d  |  j | <qWd  S(   NR   g{ŽGáz?R   g      $@R   g333333Ă?t   powerg333333ă?t   nobs1t   ratioi   R)   i
   R*   i   i˙˙˙˙(   t   defaultdictt   lowg       @t	   start_uppg      I@g      đ?g:0âyE>i   gę-q=t   uppgŃÜ˙˙˙˙ď?(   t   __dict__t   updatet   dictt	   start_ttpt   collectionsR3   t   start_bqexp(   t   selft   kwdsR3   t   key(    (    s6   lib/python2.7/site-packages/statsmodels/stats/power.pyt   __init__Ą   s       c         O  s
   t   d  S(   N(   t   NotImplementedError(   R=   t   argsR>   (    (    s6   lib/python2.7/site-packages/statsmodels/stats/power.pyR0   ˇ   s    c         O  s#   | j  d  } |  j | |   | S(   NR0   (   t   popR0   (   R=   RB   R>   t   power_(    (    s6   lib/python2.7/site-packages/statsmodels/stats/power.pyt   _power_identityş   s    c           sA  g  t    D] \ } } | d k r | ^ q   t    d k rR t d   n    d     d k r|  d = j    S d d k rď d d l } d d l m } | j d	 |    d k rĚ  d
 S  d
 k rŕ  d St d   n  d  _	     f d   } y  j
   } WnL t k
 rld } d d l } d d l m } | j d j    |  n X j   }	 g  }
 y2 t | d t |	 \ } } t } |
 j |  Wn$ t k
 rŘt } |
 j d  n Xd } | rř| j rřd } në t j |  sEt j | | d t \ } } } } | d } |
 j |  n d } d } |
 j d g  | d k rt j |  d k  rd } nX   d k rÝt j | d d d t \ } } | j rÇd n d } |
 j |  n d } | d k s$d d l } d d l m } m } | j | |  n  |
 j d |  |
  _ | S(   sc  solve for any one of the parameters of a t-test

        for t-test the keywords are:
            effect_size, nobs, alpha, power

        exactly one needs to be ``None``, all others need numeric values

        *attaches*

        cache_fit_res : list
            Cache of the result of the root finding procedure for the latest
            call to ``solve_power``, mainly for debugging purposes.
            The first element is the success indicator, one if successful.
            The remaining elements contain the return information of the up to
            three solvers that have been tried.


        i   s%   need exactly one keyword that is Nonei    R0   R   i˙˙˙˙N(   t   HypothesisTestWarnings"   Warning: Effect size of 0 detectedR   sA   Cannot detect an effect-size of 0. Try changing your effect-size.c           sd   |     < j     }  j d 7_  j d k rF t d   n  t j |  r\ t j S| Sd  S(   Ni   iô  s    possible endless loop (500 NaNs)(   RE   t   _countert   RuntimeErrorR   R   t   inf(   t   xt   fval(   R?   R>   R=   (    s6   lib/python2.7/site-packages/statsmodels/stats/power.pyt   funcě   s    
gÍĚĚĚĚĚě?(   t   ValueWarnings*   Warning: using default start_value for {0}t   full_outputt   fvecg-Cëâ6?g:0âyE>(   t   ConvergenceWarningt   convergence_doc(   R   R0   R   gGĄú˙˙ď?(   R   R   t   lenR	   R0   t   warningst   statsmodels.tools.sm_exceptionsRF   t   warnRG   R:   t   KeyErrorRM   t   formatR<   R   t   Truet   Falset   appendt	   convergedR   R   R   t   fsolvet   abst   brentqRP   RQ   t   insertt   cache_fit_res(   R=   R>   t   kt   vRS   RF   RL   t   start_valueRM   t   fit_kwdst   fit_rest   valt   rest   failedt   successt   infodictt   iert   msgRK   t   rRP   RQ   (    (   R?   R>   R=   s6   lib/python2.7/site-packages/statsmodels/stats/power.pyt   solve_powerž   sx    1
		
!		R   gŠ?c         K  sˇ  d d l  m }	 d d l m }
 |	 j |  \ } } d d l j } | j j } d } d } | d k r|
 t	 |   } g  t
 j d d	 t	 |   D] } | |  ^ q } xĂt |  D]Z \ } } |  j | | | |  } | j | | d
 | d | d | | d d | d } qť WnX| d k rÜ|
 t	 |   } g  t
 j d d	 t	 |   D] } | |  ^ qV} xt |  D]Z \ } } |  j | | | |  } | j | | d
 | d | d | | d d | d } q{Wn | d k rh|
 t	 |   } xw t |  D]Z \ } } |  j | | | |  } | j | | d
 | d | d | | d d | d } qWn t d   | d k rd } n  | j |  | j |  | j d d  | S(   sđ  plot power with number of observations or effect size on x-axis

        Parameters
        ----------
        dep_var : string in ['nobs', 'effect_size', 'alpha']
            This specifies which variable is used for the horizontal axis.
            If dep_var='nobs' (default), then one curve is created for each
            value of ``effect_size``. If dep_var='effect_size' or alpha, then
            one curve is created for each value of ``nobs``.
        nobs : scalar or array_like
            specifies the values of the number of observations in the plot
        effect_size : scalar or array_like
            specifies the values of the effect_size in the plot
        alpha : float or array_like
            The significance level (type I error) used in the power
            calculation. Can only be more than a scalar, if ``dep_var='alpha'``
        ax : None or axis instance
            If ax is None, than a matplotlib figure is created. If ax is a
            matplotlib axis instance, then it is reused, and the plot elements
            are created with it.
        title : string
            title for the axis. Use an empty string, ``''``, to avoid a title.
        plt_kwds : None or dict
            not used yet
        kwds : optional keywords for power function
            These remaining keyword arguments are used as arguments to the
            power function. Many power function support ``alternative`` as a
            keyword argument, two-sample test support ``ratio``.

        Returns
        -------
        fig : matplotlib figure instance

        Notes
        -----
        This works only for classes where the ``power`` method has
        ``effect_size``, ``nobs`` and ``alpha`` as the first three arguments.
        If the second argument is ``nobs1``, then the number of observations
        in the plot are those for the first sample.
        TODO: fix this for FTestPower and GofChisquarePower

        TODO: maybe add line variable, if we want more than nobs and effectsize
        i˙˙˙˙(   t   utils(   t   rainbowNi   i   R   i    gÍĚĚĚĚĚě?t   lwR   t   colort   labels   es=%4.2Fs   Number of Observationss   effect sizeR   t   ess   N=%4.2Fs   Effect Sizes   depvar not implementeds   Power of Testt   locs   lower right(   s   effect sizeR   Rt   (   R   (   t   statsmodels.graphicsRo   t   statsmodels.graphics.plottoolsRp   t   create_mpl_axt   matplotlib.pyplott   pyplott   cmt   Dark2RR   R   t   linspacet	   enumerateR0   t   plotR	   R   t
   set_xlabelt	   set_titlet   legend(   R=   t   dep_varR   R   R   t   axt   titlet   plt_kwdsR>   Ro   Rp   t   figt   pltt   colormapt	   plt_alphaRq   t   colorst   it   iiRt   R0   t   xlabelt   n(    (    s6   lib/python2.7/site-packages/statsmodels/stats/power.pyt
   plot_power4  sJ    /44	N(	   t   __name__t
   __module__t   __doc__R@   R0   RE   Rn   R   R   (    (    (    s6   lib/python2.7/site-packages/statsmodels/stats/power.pyR/      s   				v	t
   TTestPowerc           B  s5   e  Z d  Z d d d  Z d d d d d d  Z RS(   sK   Statistical Power calculations for one sample or paired sample t-test

    s	   two-sidedc         C  s   t  | | | d | d | S(   sđ  Calculate the power of a t-test for one sample or paired samples.

        Parameters
        ----------
        effect_size : float
            standardized effect size, mean divided by the standard deviation.
            effect size has to be positive.
        nobs : int or float
            sample size, number of observations.
        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.
        df : int or float
            degrees of freedom. By default this is None, and the df from the
            one sample or paired ttest is used, ``df = nobs1 - 1``
        alternative : string, 'two-sided' (default), 'larger', 'smaller'
            extra argument to choose whether the power is calculated for a
            two-sided (default) or one sided test. The one-sided test can be
            either 'larger', 'smaller'.
            .

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

       R   R   (   R   (   R=   R   R   R   R   R   (    (    s6   lib/python2.7/site-packages/statsmodels/stats/power.pyR0     s     c         C  s1   t  t |   j d | d | d | d | d |  S(   s	  solve for any one parameter of the power of a one sample t-test

        for the one sample t-test the keywords are:
            effect_size, nobs, alpha, power

        Exactly one needs to be ``None``, all others need numeric values.

        This test can also be used for a paired t-test, where effect size is
        defined in terms of the mean difference, and nobs is the number of
        pairs.

        Parameters
        ----------
        effect_size : float
            standardized effect size, mean divided by the standard deviation.
            effect size has to be positive.
        nobs : int or float
            sample size, number of observations.
        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.
        power : float in interval (0,1)
            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.
        alternative : string, 'two-sided' (default) or 'one-sided'
            extra argument to choose whether the power is calculated for a
            two-sided (default) or one sided test.
            'one-sided' assumes we are in the relevant tail.

        Returns
        -------
        value : float
            The value of the parameter that was set to None in the call. The
            value solves the power equation given the remaining parameters.

        *attaches*

        cache_fit_res : list
            Cache of the result of the root finding procedure for the latest
            call to ``solve_power``, mainly for debugging purposes.
            The first element is the success indicator, one if successful.
            The remaining elements contain the return information of the up to
            three solvers that have been tried.

        Notes
        -----
        The function uses scipy.optimize for finding the value that satisfies
        the power equation. It first uses ``brentq`` with a prior search for
        bounds. If this fails to find a root, ``fsolve`` is used. If ``fsolve``
        also fails, then, for ``alpha``, ``power`` and ``effect_size``,
        ``brentq`` with fixed bounds is used. However, there can still be cases
        where this fails.

        R   R   R   R0   R   (   t   superR   Rn   (   R=   R   R   R   R0   R   (    (    s6   lib/python2.7/site-packages/statsmodels/stats/power.pyRn   ˇ  s
    ;N(   R   R   R   R   R0   Rn   (    (    (    s6   lib/python2.7/site-packages/statsmodels/stats/power.pyR     s   $t   TTestIndPowerc           B  s;   e  Z d  Z d d d d  Z d d d d d d d  Z RS(   ss   Statistical Power calculations for t-test for two independent sample

    currently only uses pooled variance

    i   s	   two-sidedc   	      C  s]   | | } | d k r+ | d | d } n  d d | d | } t | | | d | d | S(   sĄ  Calculate the power of a t-test for two independent sample

        Parameters
        ----------
        effect_size : float
            standardized effect size, difference between the two means divided
            by the standard deviation. `effect_size` has to be positive.
        nobs1 : int or float
            number of observations of sample 1. The number of observations of
            sample two is ratio times the size of sample 1,
            i.e. ``nobs2 = nobs1 * ratio``
        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.
        ratio : float
            ratio of the number of observations in sample 2 relative to
            sample 1. see description of nobs1
            The default for ratio is 1; to solve for ratio given the other
            arguments, it has to be explicitly set to None.
        df : int or float
            degrees of freedom. By default this is None, and the df from the
            ttest with pooled variance is used, ``df = (nobs1 - 1 + nobs2 - 1)``
        alternative : string, 'two-sided' (default), 'larger', 'smaller'
            extra argument to choose whether the power is calculated for a
            two-sided (default) or one sided test. The one-sided test can be
            either 'larger', 'smaller'.

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

        i   g      đ?R   R   N(   R   R   (	   R=   R   R1   R   R2   R   R   t   nobs2R   (    (    s6   lib/python2.7/site-packages/statsmodels/stats/power.pyR0      s
    &
g      đ?c         C  s7   t  t |   j d | d | d | d | d | d |  S(   s	  solve for any one parameter of the power of a two sample t-test

        for t-test the keywords are:
            effect_size, nobs1, alpha, power, ratio

        exactly one needs to be ``None``, all others need numeric values

        Parameters
        ----------
        effect_size : float
            standardized effect size, difference between the two means divided
            by the standard deviation. `effect_size` has to be positive.
        nobs1 : int or float
            number of observations of sample 1. The number of observations of
            sample two is ratio times the size of sample 1,
            i.e. ``nobs2 = nobs1 * ratio``
        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.
        power : float in interval (0,1)
            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.
        ratio : float
            ratio of the number of observations in sample 2 relative to
            sample 1. see description of nobs1
            The default for ratio is 1; to solve for ratio given the other
            arguments it has to be explicitly set to None.
        alternative : string, 'two-sided' (default), 'larger', 'smaller'
            extra argument to choose whether the power is calculated for a
            two-sided (default) or one sided test. The one-sided test can be
            either 'larger', 'smaller'.

        Returns
        -------
        value : float
            The value of the parameter that was set to None in the call. The
            value solves the power equation given the remaining parameters.


        Notes
        -----
        The function uses scipy.optimize for finding the value that satisfies
        the power equation. It first uses ``brentq`` with a prior search for
        bounds. If this fails to find a root, ``fsolve`` is used. If ``fsolve``
        also fails, then, for ``alpha``, ``power`` and ``effect_size``,
        ``brentq`` with fixed bounds is used. However, there can still be cases
        where this fails.

        R   R1   R   R0   R2   R   (   R   R   Rn   (   R=   R   R1   R   R0   R2   R   (    (    s6   lib/python2.7/site-packages/statsmodels/stats/power.pyRn   0  s    4N(   R   R   R   R   R0   Rn   (    (    (    s6   lib/python2.7/site-packages/statsmodels/stats/power.pyR   ř  s
   /t   NormalIndPowerc           B  sD   e  Z d  Z d d  Z d d d  Z d d d d d d d  Z RS(	   su   Statistical Power calculations for z-test for two independent samples.

    currently only uses pooled variance

    i    c         K  s#   | |  _  t t |   j |   d  S(   N(   t   ddofR   R   R@   (   R=   R   R>   (    (    s6   lib/python2.7/site-packages/statsmodels/stats/power.pyR@   r  s    	i   s	   two-sidedc   	      C  s`   |  j  } | d k r@ | | } d d | | d | | } n
 | | } t | | | d | S(   sĆ  Calculate the power of a z-test for two independent sample

        Parameters
        ----------
        effect_size : float
            standardized effect size, difference between the two means divided
            by the standard deviation. effect size has to be positive.
        nobs1 : int or float
            number of observations of sample 1. The number of observations of
            sample two is ratio times the size of sample 1,
            i.e. ``nobs2 = nobs1 * ratio``
            ``ratio`` can be set to zero in order to get the power for a
            one sample test.
        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.
        ratio : float
            ratio of the number of observations in sample 2 relative to
            sample 1. see description of nobs1
        alternative : string, 'two-sided' (default), 'larger', 'smaller'
            extra argument to choose whether the power is calculated for a
            two-sided (default) or one sided test. The one-sided test can be
            either 'larger', 'smaller'.

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

        i    g      đ?R   (   R   R%   (	   R=   R   R1   R   R2   R   R   R   R   (    (    s6   lib/python2.7/site-packages/statsmodels/stats/power.pyR0   v  s    #	
!
g      đ?c         C  s7   t  t |   j d | d | d | d | d | d |  S(   są	  solve for any one parameter of the power of a two sample z-test

        for z-test the keywords are:
            effect_size, nobs1, alpha, power, ratio

        exactly one needs to be ``None``, all others need numeric values

        Parameters
        ----------
        effect_size : float
            standardized effect size, difference between the two means divided
            by the standard deviation.
            If ratio=0, then this is the standardized mean in the one sample
            test.
        nobs1 : int or float
            number of observations of sample 1. The number of observations of
            sample two is ratio times the size of sample 1,
            i.e. ``nobs2 = nobs1 * ratio``
            ``ratio`` can be set to zero in order to get the power for a
            one sample test.
        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.
        power : float in interval (0,1)
            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.
        ratio : float
            ratio of the number of observations in sample 2 relative to
            sample 1. see description of nobs1
            The default for ratio is 1; to solve for ration given the other
            arguments it has to be explicitly set to None.
        alternative : string, 'two-sided' (default), 'larger', 'smaller'
            extra argument to choose whether the power is calculated for a
            two-sided (default) or one sided test. The one-sided test can be
            either 'larger', 'smaller'.

        Returns
        -------
        value : float
            The value of the parameter that was set to None in the call. The
            value solves the power equation given the remaining parameters.


        Notes
        -----
        The function uses scipy.optimize for finding the value that satisfies
        the power equation. It first uses ``brentq`` with a prior search for
        bounds. If this fails to find a root, ``fsolve`` is used. If ``fsolve``
        also fails, then, for ``alpha``, ``power`` and ``effect_size``,
        ``brentq`` with fixed bounds is used. However, there can still be cases
        where this fails.

        R   R1   R   R0   R2   R   (   R   R   Rn   (   R=   R   R1   R   R0   R2   R   (    (    s6   lib/python2.7/site-packages/statsmodels/stats/power.pyRn   Ľ  s    8N(   R   R   R   R@   R0   R   Rn   (    (    (    s6   lib/python2.7/site-packages/statsmodels/stats/power.pyR   k  s   .t
   FTestPowerc           B  s8   e  Z d  Z d d  Z d d d d d d d d  Z RS(   s7   Statistical Power calculations for generic F-test

    i   c         C  s   t  | | | | d | } | S(   sE  Calculate the power of a F-test.

        Parameters
        ----------
        effect_size : float
            standardized effect size, mean divided by the standard deviation.
            effect size has to be positive.
        df_num : int or float
            numerator degrees of freedom.
        df_denom : int or float
            denominator degrees of freedom.
        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.
        ncc : int
            degrees of freedom correction for non-centrality parameter.
            see Notes

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

        Notes
        -----

        sample size is given implicitly by df_num

        set ncc=0 to match t-test, or f-test in LikelihoodModelResults.
        ncc=1 matches the non-centrality parameter in R::pwr::pwr.f2.test

        ftest_power with ncc=0 should also be correct for f_test in regression
        models, with df_num and d_denom as defined there. (not verified yet)
        R,   (   R.   (   R=   R   R)   R*   R   R,   R   (    (    s6   lib/python2.7/site-packages/statsmodels/stats/power.pyR0   ę  s    &c         C  s7   t  t |   j d | d | d | d | d | d |  S(   sW  solve for any one parameter of the power of a F-test

        for the one sample F-test the keywords are:
            effect_size, df_num, df_denom, alpha, power

        Exactly one needs to be ``None``, all others need numeric values.


        Parameters
        ----------
        effect_size : float
            standardized effect size, mean divided by the standard deviation.
            effect size has to be positive.
        nobs : int or float
            sample size, number of observations.
        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.
        power : float in interval (0,1)
            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.
        alternative : string, 'two-sided' (default) or 'one-sided'
            extra argument to choose whether the power is calculated for a
            two-sided (default) or one sided test.
            'one-sided' assumes we are in the relevant tail.

        Returns
        -------
        value : float
            The value of the parameter that was set to None in the call. The
            value solves the power equation given the remainding parameters.


        Notes
        -----
        The function uses scipy.optimize for finding the value that satisfies
        the power equation. It first uses ``brentq`` with a prior search for
        bounds. If this fails to find a root, ``fsolve`` is used. If ``fsolve``
        also fails, then, for ``alpha``, ``power`` and ``effect_size``,
        ``brentq`` with fixed bounds is used. However, there can still be cases
        where this fails.

        R   R)   R*   R   R0   R,   (   R   R   Rn   (   R=   R   R)   R*   R   R   R0   R,   (    (    s6   lib/python2.7/site-packages/statsmodels/stats/power.pyRn     s    .N(   R   R   R   R0   R   Rn   (    (    (    s6   lib/python2.7/site-packages/statsmodels/stats/power.pyR   ĺ  s   +	t   FTestAnovaPowerc           B  sJ   e  Z d  Z d d  Z d d d d d d  Z d d d d d d  Z RS(   sI   Statistical Power calculations F-test for one factor balanced ANOVA

    i   c         C  s   t  | | | d | S(   s  Calculate the power of a F-test for one factor ANOVA.

        Parameters
        ----------
        effect_size : float
            standardized effect size, mean divided by the standard deviation.
            effect size has to be positive.
        nobs : int or float
            sample size, number of observations.
        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.
        k_groups : int or float
            number of groups in the ANOVA or k-sample comparison. Default is 2.

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

       R(   (   R+   (   R=   R   R   R   R(   (    (    s6   lib/python2.7/site-packages/statsmodels/stats/power.pyR0   O  s    c         C  sŠ   | d
 k	 rD | d |  j d <t d | d d | d  |  j d <n  | d
 k rx |  j d | d | d | d | d	 |  St t |   j d | d | d | d | d	 |  S(   sM  solve for any one parameter of the power of a F-test

        for the one sample F-test the keywords are:
            effect_size, nobs, alpha, power

        Exactly one needs to be ``None``, all others need numeric values.


        Parameters
        ----------
        effect_size : float
            standardized effect size, mean divided by the standard deviation.
            effect size has to be positive.
        nobs : int or float
            sample size, number of observations.
        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.
        power : float in interval (0,1)
            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.

        Returns
        -------
        value : float
            The value of the parameter that was set to None in the call. The
            value solves the power equation given the remainding parameters.


        Notes
        -----
        The function uses scipy.optimize for finding the value that satisfies
        the power equation. It first uses ``brentq`` with a prior search for
        bounds. If this fails to find a root, ``fsolve`` is used. If ``fsolve``
        also fails, then, for ``alpha``, ``power`` and ``effect_size``,
        ``brentq`` with fixed bounds is used. However, there can still be cases
        where this fails.

        i
   R   R4   i   R5   R   R   R(   R0   N(   R   R:   R9   R<   t   _solve_effect_sizeR   R   Rn   (   R=   R   R   R   R0   R(   (    (    s6   lib/python2.7/site-packages/statsmodels/stats/power.pyRn   j  s    +c   	        sV         f d   } t  j | d d d t \ } } | j sR t |  n  | S(   sB   experimental, test failure in solve_power for effect_size
        c           s.   |  }  j  d | d  d   d  d   S(   NR   R   R   R(   R0   (   RE   (   RJ   R   (   R   R(   R   R0   R=   (    s6   lib/python2.7/site-packages/statsmodels/stats/power.pyRL   Ť  s    g:0âyE>i   RN   gGĄú˙˙ď?(   R   R^   RX   R[   t   print(	   R=   R   R   R   R0   R(   RL   Rf   Rm   (    (   R   R(   R   R0   R=   s6   lib/python2.7/site-packages/statsmodels/stats/power.pyR   §  s
    !	N(   R   R   R   R0   R   Rn   R   (    (    (    s6   lib/python2.7/site-packages/statsmodels/stats/power.pyR   J  s   <	t   GofChisquarePowerc           B  s2   e  Z d  Z d d  Z d d d d d d  Z RS(   sB   Statistical Power calculations for one sample chisquare test

    i    c         C  s)   d d l  m } | | | | | d d S(   sľ  Calculate the power of a chisquare test for one sample

        Only two-sided alternative is implemented

        Parameters
        ----------
        effect_size : float
            standardized effect size, according to Cohen's definition.
            see :func:`statsmodels.stats.gof.chisquare_effectsize`
        nobs : int or float
            sample size, number of observations.
        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.
        n_bins : int
            number of bins or cells in the distribution.

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

       i˙˙˙˙(   t   chisquare_powerR   i    (   t   statsmodels.stats.gofR   (   R=   R   R   R   t   n_binsR   R   (    (    s6   lib/python2.7/site-packages/statsmodels/stats/power.pyR0   ž  s    i   c         C  s1   t  t |   j d | d | d | d | d |  S(   sţ  solve for any one parameter of the power of a one sample chisquare-test

        for the one sample chisquare-test the keywords are:
            effect_size, nobs, alpha, power

        Exactly one needs to be ``None``, all others need numeric values.

        n_bins needs to be defined, a default=2 is used.


        Parameters
        ----------
        effect_size : float
            standardized effect size, according to Cohen's definition.
            see :func:`statsmodels.stats.gof.chisquare_effectsize`
        nobs : int or float
            sample size, number of observations.
        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.
        power : float in interval (0,1)
            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.
        n_bins : int
            number of bins or cells in the distribution

        Returns
        -------
        value : float
            The value of the parameter that was set to None in the call. The
            value solves the power equation given the remaining parameters.


        Notes
        -----
        The function uses scipy.optimize for finding the value that satisfies
        the power equation. It first uses ``brentq`` with a prior search for
        bounds. If this fails to find a root, ``fsolve`` is used. If ``fsolve``
        also fails, then, for ``alpha``, ``power`` and ``effect_size``,
        ``brentq`` with fixed bounds is used. However, there can still be cases
        where this fails.

        R   R   RĄ   R   R0   (   R   R   Rn   (   R=   R   R   R   R0   RĄ   (    (    s6   lib/python2.7/site-packages/statsmodels/stats/power.pyRn   Ü  s
    .N(   R   R   R   R0   R   Rn   (    (    (    s6   lib/python2.7/site-packages/statsmodels/stats/power.pyR   š  s   	t   _GofChisquareIndPowerc           B  s8   e  Z d  Z d d d  Z d d d d d d d  Z RS(   s  Statistical Power calculations for chisquare goodness-of-fit test

    TODO: this is not working yet
          for 2sample case need two nobs in function
          no one-sided chisquare test, is there one? use normal distribution?
          -> drop one-sided options?
    i   s	   two-sidedc   	      C  s@   d d l  m } | | } d d | d | } | | | |  S(   sß  Calculate the power of a chisquare for two independent sample

        Parameters
        ----------
        effect_size : float
            standardize effect size, difference between the two means divided
            by the standard deviation. effect size has to be positive.
        nobs1 : int or float
            number of observations of sample 1. The number of observations of
            sample two is ratio times the size of sample 1,
            i.e. ``nobs2 = nobs1 * ratio``
        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.
        ratio : float
            ratio of the number of observations in sample 2 relative to
            sample 1. see description of nobs1
            The default for ratio is 1; to solve for ration given the other
            arguments it has to be explicitely set to None.
        alternative : string, 'two-sided' (default) or 'one-sided'
            extra argument to choose whether the power is calculated for a
            two-sided (default) or one sided test.
            'one-sided' assumes we are in the relevant tail.

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

        i˙˙˙˙(   R   g      đ?(   R    R   (	   R=   R   R1   R   R2   R   R   R   R   (    (    s6   lib/python2.7/site-packages/statsmodels/stats/power.pyR0     s    #
g      đ?c         C  s7   t  t |   j d | d | d | d | d | d |  S(   sâ  solve for any one parameter of the power of a two sample z-test

        for z-test the keywords are:
            effect_size, nobs1, alpha, power, ratio

        exactly one needs to be ``None``, all others need numeric values

        Parameters
        ----------
        effect_size : float
            standardize effect size, difference between the two means divided
            by the standard deviation.
        nobs1 : int or float
            number of observations of sample 1. The number of observations of
            sample two is ratio times the size of sample 1,
            i.e. ``nobs2 = nobs1 * ratio``
        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.
        power : float in interval (0,1)
            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.
        ratio : float
            ratio of the number of observations in sample 2 relative to
            sample 1. see description of nobs1
            The default for ratio is 1; to solve for ration given the other
            arguments it has to be explicitely set to None.
        alternative : string, 'two-sided' (default) or 'one-sided'
            extra argument to choose whether the power is calculated for a
            two-sided (default) or one sided test.
            'one-sided' assumes we are in the relevant tail.

        Returns
        -------
        value : float
            The value of the parameter that was set to None in the call. The
            value solves the power equation given the remainding parameters.


        Notes
        -----
        The function uses scipy.optimize for finding the value that satisfies
        the power equation. It first uses ``brentq`` with a prior search for
        bounds. If this fails to find a root, ``fsolve`` is used. If ``fsolve``
        also fails, then, for ``alpha``, ``power`` and ``effect_size``,
        ``brentq`` with fixed bounds is used. However, there can still be cases
        where this fails.

        R   R1   R   R0   R2   R   (   R   R˘   Rn   (   R=   R   R1   R   R0   R2   R   (    (    s6   lib/python2.7/site-packages/statsmodels/stats/power.pyRn   D  s    4N(   R   R   R   R0   R   Rn   (    (    (    s6   lib/python2.7/site-packages/statsmodels/stats/power.pyR˘     s
   )(   R   t
   __future__R    t   statsmodels.compat.pythonR   t   numpyR   t   scipyR   R   t   statsmodels.tools.rootfindingR   R   R   R%   R+   R.   t   objectR/   R   R   R   R   R   R   R˘   Rn   t   tt_solve_powert   tt_ind_solve_powert   zt_ind_solve_power(    (    (    s6   lib/python2.7/site-packages/statsmodels/stats/power.pyt   <module>    s(   ".ójszeoWp