U
    QvfM                     @   sx   d dl Z d dlZd dlmZ d dlmZ dddddd	gZdddZdddZ	dddZ
dddZdddZddd	ZdS )    N)stats)brenthpower_ttestpower_ttest2npower_anovapower_rm_anova
power_corr
power_chi2皙?two-samples	two-sidedc                    s4  t dd | |||fD }|dkr*td|dks:td| dksJt| dkrZd	nd|d
krjd	nd| dk	rd	krt| } |dk	rd|  k rdksn t|dk	rd|  k rdksn t|dkrfdd n&|d
krfdd nfdd |dkr" | |d|dS |dkrp fdd}zt|dd| ||fdW S  tk
rl   tj Y S X n| dkr|d
krd\}}	n|dkrd\}}	nd\}}	 fdd}
zt|
||	|||fdW S  tk
r   tj Y S X nB fdd}zt|dd | ||fdW S  tk
r.   tj Y S X dS )!aK  
    Evaluate power, sample size, effect size or significance level of a one-sample T-test,
    a paired T-test or an independent two-samples T-test with equal sample sizes.

    Parameters
    ----------
    d : float
        Cohen d effect size
    n : int
        Sample size
        In case of a two-sample T-test, sample sizes are assumed to be equal.
        Otherwise, see the :py:func:`power_ttest2n` function.
    power : float
        Test power (= 1 - type II error).
    alpha : float
        Significance level (type I error probability).
        The default is 0.05.
    contrast : str
        Can be `"one-sample"`, `"two-samples"` or `"paired"`.
        Note that `"one-sample"` and `"paired"` have the same behavior.
    alternative : string
        Defines the alternative hypothesis, or tail of the test. Must be one of
        "two-sided" (default), "greater" or "less".

    Notes
    -----
    Exactly ONE of the parameters ``d``, ``n``, ``power`` and ``alpha`` must be passed as None, and
    that parameter is determined from the others.

    For a paired T-test, the sample size ``n`` corresponds to the number of pairs. For an
    independent two-sample T-test with equal sample sizes, ``n`` corresponds to the sample size of
    each group (i.e. number of observations in one group). If the sample sizes are unequal, please
    use the :py:func:`power_ttest2n` function instead.

    ``alpha`` has a default value of 0.05 so None must be explicitly passed if you want to
    compute it.

    This function is a Python adaptation of the `pwr.t.test` function implemented in the
    `pwr <https://cran.r-project.org/web/packages/pwr/pwr.pdf>`_ R package.

    Statistical power is the likelihood that a study will detect an effect when there is an effect
    there to be detected. A high statistical power means that there is a low probability of
    concluding that there is no effect when there is one. Statistical power is mainly affected by
    the effect size and the sample size.

    The first step is to use the Cohen's d to calculate the non-centrality parameter
    :math:`\delta` and degrees of freedom :math:`v`. In case of paired groups, this is:

    .. math:: \delta = d * \sqrt n
    .. math:: v = n - 1

    and in case of independent groups with equal sample sizes:

    .. math:: \delta = d * \sqrt{\frac{n}{2}}
    .. math:: v = (n - 1) * 2

    where :math:`d` is the Cohen d and :math:`n` the sample size.

    The critical value is then found using the percent point function of the T distribution with
    :math:`q = 1 - alpha` and :math:`v` degrees of freedom.

    Finally, the power of the test is given by the survival function of the non-central
    distribution using the previously calculated critical value, degrees of freedom and
    non-centrality parameter.

    :py:func:`scipy.optimize.brenth` is used to solve power equations for other variables (i.e.
    sample size, effect size, or significance level). If the solving fails, a nan value is
    returned.

    Results have been tested against GPower and the
    `pwr <https://cran.r-project.org/web/packages/pwr/pwr.pdf>`_ R package.

    Examples
    --------
    1. Compute power of a one-sample T-test given ``d``, ``n`` and ``alpha``

    >>> from pingouin import power_ttest
    >>> print('power: %.4f' % power_ttest(d=0.5, n=20, contrast='one-sample'))
    power: 0.5645

    2. Compute required sample size given ``d``, ``power`` and ``alpha``

    >>> print('n: %.4f' % power_ttest(d=0.5, power=0.80, alternative='greater'))
    n: 50.1508

    3. Compute achieved ``d`` given ``n``, ``power`` and ``alpha`` level

    >>> print('d: %.4f' % power_ttest(n=20, power=0.80, alpha=0.05, contrast='paired'))
    d: 0.6604

    4. Compute achieved alpha level given ``d``, ``n`` and ``power``

    >>> print('alpha: %.4f' % power_ttest(d=0.5, n=20, power=0.80, alpha=None))
    alpha: 0.4430

    5. One-sided tests

    >>> from pingouin import power_ttest
    >>> print('power: %.4f' % power_ttest(d=0.5, n=20, alternative='greater'))
    power: 0.4634

    >>> print('power: %.4f' % power_ttest(d=0.5, n=20, alternative='less'))
    power: 0.0007
    c                 S   s   g | ]}|d kqS N .0vr   r   2/tmp/pip-unpacked-wheel-2te3nxqf/pingouin/power.py
