def compute_effsize(x, y, paired=False, eftype='cohen'):
    """Calculate effect size between two set of observations.

    Parameters
    ----------
    x : np.array or list
        First set of observations.
    y : np.array or list
        Second set of observations.
    paired : boolean
        If True, uses Cohen d-avg formula to correct for repeated measurements
        (Cumming 2012)
    eftype : string
        Desired output effect size.
        Available methods are ::

        'none' : no effect size
        'cohen' : Unbiased Cohen d
        'hedges' : Hedges g
        'glass': Glass delta
        'r' : correlation coefficient
        'eta-square' : Eta-square
        'odds-ratio' : Odds ratio
        'AUC' : Area Under the Curve
        'CLES' : Common language effect size

    Returns
    -------
    ef : float
        Effect size

    See Also
    --------
    convert_effsize : Conversion between effect sizes.
    compute_effsize_from_t : Convert a T-statistic to an effect size.

    Notes
    -----
    Missing values are automatically removed from the data. If ``x`` and ``y``
    are paired, the entire row is removed.

    If ``x`` and ``y`` are independent, the Cohen's d is:

    .. math::

        d = \\frac{\\overline{X} - \\overline{Y}}
        {\\sqrt{\\frac{(n_{1} - 1)\\sigma_{1}^{2} + (n_{2} - 1)
        \\sigma_{2}^{2}}{n1 + n2 - 2}}}

    If ``x`` and ``y`` are paired, the Cohen :math:`d_{avg}` is computed:

    .. math::

        d_{avg} = \\frac{\\overline{X} - \\overline{Y}}
        {0.5 * (\\sigma_1 + \\sigma_2)}

    The Cohen’s d is a biased estimate of the population effect size,
    especially for small samples (n < 20). It is often preferable
    to use the corrected effect size, or Hedges’g, instead:

    .. math:: g = d * (1 - \\frac{3}{4(n_1 + n_2) - 9})

    If eftype = 'glass', the Glass :math:`\\delta` is reported, using the
    group with the lowest variance as the control group:

    .. math::

        \\delta = \\frac{\\overline{X} - \\overline{Y}}{\\sigma_{control}}

    References
    ----------
    .. [1] Lakens, D., 2013. Calculating and reporting effect sizes to
       facilitate cumulative science: a practical primer for t-tests and
       ANOVAs. Front. Psychol. 4, 863. https://doi.org/10.3389/fpsyg.2013.00863

    .. [2] Cumming, Geoff. Understanding the new statistics: Effect sizes,
           confidence intervals, and meta-analysis. Routledge, 2013.

    Examples
    --------
    1. Compute Cohen d from two independent set of observations.

    >>> import numpy as np
    >>> from pingouin import compute_effsize
    >>> np.random.seed(123)
    >>> x = np.random.normal(2, size=100)
    >>> y = np.random.normal(2.3, size=95)
    >>> d = compute_effsize(x=x, y=y, eftype='cohen', paired=False)
    >>> print(d)
    -0.2835170152506578

    2. Compute Hedges g from two paired set of observations.

    >>> import numpy as np
    >>> from pingouin import compute_effsize
    >>> x = [1.62, 2.21, 3.79, 1.66, 1.86, 1.87, 4.51, 4.49, 3.3 , 2.69]
    >>> y = [0.91, 3., 2.28, 0.49, 1.42, 3.65, -0.43, 1.57, 3.27, 1.13]
    >>> g = compute_effsize(x=x, y=y, eftype='hedges', paired=True)
    >>> print(g)
    0.8370985097811404

    3. Compute Glass delta from two independent set of observations. The group
       with the lowest variance will automatically be selected as the control.

    >>> import numpy as np
    >>> from pingouin import compute_effsize
    >>> np.random.seed(123)
    >>> x = np.random.normal(2, scale=1, size=50)
    >>> y = np.random.normal(2, scale=2, size=45)
    >>> d = compute_effsize(x=x, y=y, eftype='glass')
    >>> print(d)
    -0.1170721973604153
    """
    # Check arguments
    if not _check_eftype(eftype):
        err = "Could not interpret input '{}'".format(eftype)
        raise ValueError(err)

    x = np.asarray(x)
    y = np.asarray(y)

    if x.size != y.size and paired:
        warnings.warn("x and y have unequal sizes. Switching to "
                      "paired == False.")
        paired = False

    # Remove rows with missing values
    x, y = remove_na(x, y, paired=paired)
    nx, ny = x.size, y.size

    if ny == 1:
        # Case 1: One-sample Test
        d = (x.mean() - y) / x.std(ddof=1)
        return d
    if eftype.lower() == 'glass':
        # Find group with lowest variance
        sd_control = np.min([x.std(ddof=1), y.std(ddof=1)])
        d = (x.mean() - y.mean()) / sd_control
        return d
    elif eftype.lower() == 'r':
        # Return correlation coefficient (useful for CI bootstrapping)
        from scipy.stats import pearsonr
        r, _ = pearsonr(x, y)
        return r
    elif eftype.lower() == 'cles':
        # Compute exact CLES
        diff = x[:, None] - y
        return max((diff < 0).sum(), (diff > 0).sum()) / diff.size
    else:
        # Test equality of variance of data with a stringent threshold
        # equal_var, p = homoscedasticity(x, y, alpha=.001)
        # if not equal_var:
        #     print('Unequal variances (p<.001). You should report',
        #           'Glass delta instead.')

        # Compute unbiased Cohen's d effect size
        if not paired:
            # https://en.wikipedia.org/wiki/Effect_size
            dof = nx + ny - 2
            poolsd = np.sqrt(((nx - 1) * x.var(ddof=1)
                              + (ny - 1) * y.var(ddof=1)) / dof)
            d = (x.mean() - y.mean()) / poolsd
        else:
            # Report Cohen d-avg (Cumming 2012; Lakens 2013)
            d = (x.mean() - y.mean()) / (.5 * (x.std(ddof=1)
                                               + y.std(ddof=1)))
        return convert_effsize(d, 'cohen', eftype, nx=nx, ny=ny)
