jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def __init__(self, l=0, fused: bool = False, **kwargs):
        r"""Initialize layer with constant l.

        Args:
            l (int): Positive integer for :math:`l` in :math:`Y_l(\cos\theta)`.
            fused (bool): Whether to compute polynomial in a fused tensor representation.
        """
        super(SphericalHarmonicsYl, self).__init__(**kwargs)
        self.l = l
        self.fused = fused
        self._pre_factors = [
            float((-1) ** k * sp.special.factorial(2 * l - 2 * k) / sp.special.factorial(l - k) / sp.special.factorial(
                l - 2 * k) / sp.special.factorial(k) / 2 ** l) for k in range(0, int(np.floor(l / 2)) + 1)]
        self._powers = [float(l - 2 * k) for k in range(0, int(np.floor(l / 2)) + 1)]
        self._scale = float(np.sqrt((2 * l + 1) / 4 / np.pi))
        if self.fused:
            # Or maybe also as weight.
            self._powers = ops.convert_to_tensor(self._powers, dtype=self.dtype)
            self._pre_factors = ops.convert_to_tensor(self._pre_factors, dtype=self.dtype)
//  [python]
def tf_spherical_harmonics_yl(theta, l=0):
    r"""Compute the spherical harmonics :math:`Y_{ml}(\cos\theta)` for :math:`m=0` and constant non-integer :math:`l`.
    TensorFlow has to cache the function for each :math:`l`. No gradient through :math:`l` or very large number
    of :math:`n` is possible. Uses a simplified formula with :math:`m=0` from
    https://en.wikipedia.org/wiki/Spherical_harmonics:

    :math:`Y_{l}^{m}(\theta ,\phi)=\sqrt{\frac{(2l+1)}{4\pi} \frac{(l -m)!}{(l +m)!}} \, P_{l}^{m}(\cos{\theta }) \,
    e^{i m \phi}`

    where the associated Legendre polynomial simplifies to :math:`P_l(x)` for :math:`m=0`:

    :math:`P_n(x)=\sum_{k=0}^{\lfloor n/2\rfloor} (-1)^k \frac{(2n - 2k)! \, }{(n-k)! \, (n-2k)! \, k! \, 2^n} x^{n-2k}`

    Args:
        theta (Tensor): Values to compute :math:`Y_l(\cos\theta)` for.
        l (int): Positive integer for :math:`l` in :math:`Y_l(\cos\theta)`.

    Returns:
        Tensor: Spherical harmonics for :math:`m=0` and constant non-integer :math:`l`.
    """
    x = ops.cos(theta)
    out_sum = ops.zeros_like(x)
    prefactors = [
        float((-1) ** k * s.factorial(2 * l - 2 * k) / s.factorial(l - k) / s.factorial(
            l - 2 * k) / s.factorial(k) / 2 ** l) for k in range(0, int(np.floor(l / 2)) + 1)]
    powers = [float(l - 2 * k) for k in range(0, int(np.floor(l / 2)) + 1)]
    for i in range(len(powers)):
        out_sum = out_sum + prefactors[i] * ops.power(x, powers[i])
    out_sum = out_sum * float(np.sqrt((2 * l + 1) / 4 / np.pi))
    return out_sum
//  [python]
def tf_spherical_harmonics_yl(theta, l=0):
    r"""Compute the spherical harmonics :math:`Y_{ml}(\cos\theta)` for :math:`m=0` and constant non-integer :math:`l`.
    TensorFlow has to cache the function for each :math:`l`. No gradient through :math:`l` or very large number
    of :math:`n` is possible. Uses a simplified formula with :math:`m=0` from
    https://en.wikipedia.org/wiki/Spherical_harmonics:

    :math:`Y_{l}^{m}(\theta ,\phi)=\sqrt{\frac{(2l+1)}{4\pi} \frac{(l -m)!}{(l +m)!}} \, P_{l}^{m}(\cos{\theta }) \,
    e^{i m \phi}`

    where the associated Legendre polynomial simplifies to :math:`P_l(x)` for :math:`m=0`:

    :math:`P_n(x)=\sum_{k=0}^{\lfloor n/2\rfloor} (-1)^k \frac{(2n - 2k)! \, }{(n-k)! \, (n-2k)! \, k! \, 2^n} x^{n-2k}`

    Args:
        theta (Tensor): Values to compute :math:`Y_l(\cos\theta)` for.
        l (int): Positive integer for :math:`l` in :math:`Y_l(\cos\theta)`.

    Returns:
        Tensor: Spherical harmonics for :math:`m=0` and constant non-integer :math:`l`.
    """
    x = ops.cos(theta)
    out_sum = ops.zeros_like(x)
    prefactors = [
        float((-1) ** k * sp.special.factorial(2 * l - 2 * k) / sp.special.factorial(l - k) / sp.special.factorial(
            l - 2 * k) / sp.special.factorial(k) / 2 ** l) for k in range(0, int(np.floor(l / 2)) + 1)]
    powers = [float(l - 2 * k) for k in range(0, int(np.floor(l / 2)) + 1)]
    for i in range(len(powers)):
        out_sum = out_sum + prefactors[i] * ops.power(x, powers[i])
    out_sum = out_sum * float(np.sqrt((2 * l + 1) / 4 / np.pi))
    return out_sum
//  [python]
def tf_associated_legendre_polynomial(x, l=0, m=0):
    r"""Compute the associated Legendre polynomial :math:`P_{l}^{m}(x)` for :math:`m` and constant positive
    integer :math:`l` via explicit formula.
    Closed Form from taken from https://en.wikipedia.org/wiki/Associated_Legendre_polynomials.

    :math:`P_{l}^{m}(x)=(-1)^{m}\cdot 2^{l}\cdot (1-x^{2})^{m/2}\cdot \sum_{k=m}^{l}\frac{k!}{(k-m)!}\cdot x^{k-m}
    \cdot \binom{l}{k}\binom{\frac{l+k-1}{2}}{l}`.

    Args:
        x (Tensor): Values to compute :math:`P_{l}^{m}(x)` for.
        l (int): Positive integer for :math:`l` in :math:`P_{l}^{m}(x)`.
        m (int): Positive/Negative integer for :math:`m` in :math:`P_{l}^{m}(x)`.

    Returns:
        Tensor: Legendre Polynomial of order n.
    """
    if np.abs(m) > l:
        raise ValueError("Error: Legendre polynomial must have -l<= m <= l")
    if l < 0:
        raise ValueError("Error: Legendre polynomial must have l>=0")
    if m < 0:
        m = -m
        neg_m = float(np.power(-1, m) * s.factorial(l - m) / s.factorial(l + m))
    else:
        neg_m = 1

    x_prefactor = ops.power(1 - ops.square(x), m / 2) * float(np.power(-1, m) * np.power(2, l))
    sum_out = ops.zeros_like(x)
    for k in range(m, l + 1):
        sum_out += ops.power(x, k - m) * float(
            s.factorial(k) / s.factorial(k - m) * s.binom(l, k) *
            s.binom((l + k - 1) / 2, l))

    return sum_out * x_prefactor * neg_m