<listcomp>~   s     zpower_ttest.<locals>.<listcomp>   z3Exactly one of n, d, power, and alpha must be None.r   greaterlessFAlternative must be one of 'two-sided' (default), 'greater' or 'less'.)z
one-sampleZpairedr   r      r   Nr   r   c                    s@   |d   }| t |   }tj| |}tj|||S Nr   npsqrtr   tppfnctcdfdnpoweralphadofnctcrittsampletsider   r   func   s    zpower_ttest.<locals>.funcc                    sV   |d   }| t |   }tjd|  |}tj|||tj| || S r   r   r   r   r   r   r    sfr!   r"   r*   r   r   r-      s    c                    sD   |d   }| t |   }tjd|  |}tj|||S r   r   r   r   r   r   r    r/   r"   r*   r   r   r-      s    r%   r&   c                    s    || ||| S r   r   )r$   r#   r%   r&   r-   r   r   _eval_n   s    zpower_ttest.<locals>._eval_ngo    @    cAargsgHz>
   i   r8   c                    s    | |||| S r   r   )r#   r$   r%   r&   r2   r   r   _eval_d   s    zpower_ttest.<locals>._eval_dc                    s    |||| | S r   r   )r&   r#   r$   r%   r2   r   r   _eval_alpha   s    z power_ttest.<locals>._eval_alpha绽|=A?)sum
ValueErrorAssertionErrorlowerabsr   r   nan)r#   r$   r%   r&   Zcontrastalternativen_noner3   b0b1r=   r>   r   )r-   r+   r,   r   r      sX    l







