<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def sphericity(data, method='mauchly', alpha=.05):
 """Mauchly and JNS test for sphericity. Parameters data : pd.DataFrame DataFrame containing the repeated measurements. ``data`` must be in wide-format. To convert from wide to long format, use the :py:func:`pandas.pivot_table` function. method : str Method to compute sphericity :: 'jns' : John, Nagao and Sugiura test. 'mauchly' : Mauchly test. alpha : float Significance level Returns ------- spher : boolean True if data have the sphericity property. W : float Test statistic chi_sq : float Chi-square statistic ddof : int Degrees of freedom p : float P-value. See Also -------- homoscedasticity : Test equality of variance. normality : Test the univariate normality of one or more array(s). Notes ----- The **Mauchly** :math:`W` statistic is defined by: .. math:: W = \\frac{\\prod_{j=1}^{r-1} \\lambda_j}{(\\frac{1}{r-1} \\cdot \\sum_{j=1}^{^{r-1}} \\lambda_j)^{r-1}} where :math:`\\lambda_j` are the eigenvalues of the population covariance matrix (= double-centered sample covariance matrix) and :math:`r` is the number of conditions. From then, the :math:`W` statistic is transformed into a chi-square score using the number of observations per condition :math:`n` .. math:: f = \\frac{2(r-1)^2+r+1}{6(r-1)(n-1)} .. math:: \\chi_w^2 = (f-1)(n-1) log(W) The p-value is then approximated using a chi-square distribution: .. math:: \\chi_w^2 \\sim \\chi^2(\\frac{r(r-1)}{2}-1) The **JNS** :math:`V` statistic is defined by: .. math:: V = \\frac{(\\sum_j^{r-1} \\lambda_j)^2}{\\sum_j^{r-1} \\lambda_j^2} .. math:: \\chi_v^2 = \\frac{n}{2} (r-1)^2 (V - \\frac{1}{r-1}) and the p-value approximated using a chi-square distribution .. math:: \\chi_v^2 \\sim \\chi^2(\\frac{r(r-1)}{2}-1) References .. [1] Mauchly, J. W. (1940). Significance test for sphericity of a normal n-variate distribution. The Annals of Mathematical Statistics, 11(2), 204-209. .. [2] Nagao, H. (1973). On some test criteria for covariance matrix. The Annals of Statistics, 700-709. .. [3] Sugiura, N. (1972). Locally best invariant test for sphericity and the limiting distributions. The Annals of Mathematical Statistics, 1312-1316. .. [4] John, S. (1972). The distribution of a statistic used for testing sphericity of normal distributions. Biometrika, 59(1), 169-173. .. [5] http://www.real-statistics.com/anova-repeated-measures/sphericity/ Examples -------- 1. Mauchly test for sphericity (True, 0.21, 4.677, 2, 0.09649016283209666) 2. JNS test for sphericity (False, 1.118, 6.176, 2, 0.04560424030751982) """

    from scipy.stats import chi2
    S = data.cov().values
    n = data.shape[0]
    p = data.shape[1]
    d = p - 1

    # Estimate of the population covariance (= double-centered)
    S_pop = S - S.mean(0)[:, np.newaxis] - S.mean(1)[np.newaxis, :] + S.mean()

    # p - 1 eigenvalues (sorted by ascending importance)
    eig = np.linalg.eigvalsh(S_pop)[1:]

    if method == 'jns':
        # eps = epsilon(data, correction='gg')
        # W = eps * d
        W = eig.sum()**2 / np.square(eig).sum()
        chi_sq = 0.5 * n * d ** 2 * (W - 1 / d)

    if method == 'mauchly':
        # Mauchly's statistic
        W = np.product(eig) / (eig.sum() / d)**d
        # Chi-square
        f = (2 * d**2 + p + 1) / (6 * d * (n - 1))
        chi_sq = (f - 1) * (n - 1) * np.log(W)

    # Compute dof and pval
    ddof = 0.5 * d * p - 1
    # Ensure that dof is not zero
    ddof = 1 if ddof == 0 else ddof
    pval = chi2.sf(chi_sq, ddof)

    # Second order approximation
    # pval2 = chi2.sf(chi_sq, ddof + 4)
    # w2 = (d + 2) * (d - 1) * (d - 2) * (2 * d**3 + 6 * d * d + 3 * d + 2) / \
    #      (288 * d * d * nr * nr * dd * dd)
    # pval += w2 * (pval2 - pval)

    sphericity = True if pval > alpha else False
    return sphericity, np.round(W, 3), np.round(chi_sq, 3), int(ddof), pval