//  [python]
def tf_associated_legendre_polynomial(x, l=0, m=0):
    r"""Compute the associated Legendre polynomial :math:`P_{l}^{m}(x)` for :math:`m` and constant positive
    integer :math:`l` via explicit formula.
    Closed Form from taken from https://en.wikipedia.org/wiki/Associated_Legendre_polynomials.

    :math:`P_{l}^{m}(x)=(-1)^{m}\cdot 2^{l}\cdot (1-x^{2})^{m/2}\cdot \sum_{k=m}^{l}\frac{k!}{(k-m)!}\cdot x^{k-m}
    \cdot \binom{l}{k}\binom{\frac{l+k-1}{2}}{l}`.

    Args:
        x (Tensor): Values to compute :math:`P_{l}^{m}(x)` for.
        l (int): Positive integer for :math:`l` in :math:`P_{l}^{m}(x)`.
        m (int): Positive/Negative integer for :math:`m` in :math:`P_{l}^{m}(x)`.

    Returns:
        Tensor: Legendre Polynomial of order n.
    """
    if np.abs(m) > l:
        raise ValueError("Error: Legendre polynomial must have -l<= m <= l")
    if l < 0:
        raise ValueError("Error: Legendre polynomial must have l>=0")
    if m < 0:
        m = -m
        neg_m = float(np.power(-1, m) * sp.special.factorial(l - m) / sp.special.factorial(l + m))
    else:
        neg_m = 1

    x_prefactor = ops.power(1 - ops.square(x), m / 2) * float(np.power(-1, m) * np.power(2, l))
    sum_out = ops.zeros_like(x)
    for k in range(m, l + 1):
        sum_out += ops.power(x, k - m) * float(
            sp.special.factorial(k) / sp.special.factorial(k - m) * sp.special.binom(l, k) *
            sp.special.binom((l + k - 1) / 2, l))

    return sum_out * x_prefactor * neg_m
//  [python]
def R(r, n=1, l=0):
  coeff = np.sqrt(
    (2.0 / n)**3 * s.factorial(n - l - 1) / (2.0 * n * s.factorial(n + l)))
  laguerre = spe.assoc_laguerre(2.0 * r / n, n - l - 1, 2 * l + 1)
  return coeff * np.exp(-r / n) * (2.0 * r / n)**l * laguerre
//  [python]
def R(r, n=1, l=0):
  coeff = np.sqrt(
    (2.0 / n)**3 * spe.factorial(n - l - 1) / (2.0 * n * spe.factorial(n + l)))
  laguerre = spe.assoc_laguerre(2.0 * r / n, n - l - 1, 2 * l + 1)
  return coeff * np.exp(-r / n) * (2.0 * r / n)**l * laguerre
//  [python]
def test_factorial():
    n = np.random.randint(0, 170)
    assert roundScalar(NumCpp.factorial_Scalar(n), NUM_DECIMALS_ROUND) == roundScalar(
        s.factorial(n).item(), NUM_DECIMALS_ROUND
    )

    shapeInput = np.random.randint(
        20,
        100,
        [
            2,
        ],
    )
    shape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item())
    cArray = NumCpp.NdArrayUInt32(shape)
    data = np.random.randint(0, 170, [shape.rows, shape.cols], dtype=np.uint32)
    cArray.setArray(data)
    assert np.array_equal(
        roundArray(NumCpp.factorial_Array(cArray), NUM_DECIMALS_ROUND),
        roundArray(s.factorial(data), NUM_DECIMALS_ROUND),
    )
//  [python]
def test_factorial():
    n = np.random.randint(0, 170)
    assert roundScalar(NumCpp.factorial_Scalar(n), NUM_DECIMALS_ROUND) == roundScalar(
        sp.factorial(n).item(), NUM_DECIMALS_ROUND
    )

    shapeInput = np.random.randint(
        20,
        100,
        [
            2,
        ],
    )
    shape = NumCpp.Shape(shapeInput[0].item(), shapeInput[1].item())
    cArray = NumCpp.NdArrayUInt32(shape)
    data = np.random.randint(0, 170, [shape.rows, shape.cols], dtype=np.uint32)
    cArray.setArray(data)
    assert np.array_equal(
        roundArray(NumCpp.factorial_Array(cArray), NUM_DECIMALS_ROUND),
        roundArray(sp.factorial(data), NUM_DECIMALS_ROUND),
    )
//  [python]
def get_num_coeffs(order, dim):
    """
    Calculate the number of gPC coefficients by the maximum order and the number of random variables.

    num_coeffs = (order+dim)! / (order! * dim!)

    num_coeffs = get_num_coeffs(order , dim)

    Parameters
    ----------
    order: int
        Maximum order of expansion
    dim: int
        Number of random variables

    Returns
    -------
    num_coeffs: int
        Number of gPC coefficients and polynomials
    """

    return s.factorial(order + dim) / (s.factorial(order) * s.factorial(dim))
//  [python]
def get_num_coeffs(order, dim):
    """
    Calculate the number of gPC coefficients by the maximum order and the number of random variables.

    num_coeffs = (order+dim)! / (order! * dim!)

    num_coeffs = get_num_coeffs(order , dim)

    Parameters
    ----------
    order: int
        Maximum order of expansion
    dim: int
        Number of random variables

    Returns
    -------
    num_coeffs: int
        Number of gPC coefficients and polynomials
    """

    return scipy.special.factorial(order + dim) / (scipy.special.factorial(order) * scipy.special.factorial(dim))
//  [python]
def check_sample_var(sv,n, popvar):
    # two-sided chisquare test for sample variance equal to hypothesized variance
    df = n-1
    chi2 = (n-1)*popvar/float(popvar)
    pval = stats.chisqprob(chi2,df)*2
    npt.assert_(pval > 0.01, 'var fail, t, pval = %f, %f, v, sv=%f, %f' %
            (chi2,pval,popvar,sv))
//  [python]
def check_sample_var(sv,n, popvar):
    # two-sided chisquare test for sample variance equal to hypothesized variance
    df = n-1
    chi2 = (n-1)*sv/float(popvar)
    pval = stats.chi2.sf(chi2,df)*2
    npt.assert_(pval > 0.01, 'var fail, t, pval = %f, %f, v, sv=%f, %f' %
            (chi2,pval,popvar,sv))