c                    s  t dd |||fD }|dkr(td|dks8td|dkrDdnd|d	k	r`dkr`t|}|d	k	rd
|  k r|dksn t|d	k	rd
|  k rdksn t|dkrfdd n"|dkrЇfdd nfdd |d	kr || |d	|dS |d	krv|dkrd\}}n|dkr(d\}}nd\}} fdd}	zt|	||| |||fdW S  tk
rr   tj Y S X nD fdd}
zt|
dd|| ||fdW S  tk
r   tj Y S X d	S )a  
    Evaluate power, effect size or  significance level of an independent two-samples T-test
    with unequal sample sizes.

    Parameters
    ----------
    nx, ny : int
        Sample sizes. Must be specified. If the sample sizes are equal, you should use the
        :py:func:`power_ttest` function instead.
    d : float
        Cohen d effect size
    power : float
        Test power (= 1 - type II error).
    alpha : float
        Significance level (type I error probability). The default is 0.05.
    alternative : string
        Defines the alternative hypothesis, or tail of the test. Must be one of "two-sided"
        (default), "greater" or "less".

    Notes
    -----
    Exactly ONE of the parameters ``d``, ``power`` and ``alpha`` must be passed as None, and that
    parameter is determined from the others.

    ``alpha`` has a default value of 0.05 so None must be explicitly passed if you want to compute
    it.

    This function is a Python adaptation of the `pwr.t2n.test` function implemented in the
    `pwr <https://cran.r-project.org/web/packages/pwr/pwr.pdf>`_ R package.

    Statistical power is the likelihood that a study will detect an effect when there is an effect
    there to be detected. A high statistical power means that there is a low probability of
    concluding that there is no effect when there is one. Statistical power is mainly affected by
    the effect size and the sample size.

    The first step is to use the Cohen's d to calculate the non-centrality parameter
    :math:`\delta` and degrees of freedom :math:`v`.cIn case of two independent groups with
    unequal sample sizes, this is:

    .. math:: \delta = d * \sqrt{\frac{n_i * n_j}{n_i + n_j}}
    .. math:: v = n_i + n_j - 2

    where :math:`d` is the Cohen d, :math:`n` the sample size,
    :math:`n_i` the sample size of the first group and
    :math:`n_j` the sample size of the second group,

    The critical value is then found using the percent point function of the T distribution with
    :math:`q = 1 - alpha` and :math:`v` degrees of freedom.

    Finally, the power of the test is given by the survival function of the non-central
    distribution using the previously calculated critical value, degrees of freedom and
    non-centrality parameter.

    :py:func:`scipy.optimize.brenth` is used to solve power equations for other variables (i.e.
    sample size, effect size, or significance level). If the solving fails, a nan value is
    returned.

    Results have been tested against GPower and the
    `pwr <https://cran.r-project.org/web/packages/pwr/pwr.pdf>`_ R package.

    Examples
    --------
    1. Compute achieved power of a T-test given ``d``, ``n`` and ``alpha``

    >>> from pingouin import power_ttest2n
    >>> print('power: %.4f' % power_ttest2n(nx=20, ny=15, d=0.5, alternative='greater'))
    power: 0.4164

    2. Compute achieved ``d`` given ``n``, ``power`` and ``alpha`` level

    >>> print('d: %.4f' % power_ttest2n(nx=20, ny=15, power=0.80, alpha=0.05))
    d: 0.9859

    3. Compute achieved alpha level given ``d``, ``n`` and ``power``

    >>> print('alpha: %.4f' % power_ttest2n(nx=20, ny=15, d=0.5, power=0.80, alpha=None))
    alpha: 0.5000
    c                 S   s   g | ]}|d kqS r   r   r   r   r   r   r   '  s     z!power_ttest2n.<locals>.<listcomp>r   z/Exactly one of d, power, and alpha must be Noner   r   r   r   Nr   r   c                    sL   || d }| dt d| d|    }tj|  |}tj|||S Nr   r   r   r#   nxnyr%   r&   r'   r(   r)   r,   r   r   r-   ;  s    zpower_ttest2n.<locals>.funcc                    sb   || d }| dt d| d|    }tjd|   |}tj|||tj| || S rK   r.   rL   rO   r   r   r-   C  s    c                    sP   || d }| dt d| d|    }tjd|   |}tj|||S rK   r0   rL   rO   r   r   r-   K  s    r1   r7   r9   r;   c                    s    | ||||| S r   r   )r#   rM   rN   r%   r&   r2   r   r   r=   _  s    zpower_ttest2n.<locals>._eval_dr5   c                    s    ||||| | S r   r   )r&   r#   rM   rN   r%   r2   r   r   r>   j  s    z"power_ttest2n.<locals>._eval_alphar?   r@   rA   rB   rC   rE   r   r   rF   )rM   rN   r#   r%   r&   rG   rH   rI   rJ   r=   r>   r   )r-   r,   r   r      sH    P





c                    s  t dd | ||||fD }|dkr0d}t|| dk	rLt| } | d|   }|dk	rnd|  k rhdksnn t|dk	rd|  k rdksn tdd  |dkr |||||S |dkr fd	d
}zt|dd||||fdW S  tk
 r   tj Y S X  n|dkrR fdd}	zt|	dd||||fdW S  tk
