
    M/Ph                        d Z ddlZddlZddlmZmZmZ ddlm	Z	 d Z
d Zd Zd Zd	 Zd'dZd(dZ	 	 d)dZ	 	 d*dZd+dZd,dZd,dZ G d d          Z G d de          Z G d de          Z G d de          Z G d de          Z G d d e          Z G d! d"e          Z G d# d$e          Z G d% d&e          Z e            j        Z  e            j        Z! e            j        Z"dS )-aD  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 ?


    N)statsoptimizespecial)brentq_expandingc                 .    t          j        |||           S Nr   nctdtrxdfncs      W/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/stats/power.pynct_cdfr   '   s    >"b!$$$    c                 4    dt          j        |||           z
  S N   r	   r   s      r   nct_sfr   +   s    w~b"a((((r   c                 0    t          j        ||||           S r   r   ncfdtrr   dfndfdr   s       r   ncf_cdfr   /   s    >#sB***r   c                 6    dt          j        ||||           z
  S r   r   r   s       r   ncf_sfr   3   s    w~c3A....r   c                 0    t          j        ||||           S r   )r   ncfdtri)qr   r   r   s       r   ncf_ppfr"   7   s    ?3R+++r   	two-sidedc           	      V   | }||dz
  }|dv r|dz  }n|dv r|}nt          d          d}|dv ryt          j                            ||          }t	          j        t	          j        |                    rt          j        }n&t          |||t	          j	        |          z            }|d	v r|t          j        
                    ||          }	t	          j        t	          j        |	                    rt          j        }n)|t          |	||t	          j	        |          z            z  }|S )
zCalculate power of a ttest
    Nr   r#   2s       @smallerlarger8alternative has to be 'two-sided', 'larger' or 'smaller'r   r#   r&   r*   r#   r&   r)   )
ValueErrorr   tisfnpanyisnannanr   sqrtppfr   )
effect_sizenobsalphar   alternativedalpha_pow_crit_uppcrit_lows
             r   ttest_powerr@   ;   s5    	A	zAX)))	-	-	- ( ) ) 	) D3337;;vr** 6"(8$$%% 	96DD (B"'$--88D4447;;vr**6"(8$$%% 	;6DD GHb!BGDMM/:::DKr         ?c                    | }|dv r|dz  }n|dv r|}nt          d          d}|dv rYt          j                            |          }t          j                            ||t          j        |          z  |z  z
            }|dv r\t          j                            |          }|t          j                            ||t          j        |          z  |z  z
            z  }|S )aV  Calculate power of a normal distributed test statistic

    This is an generalization of `normal_power` when variance under Null and
    Alternative differ.

    Parameters
    ----------
    effect size : float
        difference in the estimated means or statistics under the alternative
        normalized by the standard deviation (without division by sqrt(nobs).
    nobs : float or int
        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.
    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'.
    r%   r'   r(   r+   r   r,   r-   	r.   r   normr0   sfr1   r5   r6   cdf)	r7   r8   r9   r:   sigmar;   r<   r=   crits	            r   normal_powerrI   a   s    , 	A)))	-	-	- ( ) ) 	) D333z~~f%%z}}TAbgdmmOE$99::444z~~f%%
taoe&;;<<<Kr   c                    | }||}|dv r|dz  }n|dv r|}nt          d          ||z  }d}	|dv r\t          j                            |          }
t          j                            |
|z  |t          j        |          z  |z  z
            }	|dv r_t          j                            |          }
|	t          j                            |
|z  |t          j        |          z  |z  z
            z  }	|	S )	a  Calculate power of a normal distributed test statistic

    This is an generalization of `normal_power` when variance under Null and
    Alternative differ.

    Parameters
    ----------
    diff : float
        difference in the estimated means or statistics under the alternative.
    nobs : float or int
        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.
    std_null : float
        standard deviation under the Null hypothesis without division by
        sqrt(nobs)
    std_alternative : float
        standard deviation under the Alternative hypothesis without division
        by sqrt(nobs)
    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
    Nr%   r'   r(   r+   r   r,   r-   rC   )diffr8   r9   std_nullstd_alternativer:   r;   r<   	std_ratior=   rH   s              r   normal_power_hetrO      s!   @ 	A")))	-	-	- ( ) ) 	) ?*ID333z~~f%%z}}TI-rwt}}_>? @ @444z~~f%%