//  [python]
def __init__(self, l: int = 0, m: int = 0, fused: bool = False, **kwargs):
        r"""Initialize layer with constant m, l.

        Args:
            l (int): Positive integer for :math:`l` in :math:`P_{l}^{m}(x)`.
            m (int): Positive/Negative integer for :math:`m` in :math:`P_{l}^{m}(x)`.
            fused (bool): Whether to compute polynomial in a fused tensor representation.
        """
        super(AssociatedLegendrePolynomialPlm, self).__init__(**kwargs)
        self.m = m
        self.fused = fused
        self.l = l
        if np.abs(m) > l:
            raise ValueError("Error: Legendre polynomial must have -l<= m <= l")
        if l < 0:
            raise ValueError("Error: Legendre polynomial must have l>=0")
        if m < 0:
            m = -m
            neg_m = float(np.power(-1, m) * s.factorial(l - m) / s.factorial(l + m))
        else:
            neg_m = 1
        self._m = m
        self._neg_m = neg_m
        self._x_pre_factor = float(np.power(-1, m) * np.power(2, l))
        self._powers = []
        self._pre_factors = []
        for k in range(m, l + 1):
            self._powers.append(k - m)
            fac = float(
                s.factorial(k) / s.factorial(k - m) * s.binom(l, k) *
                s.binom((l + k - 1) / 2, l))
            self._pre_factors.append(fac)

        if self.fused:
            # Or maybe also as weight.
            self._powers = ops.convert_to_tensor(self._powers, dtype=self.dtype)
            self._pre_factors = ops.convert_to_tensor(self._pre_factors, dtype=self.dtype)
//  [python]
def __init__(self, l: int = 0, m: int = 0, fused: bool = False, **kwargs):
        r"""Initialize layer with constant m, l.

        Args:
            l (int): Positive integer for :math:`l` in :math:`P_{l}^{m}(x)`.
            m (int): Positive/Negative integer for :math:`m` in :math:`P_{l}^{m}(x)`.
            fused (bool): Whether to compute polynomial in a fused tensor representation.
        """
        super(AssociatedLegendrePolynomialPlm, self).__init__(**kwargs)
        self.m = m
        self.fused = fused
        self.l = l
        if np.abs(m) > l:
            raise ValueError("Error: Legendre polynomial must have -l<= m <= l")
        if l < 0:
            raise ValueError("Error: Legendre polynomial must have l>=0")
        if m < 0:
            m = -m
            neg_m = float(np.power(-1, m) * sp.special.factorial(l - m) / sp.special.factorial(l + m))
        else:
            neg_m = 1
        self._m = m
        self._neg_m = neg_m
        self._x_pre_factor = float(np.power(-1, m) * np.power(2, l))
        self._powers = []
        self._pre_factors = []
        for k in range(m, l + 1):
            self._powers.append(k - m)
            fac = float(
                sp.special.factorial(k) / sp.special.factorial(k - m) * sp.special.binom(l, k) *
                sp.special.binom((l + k - 1) / 2, l))
            self._pre_factors.append(fac)

        if self.fused:
            # Or maybe also as weight.
            self._powers = ops.convert_to_tensor(self._powers, dtype=self.dtype)
            self._pre_factors = ops.convert_to_tensor(self._pre_factors, dtype=self.dtype)
//  [python]
def test_simple(self):
        x = [1]
        y = [2]
        h, p = stats.kruskal(x, y)
        assert_equal(h, 1.0)
        assert_approx_equal(p, stats.chisqprob(h, 1))
        h, p = stats.kruskal(np.array(x), np.array(y))
        assert_equal(h, 1.0)
        assert_approx_equal(p, stats.chisqprob(h, 1))
//  [python]
def test_simple(self):
        x = [1]
        y = [2]
        h, p = stats.kruskal(x, y)
        assert_equal(h, 1.0)
        assert_approx_equal(p, stats.chi2.sf(h, 1))
        h, p = stats.kruskal(np.array(x), np.array(y))
        assert_equal(h, 1.0)
        assert_approx_equal(p, stats.chi2.sf(h, 1))
//  [python]
def llr_pvalue(self):
        return stats.chisqprob(self.llr, self.df_model)
//  [python]
def llr_pvalue(self):
        return stats.chi2.sf(self.llr, self.df_model)
//  [python]
def test_basic(self):
        x = [1, 3, 5, 7, 9]
        y = [2, 4, 6, 8, 10]
        h, p = stats.kruskal(x, y)
        assert_approx_equal(h, 3./11, significant=10)
        assert_approx_equal(p, stats.chisqprob(3./11, 1))
        h, p = stats.kruskal(np.array(x), np.array(y))
        assert_approx_equal(h, 3./11, significant=10)
        assert_approx_equal(p, stats.chisqprob(3./11, 1))
//  [python]
def test_basic(self):
        x = [1, 3, 5, 7, 9]
        y = [2, 4, 6, 8, 10]
        h, p = stats.kruskal(x, y)
        assert_approx_equal(h, 3./11, significant=10)
        assert_approx_equal(p, stats.chi2.sf(3./11, 1))
        h, p = stats.kruskal(np.array(x), np.array(y))
        assert_approx_equal(h, 3./11, significant=10)
        assert_approx_equal(p, stats.chi2.sf(3./11, 1))
//  [python]
def check_power_divergence(self, f_obs, f_exp, ddof, axis, lambda_,
                               expected_stat):
        f_obs = np.asarray(f_obs)
        if axis is None:
            num_obs = f_obs.size
        else:
            b = np.broadcast(f_obs, f_exp)
            num_obs = b.shape[axis]
        stat, p = stats.power_divergence(f_obs=f_obs, f_exp=f_exp, ddof=ddof,
                                         axis=axis, lambda_=lambda_)
        assert_allclose(stat, expected_stat)

        if lambda_ == 1 or lambda_ == "pearson":
            # Also test stats.chisquare.
            stat, p = stats.chisquare(f_obs=f_obs, f_exp=f_exp, ddof=ddof,
                                      axis=axis)
            assert_allclose(stat, expected_stat)

        ddof = np.asarray(ddof)
        expected_p = stats.chisqprob(expected_stat, num_obs - 1 - ddof)
        assert_allclose(p, expected_p)
//  [python]
def check_power_divergence(self, f_obs, f_exp, ddof, axis, lambda_,
                               expected_stat):
        f_obs = np.asarray(f_obs)
        if axis is None:
            num_obs = f_obs.size
        else:
            b = np.broadcast(f_obs, f_exp)
            num_obs = b.shape[axis]
        stat, p = stats.power_divergence(f_obs=f_obs, f_exp=f_exp, ddof=ddof,
                                         axis=axis, lambda_=lambda_)
        assert_allclose(stat, expected_stat)

        if lambda_ == 1 or lambda_ == "pearson":
            # Also test stats.chisquare.
            stat, p = stats.chisquare(f_obs=f_obs, f_exp=f_exp, ddof=ddof,
                                      axis=axis)
            assert_allclose(stat, expected_stat)

        ddof = np.asarray(ddof)
        expected_p = stats.chi2.sf(expected_stat, num_obs - 1 - ddof)
        assert_allclose(p, expected_p)