rN   tj Y S X n| dkr fdd}
z&t|
dd||||fd}||d  W S  tk
r   tj Y S X nD fdd}zt|dd||||fdW S  tk
r   tj Y S X dS )a  
    Evaluate power, sample size, effect size or significance level of a one-way balanced ANOVA.

    Parameters
    ----------
    eta_squared : float
        ANOVA effect size (eta-squared, :math:`\eta^2`).
    k : int
        Number of groups
    n : int
        Sample size per group. Groups are assumed to be balanced (i.e. same sample size).
    power : float
        Test power (= 1 - type II error).
    alpha : float
        Significance level :math:`\alpha` (type I error probability). The default is 0.05.

    Notes
    -----
    Exactly ONE of the parameters ``eta_squared``, ``k``, ``n``, ``power`` and ``alpha``
    must be passed as None, and that parameter is determined from the others.

    ``alpha`` has a default value of 0.05 so None must be explicitly passed if you want to
    compute it.

    This function is a Python adaptation of the `pwr.anova.test` function implemented in the
    `pwr <https://cran.r-project.org/web/packages/pwr/pwr.pdf>`_ R package.

    Statistical power is the likelihood that a study will detect an effect when there is an
    effect there to be detected. A high statistical power means that there is a low probability of
    concluding that there is no effect when there is one. Statistical power is mainly affected by
    the effect size and the sample size.

    For one-way ANOVA, eta-squared is the same as partial eta-squared. It can be evaluated from the
    F-value (:math:`F^*`) and the degrees of freedom of the ANOVA (:math:`v_1, v_2`) using the
    following formula:

    .. math:: \eta^2 = \frac{v_1 F^*}{v_1 F^* + v_2}

    GPower uses the :math:`f` effect size instead of the :math:`\eta^2`. The formula to convert
    from one to the other are given below:

    .. math:: f = \sqrt{\frac{\eta^2}{1 - \eta^2}}

    .. math:: \eta^2 = \frac{f^2}{1 + f^2}

    Using :math:`\eta^2` and the total sample size :math:`N`, the non-centrality parameter is
    defined by:

    .. math:: \delta = N * \frac{\eta^2}{1 - \eta^2}

    Then the critical value of the non-central F-distribution is computed using the percentile
    point function of the F-distribution with:

    .. math:: q = 1 - \alpha
    .. math:: v_1 = k - 1
    .. math:: v_2 = N - k

    where :math:`k` is the number of groups.

    Finally, the power of the ANOVA is calculated using the survival function of the non-central
    F-distribution using the previously computed critical value, non-centrality parameter, and
    degrees of freedom.

    :py:func:`scipy.optimize.brenth` is used to solve power equations for other variables (i.e.
    sample size, effect size, or significance level). If the solving fails, a nan value is
    returned.

    Results have been tested against GPower and the
    `pwr <https://cran.r-project.org/web/packages/pwr/pwr.pdf>`_ R package.

    Examples
    --------
    1. Compute achieved power

    >>> from pingouin import power_anova
    >>> print('power: %.4f' % power_anova(eta_squared=0.1, k=3, n=20))
    power: 0.6082

    2. Compute required number of groups

    >>> print('k: %.4f' % power_anova(eta_squared=0.1, n=20, power=0.80))
    k: 6.0944

    3. Compute required sample size

    >>> print('n: %.4f' % power_anova(eta_squared=0.1, k=3, power=0.80))
    n: 29.9256

    4. Compute achieved effect size

    >>> print('eta-squared: %.4f' % power_anova(n=20, k=4, power=0.80, alpha=0.05))
    eta-squared: 0.1255

    5. Compute achieved alpha (significance)

    >>> print('alpha: %.4f' % power_anova(eta_squared=0.1, n=20, k=4, power=0.80, alpha=None))
    alpha: 0.1085
    c                 S   s   g | ]}|d kqS r   r   r   r   r   r   r     s     zpower_anova.<locals>.<listcomp>r   z8Exactly one of eta, k, n, power, and alpha must be None.Nr   c           	      S   sF   || |  }|d }|| | }t jd| ||}t j||||S r   r   fr   Zncfr/   )	f_sqkr$   r%   r&   r(   dof1dof2fcritr   r   r   r-     s
    zpower_anova.<locals>.funcc                    s    || |||| S r   r   )rT   rS   r$   r%   r&   r2   r   r   _eval_k  s    zpower_anova.<locals>._eval_kr   d   r5   c                    s    ||| ||| S r   r   )r$   rS   rT   r%   r&   r2   r   r   r3     s    zpower_anova.<locals>._eval_nr4   c                    s    | ||||| S r   r   )rS   rT   r$   r%   r&   r2   r   r   	_eval_eta
  s    zpower_anova.<locals>._eval_etar?   r@   c                    s    ||||| | S r   r   )r&   rS   rT   r$   r%   r2   r   r   r>     s    z power_anova.<locals>._eval_alpha)rA   rB   rE   rC   r   r   rF   )eta_squaredrT   r$   r%   r&   rH   errrS   rX   r3   rZ   r>   r   r2   r   r   s  sL    d


      ?r   c              	      s~  t dd | ||||fD }|dkr0d}t|d   k rDdksNn tdd|  k rbdk sln td| d	k	rt| } | d|   }	|d	k	rd|  k rdksn td