ti/ @ A B B 	BKr   c                     ||}t           j                            |          }t           j                            |          }t          j        ||z  ||z  z
  d          | z  dz  }|S )am  explicit sample size computation if only one tail is relevant

    The sample size is based on the power in one tail assuming that the
    alternative is in the tail where the test has power that increases
    with sample size.
    Use alpha/2 to compute the one tail approximation to the two-sided
    test, i.e. consider only one tail of two-sided test.

    Parameters
    ----------
    diff : float
        difference in the estimated means or statistics under the alternative.
    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.
    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.
        Note: alpha is used for one tail. Use alpha/2 for two-sided
        alternative.
    std_null : float
        standard deviation under the Null hypothesis without division by
        sqrt(nobs)
    std_alternative : float
        standard deviation under the Alternative hypothesis without division
        by sqrt(nobs). Defaults to None. If None, ``std_alternative`` is set
        to the value of ``std_null``.

    Returns
    -------
    nobs : float
        Sample size to achieve (at least) the desired power.
        If the minimum power is satisfied for all positive sample sizes, then
        ``nobs`` will be zero. This will be the case when power <= alpha if
        std_alternative is equal to std_null.

    Nr      )r   rD   r0   r1   maximum)rK   powerr9   rL   rM   
crit_powerrH   n1s           r   normal_sample_size_one_tailrV      sj    R "&&J:>>%  D
*TH_zO'CCQ
G
G
BIr   rQ   c                     |dz
  }||z
  }t           j                            |||          }t          |||| dz  |z            }|S )zpower 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 ???
    r   rQ   r   fr0   r   )	r7   r8   r9   k_groupsr   df_numdf_denomrH   r=   s	            r   ftest_anova_powerr]      sN     \FhH7;;ufh//D$+q.4*?@@DKr   r   c                     ||}}| dz  ||z   |z   z  }t           j                            |||          }t          ||||          }	|	S )a  Calculate the power of a F-test.

    Parameters
    ----------
    effect_size : float
        The effect size is here Cohen's ``f``, the square root of ``f2``.
    df2 : int or float
        Denominator degrees of freedom.
        This corresponds to the df_resid in Wald tests.
    df1 : int or float
        Numerator degrees of freedom.
        This corresponds to the number of constraints in Wald tests.
    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
    -----
    changed in 0.14: use df2, df1 instead of df_num, df_denom as arg names.
    The latter had reversed meaning.

    The sample size is given implicitly by ``df2`` with fixed number of
    constraints given by numerator degrees of freedom ``df1``:

        nobs = df2 + df1 + ncc

    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 (df1) as number of constraints and d_denom (df2) as
    df_resid.
    rQ   rX   )
r7   df2df1r9   nccr[   r\   r   rH   r=   s
             r   ftest_powerrb     sW    X CHF	a8f,s2	3B7;;ufh//D $"--DKr   c                     | ||z   |z   z  }t           j                            |||          }t          ||||          }|S )a  Calculate the power of a F-test.

    Based on Cohen's `f^2` effect size.

    This assumes

        df_num : numerator degrees of freedom, (number of constraints)
        df_denom : denominator degrees of freedom (df_resid in regression)
        nobs = df_denom + df_num + ncc
        nc = effect_size * nobs  (noncentrality index)

    Power is computed one-sided in the upper tail.

    Parameters
    ----------
    effect_size : float
        Cohen's f2 effect size or noncentrality divided by nobs.
    df_num : int or float
        Numerator degrees of freedom.
        This corresponds to the number of constraints in Wald tests.
    df_denom : int or float
        Denominator degrees of freedom.
        This corresponds to the df_resid in Wald tests.
    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

    The sample size is given implicitly by ``df_denom`` with fixed number of
    constraints given by numerator degrees of freedom ``df_num``:

        nobs = df_denom + df_num + ncc

    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 (df1) as number of constraints and d_denom (df2) as
    df_resid.
    rX   )r7   r[   r\   r9   ra   r   rH   r=   s           r   ftest_power_f2rd   :  sJ    j 
6)C/	0B7;;ufh//D $"--DKr   c                   6    e Zd ZdZd Zd Zd Zd Z	 	 d
d	ZdS )Powerz[Statistical Power calculations, Base Class

    so far this could all be class methods
    c           
         | j                             |           t          dddddddd          | _        d	d
lm}  |t                    | _        dD ]}t          dd          | j        |<   dD ]}t          dd          | j        |<   dD ]}t          dd          | j        |<   dD ]}t          dd          | j        |<   d S )Ng{Gz?g      $@g333333?g333333?r   
      )r7   r8   r9   rS   nobs1ratior[   r\   r   )defaultdict)r8   rj   r[   r\   r'   g      I@low	start_upp)r\   rA   )rk   :0yE>rQ   r9   g-q=g?)rn   upp)__dict__updatedict	start_ttpcollectionsrl   start_bqexp)selfkwdsrl   keys       r   __init__zPower.__init__  s*   T"""$S$'s!%'!      	,+++++&;t,,: 	@ 	@C$(R3$?$?$?DS!! 	@ 	@C$(R3$?$?$?DS!! 	@ 	@C$(TQ$?$?$?DS!! 	C 	CC$(U	$B$B$BDS!!	C 	Cr   c                     t           r   )NotImplementedError)ry   argsrz   s      r   rS   zPower.power  s    !!r   c                 L    |                     d          } | j        |i ||z
  S )NrS   )poprS   )ry   r   rz   power_s       r   _power_identityzPower._power_identity  s/    '""tz4(4((611r   c                     d                                  D             t                    dk    rt          d          d         dk    rd=   j        di S d         dk    rFddl}ddlm}  |j        d	|           dk    rd
         S d
