
    >h                         S r SSKrSSKJr  SSKJr  SSKJrJ	r	  SSK
JrJr  SSKJr  SSKJr  SS	 jrSS
 jrS rS rS rS r  SS jrSS jr  SS jr  SS jr S S jr  S!S jrS"S jr  S#S jr   S$S jr  S%S jr    S&S jr!g)'zM
Created on Wed Mar 18 10:33:38 2020

Author: Josef Perktold
License: BSD-3

    N)stats)	ncfdtrinc)ncf_cdfncf_ppf)TrimmedMeanscale_transformHolder)HolderTuplec                    [         R                  " U 5      n U R                  S   n[         R                  " U5      S:X  a  [         R                  " U5      U-  nUR                  5       nUS:X  aP  [         R                  " U5      S:X  a  UnO1[         R                  " U5      nUS-
  U-  R                  5       Xe-
  -  nUnX!-  nUR                  5       n	X-  n
X-  n[         R                  " XU-
  S-  5      Xd-
  -  nUR                  5       S:X  a  UnUR                  5       n	X-  n
X-  nSX&-  -
  U-  R                  5       nSX U-
  S-  -  R                  5       -  nX-  nUSX&-  -
  R                  5       -  U-  nUS-
  nUS-  US-  R                  5       X&-  U-  R                  5       S-  -   SX&-  US-  -  R                  5       -  -
  -  nUUU-  -  nU$ )a  
Effect size corresponding to Cohen's f = nc / nobs for oneway anova

This contains adjustment for Welch and Brown-Forsythe Anova so that
effect size can be used with FTestAnovaPower.

Parameters
----------
means : array_like
    Mean of samples to be compared
vars_ : float or array_like
    Residual (within) variance of each sample or pooled
    If ``vars_`` is scalar, then it is interpreted as pooled variance that
    is the same for all samples, ``use_var`` will be ignored.
    Otherwise, the variances are used depending on the ``use_var`` keyword.
nobs : int or array_like
    Number of observations for the samples.
    If nobs is scalar, then it is assumed that all samples have the same
    number ``nobs`` of observation, i.e. a balanced sample case.
    Otherwise, statistics will be weighted corresponding to nobs.
    Only relative sizes are relevant, any proportional change to nobs does
    not change the effect size.
use_var : {"unequal", "equal", "bf"}
    If ``use_var`` is "unequal", then the variances can differ across
    samples and the effect size for Welch anova will be computed.
ddof_between : int
    Degrees of freedom correction for the weighted between sum of squares.
    The denominator is ``nobs_total - ddof_between``
    This can be used to match differences across reference literature.

Returns
-------
f2 : float
    Effect size corresponding to squared Cohen's f, which is also equal
    to the noncentrality divided by total number of observations.

Notes
-----
This currently handles the following cases for oneway anova

- balanced sample with homoscedastic variances
- samples with different number of observations and with homoscedastic
  variances
- samples with different number of observations and with heteroskedastic
  variances. This corresponds to Welch anova

In the case of "unequal" and "bf" methods for unequal variances, the
effect sizes do not directly correspond to the test statistic in Anova.
Both have correction terms dropped or added, so the effect sizes match up
with using FTestAnovaPower.
If all variances are equal, then all three methods result in the same
effect size. If variances are unequal, then the three methods produce
small differences in effect size.

Note, the effect size and power computation for BF Anova was not found in
the literature. The correction terms were added so that FTestAnovaPower
provides a good approximation to the power.

Status: experimental
We might add additional returns, if those are needed to support power
and sample size applications.

Examples
--------
The following shows how to compute effect size and power for each of the
three anova methods. The null hypothesis is that the means are equal which
corresponds to a zero effect size. Under the alternative, means differ
with two sample means at a distance delta from the mean. We assume the
variance is the same under the null and alternative hypothesis.

``nobs`` for the samples defines the fraction of observations in the
samples. ``nobs`` in the power method defines the total sample size.

In simulations, the computed power for standard anova,
i.e.``use_var="equal"`` overestimates the simulated power by a few percent.
The equal variance assumption does not hold in this example.

>>> from statsmodels.stats.oneway import effectsize_oneway
>>> from statsmodels.stats.power import FTestAnovaPower
>>>
>>> nobs = np.array([10, 12, 13, 15])
>>> delta = 0.5
>>> means_alt = np.array([-1, 0, 0, 1]) * delta
>>> vars_ = np.arange(1, len(means_alt) + 1)
>>>
>>> f2_alt = effectsize_oneway(means_alt, vars_, nobs, use_var="equal")
>>> f2_alt
0.04581300813008131
>>>
>>> kwds = {'effect_size': np.sqrt(f2_alt), 'nobs': 100, 'alpha': 0.05,
...         'k_groups': 4}
>>> power = FTestAnovaPower().power(**kwds)
>>> power
0.39165892158983273
>>>
>>> f2_alt = effectsize_oneway(means_alt, vars_, nobs, use_var="unequal")
>>> f2_alt
0.060640138408304504
>>>
>>> kwds['effect_size'] = np.sqrt(f2_alt)
>>> power = FTestAnovaPower().power(**kwds)
>>> power
0.5047366512800622
>>>
>>> f2_alt = effectsize_oneway(means_alt, vars_, nobs, use_var="bf")
>>> f2_alt
0.04391324307956788
>>>
>>> kwds['effect_size'] = np.sqrt(f2_alt)
>>> power = FTestAnovaPower().power(**kwds)
>>> power
0.3765792117047725

r      equal   bf      ?)npasarrayshapesizeonessumdotlower)meansvars_nobsuse_varddof_betweenn_groupsnobs_t	var_residweightsw_totalw_relmeanw_tf2tmp	statisticdf_num2df_nums                    kC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\statsmodels/stats/oneway.pyeffectsize_onewayr,      s   j JJuE{{1~H	wwt}wwx 4'XXZF'775>QIJJu%E(e+002f6GHIlGkkmGEmG	'/A-	.&2G	HB}}$++-!-T]"e+002$'/A!55::<<		"t},1133f<Q,aE1H>>+ ME1668!;<q 8==??@ A 	fwI    c                 L    U b  SSSU -  -   -  nOUb  USU-
  -  n [        XS9nU$ )a/  Convert squared effect sizes in f family

f2 is signal to noise ratio, var_explained / var_residual

eta2 is proportion of explained variance, var_explained / var_total

uses the relationship:
f2 = eta2 / (1 - eta2)

Parameters
----------
f2 : None or float
   Squared Cohen's F effect size. If f2 is not None, then eta2 will be
   computed.
eta2 : None or float
   Squared eta effect size. If f2 is None and eta2 is not None, then f2 is
   computed.

Returns
-------
res : Holder instance
    An instance of the Holder class with f2 and eta2 as attributes.

r   )r&   eta2r	   )r&   r/   ress      r+   convert_effectsize_fsqur1      s>    2 
~ABJ		QX
B
"CJr-   c           	          Uu  p#X-  U-  nXDS-   -  nU S-
  XS-   U-  -   -  nXBU-  -
  US-   SU-  -   -  nU S-
  XU-  -   -  nXBU-  -
  US-   -  n	[        XEXyUUS9$ )a6  Compute anova effect size from F-statistic

This might be combined with convert_effectsize_fsqu

Parameters
----------
f_stat : array_like
    Test statistic of an F-test
df : tuple
    degrees of freedom ``df = (df1, df2)`` where
     - df1 : numerator degrees of freedom, number of constraints
     - df2 : denominator degrees of freedom, df_resid

Returns
-------
res : Holder instance
    This instance contains effect size measures f2, eta2, omega2 and eps2
    as attributes.

Notes
-----
This uses the following definitions:

- f2 = f_stat * df1 / df2
- eta2 = f2 / (f2 + 1)
- omega2 = (f2 - df1 / df2) / (f2 + 2)
- eps2 = (f2 - df1 / df2) / (f2 + 1)

This differs from effect size measures in other function which define
``f2 = f_stat * df1 / nobs``
or an equivalent expression for power computation. The noncentrality
index for the hypothesis test is in those cases given by
``nc = f_stat * df1``.

Currently omega2 and eps2 are computed in two different ways. Those
values agree for regular cases but can show different behavior in corner
cases (e.g. zero division).

r   )r&   r/   omega2eps2eps2_omega2_r	   )
f_statdfdf1df2r&   r/   r6   r3   r5   r4   s
             r+   _fstat2effectsizer;      s    P HC		Ba=Dzfa367G9na!c'!12FaZF3Y./EsNrAv&DR6E!# #r-   c                     SU-  U S-  -  nU$ )aW  Convert Wellek's effect size (sqrt) to Cohen's f-squared