//  [python]
def test_three_groups(self):
        # A test of stats.kruskal with three groups, with ties.
        x = [1, 1, 1]
        y = [2, 2, 2]
        z = [2, 2]
        h_uncorr = (12. / 8. / 9.) * (3*2**2 + 3*6**2 + 2*6**2) - 3 * 9  # 5.0
        corr = 1 - float(3**3 - 3 + 5**3 - 5) / (8**3 - 8)
        expected = h_uncorr / corr  # 7.0
        h, p = stats.kruskal(x, y, z)
        assert_approx_equal(h, expected)
        assert_approx_equal(p, stats.chisqprob(h, 2))
//  [python]
def test_three_groups(self):
        # A test of stats.kruskal with three groups, with ties.
        x = [1, 1, 1]
        y = [2, 2, 2]
        z = [2, 2]
        h_uncorr = (12. / 8. / 9.) * (3*2**2 + 3*6**2 + 2*6**2) - 3 * 9  # 5.0
        corr = 1 - float(3**3 - 3 + 5**3 - 5) / (8**3 - 8)
        expected = h_uncorr / corr  # 7.0
        h, p = stats.kruskal(x, y, z)
        assert_approx_equal(h, expected)
        assert_approx_equal(p, stats.chi2.sf(h, 2))
//  [python]
def check_sample_var(sv,n, popvar):
    '''
two-sided chisquare test for sample variance equal to hypothesized variance
    '''
    df = n-1
    chi2 = (n-1)*popvar/float(popvar)
    pval = stats.chisqprob(chi2,df)*2
    assert pval>0.01, 'var fail, t,pval = %f, %f, v,sv=%f,%f' % (chi2,pval,popvar,sv)
//  [python]
def check_sample_var(sv,n, popvar):
    '''
two-sided chisquare test for sample variance equal to hypothesized variance
    '''
    df = n-1
    chi2 = (n-1)*sv/float(popvar)
    pval = 2*stats.chi2.sf(chi2,df)
    assert pval>0.01, 'var fail, t,pval = %f, %f, v,sv=%f,%f' % (chi2,pval,popvar,sv)
//  [python]
def fill_p(sumstats,log,df=None,only_sig=False,sig_level=5e-8,overwrite=False,verbose=True,filled_count=0):
        # MLOG10P -> P
    if "MLOG10P" in sumstats.columns:    
        log.write("  - Filling P value using MLOG10P column...", verbose=verbose)
        sumstats["P"] = np.power(10,-sumstats["MLOG10P"])
        filled_count +=1

    # Z -> P
    elif "Z" in sumstats.columns:
        log.write("  - Filling P value using Z column...", verbose=verbose)
        stats.chisqprob = lambda chisq, degree_of_freedom: stats.chi2.sf(chisq, degree_of_freedom)
        sumstats["P"] = ss.chisqprob(sumstats["Z"]**2,1)
        filled_count +=1

    elif "CHISQ" in sumstats.columns:
    #CHISQ -> P
        log.write("  - Filling P value using CHISQ column...", verbose=verbose)
        stats.chisqprob = lambda chisq, degree_of_freedom: stats.chi2.sf(chisq, degree_of_freedom)
        if df is None:
            if only_sig is True and overwrite is True:
                sumstats.loc[sumstats["P"]<sig_level,"P"] = stats.chisqprob(sumstats.loc[sumstats["P"]<sig_level,"CHISQ"],1)
                filled_count +=1
            else:
                sumstats["P"] = stats.chisqprob(sumstats["CHISQ"],1)
                filled_count +=1
        else:
            if only_sig is True and overwrite is True:
                log.write("  - Filling P value using CHISQ column for variants:" , sum(sumstats["P"]<sig_level), verbose=verbose)
                sumstats.loc[sumstats["P"]<sig_level,"P"] = stats.chisqprob(sumstats.loc[sumstats["P"]<sig_level,"CHISQ"],sumstats.loc[sumstats["P"]<sig_level,df].astype("int"))
                filled_count +=1
            else:
                log.write("  - Filling P value using CHISQ column for all valid variants:", verbose=verbose)
                sumstats["P"] = stats.chisqprob(sumstats["CHISQ"],sumstats[df].astype("int"))
                filled_count +=1
    else:
        return 0,filled_count
    return 1,filled_count
//  [python]
def fill_p(sumstats,log,df=None,only_sig=False,sig_level=5e-8,overwrite=False,verbose=True,filled_count=0):
        # MLOG10P -> P
    if "MLOG10P" in sumstats.columns:    
        log.write("  - Filling P value using MLOG10P column...", verbose=verbose)
        sumstats["P"] = np.power(10,-sumstats["MLOG10P"])
        filled_count +=1

    # Z -> P
    elif "Z" in sumstats.columns:
        log.write("  - Filling P value using Z column...", verbose=verbose)
        sumstats["P"] = stats.chi2.sf(sumstats["Z"]**2,1)
        filled_count +=1

    elif "CHISQ" in sumstats.columns:
    #CHISQ -> P
        log.write("  - Filling P value using CHISQ column...", verbose=verbose)
        if df is None:
            if only_sig is True and overwrite is True:
                sumstats.loc[sumstats["P"]<sig_level,"P"] = stats.chi2.sf(sumstats.loc[sumstats["P"]<sig_level,"CHISQ"],1)
                filled_count +=1
            else:
                sumstats["P"] = stats.chi2.sf(sumstats["CHISQ"],1)
                filled_count +=1
        else:
            if only_sig is True and overwrite is True:
                log.write("  - Filling P value using CHISQ column for variants:" , sum(sumstats["P"]<sig_level), verbose=verbose)
                sumstats.loc[sumstats["P"]<sig_level,"P"] = stats.chi2.sf(sumstats.loc[sumstats["P"]<sig_level,"CHISQ"],sumstats.loc[sumstats["P"]<sig_level,df].astype("int"))
                filled_count +=1
            else:
                log.write("  - Filling P value using CHISQ column for all valid variants:", verbose=verbose)
                sumstats["P"] = stats.chi2.sf(sumstats["CHISQ"],sumstats[df].astype("int"))
                filled_count +=1
    else:
        return 0,filled_count
    return 1,filled_count
//  [python]
def deviance(X, y_true, y_pred):
    """Computes the deviance statistic.

    Parameters
    ----------
    X : array
        Design matrix.
    y_true : array
        Observed labels, either 0 or 1.
    y_pred : array
        Predicted probabilities, floats on [0, 1].

    Returns
    -------
    D : float
        The test statistic :math:`D`.
    df : int
        The degrees of freedom of the test.
    p : float
        The p-value of the test.
    """
    __, p = X.shape

    d = deviance_residuals(y_true, y_pred)

    D = np.sum(np.square(d))
    df = len(y_true) - (p + 1)
    p = scipy.stats.chisqprob(D, df)

    TestResult = namedtuple('Deviance', ('D', 'df', 'p'))
    return TestResult(D, df, p)
