def compute_esci(stat=None, nx=None, ny=None, paired=False, eftype='cohen',
                 confidence=.95, decimals=2):
    """Parametric confidence intervals around a Cohen d or a
    correlation coefficient.

    Parameters
    ----------
    stat : float
        Original effect size. Must be either a correlation coefficient or a
        Cohen-type effect size (Cohen d or Hedges g).
    nx, ny : int
        Length of vector x and y.
    paired : bool
        Indicates if the effect size was estimated from a paired sample.
        This is only relevant for cohen or hedges effect size.
    eftype : string
        Effect size type. Must be 'r' (correlation) or 'cohen'
        (Cohen d or Hedges g).
    confidence : float
        Confidence level (0.95 = 95%)
    decimals : int
        Number of rounded decimals.

    Returns
    -------
    ci : array
        Desired converted effect size

    Notes
    -----
    To compute the parametric confidence interval around a
    **Pearson r correlation** coefficient, one must first apply a
    Fisher's r-to-z transformation:

    .. math:: z = 0.5 \\cdot \\ln \\frac{1 + r}{1 - r} = \\text{arctanh}(r)

    and compute the standard deviation:

    .. math:: se = \\frac{1}{\\sqrt{n - 3}}

    where :math:`n` is the sample size.

    The lower and upper confidence intervals - *in z-space* - are then
    given by:

    .. math:: ci_z = z \\pm crit \\cdot se

    where :math:`crit` is the critical value of the nomal distribution
    corresponding to the desired confidence level (e.g. 1.96 in case of a 95%
    confidence interval).

    These confidence intervals can then be easily converted back to *r-space*:

    .. math::

        ci_r = \\frac{\\exp(2 \\cdot ci_z) - 1}{\\exp(2 \\cdot ci_z) + 1} =
        \\text{tanh}(ci_z)

    A formula for calculating the confidence interval for a
    **Cohen d effect size** is given by Hedges and Olkin (1985, p86).
    If the effect size estimate from the sample is :math:`d`, then it is
    normally distributed, with standard deviation:

    .. math::

        se = \\sqrt{\\frac{n_x + n_y}{n_x \\cdot n_y} +
        \\frac{d^2}{2 (n_x + n_y)}}

    where :math:`n_x` and :math:`n_y` are the sample sizes of the two groups.

    In one-sample test or paired test, this becomes:

    .. math::

        se = \\sqrt{\\frac{1}{n_x} + \\frac{d^2}{2 \\cdot n_x}}

    The lower and upper confidence intervals are then given by:

    .. math:: ci_d = d \\pm crit \\cdot se

    where :math:`crit` is the critical value of the nomal distribution
    corresponding to the desired confidence level (e.g. 1.96 in case of a 95%
    confidence interval).

    References
    ----------
    .. [1] https://en.wikipedia.org/wiki/Fisher_transformation

    .. [2] Hedges, L., and Ingram Olkin. "Statistical models for
           meta-analysis." (1985).

    .. [3] http://www.leeds.ac.uk/educol/documents/00002182.htm

    Examples
    --------
    1. Confidence interval of a Pearson correlation coefficient

    >>> import pingouin as pg
    >>> x = [3, 4, 6, 7, 5, 6, 7, 3, 5, 4, 2]
    >>> y = [4, 6, 6, 7, 6, 5, 5, 2, 3, 4, 1]
    >>> nx, ny = len(x), len(y)
    >>> stat = np.corrcoef(x, y)[0][1]
    >>> ci = pg.compute_esci(stat=stat, nx=nx, ny=ny, eftype='r')
    >>> print(stat, ci)
    0.7468280049029223 [0.27 0.93]

    2. Confidence interval of a Cohen d

    >>> import pingouin as pg
    >>> x = [3, 4, 6, 7, 5, 6, 7, 3, 5, 4, 2]
    >>> y = [4, 6, 6, 7, 6, 5, 5, 2, 3, 4, 1]
    >>> nx, ny = len(x), len(y)
    >>> stat = pg.compute_effsize(x, y, eftype='cohen')
    >>> ci = pg.compute_esci(stat=stat, nx=nx, ny=ny, eftype='cohen')
    >>> print(stat, ci)
    0.1537753990658328 [-0.68  0.99]
    """
    from scipy.stats import norm
    # Safety check
    assert eftype.lower() in['r', 'pearson', 'spearman', 'cohen',
                             'd', 'g', 'hedges']
    assert stat is not None and nx is not None
    assert isinstance(confidence, float)
    assert 0 < confidence < 1

    # Note that we are using a normal dist and not a T dist:
    # from scipy.stats import t
    # crit = np.abs(t.ppf((1 - confidence) / 2), dof)
    crit = np.abs(norm.ppf((1 - confidence) / 2))

    if eftype.lower() in ['r', 'pearson', 'spearman']:
        # Standardize correlation coefficient
        z = np.arctanh(stat)
        se = 1 / np.sqrt(nx - 3)
        ci_z = np.array([z - crit * se, z + crit * se])
        # Transform back to r
        ci = np.tanh(ci_z)
    else:
        if ny == 1 or paired:
            # One sample or paired
            se = np.sqrt(1 / nx + stat**2 / (2 * nx))
        else:
            # Two-sample test
            se = np.sqrt(((nx + ny) / (nx * ny)) + (stat**2) / (2 * (nx + ny)))
        ci = np.array([stat - crit * se, stat + crit * se])
    return np.round(ci, decimals)