k    rd         S t          d          d _         fd}	  j	                 }n0# t          $ r# d}ddl}ddlm}  |j        d |           Y nw xY w j                 }g }	 t          |fddi|\  }	}
d}|                    |
           n'# t          $ r d}|                    d           Y nw xY wd}|s
|
j        rd}nt!          j        |          s:t%          j        ||d          \  }	}}}|d         }|                    |           nd}d}|                    dg           |dk    rt!          j        |          dk     rd}nBdv r<t%          j        |ddd          \  }	}|j        rdnd}|                    |           nd}|dk    sddl}ddlm}m}  |j        ||           |                    d|           | _        |	S )ac  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.


        c                     g | ]	\  }}||
S r    ).0kvs      r   
<listcomp>z%Power.solve_power.<locals>.<listcomp>  s    666SQqAIqIIIr   r   z%need exactly one keyword that is Noner   rS   r7   N)HypothesisTestWarningz"Warning: Effect size of 0 detectedr9   zACannot detect an effect-size of 0. Try changing your effect-size.c                     | <    j         di }xj        dz  c_        j        dk    rt          d          t          j        |          rt          j        S |S )Nr   i  z possible endless loop (500 NaNs)r   )r   _counterRuntimeErrorr1   r3   inf)r   fvalr{   rz   ry   s     r   funczPower.solve_power.<locals>.func  sl    DI'4'//$//DMMQMM}s"""#EFFFx~~ vr   ?)ValueWarningz'Warning: using default start_value for full_outputTFr   fvecg-C6?)r9   rS   r7   rp   G?)ConvergenceWarningconvergence_docr   )itemslenr.   rS   warningsstatsmodels.tools.sm_exceptionsr   warnr   rv   KeyErrorr   rx   r   append	convergedr1   r3   r   fsolveabsbrentqr   r   insertcache_fit_res)ry   rz   r   r   r   start_valuer   fit_kwdsfit_resvalresfailedsuccessinfodictiermsgr   rr   r   r{   s   ``                  @r   solve_powerzPower.solve_power  s   , 76DJJLL666s88q==DEEE!f'>>W4:%%%%%!##OOOMMMMMMHM>@UVVVg~~G}$g~~G}$ !deee 
	 
	 
	 
	 
	 
	 
		Y.-KK 	Y 	Y 	YKOOODDDDDDHMICII<XXXXX		Y #C(	!'KK$K(KKHCFNN3 	! 	! 	!FNN4     	!  	 CM 	 GG 8K(( 	'*2/$FJ+L +L +L'XsC  'x((((v&&&axxBF4LL4// ;;;%_T49=? ? ?FC#$;5aaAGNN1%%%%G!||OOO! ! ! ! ! ! ! !HM/+=>>> 	q'"""$
s$   C *C>=C>)D; ;!EEr8   N皙?c           
      T   ddl m}	 ddlm}
 |	                    |          \  }}ddlm} |j        j        d}d}|dk    r |