|d	k	rd|  k rdksn td|d	k	r|dkstd|d	k	r|dkstd fdd|d	kr0|	|||||S |d	krfdd}
zt|
dd|	||||fdW S  tk
r~   tj Y S X  n|d	krևfdd}zt|dd|	||||fdW S  tk
r   tj Y S X n| d	kr4fdd}z(t|dd|||||fd}	|	|	d  W S  tk
r0   tj Y S X nFfdd}zt|dd|	||||fdW S  tk
rx   tj Y S X d	S )a)  
    Evaluate power, sample size, effect size or significance level of a balanced one-way
    repeated measures ANOVA.

    Parameters
    ----------
    eta_squared : float
        ANOVA effect size (eta-squared, :math:`\eta^2`).
    m : int
        Number of repeated measurements.
    n : int
        Sample size per measurement. All measurements must have the same sample size.
    power : float
        Test power (= 1 - type II error).
    alpha : float
        Significance level :math:`\alpha` (type I error probability). The default is 0.05.
    corr : float
        Average correlation coefficient among repeated measurements. The default is :math:`r=0.5`.
    epsilon : float
        Epsilon adjustement factor for sphericity. This can be calculated using the
        :py:func:`pingouin.epsilon` function.

    Notes
    -----
    Exactly ONE of the parameters ``eta_squared``, ``m``, ``n``, ``power`` and ``alpha`` must be
    passed as None, and that parameter is determined from the others.

    ``alpha`` has a default value of 0.05 so None must be explicitly passed if you want to
    compute it.

    Statistical power is the likelihood that a study will detect an effect when there is an effect
    there to be detected. A high statistical power means that there is a low probability of
    concluding that there is no effect when there is one. Statistical power is mainly affected by
    the effect size and the sample size.

    GPower uses the :math:`f` effect size instead of the :math:`\eta^2`. The formula to convert
    from one to the other are given below:

    .. math:: f = \sqrt{\frac{\eta^2}{1 - \eta^2}}

    .. math:: \eta^2 = \frac{f^2}{1 + f^2}

    Using :math:`\eta^2`, the sample size :math:`N`, the number of repeated measurements
    :math:`m`, the epsilon correction factor :math:`\epsilon` (see :py:func:`pingouin.epsilon`),
    and the average correlation between the repeated measures :math:`c`, one can then calculate the
    non-centrality parameter as follow:

    .. math:: \delta = \frac{f^2 * N * m * \epsilon}{1 - c}

    Then the critical value of the non-central F-distribution is computed using the percentile
    point function of the F-distribution with:

    .. math:: q = 1 - \alpha
    .. math:: v_1 = (m - 1) * \epsilon
    .. math:: v_2 = (N - 1) * v_1

    Finally, the power of the ANOVA is calculated using the survival function of the non-central
    F-distribution using the previously computed critical value, non-centrality parameter,
    and degrees of freedom.

    :py:func:`scipy.optimize.brenth` is used to solve power equations for other variables
    (i.e. sample size, effect size, or significance level). If the solving fails, a nan value is
    returned.

    Results have been tested against GPower and the
    `pwr <https://cran.r-project.org/web/packages/pwr/pwr.pdf>`_ R package.

    Examples
    --------
    1. Compute achieved power

    >>> from pingouin import power_rm_anova
    >>> print('power: %.4f' % power_rm_anova(eta_squared=0.1, m=3, n=20))
    power: 0.8913

    2. Compute required number of groups

    >>> print('m: %.4f' % power_rm_anova(eta_squared=0.1, n=20, power=0.90))
    m: 3.1347

    3. Compute required sample size

    >>> print('n: %.4f' % power_rm_anova(eta_squared=0.1, m=3, power=0.80))
    n: 15.9979

    4. Compute achieved effect size

    >>> print('eta-squared: %.4f' % power_rm_anova(n=20, m=4, power=0.80, alpha=0.05))
    eta-squared: 0.0680

    5. Compute achieved alpha (significance)

    >>> print('alpha: %.4f' % power_rm_anova(eta_squared=0.1, n=20, m=4, power=0.80, alpha=None))
    alpha: 0.0081

    Let's take a more concrete example. First, we'll load a repeated measures
    dataset in wide-format. Each row is an observation (e.g. a subject), and
    each column a successive repeated measurements (e.g t=0, t=1, ...).

    >>> import pingouin as pg
    >>> data = pg.read_dataset('rm_anova_wide')
    >>> data.head()
       Before  1 week  2 week  3 week
    0     4.3     5.3     4.8     6.3
    1     3.9     2.3     5.6     4.3
    2     4.5     2.6     4.1     NaN
    3     5.1     4.2     6.0     6.3
    4     3.8     3.6     4.8     6.8

    Note that this dataset has some missing values. We'll simply delete any row with one or more
    missing values, and then compute a repeated measures ANOVA:

    >>> data = data.dropna()
    >>> pg.rm_anova(data, effsize="n2").round(3)
       Source  ddof1  ddof2      F  p-unc     n2    eps
    0  Within      3     24  5.201  0.007  0.346  0.694

    The repeated measures ANOVA is significant at the 0.05 level. Now, we can
    easily compute the power of the ANOVA with the information in the ANOVA table:

    >>> # n is the sample size and m is the number of repeated measures
    >>> n, m = data.shape
    >>> round(pg.power_rm_anova(eta_squared=0.346, m=m, n=n, epsilon=0.694), 3)
    0.99

    Our ANOVA has a very high statistical power. However, to be even more accurate in our power
    calculation, we should also fill in the average correlation among repeated measurements.
    Since our dataframe is in wide-format (with each column being a successive measurement), this
    can be done by taking the mean of the superdiagonal of the correlation matrix, which is similar
    to manually calculating the correlation between each successive pairwise measurements and then
    taking the mean. Since correlation coefficients are not normally distributed, we use the
    *r-to-z* transform prior to averaging (:py:func:`numpy.arctanh`), and then the *z-to-r*
    transform (:py:func:`numpy.tanh`) to convert back to a correlation coefficient. This gives a
    more precise estimate of the mean.

    >>> import numpy as np
    >>> corr = np.diag(data.corr(), k=1)
    >>> avgcorr = np.tanh(np.arctanh(corr).mean())
    >>> round(avgcorr, 4)
    -0.1996

    In this example, we're using a fake dataset and the average correlation is negative. However,
    it will most likely be positive with real data. Let's now compute the final power of the
    repeated measures ANOVA:

    >>> round(pg.power_rm_anova(eta_squared=0.346, m=m, n=n, epsilon=0.694, corr=avgcorr), 3)
    0.771
    c                 S   s   g | ]}|d kqS r   r   r   r   r   r   r     s     z"power_rm_anova.<locals>.<listcomp>r   z8Exactly one of eta, m, n, power, and alpha must be None.r   z epsilon must be between 0 and 1.zcorr must be between -1 and 1.Nzalpha must be between 0 and 1.zpower must be between 0 and 1.zThe sample size n must be > 1.z.The number of repeated measures m must be > 1.c           
         sV   |d   }|d | }| | |   d|  }t jd| ||}	t j|	|||S r   rQ   )