Parametric confidence intervals around a Cohen d or a
    correlation coefficient.

    Parameters
    ----------
    stat : float
        Original effect size. Must be either a correlation coefficient or a
        Cohen-type effect size (Cohen d or Hedges g).
    nx, ny : int
        Length of vector x and y.
    paired : bool
        Indicates if the effect size was estimated from a paired sample.
        This is only relevant for cohen or hedges effect size.
    eftype : string
        Effect size type. Must be 'r' (correlation) or 'cohen'
        (Cohen d or Hedges g).
    confidence : float
        Confidence level (0.95 = 95%)
    decimals : int
        Number of rounded decimals.

    Returns
    -------
    ci : array
        Desired converted effect size

    Notes
    -----
    To compute the parametric confidence interval around a
    **Pearson r correlation** coefficient, one must first apply a
    Fisher's r-to-z transformation:

    .. math:: z = 0.5 \\cdot \\ln \\frac{1 + r}{1 - r} = \\text{arctanh}(r)

    and compute the standard deviation:

    .. math:: se = \\frac{1}{\\sqrt{n - 3}}

    where :math:`n` is the sample size.

    The lower and upper confidence intervals - *in z-space* - are then
    given by:

    .. math:: ci_z = z \\pm crit \\cdot se

    where :math:`crit` is the critical value of the nomal distribution
    corresponding to the desired confidence level (e.g. 1.96 in case of a 95%
    confidence interval).

    These confidence intervals can then be easily converted back to *r-space*:

    .. math::

        ci_r = \\frac{\\exp(2 \\cdot ci_z) - 1}{\\exp(2 \\cdot ci_z) + 1} =
        \\text{tanh}(ci_z)

    A formula for calculating the confidence interval for a
    **Cohen d effect size** is given by Hedges and Olkin (1985, p86).
    If the effect size estimate from the sample is :math:`d`, then it is
    normally distributed, with standard deviation:

    .. math::

        se = \\sqrt{\\frac{n_x + n_y}{n_x \\cdot n_y} +
        \\frac{d^2}{2 (n_x + n_y)}}

    where :math:`n_x` and :math:`n_y` are the sample sizes of the two groups.

    In one-sample test or paired test, this becomes:

    .. math::

        se = \\sqrt{\\frac{1}{n_x} + \\frac{d^2}{2 \\cdot n_x}}

    The lower and upper confidence intervals are then given by:

    .. math:: ci_d = d \\pm crit \\cdot se

    where :math:`crit` is the critical value of the nomal distribution
    corresponding to the desired confidence level (e.g. 1.96 in case of a 95%
    confidence interval).

    References
    ----------
    .. [1] https://en.wikipedia.org/wiki/Fisher_transformation

    .. [2] Hedges, L., and Ingram Olkin. "Statistical models for
           meta-analysis." (1985).

    .. [3] http://www.leeds.ac.uk/educol/documents/00002182.htm

    Examples
    --------
    1. Confidence interval of a Pearson correlation coefficient

    >>> import pingouin as pg
    >>> x = [3, 4, 6, 7, 5, 6, 7, 3, 5, 4, 2]
    >>> y = [4, 6, 6, 7, 6, 5, 5, 2, 3, 4, 1]
    >>> nx, ny = len(x), len(y)
    >>> stat = np.corrcoef(x, y)[0][1]
    >>> ci = pg.compute_esci(stat=stat, nx=nx, ny=ny, eftype='r')
    >>> print(stat, ci)
    0.7468280049029223 [0.27 0.93]

    2. Confidence interval of a Cohen d

    >>> import pingouin as pg
    >>> x = [3, 4, 6, 7, 5, 6, 7, 3, 5, 4, 2]
    >>> y = [4, 6, 6, 7, 6, 5, 5, 2, 3, 4, 1]
    >>> nx, ny = len(x), len(y)
    >>> stat = pg.compute_effsize(x, y, eftype='cohen')
    >>> ci = pg.compute_esci(stat=stat, nx=nx, ny=ny, eftype='cohen')
    >>> print(stat, ci)
    0.1537753990658328 [-0.68  0.99]