//  [python]
def deviance(X, y_true, y_pred):
    """Computes the deviance statistic.

    Parameters
    ----------
    X : array
        Design matrix.
    y_true : array
        Observed labels, either 0 or 1.
    y_pred : array
        Predicted probabilities, floats on [0, 1].

    Returns
    -------
    D : float
        The test statistic :math:`D`.
    df : int
        The degrees of freedom of the test.
    p : float
        The p-value of the test.
    """
    __, p = X.shape

    d = deviance_residuals(y_true, y_pred)

    D = np.sum(np.square(d))
    df = len(y_true) - (p + 1)
    p = scipy.stats.chi2.sf(D, df)

    TestResult = namedtuple('Deviance', ('D', 'df', 'p'))
    return TestResult(D, df, p)
//  [python]
def pearson_chisquare(X, y_true, y_pred):
    """Computes the Pearson chi-square statistic.

    Parameters
    ----------
    X : array
        Design matrix.
    y_true : array
        Observed labels, either 0 or 1.
    y_pred : array
        Predicted probabilities, floats on [0, 1].

    Returns
    -------
    chisquare : float
        The test statistic :math:`X^2`.
    df : int
        The degrees of freedom of the test.
    p : float
        The p-value of the test.
    """
    __, p = X.shape

    r = pearson_residuals(y_true, y_pred)

    chisquare = np.sum(np.square(r))
    df = len(y_true) - (p + 1)
    p = scipy.stats.chisqprob(chisquare, df)

    TestResult = namedtuple('PearsonChiSquare', ('chisquare', 'df', 'p'))
    return TestResult(chisquare, df, p)
//  [python]
def pearson_chisquare(X, y_true, y_pred):
    """Computes the Pearson chi-square statistic.

    Parameters
    ----------
    X : array
        Design matrix.
    y_true : array
        Observed labels, either 0 or 1.
    y_pred : array
        Predicted probabilities, floats on [0, 1].

    Returns
    -------
    chisquare : float
        The test statistic :math:`X^2`.
    df : int
        The degrees of freedom of the test.
    p : float
        The p-value of the test.
    """
    __, p = X.shape

    r = pearson_residuals(y_true, y_pred)

    chisquare = np.sum(np.square(r))
    df = len(y_true) - (p + 1)
    p = scipy.stats.chi2.sf(chisquare, df)

    TestResult = namedtuple('PearsonChiSquare', ('chisquare', 'df', 'p'))
    return TestResult(chisquare, df, p)
//  [python]
def install(download_dir):
    local_filename = "%s/MEGSA_beta.zip" % download_dir
    if not os.path.exists(local_filename):
        filename, response = urllib.urlretrieve(MEGSA_URL, local_filename)
    else:
        filename = local_filename
    nbsupport.util.check_digest(filename, "655e1ec48a67530672303d8ac7fbc925")

    with zipfile.ZipFile(local_filename) as archive:
        with io.TextIOWrapper(archive.open("version beta/MEGSA.R")) as stream:
            robjects.reval(stream.read())

    global megsa
    def megsa(events):
        s = robjects.r.funEstimate(events.T).rx2("S")[0]
        return 0.5 * scipy.stats.chisqprob(s, 1) + 0.5 * int(s == 0)
//  [python]
def install(download_dir):
    local_filename = "%s/MEGSA_beta.zip" % download_dir
    if not os.path.exists(local_filename):
        filename, response = urllib.urlretrieve(MEGSA_URL, local_filename)
    else:
        filename = local_filename
    nbsupport.util.check_digest(filename, "655e1ec48a67530672303d8ac7fbc925")

    with zipfile.ZipFile(local_filename) as archive:
        with io.TextIOWrapper(archive.open("version beta/MEGSA.R")) as stream:
            robjects.reval(stream.read())

    global megsa
    def megsa(events):
        s = robjects.r.funEstimate(events.T).rx2("S")[0]
        return 0.5 * scipy.stats.chi2.sf(s, 1) + 0.5 * int(s == 0)
//  [python]
def hosmer_lemeshow_test(y_true, y_pred, n_groups=10):
    """Performs a Hosmer–Lemeshow test.

    Parameters
    ----------
    y_true : array
        Observed labels, either 0 or 1.
    y_pred : array
        Predicted probabilities, floats on [0, 1].
    n_groups : int, optional
        The number of groups to create. The default value is 10, which
        corresponds to deciles of predicted probabilities.

    Returns
    -------
    C_hat : float
        The test statistic :math:`\hat{C}`.
    df : int
        The degrees of freedom of the test.
    p : float
        The p-value of the test.

    References
    ----------
    .. [1] Hosmer, David W. and Stanley Lemesbow. "Goodness of fit tests
       for the multiple logistic regression model." *Communications in
       Statistics – Theory and Methods* 9 (1980): 1043–1069.
    """
    table = hosmer_lemeshow_table(y_true, y_pred, n_groups=n_groups)

    num = np.square(table.obs_freq - table.mean_prob)
    den = table.group_size * table.mean_prob * (1 - table.mean_prob)

    C_hat = np.sum(num / den)

    df = len(table) - 2
    p = scipy.stats.chisqprob(C_hat, df)

    TestResult = namedtuple('HosmerLemeshowTest', ('C_hat', 'df', 'p'))
    return TestResult(C_hat, df, p)
//  [python]
def hosmer_lemeshow_test(y_true, y_pred, n_groups=10):
    """Performs a Hosmer–Lemeshow test.

    Parameters
    ----------
    y_true : array
        Observed labels, either 0 or 1.
    y_pred : array
        Predicted probabilities, floats on [0, 1].
    n_groups : int, optional
        The number of groups to create. The default value is 10, which
        corresponds to deciles of predicted probabilities.

    Returns
    -------
    C_hat : float
        The test statistic :math:`\hat{C}`.
    df : int
        The degrees of freedom of the test.
    p : float
        The p-value of the test.

    References
    ----------
    .. [1] Hosmer, David W. and Stanley Lemesbow. "Goodness of fit tests
       for the multiple logistic regression model." *Communications in
       Statistics – Theory and Methods* 9 (1980): 1043–1069.
    """
    table = hosmer_lemeshow_table(y_true, y_pred, n_groups=n_groups)

    num = np.square(table.obs_freq - table.mean_prob)
    den = table.group_size * table.mean_prob * (1 - table.mean_prob)

    C_hat = np.sum(num / den)

    df = len(table) - 2
    p = scipy.stats.chi2.sf(C_hat, df)

    TestResult = namedtuple('HosmerLemeshowTest', ('C_hat', 'df', 'p'))
    return TestResult(C_hat, df, p)