def compute_effsize ( x , y , paired = False , eftype = 'cohen' ) : if not _check_eftype ( eftype ) : err = "Could not interpret input '{}'" . format ( eftype ) raise ValueError ( err ) x = np . asarray ( x ) y = np . asarray ( y ) if x . size != y . size and paired : warnings . warn ( "x and y have unequal sizes. Switching to " "paired == False." ) paired = False x , y = remove_na ( x , y , paired = paired ) nx , ny = x . size , y . size if ny == 1 : d = ( x . mean ( ) - y ) / x . std ( ddof = 1 ) return d if eftype . lower ( ) == 'glass' : sd_control = np . min ( [ x . std ( ddof = 1 ) , y . std ( ddof = 1 ) ] ) d = ( x . mean ( ) - y . mean ( ) ) / sd_control return d elif eftype . lower ( ) == 'r' : from scipy . stats import pearsonr r , _ = pearsonr ( x , y ) return r elif eftype . lower ( ) == 'cles' : diff = x [ : , None ] - y return max ( ( diff < 0 ) . sum ( ) , ( diff > 0 ) . sum ( ) ) / diff . size else : if not paired : dof = nx + ny - 2 poolsd = np . sqrt ( ( ( nx - 1 ) * x . var ( ddof = 1 ) + ( ny - 1 ) * y . var ( ddof = 1 ) ) / dof ) d = ( x . mean ( ) - y . mean ( ) ) / poolsd else : d = ( x . mean ( ) - y . mean ( ) ) / ( .5 * ( x . std ( ddof = 1 ) + y . std ( ddof = 1 ) ) ) return convert_effsize ( d , 'cohen' , eftype , nx = nx , ny = ny )
Calculate effect size between two set of observations.

    Parameters
    ----------
    x : np.array or list
        First set of observations.
    y : np.array or list
        Second set of observations.
    paired : boolean
        If True, uses Cohen d-avg formula to correct for repeated measurements
        (Cumming 2012)
    eftype : string
        Desired output effect size.
        Available methods are ::

        'none' : no effect size
        'cohen' : Unbiased Cohen d
        'hedges' : Hedges g
        'glass': Glass delta
        'r' : correlation coefficient
        'eta-square' : Eta-square
        'odds-ratio' : Odds ratio
        'AUC' : Area Under the Curve
        'CLES' : Common language effect size

    Returns
    -------
    ef : float
        Effect size

    See Also
    --------
    convert_effsize : Conversion between effect sizes.
    compute_effsize_from_t : Convert a T-statistic to an effect size.

    Notes
    -----
    Missing values are automatically removed from the data. If ``x`` and ``y``
    are paired, the entire row is removed.

    If ``x`` and ``y`` are independent, the Cohen's d is:

    .. math::

        d = \\frac{\\overline{X} - \\overline{Y}}
        {\\sqrt{\\frac{(n_{1} - 1)\\sigma_{1}^{2} + (n_{2} - 1)
        \\sigma_{2}^{2}}{n1 + n2 - 2}}}

    If ``x`` and ``y`` are paired, the Cohen :math:`d_{avg}` is computed:

    .. math::

        d_{avg} = \\frac{\\overline{X} - \\overline{Y}}
        {0.5 * (\\sigma_1 + \\sigma_2)}

    The Cohen’s d is a biased estimate of the population effect size,
    especially for small samples (n < 20). It is often preferable
    to use the corrected effect size, or Hedges’g, instead:

    .. math:: g = d * (1 - \\frac{3}{4(n_1 + n_2) - 9})

    If eftype = 'glass', the Glass :math:`\\delta` is reported, using the
    group with the lowest variance as the control group:

    .. math::

        \\delta = \\frac{\\overline{X} - \\overline{Y}}{\\sigma_{control}}

    References
    ----------
    .. [1] Lakens, D., 2013. Calculating and reporting effect sizes to
       facilitate cumulative science: a practical primer for t-tests and
       ANOVAs. Front. Psychol. 4, 863. https://doi.org/10.3389/fpsyg.2013.00863

    .. [2] Cumming, Geoff. Understanding the new statistics: Effect sizes,
           confidence intervals, and meta-analysis. Routledge, 2013.

    Examples
    --------
    1. Compute Cohen d from two independent set of observations.

    >>> import numpy as np
    >>> from pingouin import compute_effsize
    >>> np.random.seed(123)
    >>> x = np.random.normal(2, size=100)
    >>> y = np.random.normal(2.3, size=95)
    >>> d = compute_effsize(x=x, y=y, eftype='cohen', paired=False)
    >>> print(d)
    -0.2835170152506578

    2. Compute Hedges g from two paired set of observations.

    >>> import numpy as np
    >>> from pingouin import compute_effsize
    >>> x = [1.62, 2.21, 3.79, 1.66, 1.86, 1.87, 4.51, 4.49, 3.3 , 2.69]
    >>> y = [0.91, 3., 2.28, 0.49, 1.42, 3.65, -0.43, 1.57, 3.27, 1.13]
    >>> g = compute_effsize(x=x, y=y, eftype='hedges', paired=True)
    >>> print(g)
    0.8370985097811404

    3. Compute Glass delta from two independent set of observations. The group
       with the lowest variance will automatically be selected as the control.

    >>> import numpy as np
    >>> from pingouin import compute_effsize
    >>> np.random.seed(123)
    >>> x = np.random.normal(2, scale=1, size=50)
    >>> y = np.random.normal(2, scale=2, size=45)
    >>> d = compute_effsize(x=x, y=y, eftype='glass')
    >>> print(d)
    -0.1170721973604153