rS   mr$   r%   r&   corrrU   rV   r(   rW   )epsilonr   r   r-     s
    zpower_rm_anova.<locals>.funcc                    s    || ||||| S r   r   )r_   rS   r$   r%   r&   r`   r2   r   r   _eval_m  s    zpower_rm_anova.<locals>._eval_mr   rY   r5   c                    s    ||| |||| S r   r   )r$   rS   r_   r%   r&   r`   r2   r   r   r3     s    zpower_rm_anova.<locals>._eval_nr:   g    .Ac                    s    | |||||| S r   r   )rS   r_   r$   r%   r&   r`   r2   r   r   rZ     s    z!power_rm_anova.<locals>._eval_etar?   r@   c                    s    ||||| || S r   r   )r&   rS   r_   r$   r%   r`   r2   r   r   r>     s    z#power_rm_anova.<locals>._eval_alpharP   )r[   r_   r$   r%   r&   r`   ra   rH   msgrS   rb   r3   rZ   r>   r   )ra   r-   r   r     sZ     




c           	         sZ  t dd | |||fD }|dkr*td|dks:td| dk	rld|   krVdks\n t|d	krlt| } |dk	rd
|  k rdksn t|dk	rd
|  k rdksn t|dk	r|dkrtd tjS |d	krdd  n|dkrdd  ndd  |dkr*|dk	r*| dk	r* | |d|dS |dkr|dk	r| dk	r fdd}zt|dd| ||fdW S  tk
r   tj Y S X n| dkr|dk	r|dk	r fdd}z>|d	krt|dd|||fdW S t|dd|||fdW S W n tk
r   tj Y S X nB fdd}zt|dd| ||fdW S  tk
rT   tj Y S X dS )a<	  
    Evaluate power, sample size, correlation coefficient or significance level of a correlation
    test.

    Parameters
    ----------
    r : float
        Correlation coefficient.
    n : int
        Number of observations (sample size).
    power : float
        Test power (= 1 - type II error).
    alpha : float
        Significance level (type I error probability). The default is 0.05.
    alternative : string
        Defines the alternative hypothesis, or tail of the correlation. Must be one of
        "two-sided" (default), "greater" or "less". Both "greater" and "less" return a one-sided
        p-value. "greater" tests against the alternative hypothesis that the correlation is
        positive (greater than zero), "less" tests against the hypothesis that the correlation is
        negative.

    Notes
    -----
    Exactly ONE of the parameters ``r``, ``n``, ``power`` and ``alpha`` must be passed as None,
    and that parameter is determined from the others.

    ``alpha`` has a default value of 0.05 so None must be explicitly passed if you want to
    compute it.

    :py:func:`scipy.optimize.brenth` is used to solve power equations for other variables (i.e.
    sample size, effect size, or significance level). If the solving fails, a nan value is
    returned.

    This function is a Python adaptation of the `pwr.r.test` function implemented in the
    `pwr <https://cran.r-project.org/web/packages/pwr/pwr.pdf>`_ R package.

    Examples
    --------
    1. Compute achieved power given ``r``, ``n`` and ``alpha``

    >>> from pingouin import power_corr
    >>> print('power: %.4f' % power_corr(r=0.5, n=20))
    power: 0.6379

    2. Same but one-sided test

    >>> print('power: %.4f' % power_corr(r=0.5, n=20, alternative="greater"))
    power: 0.7510

    >>> print('power: %.4f' % power_corr(r=0.5, n=20, alternative="less"))
    power: 0.0000

    3. Compute required sample size given ``r``, ``power`` and ``alpha``

    >>> print('n: %.4f' % power_corr(r=0.5, power=0.80))
    n: 28.2484

    4. Compute achieved ``r`` given ``n``, ``power`` and ``alpha`` level

    >>> print('r: %.4f' % power_corr(n=20, power=0.80, alpha=0.05))
    r: 0.5822

    5. Compute achieved alpha level given ``r``, ``n`` and ``power``

    >>> print('alpha: %.4f' % power_corr(r=0.5, n=20, power=0.80, alpha=None))
    alpha: 0.1377
    c                 S   s   g | ]}|d kqS r   r   r   r   r   r   r   H  s     zpower_corr.<locals>.<listcomp>r   z2Exactly one of n, r, power, and alpha must be Noner   r   Nr^   r   r      zCSample size is too small to estimate power (n <= 4). Returning NaN.c           	      S   s   |d }t jd|d  |}t|d |d |  }t| | d|d    }t|}t j|| t|d  t j| | t|d   }|S Nr   r      r   r   r   r   r   ZarctanhZnormr!   	rr$   r%   r&   r'   ZtttrcZzrZzrcr   r   r   r-   c  s    