This computes the following effect size :

   f2 = 1 / n_groups * eps**2

Parameters
----------
eps : float or ndarray
    Wellek's effect size used in anova equivalence test
n_groups : int
    Number of groups in oneway comparison

Returns
-------
f2 : effect size Cohen's f-squared

r   r    )epsr   r&   s      r+   wellek_to_f2r?     s    & 
XQ	BIr-   c                 6    [         R                  " X-  5      nU$ )aj  Convert Cohen's f-squared to Wellek's effect size (sqrt)

This computes the following effect size :

   eps = sqrt(n_groups * f2)

Parameters
----------
f2 : float or ndarray
    Effect size Cohen's f-squared
n_groups : int
    Number of groups in oneway comparison

Returns
-------
eps : float or ndarray
    Wellek's effect size used in anova equivalence test
)r   sqrt)r&   r   r>   s      r+   f2_to_wellekrB   (  s    & ''(-
 CJr-   c                     XS-
  -  U-  nU$ )a  Convert F statistic to wellek's effect size eps squared

This computes the following effect size :

   es = f_stat * (n_groups - 1) / nobs_mean

Parameters
----------
f_stat : float or ndarray
    Test statistic of an F-test.
n_groups : int
    Number of groups in oneway comparison
nobs_mean : float or ndarray
    Average number of observations across groups.

Returns
-------
eps : float or ndarray
    Wellek's effect size used in anova equivalence test

r   r=   )r7   r   	nobs_meaness       r+   fstat_to_wellekrF   ?  s    , 
a<	 9	,BIr-   c                 T    Uu  pEUS;   a  US-  n[        XESU-
  U/U 5      nU$ [        e)u  
Confidence interval for noncentrality parameter in F-test

This does not yet handle non-negativity constraint on nc.
Currently only two-sided alternative is supported.

Parameters
----------
f_stat : float
df : tuple
    degrees of freedom ``df = (df1, df2)`` where

    - df1 : numerator degrees of freedom, number of constraints
    - df2 : denominator degrees of freedom, df_resid

alpha : float, default 0.05
alternative : {"two-sided"}
    Other alternatives have not been implements.

Returns
-------
float
    The end point of the confidence interval.

Notes
-----
The algorithm inverts the cdf of the noncentral F distribution with
respect to the noncentrality parameters.
See Steiger 2004 and references cited in it.

References
----------
.. [1] Steiger, James H. 2004. “Beyond the F Test: Effect Size Confidence
   Intervals and Tests of Close Fit in the Analysis of Variance and
   Contrast Analysis.” Psychological Methods 9 (2): 164–82.
   https://doi.org/10.1037/1082-989X.9.2.164.

See Also
--------
confint_effectsize_oneway
)	two-sided2stsr   r   )r   NotImplementedError)r7   r8   alphaalternativer9   r:   alpha1scis           r+   confint_noncentralityrP   Y  sD    X HC//!)s!g+w!7@ I "!r-   c                 |   Uu  pEUc  XE-   S-   n[        XUS9nXc-  n[        US9nXtU-  -
  US-   SU-  -   -  Ul        Xhl        [        R
                  " UR                  5      Ul        [        R
                  " UR                  5      Ul	        [        R
                  " UR                  US-   -  U-  5      Ul
        U$ )a  