t          |                    }fdt          j        dd	t          |                    D             }t          |          D ];\  }} | j        |||fi |}|                    ||||||         d
|z             d}<n|dv r |
t          |                    }fdt          j        dd	t          |                    D             }t          |          D ];\  }} | j        |||fi |}|                    ||||||         d|z             d}<nw|dv rd |
t          |                    }t          |          D ];\  }} | j        |||fi |}|                    ||||||         d|z             d}<nt          d          |d}|                    |           |                    |           |                    d           |S )a!  
        Plot power with number of observations or effect size on x-axis

        Parameters
        ----------
        dep_var : {'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, array_like}
            specifies the values of the number of observations in the plot
        effect_size : {scalar, array_like}
            specifies the values of the effect_size in the plot
        alpha : {float, 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 : str
            title for the axis. Use an empty string, ``''``, to avoid a title.
        plt_kwds : {None, dict}
            not used yet
        kwds : dict
            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
        -------
        Figure
            If `ax` is None, the created figure.  Otherwise the figure to which
            `ax` is connected.

        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
        r   )utils)rainbowNr   rQ   r8   c                 &    g | ]} |          S r   r   r   icolormaps     r   r   z$Power.plot_power.<locals>.<listcomp>N  s!    QQQahhqkkQQQr   r   zes=%4.2F)lwr9   colorlabelzNumber of Observations)zeffect sizer7   esc                 &    g | ]} |          S r   r   r   s     r   r   z$Power.plot_power.<locals>.<listcomp>V  s!    JJJahhqkkJJJr   zN=%4.2FzEffect Sizerq   r9   zdepvar not implementedzPower of Testzlower right)loc)statsmodels.graphicsr   statsmodels.graphics.plottoolsr   create_mpl_axmatplotlib.pyplotpyplotcmDark2r   r1   linspace	enumeraterS   plotr.   
set_xlabel	set_titlelegend)ry   dep_varr8   r7   r9   axtitleplt_kwdsrz   r   r   figplt	plt_alphar   colorsiir   rS   xlabelnr   s                        @r   
plot_powerzPower.plot_power  s   d 	/.....::::::%%b))R''''''6<	fWS--..FQQQQ2;q#s;?O?O+P+PQQQF#K00 2 2B"
2tU;;d;;e)$Rj
R  A A A1	2
 <<<WSYY''FJJJJ2;q#s4yy+I+IJJJF"4 ' 'A"
;5AADAAUr$Rj	A  ? ? ?&	'
 	!!WSYY''F"4 ! !A"
;5AADAAu9$Rj	A  ? ? ? 	! 5666=#E
f
U
		m	$$$