"zpower_corr.<locals>.funcr   c           	      S   sz   |d }t jd| |}t|d |d |  }t| | d|d    }t|}t j|| t|d  }|S re   rg   rh   r   r   r   r-   p  s    
c           	      S   s   |  } |d }t jd| |}t|d |d |  }t| | d|d    }t|}t j|| t|d  }|S re   rg   rh   r   r   r   r-   {  s    
r1   c                    s    || ||| S r   r   )r$   ri   r%   r&   r2   r   r   r3     s    zpower_corr.<locals>._eval_ngη   @g    eAr5   c                    s    | |||| S r   r   )ri   r$   r%   r&   r2   r   r   _eval_r  s    zpower_corr.<locals>._eval_rr?   r@   gAc                    s    |||| | S r   r   )r&   ri   r$   r%   r2   r   r   r>     s    zpower_corr.<locals>._eval_alpha)	rA   rB   rC   rE   warningswarnr   rF   r   )	ri   r$   r%   r&   rG   rH   r3   rk   r>   r   r2   r   r     sX    E




c           
         s  t  ttfsttdd ||||fD }|dkr@d}t||dk	rPt|}|dk	rrd|  k rldksrn t|dk	rd|  k rdksn t fdd|dkr||||S |dkrfd	d
}zt|dd|||fdW S  tk
r    tj	 Y S X n|dkrRfdd}zt|dd|||fdW S  tk
rN   tj	 Y S X nBfdd}	zt|	dd|||fdW S  tk
r   tj	 Y S X dS )a  
    Evaluate power, sample size, effect size or significance level of chi-squared tests.

    Parameters
    ----------
    dof : float
        Degree of freedom (depends on the chosen test).
    w : float
        Cohen's w effect size [1]_.
    n : int
        Total number of observations.
    power : float
        Test power (= 1 - type II error).
    alpha : float
        Significance level (type I error probability). The default is 0.05.

    Notes
    -----
    Exactly ONE of the parameters ``w``, ``n``, ``power`` and ``alpha`` must be passed as None,
    and that parameter is determined from the others. The degrees of freedom ``dof`` must always
    be specified.

    ``alpha`` has a default value of 0.05 so None must be explicitly passed if you want to
    compute it.

    This function is a Python adaptation of the `pwr.chisq.test` function implemented in the
    `pwr <https://cran.r-project.org/web/packages/pwr/pwr.pdf>`_ R package.

    Statistical power is the likelihood that a study will detect an effect when there is an effect
    there to be detected. A high statistical power means that there is a low probability of
    concluding that there is no effect when there is one. Statistical power is mainly affected by
    the effect size and the sample size.

    The non-centrality parameter is defined by:

    .. math:: \delta = N * w^2

    Then the critical value is computed using the percentile point function of the :math:`\chi^2`
    distribution with the alpha level and degrees of freedom.

    Finally, the power of the chi-squared test is calculated using the survival function of the
    non-central :math:`\chi^2` distribution using the previously computed critical value,
    non-centrality parameter, and the degrees of freedom of the test.

    :py:func:`scipy.optimize.brenth` is used to solve power equations for other variables (i.e.
    sample size, effect size, or significance level). If the solving fails, a nan value is
    returned.

    Results have been tested against GPower and the
    `pwr <https://cran.r-project.org/web/packages/pwr/pwr.pdf>`_ R package.

    References
    ----------
    .. [1] Cohen, J. (1988). Statistical power analysis for the behavioral sciences (2nd ed.).

    Examples
    --------
    1. Compute achieved power

    >>> from pingouin import power_chi2
    >>> print('power: %.4f' % power_chi2(dof=1, w=0.3, n=20))
    power: 0.2687

    2. Compute required sample size

    >>> print('n: %.4f' % power_chi2(dof=3, w=0.3, power=0.80))
    n: 121.1396

    3. Compute achieved effect size

    >>> print('w: %.4f' % power_chi2(dof=2, n=20, power=0.80, alpha=0.05))
    w: 0.6941

    4. Compute achieved alpha (significance)

    >>> print('alpha: %.4f' % power_chi2(dof=1, w=0.5, n=20, power=0.80, alpha=None))
    alpha: 0.1630
    c                 S   s   g | ]}|d kqS r   r   r   r   r   r   r      s     zpower_chi2.<locals>.<listcomp>r   z3Exactly one of w, n, power, and alpha must be None.Nr   c                    s.   t jd|  }|| d  }t j| |S )Nr   r   )r   Zchi2r   Zncx2r/   )wr$   r%   r&   rT   r(   )r'   r   r   r-     s    zpower_chi2.<locals>.funcc                    s    || ||| S r   r   )r$   rn   r%   r&   r2   r   r   r3     s    zpower_chi2.<locals>._eval_nr4   r5   c                    s    | |||| S r   r   )rn   r$   r%   r&   r2   r   r   _eval_w%  s    zpower_chi2.<locals>._eval_wr?   rY   c                    s    |||| | S r   r   )r&   rn   r$   r%   r2   r   r   r>   0  s    zpower_chi2.<locals>._eval_alphar@   )

isinstanceintfloatrC   rA   rB   rE   r   r   rF   )
r'   rn   r$   r%   r&   rH   r\   r3   ro   r>   r   )r'   r-   r   r	     s>    O

)NNNr
   r   r   )NNr
   r   )NNNNr
   )NNNNr
   r]   r   )NNNr
   r   )NNNr
   )rl   Znumpyr   Zscipyr   Zscipy.optimizer   __all__r   r   r   r   r   r	   r   r   r   r   <module>   s6              
 F
 
 -
 e
 -