Confidence interval for effect size in oneway anova for F distribution

This does not yet handle non-negativity constraint on nc.
Currently only two-sided alternative is supported.

Parameters
----------
f_stat : float
df : tuple
    degrees of freedom ``df = (df1, df2)`` where

    - df1 : numerator degrees of freedom, number of constraints
    - df2 : denominator degrees of freedom, df_resid

alpha : float, default 0.05
nobs : int, default None

Returns
-------
Holder
    Class with effect size and confidence attributes

Notes
-----
The confidence interval for the noncentrality parameter is obtained by
inverting the cdf of the noncentral F distribution. Confidence intervals
for other effect sizes are computed by endpoint transformation.


R package ``effectsize`` does not compute the confidence intervals in the
same way. Their confidence intervals can be replicated with

>>> ci_nc = confint_noncentrality(f_stat, df1, df2, alpha=0.1)
>>> ci_es = smo._fstat2effectsize(ci_nc / df1, df1, df2)

See Also
--------
confint_noncentrality
r   )rL   )r&   )rP   r1   	ci_omega2ci_ncr   rA   r&   ci_fr/   ci_etaci_f_corrected)	r7   r8   rL   r   r9   r:   rS   ci_f2ci_ress	            r+   confint_effectsize_onewayrY     s    T HC|y1}!&E:ELE$.Fc	)eai!c'.ABFL''&))$FKGGFKK(FMGGFIIq$9C$?@FMr-   c                 b   UUS.nU R                   S:w  a  [        S5      eUR                  5       n[        U 5      nUS:X  a  X!-  n	OUn	U	R                  5       n
X-  nX-  n[        R
                  " XU-
  S-  5      US-
  -  nUS-
  nUS:X  aG  SU-
  S-  US-
  -  R                  5       US-  S-
  -  nU(       a  USSUS-
  -  U-  -   -  nSSU-  -  nGO"US:X  a$  US-
  U-  R                  5       Xx-
  -  nX-  nXx-
  nOUS	:X  a  SX'-  -
  U-  R                  5       nSX U-
  S-  -  R                  5       -  nX-  nUS-
  nUS-  SX'-  -
  S-  US-  -  US-
  -  R                  5       -  nUS-  US-  R                  5       X'-  U-  R                  5       S-  -   SX'-  US-  -  R                  5       -  -
  -  n[        R                  R                  UUU5      nUU4US
'   UUS'   UUS'   O[        S5      e[        R                  R                  XU5      n[        SUUUU4UUUUU UUS.
UD6nU$ )a  
Oneway Anova based on summary statistics

Parameters
----------
means : array_like
    Mean of samples to be compared
variances : float or array_like
    Residual (within) variance of each sample or pooled.
    If ``variances`` is scalar, then it is interpreted as pooled variance
    that is the same for all samples, ``use_var`` will be ignored.
    Otherwise, the variances are used depending on the ``use_var`` keyword.
nobs : int or array_like
    Number of observations for the samples.
    If nobs is scalar, then it is assumed that all samples have the same
    number ``nobs`` of observation, i.e. a balanced sample case.
    Otherwise, statistics will be weighted corresponding to nobs.
    Only relative sizes are relevant, any proportional change to nobs does
    not change the effect size.
use_var : {"unequal", "equal", "bf"}
    If ``use_var`` is "unequal", then the variances can differ across
    samples and the effect size for Welch anova will be computed.
welch_correction : bool
    If this is false, then the Welch correction to the test statistic is
    not included. This allows the computation of an effect size measure
    that corresponds more closely to Cohen's f.
info : not used yet

Returns
-------
res : results instance
    This includes `statistic` and `pvalue`.

r   welch_correctionr   z+data (means, ...) has to be one-dimensionalunequalr   r   g      @r   r   r:   r)   pvalue2z2use_var is to be one of "unequal", "equal" or "bf")
r(   pvaluer8   r*   df_denomr    r   r   r   r   r=   )
ndim
ValueErrorr   lenr   r   r   fsfr   )r   	variancesr   r   r\   infooptionsr    r   r"   r#   r$   r%   r(   r*   r'   r`   r)   pval2pvalr0   s                        r+   anova_genericrk     s   H "#3G zzQFGGXXZF5zH)"kkmGEmGw1 45BGI]F)E	A~*//1Xq[1_EQ(Q,/#555Ic?	G	qI%**,0AB	$	DT]"i/446$'/A!55::<<		Q,6b4=0Q6&!^,/3ax9:=#%@aIN//1 MI5::<AB	Q >CCEEF G 