r   )r8   NNr   NNN)	__name__
__module____qualname____doc__r|   rS   r   r   r   r   r   r   rf   rf   z  s         
C C C," " "2 2 2t t tl AE=AZ Z Z Z Z Zr   rf   c                   0     e Zd ZdZddZ	 	 d fd	Z xZS )
TTestPowerzKStatistical Power calculations for one sample or paired sample t-test

    Nr#   c                 *    t          |||||          S )a  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 : str, '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@   )ry   r7   r8   r9   r   r:   s         r   rS   zTTestPower.poweru  s'    @ ;e'24 4 4 	4r   c                 P    t                                          |||||          S )at	  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.The effect size is here Cohen's f, square
            root of "f2".
        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 : str, '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.

        )r7   r8   r9   rS   r:   superr   )ry   r7   r8   r9   rS   r:   	__class__s         r   r   zTTestPower.solve_power  s8    v ww""{;?<A<ABM	 # O O 	Or   Nr#   )NNNNr#   r   r   r   r   rS   r   __classcell__r   s   @r   r   r   p  sp         !4 !4 !4 !4H JN +?O ?O ?O ?O ?O ?O ?O ?O ?O ?Or   r   c                   4     e Zd ZdZ	 	 ddZ	 	 d	 fd	Z xZS )
TTestIndPowerzsStatistical Power calculations for t-test for two independent sample

    currently only uses pooled variance

    r   Nr#   c                 j    ||z  }||dz
  |z   dz
  }dd|z  d|z  z   z  }t          |||||          S )a  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 : str, '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.

        Nr   rA   r   r   )	ry   r7   rj   r9   rk   r   r:   nobs2r8   s	            r   rS   zTTestIndPower.power  sV    L e:!)e#a'BBJe+,;eTTTTr   rA   c                 R    t                                          ||||||          S )a
	  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 : str, '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.

        r7   rj   r9   rS   rk   r:   r   ry   r7   rj   r9   rS   rk   r:   r   s          r   r   zTTestIndPower.solve_power  ;    h ww""{<A<A<A<ABM # O O 	Or   )r   Nr#   NNNNrA   r#   r   r   s   @r   r   r     s}          <@%-U -U -U -U` KO*59O 9O 9O 9O 9O 9O 9O 9O 9O 9Or   r   c                   @     e Zd ZdZd
 fd	Z	 	 ddZ	 	 d fd		Z xZS )NormalIndPowerzuStatistical Power calculations for z-test for two independent samples.

    currently only uses pooled variance

    r   c                 H    || _          t                      j        di | d S )Nr   )ddofr   r|   )ry   r   rz   r   s      r   r|   zNormalIndPower.__init__T  s-    	  4     r   r   r#   c                     | j         }|dk    r||z  }dd||z
  z  d||z
  z  z   z  }n||z
  }t          ||||          S )a  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 : str, '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   rA   )r:   )r   rI   )	ry   r7   rj   r9   rk   r:   r   r   r8   s	            r   rS   zNormalIndPower.powerX  sc    F y 199%KEedl+bEDL.AABDD4<DKu+NNNNr   NrA   c                 R    t                                          ||||||          S )a	  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 : str, '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   r   r   s          r   r   zNormalIndPower.solve_power  s;    p ww""{<A<A<A<ABM # O O 	Or   r   r   r#   r   )r   r   r   r   r|   rS   r   r   r   s   @r   r   r   M  s         ! ! ! ! ! ! 67%,O ,O ,O ,O^ KO*5=O =O =O =O =O =O =O =O =O =Or   r   c                   0     e Zd ZdZddZ	 	 d fd	Z xZS )
FTestPowera  Statistical Power calculations for generic F-test of a constraint

    This class is not recommended, use `FTestPowerF2` with corrected interface.

    This is based on Cohen's f as effect size measure.

    Warning: Methods in this class have the names df_num and df_denom reversed.

    See Also
    --------
    FTestPowerF2 :
        Class with Cohen's f-squared as effect size, corrected keyword names.

    Examples
    --------
    Sample size and power for multiple regression base on R-squared

    Compute effect size from R-squared

    >>> r2 = 0.1
    >>> f2 = r2 / (1 - r2)
    >>> f = np.sqrt(f2)
    >>> r2, f2, f
    (0.1, 0.11111111111111112, 0.33333333333333337)

    Find sample size by solving for denominator df, wrongly named ``df_num``

    >>> df1 = 1  # number of constraints in hypothesis test
    >>> df2 = FTestPower().solve_power(effect_size=f, alpha=0.1, power=0.9,
                                       df_denom=df1)
    >>> ncc = 1  # default
    >>> nobs = df2 + df1 + ncc
    >>> df2, nobs
    (76.46459758305376, 78.46459758305376)

    verify power at df2

    >>> FTestPower().power(effect_size=f, alpha=0.1, df_denom=df1, df_num=df2)
    0.8999999972109698

    r   c                 .    t          |||||          }|S )a  Calculate the power of a F-test.

        The effect size is Cohen's ``f``, square root of ``f2``.

        The sample size is given by ``nobs = df_denom + df_num + ncc``

        Warning: The meaning of df_num and df_denom is reversed.

        Parameters
        ----------
        effect_size : float
            Standardized effect size. The effect size is here Cohen's ``f``,
            square root of ``f2``.
        df_num : int or float
            Warning incorrect name
            denominator degrees of freedom,
            This corresponds to the number of constraints in Wald tests.
        df_denom : int or float
            Warning incorrect name
            numerator degrees of freedom.
            This corresponds to the df_resid in Wald tests.
        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)
        ra   )rb   ry   r7   r[   r\   r9   ra   r=   s          r   rS   zFTestPower.power  s"    ` ;%SIIIr   Nc                     |r+d|v rt          j        d           nt          d|           t                                          ||||||          S )a	  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.

        The effect size is Cohen's ``f``, square root of ``f2``.

        The sample size is given by ``nobs = df_denom + df_num + ncc``.

        Warning: The meaning of df_num and df_denom is reversed.

        Parameters
        ----------
        effect_size : float
            Standardized effect size. The effect size is here Cohen's ``f``,
            square root of ``f2``.
        df_num : int or float
            Warning incorrect name
            denominator degrees of freedom,
            This corresponds to the number of constraints in Wald tests.
            Sample size is given by ``nobs = df_denom + df_num + ncc``
        df_denom : int or float
            Warning incorrect name
            numerator degrees of freedom.
            This corresponds to the df_resid in Wald tests.
        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.
        ncc : int
            degrees of freedom correction for non-centrality parameter.
            see Notes
        kwargs : empty
            ``kwargs`` are not used and included for backwards compatibility.
            If ``nobs`` is used as keyword, then a warning is issued. All
            other keywords in ``kwargs`` raise a ValueError.

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

        r8   znobs is not usedzincorrect keyword(s) r7   r[   r\   r9   rS   ra   )r   r   r.   r   r   )	ry   r7   r[   r\   r9   rS   ra   kwargsr   s	           r   r   zFTestPower.solve_power'  sw    z  	C01111 !A!A!ABBBww""{=C?G<A<A:= # ? ? 	?r   r   NNNNNr   r   r   s   @r   r   r     sq        ( (T2 2 2 2j CG01G? G? G? G? G? G? G? G? G? G?r   r   c                   0     e Zd ZdZddZ	 	 d fd	Z xZS )FTestPowerF2a  Statistical Power calculations for generic F-test of a constraint

    This is based on Cohen's f^2 as effect size measure.

    Examples
    --------
    Sample size and power for multiple regression base on R-squared

    Compute effect size from R-squared

    >>> r2 = 0.1
    >>> f2 = r2 / (1 - r2)
    >>> f = np.sqrt(f2)
    >>> r2, f2, f
    (0.1, 0.11111111111111112, 0.33333333333333337)

    Find sample size by solving for denominator degrees of freedom.

    >>> df1 = 1  # number of constraints in hypothesis test
    >>> df2 = FTestPowerF2().solve_power(effect_size=f2, alpha=0.1, power=0.9,
                                         df_num=df1)
    >>> ncc = 1  # default
    >>> nobs = df2 + df1 + ncc
    >>> df2, nobs
    (76.46459758305376, 78.46459758305376)

    verify power at df2

    >>> FTestPowerF2().power(effect_size=f, alpha=0.1, df_num=df1, df_denom=df2)
    0.8999999972109698

    r   c                 .    t          |||||          }|S )aK  Calculate the power of a F-test.

        The effect size is Cohen's ``f^2``.

        The sample size is given by ``nobs = df_denom + df_num + ncc``

        Parameters
        ----------
        effect_size : float
            The effect size is here Cohen's ``f2``. This is equal to
            the noncentrality of an F-test divided by nobs.
        df_num : int or float
            Numerator degrees of freedom,
            This corresponds to the number of constraints in Wald tests.
        df_denom : int or float
            Denominator degrees of freedom.
            This corresponds to the df_resid in Wald tests.
        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
        -----
        The sample size is given implicitly by df_denom

        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   )rd   r  s          r   rS   zFTestPowerF2.power  s"    V k68ULLLr   Nc                 R    t                                          ||||||          S )aA  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.

        The effect size is Cohen's ``f2``.

        The sample size is given by ``nobs = df_denom + df_num + ncc``, and
        can be found by solving for df_denom.

        Parameters
        ----------
        effect_size : float
            The effect size is here Cohen's ``f2``. This is equal to
            the noncentrality of an F-test divided by nobs.
        df_num : int or float
            Numerator degrees of freedom,
            This corresponds to the number of constraints in Wald tests.
        df_denom : int or float
            Denominator degrees of freedom.
            This corresponds to the df_resid in Wald tests.
        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.
        ncc : int
            degrees of freedom correction for non-centrality parameter.
            see Notes

        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   )ry   r7   r[   r\   r9   rS   ra   r   s          r   r   zFTestPowerF2.solve_power  s8    l ww""{=C?G<A<A:= # ? ? 	?r   r  r  r   r   s   @r   r  r  r  sg         B, , , ,^ CG01;? ;? ;? ;? ;? ;? ;? ;? ;? ;?r   r  c                   <     e Zd ZdZddZ	 	 d fd	Z	 	 ddZ xZS )	FTestAnovaPowerzStatistical Power calculations F-test for one factor balanced ANOVA

    This is based on Cohen's f as effect size measure.

    See Also
    --------
    statsmodels.stats.oneway.effectsize_oneway

    rQ   c                 (    t          ||||          S )a  Calculate the power of a F-test for one factor ANOVA.

        Parameters
        ----------
        effect_size : float
            standardized effect size. The effect size is here Cohen's f, square
            root of "f2".
        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.

       )rZ   )r]   )ry   r7   r8   r9   rZ   s        r   rS   zFTestAnovaPower.power  s    0 !dEHMMMMr   Nc                     |,|dz  | j         d<   t          |dz  |dz            | j        d<   ||                     |||||          S t	                                          |||||          S )aL  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 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.

        Nrh   r8   rQ   rm   r7   r8   r9   rZ   rS   )rv   ru   rx   _solve_effect_sizer   r   )ry   r7   r8   r9   rS   rZ   r   s         r   r   zFTestAnovaPower.solve_power'  s    V %-]DN6"'+16>m(E (E (EDV$ **{04164<16	 + 8 8 8 ww""{;?<A?G<A	 # C C 	Cr   c                       fd}t          j        |ddd          \  }}|j        st          |           |S )zBexperimental, test failure in solve_power for effect_size
        c                 <    | }                     |          S )Nr  )r   )r   r7   r9   rZ   r8   rS   ry   s     r   r   z0FTestAnovaPower._solve_effect_size.<locals>.funch  s3    K''K/3053;05	 ( 7 7 7r   rp   r   Tr   )r   r   r   print)	ry   r7   r8   r9   rS   rZ   r   r   r   s	   ` ````   r   r  z"FTestAnovaPower._solve_effect_sized  sl    	7 	7 	7 	7 	7 	7 	7 	7 	7 tVFFFQ{ 	!HHH
r   )rQ   NNNNrQ   )r   r   r   r   rS   r   r  r   r   s   @r   r  r    s         N N N N6 JN;C ;C ;C ;C ;C ;Cz EI01       r   r  c                   0     e Zd ZdZddZ	 	 d fd	Z xZS )	GofChisquarePowerzBStatistical Power calculations for one sample chisquare test

    r   c                 .    ddl m}  |||||d          S )a  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.

       r   chisquare_power)r   statsmodels.stats.gofr  )ry   r7   r8   r9   n_binsr   r  s          r   rS   zGofChisquarePower.power{  s1    4 	:99999{D&%aHHHHr   NrQ   c                 P    t                                          |||||          S )a  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.

        )r7   r8   r  r9   rS   r   )ry   r7   r8   r9   rS   r  r   s         r   r   zGofChisquarePower.solve_power  s8    \ ww""{;?=C<A<A	 # C C 	Cr   r   r  r   r   s   @r   r  r  v  sr         I I I I< >B'(2C 2C 2C 2C 2C 2C 2C 2C 2C 2Cr   r  c                   4     e Zd ZdZ	 	 ddZ	 	 d	 fd	Z xZS )
_GofChisquareIndPowera  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?
    r   r#   c                 N    ddl m} ||z  }dd|z  d|z  z   z  } ||||          S )a  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 : str, '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.

        r   r  rA   r  )	ry   r7   rj   r9   rk   r:   r  r   r8   s	            r   rS   z_GofChisquareIndPower.power  sK    F 	:99999eBJe+,{D%888r   NrA   c                 R    t                                          ||||||          S )a  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 : str, '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.


        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   s          r   r   z!_GofChisquareIndPower.solve_power  r   r   r   r   r   r   s   @r   r  r    sx          67%'9 '9 '9 '9T KO*59O 9O 9O 9O 9O 9O 9O 9O 9O 9Or   r  r   )r#   rA   )rA   Nr#   )rA   N)rQ   Nr  )#r   r   numpyr1   scipyr   r   r   statsmodels.tools.rootfindingr   r   r   r   r   r"   r@   rI   rO   rV   r]   rb   rd   rf   r   r   r   r   r  r  r  r  r   tt_solve_powertt_ind_solve_powerzt_ind_solve_powerr   r   r   <module>r(     sm   <      * * * * * * * * * * : : : : : :% % %) ) )+ + +/ / /, , ,# # # #L' ' ' 'T FJ(6 6 6 6r >@040 0 0 0f   2 2 2 2j: : : :@s s s s s s s slhO hO hO hO hO hO hO hOTqO qO qO qO qOE qO qO qOfwO wO wO wO wOU wO wO wOtg? g? g? g? g? g? g? g?VL? L? L? L? L?5 L? L? L?^r r r r re r r rjUC UC UC UC UC UC UC UCnmO mO mO mO mOE mO mO mO` )"]__0 #^%%1   r   