//  [python]
def osius_rojek_test(X, y_true, y_pred):
    """Tests the normal approximation to the distribution of the Pearson
    chi-square statistic.

    Parameters
    ----------
    X : array
        Design matrix.
    y_true : array
        Observed labels, either 0 or 1.
    y_pred : array
        Predicted probabilities, floats on [0, 1].

    Returns
    -------
    z : float
        The test statistic :math:`z`.
    df : int
        The degrees of freedom of the test.
    p : float
        The p-value of the test.

    References
    ----------
    .. [1] Osius, Gerhard, and Dieter Rojek. "Normal goodness-of-fit tests
       for multinomial models with large degrees of freedom." *Journal of
       the American Statistical Association* 87 (1992): 1145–1152.
    """
    v = y_pred * (1 - y_pred)
    c = (1 - 2 * y_pred) / v

    chisquare, __, __ = pearson_chisquare(X, y_true, y_pred)

    # Compute the weighted least-squares solution.
    Xv = X * np.sqrt(v)[:, np.newaxis]
    cv = c * np.sqrt(v)

    __, residuals, __, __ = scipy.linalg.lstsq(Xv, cv)

    A = 0  # No variance correction factor; assume unique covariate patterns

    df = len(y_true) - (2 + 1)  # ``p`` = 2 for binary logistic regression
    z = (chisquare - df) / np.sqrt(A + residuals)
    p = scipy.stats.chisqprob(z, df)

    TestResult = namedtuple('OsiusRojekTest', ('z', 'df', 'p'))
    return TestResult(z, df, p)
//  [python]
def osius_rojek_test(X, y_true, y_pred):
    """Tests the normal approximation to the distribution of the Pearson
    chi-square statistic.

    Parameters
    ----------
    X : array
        Design matrix.
    y_true : array
        Observed labels, either 0 or 1.
    y_pred : array
        Predicted probabilities, floats on [0, 1].

    Returns
    -------
    z : float
        The test statistic :math:`z`.
    df : int
        The degrees of freedom of the test.
    p : float
        The p-value of the test.

    References
    ----------
    .. [1] Osius, Gerhard, and Dieter Rojek. "Normal goodness-of-fit tests
       for multinomial models with large degrees of freedom." *Journal of
       the American Statistical Association* 87 (1992): 1145–1152.
    """
    v = y_pred * (1 - y_pred)
    c = (1 - 2 * y_pred) / v

    chisquare, __, __ = pearson_chisquare(X, y_true, y_pred)

    # Compute the weighted least-squares solution.
    Xv = X * np.sqrt(v)[:, np.newaxis]
    cv = c * np.sqrt(v)

    __, residuals, __, __ = scipy.linalg.lstsq(Xv, cv)

    A = 0  # No variance correction factor; assume unique covariate patterns

    df = len(y_true) - (2 + 1)  # ``p`` = 2 for binary logistic regression
    z = (chisquare - df) / np.sqrt(A + residuals)
    p = scipy.stats.chi2.sf(z, df)

    TestResult = namedtuple('OsiusRojekTest', ('z', 'df', 'p'))
    return TestResult(z, df, p)
//  [python]
def _calc_stats(self):
        pars, pcov = extract_mcmc_stats(self._trials)
        perr = sqrt(diag(pcov))

        npars = len(pars)

        lnprob = self.get_lnprob(pars)

        npts = self.get_npoints()

        chi2 = lnprob / (-0.5)
        dof = npts - npars
        chi2per = chi2 / dof

        aic = -2 * lnprob + 2 * npars
        bic = -2 * lnprob + npars * log(npts)

        res = {
            "pars": pars,
            "perr": perr,
            "pcov": pcov,
            "lnprob": lnprob,
            "aic": aic,
            "bic": bic,
            "chi2": chi2,
            "dof": dof,
            "chi2per": chi2per,
        }
        try:
            import scipy.stats

            prob = scipy.stats.chisqprob(chi2, dof)

            res["prob"] = prob
        except Exception:
            pass

        self._result = res
//  [python]
def _calc_stats(self):
        pars, pcov = extract_mcmc_stats(self._trials)
        perr = sqrt(diag(pcov))

        npars = len(pars)

        lnprob = self.get_lnprob(pars)

        npts = self.get_npoints()

        chi2 = lnprob / (-0.5)
        dof = npts - npars
        chi2per = chi2 / dof

        aic = -2 * lnprob + 2 * npars
        bic = -2 * lnprob + npars * log(npts)

        res = {
            "pars": pars,
            "perr": perr,
            "pcov": pcov,
            "lnprob": lnprob,
            "aic": aic,
            "bic": bic,
            "chi2": chi2,
            "dof": dof,
            "chi2per": chi2per,
        }
        try:
            import scipy.stats

            prob = scipy.stats.chi2.sf(chi2, dof)

            res["prob"] = prob
        except Exception:
            pass

        self._result = res
//  [python]
def test_simple(self):
        """A really simple case for stats.kruskal"""
        x = [1]
        y = [2]
        h, p = stats.kruskal(x, y)
        assert_equal(h, 1.0)
        assert_approx_equal(p, stats.chisqprob(h, 1))
        h, p = stats.kruskal(np.array(x), np.array(y))
        assert_equal(h, 1.0)
        assert_approx_equal(p, stats.chisqprob(h, 1))
//  [python]
def test_simple(self):
        """A really simple case for stats.kruskal"""
        x = [1]
        y = [2]
        h, p = stats.kruskal(x, y)
        assert_equal(h, 1.0)
        assert_approx_equal(p, stats.chi2.sf(h, 1))
        h, p = stats.kruskal(np.array(x), np.array(y))
        assert_equal(h, 1.0)
        assert_approx_equal(p, stats.chi2.sf(h, 1))
//  [python]
def _calc_stats(self):
        pars, pcov = extract_mcmc_stats(self.trials)
        perr = sqrt(diag(pcov))

        npars = len(pars)

        lnprob = self.get_lnprob(pars)

        chi2 = lnprob / (-0.5)
        dof = self.x.size - npars
        chi2per = chi2 / dof

        aic = -2 * lnprob + 2 * npars
        bic = -2 * lnprob + npars * log(self.x.size)

        res = {
            "pars": pars,
            "perr": perr,
            "pcov": pcov,
            "lnprob": lnprob,
            "aic": aic,
            "bic": bic,
            "chi2": chi2,
            "dof": dof,
            "chi2per": chi2per,
        }
        try:
            import scipy.stats

            prob = scipy.stats.chisqprob(chi2, dof)

            res["prob"] = prob
        except Exception:
            pass

        self._result = res
        try:
            self._ply = np.poly1d(res["pars"])
        except Exception:
            print("could not set poly")
            self._ply = None