9gx8!8,$	"	 MNN77::i2D
 	! (+#'#'!%  C Jr-   c           	      l   Ub-  [         R                  " U5      nU Vs/ s H	  o`X:H     PM     n nO [        [        [         R                  U 5      5      n[        U Vs/ s H  oR                  S:g  PM     sn5      (       a  [        S5      e[         R                  " U Vs/ s H  n[        U5      PM     sn[        5      n	US:X  ah  [         R                  " U Vs/ s H  oR                  5       PM     sn5      n
[         R                  " U Vs/ s H  oR                  SS9PM     sn5      nOU Vs/ s H  n[        X5      PM     nn[         R                  " U Vs/ s H  oR                  PM     sn5      n
[         R                  " U Vs/ s H/  nUR                  UR                   S-
  -  UR"                  S-
  -  PM1     sn5      n[         R                  " U Vs/ s H  oR"                  PM     sn5      n	[%        XXUS9nU$ s  snf s  snf s  snf s  snf s  snf s  snf s  snf s  snf s  snf )u  Oneway Anova

This implements standard anova, Welch and Brown-Forsythe, and trimmed
(Yuen) variants of those.

Parameters
----------
data : tuple of array_like or DataFrame or Series
    Data for k independent samples, with k >= 2.
    The data can be provided as a tuple or list of arrays or in long
    format with outcome observations in ``data`` and group membership in
    ``groups``.
groups : ndarray or Series
    If data is in long format, then groups is needed as indicator to which
    group or sample and observations belongs.
use_var : {"unequal", "equal" or "bf"}
    `use_var` specified how to treat heteroscedasticity, unequal variance,
    across samples. Three approaches are available

    "unequal" : Variances are not assumed to be equal across samples.
        Heteroscedasticity is taken into account with Welch Anova and
        Satterthwaite-Welch degrees of freedom.
        This is the default.
    "equal" : Variances are assumed to be equal across samples.
        This is the standard Anova.
    "bf" : Variances are not assumed to be equal across samples.
        The method is Browne-Forsythe (1971) for testing equality of means
        with the corrected degrees of freedom by Merothra. The original BF
        degrees of freedom are available as additional attributes in the
        results instance, ``df_denom2`` and ``p_value2``.

welch_correction : bool
    If this is false, then the Welch correction to the test statistic is
    not included. This allows the computation of an effect size measure
    that corresponds more closely to Cohen's f.
trim_frac : float in [0, 0.5)
    Optional trimming for Anova with trimmed mean and winsorized variances.
    With the default trim_frac equal to zero, the oneway Anova statistics
    are computed without trimming. If `trim_frac` is larger than zero,
    then the largest and smallest observations in each sample are trimmed.
    The number of trimmed observations is the fraction of number of
    observations in the sample truncated to the next lower integer.
    `trim_frac` has to be smaller than 0.5, however, if the fraction is
    so large that there are not enough observations left over, then `nan`
    will be returned.

Returns
-------
res : results instance
    The returned HolderTuple instance has the following main attributes
    and some additional information in other attributes.

    statistic : float
        Test statistic for k-sample mean comparison which is approximately
        F-distributed.
    pvalue : float
        If ``use_var="bf"``, then the p-value is based on corrected
        degrees of freedom following Mehrotra 1997.
    pvalue2 : float
        This is the p-value based on degrees of freedom as in
        Brown-Forsythe 1974 and is only available if ``use_var="bf"``.
    df = (df_denom, df_num) : tuple of floats
        Degreeds of freedom for the F-distribution depend on ``use_var``.
        If ``use_var="bf"``, then `df_denom` is for Mehrotra p-values
        `df_denom2` is available for Brown-Forsythe 1974 p-values.
        `df_num` is the same numerator degrees of freedom for both
        p-values.

Notes
-----
Welch's anova is correctly sized (not liberal or conservative) in smaller
samples if the distribution of the samples is not very far away from the
normal distribution. The test can become liberal if the data is strongly
skewed. Welch's Anova can also be correctly sized for discrete
distributions with finite support, like Lickert scale data.
The trimmed version is robust to many non-normal distributions, it stays
correctly sized in many cases, and is more powerful in some cases with
skewness or heavy tails.

Trimming is currently based on the integer part of ``nobs * trim_frac``.
The default might change to including fractional observations as in the
original articles by Yuen.


See Also
--------
anova_generic

References
----------
Brown, Morton B., and Alan B. Forsythe. 1974. “The Small Sample Behavior
of Some Statistics Which Test the Equality of Several Means.”
Technometrics 16 (1) (February 1): 129–132. doi:10.2307/1267501.

Mehrotra, Devan V. 1997. “Improving the Brown-Forsythe Solution to the
Generalized Behrens-Fisher Problem.” Communications in Statistics -
Simulation and Computation 26 (3): 1139–1145.
doi:10.1080/03610919708813431.
r   z&data arrays have to be one-dimensionalr   )ddofr[   )r   uniquelistmapr   anyra   rb   arrayrc   floatmeanvarr   mean_trimmedvar_winsorizedr   nobs_reducedrk   )datagroupsr   r\   	trim_fracuniquesuniargsxr   r   r   tmstmr0   s                  r+   anova_onewayr   1  s   J ))F#/67wV]#w7 	BJJ%&D
&AFFaK&''ABB88T*TSVT*E2D
 A~D1Dq&&(D126A%%Q%-67267$Q{1($7C8Cb//C89 :=?:=B ++rww{;??Q.0:=? @ xx373R378
d)9;C J; 8
 ' + 2678? 8s5   H	H!HHH4H"H'6H, H1c                 p   UR                  5       nXq-  nUS:X  a  XS-  -  n	XS-
  -  U-  n
SnOUS;   a  Xs-  n	X-  n
SnO[        S5      e[        XTS   US   U	5      nUS:X  a  XS-
  -  U-  nO
US;   a  X-  nU
W:  n[        XS   US   U	5      n[        XS   US   S	5      n[	        U UU
UUUUUU US
9
nU$ )u~	  Equivalence test for oneway anova (Wellek and extensions)

This is an helper function when summary statistics are available.
Use `equivalence_oneway` instead.

The null hypothesis is that the means differ by more than `equiv_margin`
in the anova distance measure.
If the Null is rejected, then the data supports that means are equivalent,
i.e. within a given distance.

Parameters
----------
f_stat : float
    F-statistic
n_groups : int
    Number of groups in oneway comparison.
nobs : ndarray
    Array of number of observations in groups.
equiv_margin : float
    Equivalence margin in terms of effect size. Effect size can be chosen
    with `margin_type`. default is squared Cohen's f.
df : tuple
    degrees of freedom ``df = (df1, df2)`` where

    - df1 : numerator degrees of freedom, number of constraints
    - df2 : denominator degrees of freedom, df_resid

alpha : float in (0, 1)
    Significance level for the hypothesis test.
margin_type : "f2" or "wellek"
    Type of effect size used for equivalence margin.

Returns
-------
results : instance of HolderTuple class
    The two main attributes are test statistic `statistic` and p-value
    `pvalue`.

Notes
-----
Equivalence in this function is defined in terms of a squared distance
measure similar to Mahalanobis distance.
Alternative definitions for the oneway case are based on maximum difference
between pairs of means or similar pairwise distances.

The equivalence margin is used for the noncentrality parameter in the
noncentral F distribution for the test statistic. In samples with unequal
variances estimated using Welch or Brown-Forsythe Anova, the f-statistic
depends on the unequal variances and corrections to the test statistic.
This means that the equivalence margins are not fully comparable across
methods for treating unequal variances.

References
----------
Wellek, Stefan. 2010. Testing Statistical Hypotheses of Equivalence and
Noninferiority. 2nd ed. Boca Raton: CRC Press.

Cribbie, Robert A., Chantal A. Arpin-Cribbie, and Jamie A. Gruman. 2009.
“Tests of Equivalence for One-Way Independent Groups Designs.” The Journal
of Experimental Education 78 (1): 1–13.
https://doi.org/10.1080/00220970903224552.

Jan, Show-Li, and Gwowen Shieh. 2019. “On the Extended Welch Test for
Assessing Equivalence of Standardized Means.” Statistics in
Biopharmaceutical Research 0 (0): 1–8.
https://doi.org/10.1080/19466315.2019.1654915.

wellekr   r   zWellek's psi_squaredr&   fsqufsquaredzCohen's f_squared(`margin_type` should be "f2" or "wellek"r   vIh%<=)
r(   r_   
effectsizecrit_fcrit_esreject
power_zeror8   r7   type_effectsize)r   rb   r   r   r   )r7   r   r   equiv_marginr8   rL   margin_typer    rD   nc_nullrE   r   r   r   r   pvpwrr0   s                     r+   equivalence_oneway_genericr     s   L XXZF!IhAo-!|$y00	2	2'_-CDDUqE2a5'2FhqL)I5	2	2/7lF	A1w	/B
&Q%A
.C
!##%#!$#&5
C Jr-   c           
          [        XUUUS9nUR                  n[        XR                  UR                  XR
                  SUS9n	U	$ )a+  equivalence test for oneway anova (Wellek's Anova)

The null hypothesis is that the means differ by more than `equiv_margin`
in the anova distance measure.
If the Null is rejected, then the data supports that means are equivalent,
i.e. within a given distance.

Parameters
----------
data : tuple of array_like or DataFrame or Series
    Data for k independent samples, with k >= 2.
    The data can be provided as a tuple or list of arrays or in long
    format with outcome observations in ``data`` and group membership in
    ``groups``.
equiv_margin : float
    Equivalence margin in terms of effect size. Effect size can be chosen
    with `margin_type`. default is squared Cohen's f.
groups : ndarray or Series
    If data is in long format, then groups is needed as indicator to which
    group or sample and observations belongs.
use_var : {"unequal", "equal" or "bf"}
    `use_var` specified how to treat heteroscedasticity, unequal variance,
    across samples. Three approaches are available

    "unequal" : Variances are not assumed to be equal across samples.
        Heteroscedasticity is taken into account with Welch Anova and
        Satterthwaite-Welch degrees of freedom.
        This is the default.
    "equal" : Variances are assumed to be equal across samples.
        This is the standard Anova.
    "bf: Variances are not assumed to be equal across samples.
        The method is Browne-Forsythe (1971) for testing equality of means
        with the corrected degrees of freedom by Merothra. The original BF
        degrees of freedom are available as additional attributes in the
        results instance, ``df_denom2`` and ``p_value2``.

welch_correction : bool
    If this is false, then the Welch correction to the test statistic is
    not included. This allows the computation of an effect size measure
    that corresponds more closely to Cohen's f.
trim_frac : float in [0, 0.5)
    Optional trimming for Anova with trimmed mean and winsorized variances.
    With the default trim_frac equal to zero, the oneway Anova statistics
    are computed without trimming. If `trim_frac` is larger than zero,
    then the largest and smallest observations in each sample are trimmed.
    The number of trimmed observations is the fraction of number of
    observations in the sample truncated to the next lower integer.
    `trim_frac` has to be smaller than 0.5, however, if the fraction is
    so large that there are not enough observations left over, then `nan`
    will be returned.
margin_type : "f2" or "wellek"
    Type of effect size used for equivalence margin, either squared
    Cohen's f or Wellek's psi. Default is "f2".

Returns
-------
results : instance of HolderTuple class
    The two main attributes are test statistic `statistic` and p-value
    `pvalue`.

See Also
--------
anova_oneway
equivalence_scale_oneway
rz   r   r\   r{   皙?rL   r   )r   r(   r   r   r    r8   )
ry   r   rz   r   r\   r{   r   res0r7   r0   s
             r+   equivalence_onewayr   %  sM    J W)9"+-D ^^F
$V]]DKK%177$1<>C Jr-   c           
          [        XX#UUSS9nUR                  5       U-  nU nXS-
  -  U-  n	[        UR                  US   US   Xy-  5      n
U
$ )a  Empirical power of oneway equivalence test

This only returns post-hoc, empirical power.

Warning: eps is currently effect size margin as defined as in Wellek, and
not the signal to noise ratio (Cohen's f family).

Parameters
----------
f_stat : float
    F-statistic from oneway anova, used to compute empirical effect size
n_groups : int
    Number of groups in oneway comparison.
nobs : ndarray
    Array of number of observations in groups.
eps : float
    Equivalence margin in terms of effect size given by Wellek's psi.
df : tuple
    Degrees of freedom for F distribution.
alpha : float in (0, 1)
    Significance level for the hypothesis test.

Returns
-------
pow : float
    Ex-post, post-hoc or empirical power at f-statistic of the equivalence
    test.
r   r   r   r   )r   r   r   r   )r7   r   r   r>   r8   rL   r0   rD   fnesnpow_s              r+   _power_equivalence_oneway_empr   u  se    < %Vt"+0hHC 
X%I	B
1
	
)C3::r!ubeY_=DKr-   c                     Uc  Uc  [        S5      eUS-
  X#-
  4nU S:X  a  Sn US;   a  UnO8US:X  a'  Uc  [        S5      eX#-  nXS-  -  U-  nXS-  -  U-  n O[        S	5      e[        XTS   US   X'-  5      n	[        XS   US   X -  5      n
U
$ )
a  
Power of  oneway equivalence test

Parameters
----------
f2_alt : float
    Effect size, squared Cohen's f, under the alternative.
equiv_margin : float
    Equivalence margin in terms of effect size. Effect size can be chosen
    with `margin_type`. default is squared Cohen's f.
nobs_t : ndarray
    Total number of observations summed over all groups.
n_groups : int
    Number of groups in oneway comparison. If margin_type is "wellek",
    then either ``n_groups`` or ``df`` has to be given.
df : tuple
    Degrees of freedom for F distribution,
    ``df = (n_groups - 1, nobs_t - n_groups)``
alpha : float in (0, 1)
    Significance level for the hypothesis test.
margin_type : "f2" or "wellek"
    Type of effect size used for equivalence margin, either squared
    Cohen's f or Wellek's psi. Default is "f2".

Returns
-------
pow_alt : float
    Power of the equivalence test at given equivalence effect size under
    the alternative.
z(either df or n_groups has to be providedr   r   r   r   r   z:If margin_type is wellek, then n_groups has to be providedr   r   )rb   r   r   )f2_altr   r    r   r8   rL   r   f2_nullrD   crit_f_marginpwr_alts              r+   power_equivalence_onewayr     s    D 
zGHHlF-.
 { 00		  . / / %	Ao-6QY&/CDDEa5"Q%1ABMmUBqE6?CGNr-   c                     Uc  / SQnUb  [         R                  " U5      nO[         R                  " [        U 5      5      nUR	                  5       n	[        U5      n
/ n/ n/ n/ n[        U5       GH6  nS [        XU5       5       u  nnnn/ n/ n/ n/ nU H  n[        UUUU/UUS9nUR                  n[        UXR                  5       UUR                  SUS9nUR                  UR                  5        UU
S-
  -  U	-  nUR                  U5        UR                  UR                  5        UR                  UR                   UR"                  UR$                  /5        M     UR                  U5        UR                  U5        UR                  U5        UR                  U5        GM9     [         R&                  " U5      n[         R&                  " U5      n[         R&                  " U5      n[         R&                  " U5      n[)        UUUUS9nU$ )a9  Simulate Power for oneway equivalence test (Wellek's Anova)

This function is experimental and written to evaluate asymptotic power
function. This function will change without backwards compatibility
constraints. The only part that is stable is `pvalue` attribute in results.

Effect size for equivalence margin

)r]   r   r   c              3   p   #    U  H,  u  pnX#[         R                  R                  U5      -  -   v   M.     g 7f)N)r   randomrandn).0nmstds       r+   	<genexpr>4simulate_power_equivalence_oneway.<locals>.<genexpr>  s1      E-CkqS BIIOOA$666-Cs   46)r   r{   r   r   r   )r7   otherr_   r   )r   rA   r   rc   rt   rangezipr   r(   r   r   r8   appendr_   r   extendr   r   r   r   r
   )r   r   r   r   k_mcr{   options_varr   stdsrD   r   res_mcf_mc	reject_mcother_mc_y0y1y2y3res_if_ireject_iother_iuvr   r7   res1	es_wellekr0   s                                 r+   !simulate_power_equivalence_onewayr     s    0wwu~wws5z"		I4yHFDIH4[E-0d-CEBB B  RR 0"*35D^^F-fh
.:DGG48:EGD LL%(Q,/);IJJy!OODKK(NNDKKtGH   	eC" 7 : ::dDzz(#HZZF

9%I
!C
 Jr-   c           
          [        [        R                  U 5      n U  Vs/ s H  n[        XbUUS9PM     nn[	        USUSUS9nXxl        U$ s  snf )a  Oneway Anova test for equal scale, variance or dispersion

This hypothesis test performs a oneway anova test on transformed data and
includes Levene and Brown-Forsythe tests for equal variances as special
cases.

Parameters
----------
data : tuple of array_like or DataFrame or Series
    Data for k independent samples, with k >= 2. The data can be provided
    as a tuple or list of arrays or in long format with outcome
    observations in ``data`` and group membership in ``groups``.
method : {"unequal", "equal" or "bf"}
    How to treat heteroscedasticity across samples. This is used as
    `use_var` option in `anova_oneway` and refers to the variance of the
    transformed data, i.e. assumption is on 4th moment if squares are used
    as transform.
    Three approaches are available:

    "unequal" : Variances are not assumed to be equal across samples.
        Heteroscedasticity is taken into account with Welch Anova and
        Satterthwaite-Welch degrees of freedom.
        This is the default.
    "equal" : Variances are assumed to be equal across samples.
        This is the standard Anova.
    "bf" : Variances are not assumed to be equal across samples.
        The method is Browne-Forsythe (1971) for testing equality of means
        with the corrected degrees of freedom by Merothra. The original BF
        degrees of freedom are available as additional attributes in the
        results instance, ``df_denom2`` and ``p_value2``.

center : "median", "mean", "trimmed" or float
    Statistic used for centering observations. If a float, then this
    value is used to center. Default is median.
transform : "abs", "square" or callable
    Transformation for the centered observations. If a callable, then this
    function is called on the centered data.
    Default is absolute value.
trim_frac_mean=0.1 : float in [0, 0.5)
    Trim fraction for the trimmed mean when `center` is "trimmed"
trim_frac_anova : float in [0, 0.5)
    Optional trimming for Anova with trimmed mean and Winsorized variances.
    With the default trim_frac equal to zero, the oneway Anova statistics
    are computed without trimming. If `trim_frac` is larger than zero,
    then the largest and smallest observations in each sample are trimmed.
    see ``trim_frac`` option in `anova_oneway`

Returns
-------
res : results instance
    The returned HolderTuple instance has the following main attributes
    and some additional information in other attributes.

    statistic : float
        Test statistic for k-sample mean comparison which is approximately
        F-distributed.
    pvalue : float
        If ``method="bf"``, then the p-value is based on corrected
        degrees of freedom following Mehrotra 1997.
    pvalue2 : float
        This is the p-value based on degrees of freedom as in
        Brown-Forsythe 1974 and is only available if ``method="bf"``.
    df : (df_denom, df_num)
        Tuple containing degrees of freedom for the F-distribution depend
        on ``method``. If ``method="bf"``, then `df_denom` is for Mehrotra
        p-values `df_denom2` is available for Brown-Forsythe 1974 p-values.
        `df_num` is the same numerator degrees of freedom for both
        p-values.

See Also
--------
anova_oneway
scale_transform
center	transformr{   NTr   )rp   r   r   r   r   data_transformed)	ry   methodr   r   trim_frac_meantrim_frac_anovar   xxdr0   s	            r+   test_scale_onewayr   !  sl    Z rzz4 D>BD>B 1y%35>B  D s4(,ICJDs   Ac           
          [        [        R                  U 5      n U  Vs/ s H  n[        XsUUS9PM     nn[	        XUSUS9n	Xl        U	$ s  snf )a  Oneway Anova test for equivalence of scale, variance or dispersion

This hypothesis test performs a oneway equivalence anova test on
transformed data.

Note, the interpretation of the equivalence margin `equiv_margin` will
depend on the transformation of the data. Transformations like
absolute deviation are not scaled to correspond to the variance under
normal distribution.

Parameters
----------
data : tuple of array_like or DataFrame or Series
    Data for k independent samples, with k >= 2. The data can be provided
    as a tuple or list of arrays or in long format with outcome
    observations in ``data`` and group membership in ``groups``.
equiv_margin : float
    Equivalence margin in terms of effect size. Effect size can be chosen
    with `margin_type`. default is squared Cohen's f.
method : {"unequal", "equal" or "bf"}
    How to treat heteroscedasticity across samples. This is used as
    `use_var` option in `anova_oneway` and refers to the variance of the
    transformed data, i.e. assumption is on 4th moment if squares are used
    as transform.
    Three approaches are available:

    "unequal" : Variances are not assumed to be equal across samples.
        Heteroscedasticity is taken into account with Welch Anova and
        Satterthwaite-Welch degrees of freedom.
        This is the default.
    "equal" : Variances are assumed to be equal across samples.
        This is the standard Anova.
    "bf" : Variances are not assumed to be equal across samples.
        The method is Browne-Forsythe (1971) for testing equality of means
        with the corrected degrees of freedom by Merothra. The original BF
        degrees of freedom are available as additional attributes in the
        results instance, ``df_denom2`` and ``p_value2``.
center : "median", "mean", "trimmed" or float
    Statistic used for centering observations. If a float, then this
    value is used to center. Default is median.
transform : "abs", "square" or callable
    Transformation for the centered observations. If a callable, then this
    function is called on the centered data.
    Default is absolute value.
trim_frac_mean : float in [0, 0.5)
    Trim fraction for the trimmed mean when `center` is "trimmed"
trim_frac_anova : float in [0, 0.5)
    Optional trimming for Anova with trimmed mean and Winsorized variances.
    With the default trim_frac equal to zero, the oneway Anova statistics
    are computed without trimming. If `trim_frac` is larger than zero,
    then the largest and smallest observations in each sample are trimmed.
    see ``trim_frac`` option in `anova_oneway`

Returns
-------
results : instance of HolderTuple class
    The two main attributes are test statistic `statistic` and p-value
    `pvalue`.

See Also
--------
anova_oneway
scale_transform
equivalence_oneway
r   T)r   r\   r{   )rp   r   r   r   r   x_transformed)
ry   r   r   r   r   r   r   r   r   r0   s
             r+   equivalence_scale_onewayr   x  sj    H rzz4 D>BD>B 1y%35>B  D S.2oOCJDs   A
)r]   r   )NN)r   rH   )r   N)r]   TN)Nr]   Tr   )r   r&   )Nr]   Tr   r&   )r   )NNr   r&   )Ni  r   Nr&   )r   medianabsg?        )r   r   r   r   r   )"__doc__numpyr   scipyr   scipy.specialr   statsmodels.stats.powerr   r    statsmodels.stats.robust_comparer   r   statsmodels.tools.testingr
   statsmodels.stats.baser   r,   r1   r;   r?   rB   rF   rP   rY   rk   r   r   r   r   r   r   r   r   r=   r-   r+   <module>r      s      # 5 I , .`F F0#l..4 -1&13l7t 3<.2eP IMDP 8<jZ AJGKM`&R EI>B=@ HL;<DH@F EJ:=Tn FN=?-/Kr-   