//  [python]
def _calc_stats(self):
        pars, pcov = extract_mcmc_stats(self.trials)
        perr = sqrt(diag(pcov))

        npars = len(pars)

        lnprob = self.get_lnprob(pars)

        chi2 = lnprob / (-0.5)
        dof = self.x.size - npars
        chi2per = chi2 / dof

        aic = -2 * lnprob + 2 * npars
        bic = -2 * lnprob + npars * log(self.x.size)

        res = {
            "pars": pars,
            "perr": perr,
            "pcov": pcov,
            "lnprob": lnprob,
            "aic": aic,
            "bic": bic,
            "chi2": chi2,
            "dof": dof,
            "chi2per": chi2per,
        }
        try:
            import scipy.stats

            prob = scipy.stats.chi2.sf(chi2, dof)

            res["prob"] = prob
        except Exception:
            pass

        self._result = res
        try:
            self._ply = np.poly1d(res["pars"])
        except Exception:
            print("could not set poly")
            self._ply = None
//  [python]
def test_chisquare_masked_arrays():
    # Test masked arrays.
    obs = np.array([[8, 8, 16, 32, -1], [-1, -1, 3, 4, 5]]).T
    mask = np.array([[0, 0, 0, 0, 1], [1, 1, 0, 0, 0]]).T
    mobs = np.ma.masked_array(obs, mask)
    expected_chisq = np.array([24.0, 0.5])
    expected_g = np.array([2*(2*8*np.log(0.5) + 32*np.log(2.0)),
                           2*(3*np.log(0.75) + 5*np.log(1.25))])

    chisq, p = stats.chisquare(mobs)
    mat.assert_array_equal(chisq, expected_chisq)
    mat.assert_array_almost_equal(p, stats.chisqprob(expected_chisq,
                                                     mobs.count(axis=0) - 1))

    g, p = stats.power_divergence(mobs, lambda_='log-likelihood')
    mat.assert_array_almost_equal(g, expected_g, decimal=15)
    mat.assert_array_almost_equal(p, stats.chisqprob(expected_g,
                                                     mobs.count(axis=0) - 1))

    chisq, p = stats.chisquare(mobs.T, axis=1)
    mat.assert_array_equal(chisq, expected_chisq)
    mat.assert_array_almost_equal(p,
                                  stats.chisqprob(expected_chisq,
                                                  mobs.T.count(axis=1) - 1))

    g, p = stats.power_divergence(mobs.T, axis=1, lambda_="log-likelihood")
    mat.assert_array_almost_equal(g, expected_g, decimal=15)
    mat.assert_array_almost_equal(p, stats.chisqprob(expected_g,
                                                     mobs.count(axis=0) - 1))

    obs1 = np.ma.array([3, 5, 6, 99, 10], mask=[0, 0, 0, 1, 0])
    exp1 = np.ma.array([2, 4, 8, 10, 99], mask=[0, 0, 0, 0, 1])
    chi2, p = stats.chisquare(obs1, f_exp=exp1)
    # Because of the mask at index 3 of obs1 and at index 4 of exp1,
    # only the first three elements are included in the calculation
    # of the statistic.
    mat.assert_array_equal(chi2, 1/2 + 1/4 + 4/8)

    # When axis=None, the two values should have type np.float64.
    chisq, p = stats.chisquare(np.ma.array([1,2,3]), axis=None)
    assert_(isinstance(chisq, np.float64))
    assert_(isinstance(p, np.float64))
    assert_equal(chisq, 1.0)
    assert_almost_equal(p, stats.chisqprob(1.0, 2))

    # Empty arrays:
    # A data set with length 0 returns a masked scalar.
    chisq, p = stats.chisquare(np.ma.array([]))
    assert_(isinstance(chisq, np.ma.MaskedArray))
    assert_equal(chisq.shape, ())
    assert_(chisq.mask)

    empty3 = np.ma.array([[],[],[]])

    # empty3 is a collection of 0 data sets (whose lengths would be 3, if
    # there were any), so the return value is an array with length 0.
    chisq, p = stats.chisquare(empty3)
    assert_(isinstance(chisq, np.ma.MaskedArray))
    mat.assert_array_equal(chisq, [])

    # empty3.T is an array containing 3 data sets, each with length 0,
    # so an array of size (3,) is returned, with all values masked.
    chisq, p = stats.chisquare(empty3.T)
    assert_(isinstance(chisq, np.ma.MaskedArray))
    assert_equal(chisq.shape, (3,))
    assert_(np.all(chisq.mask))
//  [python]
def test_chisquare_masked_arrays():
    # Test masked arrays.
    obs = np.array([[8, 8, 16, 32, -1], [-1, -1, 3, 4, 5]]).T
    mask = np.array([[0, 0, 0, 0, 1], [1, 1, 0, 0, 0]]).T
    mobs = np.ma.masked_array(obs, mask)
    expected_chisq = np.array([24.0, 0.5])
    expected_g = np.array([2*(2*8*np.log(0.5) + 32*np.log(2.0)),
                           2*(3*np.log(0.75) + 5*np.log(1.25))])

    chisq, p = stats.chisquare(mobs)
    mat.assert_array_equal(chisq, expected_chisq)
    mat.assert_array_almost_equal(p, stats.chi2.sf(expected_chisq,
                                                     mobs.count(axis=0) - 1))

    g, p = stats.power_divergence(mobs, lambda_='log-likelihood')
    mat.assert_array_almost_equal(g, expected_g, decimal=15)
    mat.assert_array_almost_equal(p, stats.chi2.sf(expected_g,
                                                     mobs.count(axis=0) - 1))

    chisq, p = stats.chisquare(mobs.T, axis=1)
    mat.assert_array_equal(chisq, expected_chisq)
    mat.assert_array_almost_equal(p,
                                  stats.chi2.sf(expected_chisq,
                                                  mobs.T.count(axis=1) - 1))

    g, p = stats.power_divergence(mobs.T, axis=1, lambda_="log-likelihood")
    mat.assert_array_almost_equal(g, expected_g, decimal=15)
    mat.assert_array_almost_equal(p, stats.chi2.sf(expected_g,
                                                     mobs.count(axis=0) - 1))

    obs1 = np.ma.array([3, 5, 6, 99, 10], mask=[0, 0, 0, 1, 0])
    exp1 = np.ma.array([2, 4, 8, 10, 99], mask=[0, 0, 0, 0, 1])
    chi2, p = stats.chisquare(obs1, f_exp=exp1)
    # Because of the mask at index 3 of obs1 and at index 4 of exp1,
    # only the first three elements are included in the calculation
    # of the statistic.
    mat.assert_array_equal(chi2, 1/2 + 1/4 + 4/8)

    # When axis=None, the two values should have type np.float64.
    chisq, p = stats.chisquare(np.ma.array([1,2,3]), axis=None)
    assert_(isinstance(chisq, np.float64))
    assert_(isinstance(p, np.float64))
    assert_equal(chisq, 1.0)
    assert_almost_equal(p, stats.chi2.sf(1.0, 2))

    # Empty arrays:
    # A data set with length 0 returns a masked scalar.
    chisq, p = stats.chisquare(np.ma.array([]))
    assert_(isinstance(chisq, np.ma.MaskedArray))
    assert_equal(chisq.shape, ())
    assert_(chisq.mask)

    empty3 = np.ma.array([[],[],[]])

    # empty3 is a collection of 0 data sets (whose lengths would be 3, if
    # there were any), so the return value is an array with length 0.
    chisq, p = stats.chisquare(empty3)
    assert_(isinstance(chisq, np.ma.MaskedArray))
    mat.assert_array_equal(chisq, [])

    # empty3.T is an array containing 3 data sets, each with length 0,
    # so an array of size (3,) is returned, with all values masked.
    chisq, p = stats.chisquare(empty3.T)
    assert_(isinstance(chisq, np.ma.MaskedArray))
    assert_equal(chisq.shape, (3,))
    assert_(np.all(chisq.mask))
//  [python]
def chiSquare(O, E):
    '''
    chisquare test of an observed frequencies (O)
    against the expected E assuming H0 is true. 
    the same as st.chisquare()
    > chi-square
    > p-value corresponding to the computed chi-square
    '''
    assert(len(O) == len(E))
    assert np.all(O > 4), "less than five occurrences in this class %s"%np.min(O)
    assert np.all(E > 4), "less than five occurrences in this class %s"%np.min(E)
    df = len(O)-1
    Xsquare = np.sum((O-E)**2/E)
    return Xsquare, st.chisqprob(Xsquare, df)
//  [python]
def chiSquare(O, E):
    '''
    chisquare test of an observed frequencies (O)
    against the expected E assuming H0 is true. 
    the same as st.chisquare()
    > chi-square
    > p-value corresponding to the computed chi-square
    '''
    assert(len(O) == len(E))
    assert np.all(O > 4), "less than five occurrences in this class %s"%np.min(O)
    assert np.all(E > 4), "less than five occurrences in this class %s"%np.min(E)
    df = len(O)-1
    Xsquare = np.sum((O-E)**2/E)
    return Xsquare, st.chi2.sf(Xsquare, df)
//  [python]
def test_basic(self):
        """A basic test, with no ties."""
        x = [1, 3, 5, 7, 9]
        y = [2, 4, 6, 8, 10]
        h, p = stats.kruskal(x, y)
        assert_approx_equal(h, 3./11, significant=10)
        assert_approx_equal(p, stats.chisqprob(3./11, 1))
        h, p = stats.kruskal(np.array(x), np.array(y))
        assert_approx_equal(h, 3./11, significant=10)
        assert_approx_equal(p, stats.chisqprob(3./11, 1))
//  [python]
def test_basic(self):
        """A basic test, with no ties."""
        x = [1, 3, 5, 7, 9]
        y = [2, 4, 6, 8, 10]
        h, p = stats.kruskal(x, y)
        assert_approx_equal(h, 3./11, significant=10)
        assert_approx_equal(p, stats.chi2.sf(3./11, 1))
        h, p = stats.kruskal(np.array(x), np.array(y))
        assert_approx_equal(h, 3./11, significant=10)
        assert_approx_equal(p, stats.chi2.sf(3./11, 1))
//  [python]
def test_three_groups(self):
        """A test of stats.kruskal with three groups, with ties."""
        x = [1, 1, 1]
        y = [2, 2, 2]
        z = [2, 2]
        h_uncorr = (12. / 8. / 9.) * (3*2**2 + 3*6**2 + 2*6**2) - 3 * 9  # 5.0
        corr = 1 - float(3**3 - 3 + 5**3 - 5) / (8**3 - 8)
        expected = h_uncorr / corr  # 7.0
        h, p = stats.kruskal(x, y, z)
        assert_approx_equal(h, expected)
        assert_approx_equal(p, stats.chisqprob(h, 2))
//  [python]
def test_three_groups(self):
        """A test of stats.kruskal with three groups, with ties."""
        x = [1, 1, 1]
        y = [2, 2, 2]
        z = [2, 2]
        h_uncorr = (12. / 8. / 9.) * (3*2**2 + 3*6**2 + 2*6**2) - 3 * 9  # 5.0
        corr = 1 - float(3**3 - 3 + 5**3 - 5) / (8**3 - 8)
        expected = h_uncorr / corr  # 7.0
        h, p = stats.kruskal(x, y, z)
        assert_approx_equal(h, expected)
        assert_approx_equal(p, stats.chi2.sf(h, 2))
//  [python]
def Gstatistics(O, E):
    '''
    chisquare test of an observed frequencies (O)
    against the expected E assuming H0 is true. 
    > chi-square
    > p-value corresponding to the computed chi-square
    '''
    assert(len(O) == len(E))
    assert np.all(O > 4), "less than five occurrences in this class %s"%np.min(O)
    assert np.all(E > 4), "less than five occurrences in this class %s"%np.min(E)
    df = len(O) - 1
    #print(O*np.log(O/E))
    G = 2*np.sum(O*np.log(O/E))
    return G, st.chisqprob(G, df)
//  [python]
def Gstatistics(O, E):
    '''
    chisquare test of an observed frequencies (O)
    against the expected E assuming H0 is true. 
    > chi-square
    > p-value corresponding to the computed chi-square
    '''
    assert(len(O) == len(E))
    assert np.all(O > 4), "less than five occurrences in this class %s"%np.min(O)
    assert np.all(E > 4), "less than five occurrences in this class %s"%np.min(E)
    df = len(O) - 1
    #print(O*np.log(O/E))
    G = 2*np.sum(O*np.log(O/E))
    return G, st.chi2.sf(G, df)
//  [python]
def st_impl(x, k):
        return scipy.stats.chi2.sf(x, k)
//  [python]
def doLogLikelihoodTest(complex_ll, complex_np,
                        simple_ll, simple_np,
                        significance_threshold=0.05):
    """perform log-likelihood test between model1 and model2.
    """

    assert complex_ll >= simple_ll, "log likelihood of complex model smaller than for simple model: %f > %f" % (
        complex_ll, simple_ll)

    chi = 2 * (complex_ll - simple_ll)
    df = complex_np - simple_np

    if df <= 0:
        raise ValueError("difference of degrees of freedom not larger than 0")

    p = scipy.stats.chisqprob(chi, df)

    l = LogLikelihoodTest()

    l.mComplexLogLikelihood = complex_ll
    l.mSimpleLogLikelihood = simple_ll
    l.mComplexNumParameters = complex_np
    l.mSimpleNumParameters = simple_np
    l.mSignificanceThreshold = significance_threshold
    l.mProbability = p
    l.mChiSquaredValue = chi
    l.mDegreesFreedom = df

    if p < significance_threshold:
        l.mPassed = True
    else:
        l.mPassed = False

    return l