当前位置: 首页>>代码示例>>Python>>正文


Python stats.chisquare方法代码示例

本文整理汇总了Python中scipy.stats.chisquare方法的典型用法代码示例。如果您正苦于以下问题:Python stats.chisquare方法的具体用法?Python stats.chisquare怎么用?Python stats.chisquare使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在scipy.stats的用法示例。


在下文中一共展示了stats.chisquare方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_univariate_categorical

# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import chisquare [as 别名]
def test_univariate_categorical():
    # This test generates univariate data from a nominal variable with 6 levels
    # and probability vector p_theory, and performs a chi-square test on
    # posterior samples from MvKde.

    rng = gu.gen_rng(2)
    N_SAMPLES = 1000
    p_theory = [.3, .1, .2, .15, .15, .1]
    samples_test = rng.choice(range(6), p=p_theory, size=N_SAMPLES)
    kde = MultivariateKde(
        [7], None, distargs={O: {ST: [C], SA:[{'k': 6}]}}, rng=rng)
    # Incorporate observations.
    for rowid, x in enumerate(samples_test):
        kde.incorporate(rowid, {7: x})
    kde.transition()
    # Posterior samples.
    samples_gen = kde.simulate(-1, [7], N=N_SAMPLES)
    f_obs = np.bincount([s[7] for s in samples_gen])
    f_exp = np.bincount(samples_test)
    _, pval = chisquare(f_obs, f_exp)
    assert 0.05 < pval
    # Get some coverage on logpdf_score.
    assert kde.logpdf_score() < 0 
开发者ID:probcomp,项目名称:cgpm,代码行数:25,代码来源:test_mvkde.py

示例2: check_power_divergence

# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import chisquare [as 别名]
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) 
开发者ID:ktraunmueller,项目名称:Computable,代码行数:23,代码来源:test_stats.py

示例3: test_discrete_random

# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import chisquare [as 别名]
def test_discrete_random(self):
        """...Test discrete random numbers simulation
        """
        probabilities = np.array([2.0, 0.1, 3, 5, 7])
        seeded_sample = [2., 3., 3., 0., 4.]

        self._test_dist_with_seed(seeded_sample, test_discrete, probabilities,
                                  discrete=True)

        # Statistical tests
        sample = test_discrete(probabilities, self.stat_size, self.test_seed)
        f_obs = [sum(sample == i) for i in range(len(probabilities))]
        _, p = stats.chisquare(f_exp=probabilities, f_obs=f_obs)
        self.assertLess(p, 0.05)

        # Test that variable event with probability 0 never happens
        probabilities_zero = probabilities.copy()
        probabilities_zero[1] = 0
        sample = test_discrete(probabilities_zero, self.stat_size,
                               self.test_seed)
        self.assertEqual(sum(sample == 1), 0) 
开发者ID:X-DataInitiative,项目名称:tick,代码行数:23,代码来源:random_test.py

示例4: test_even_mixing

# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import chisquare [as 别名]
def test_even_mixing(verbose=True):
    """
    Testing Cython mixing code with 1000 swap attempts and uniform 0 energies
    """
    if verbose: print("Testing Cython mixing code with uniform zero energies")
    n_swaps = 1000
    n_states = 16
    corrected_threshold = 0.001 / n_states
    permutation_list = mix_replicas(n_swaps=n_swaps, n_states=n_states)
    state_counts = calculate_state_counts(permutation_list, n_swaps, n_states)
    for replica in range(n_states):
        _, p_val = stats.chisquare(state_counts[replica, :])
        if p_val < corrected_threshold:
            print("Detected a significant difference between expected even mixing\n")
            print("and observed mixing, p=%f" % p_val)
            raise Exception("Replica %d failed the even mixing test" % replica)
    return 0 
开发者ID:choderalab,项目名称:openmmtools,代码行数:19,代码来源:test_mixing.py

示例5: smart_random

# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import chisquare [as 别名]
def smart_random(self):
        """
        Choose a random training set that has an energy distribution most resembling that of the full dataset.
        Uses the Chi-Squared method to estimate the similarity of the energy distrubtions.
        """
        data = self.dataset.values
        X = data[:, :-1]
        y = data[:,-1].reshape(-1,1)
        full_dataset_dist, binedges = np.histogram(y, bins=10, density=True)
        pvalues = []
        chi = []
        for seed in range(500):
            X_train, X_test, y_train, y_test  = train_test_split(X,y,train_size=self.ntrain, random_state=seed)
            train_dist, tmpbin = np.histogram(y_train, bins=binedges, density=True)
            chisq, p = stats.chisquare(train_dist, f_exp=full_dataset_dist)
            chi.append(chisq)
            pvalues.append(p)
        best_seed = np.argmin(chi)
        #best_seed = np.argmax(chi)
        X_train, X_test, y_train, y_test  = train_test_split(X,y,train_size=self.ntrain, random_state=best_seed)
        train_dist, tmpbin = np.histogram(y_train, bins=binedges, density=True)

        indices = np.arange(self.dataset_size)
        train_indices, test_indices  = train_test_split(indices, train_size=self.ntrain, random_state=best_seed)
        self.set_indices(train_indices, test_indices) 
开发者ID:CCQC,项目名称:PES-Learn,代码行数:27,代码来源:data_sampler.py

示例6: verify_model_ts

# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import chisquare [as 别名]
def verify_model_ts(self, ts, model, discrete, verify_roots):
        alleles = model.alleles
        num_alleles = len(alleles)
        roots = np.zeros((num_alleles,))
        transitions = np.zeros((num_alleles, num_alleles))
        for site in ts.sites():
            aa = site.ancestral_state
            roots[alleles.index(aa)] += 1
        for mut in ts.mutations():
            if mut.parent == tskit.NULL:
                pa = ts.site(mut.site).ancestral_state
            else:
                pa = ts.mutation(mut.parent).derived_state
            da = mut.derived_state
            transitions[alleles.index(pa), alleles.index(da)] += 1
        num_muts = np.sum(roots)
        if verify_roots:
            exp_roots = num_muts * model.root_distribution
            self.chisquare(roots, exp_roots)
        for j, (row, p) in enumerate(zip(transitions, model.transition_matrix)):
            p[j] = 0
            p /= sum(p)
            self.chisquare(row, sum(row) * p) 
开发者ID:tskit-dev,项目名称:msprime,代码行数:25,代码来源:test_mutations.py

示例7: test_resample_1d_statistical_test_poisson

# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import chisquare [as 别名]
def test_resample_1d_statistical_test_poisson(rng):
    # poisson is behaving super weird in scipy
    x = rng.poisson(1.5, size=1000)
    mu = np.mean(x)

    xe = (0, 1, 2, 3, 10)
    # somehow location 1 is needed here...
    wref = np.diff(stats.poisson(mu, 1).cdf(xe)) * len(x)

    # compute P values for replicas compared to original
    prob = []
    for bx in resample(x, 100, method="poisson", random_state=rng):
        w = np.histogram(bx, bins=xe)[0]
        c = stats.chisquare(w, wref)
        prob.append(c.pvalue)

    # check whether P value distribution is flat
    # - test has chance probability of 1 % to fail randomly
    # - if it fails due to programming error, value is typically < 1e-20
    wp = np.histogram(prob, range=(0, 1))[0]
    c = stats.chisquare(wp)
    assert c.pvalue > 0.01 
开发者ID:dsaxton,项目名称:resample,代码行数:24,代码来源:test_bootstrap.py

示例8: chisquare

# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import chisquare [as 别名]
def chisquare(n_ij, weighted):
    """
    Calculates the chisquare for a matrix of ind_v x dep_v
    for the unweighted and SPSS weighted case
    """
    if weighted:
        m_ij = n_ij / n_ij

        nan_mask = np.isnan(m_ij)
        m_ij[nan_mask] = 0.000001  # otherwise it breaks the chi-squared test

        w_ij = m_ij
        n_ij_col_sum = n_ij.sum(axis=1)
        n_ij_row_sum = n_ij.sum(axis=0)
        alpha, beta, eps = (1, 1, 1)
        while eps > 10e-6:
            alpha = alpha * np.vstack(n_ij_col_sum / m_ij.sum(axis=1))
            beta = n_ij_row_sum / (alpha * w_ij).sum(axis=0)
            eps = np.max(np.absolute(w_ij * alpha * beta - m_ij))
            m_ij = w_ij * alpha * beta

    else:
        m_ij = (np.vstack(n_ij.sum(axis=1)) * n_ij.sum(axis=0)) / n_ij.sum().astype(float)

    dof = (n_ij.shape[0] - 1) * (n_ij.shape[1] - 1)
    chi, p_val = stats.chisquare(n_ij, f_exp=m_ij, ddof=n_ij.size - 1 - dof, axis=None)

    return (chi, p_val, dof) 
开发者ID:Rambatino,项目名称:CHAID,代码行数:30,代码来源:stats.py

示例9: two_sample_test

# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import chisquare [as 别名]
def two_sample_test(cctype, X, Y):
    model = cu.cctype_class(cctype)
    if model.is_numeric(): # XXX WRONG CHOICE FOR DISCRETE NUMERIC XXX
        _, pval = ks_2samp(X, Y)
    else:
        Xb, Yb = aligned_bincount([X, Y])
        ignore = np.logical_and(Xb==0, Yb==0)
        Xb, Yb = Xb[np.logical_not(ignore)], Yb[np.logical_not(ignore)]
        Xb = Xb/float(sum(Xb)) * 1000
        Yb = Yb/float(sum(Yb)) * 1000
        _, pval = chisquare(Yb, f_exp=Xb)
    return pval 
开发者ID:probcomp,项目名称:cgpm,代码行数:14,代码来源:disabled_test_simulate_univariate.py

示例10: chisquare

# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import chisquare [as 别名]
def chisquare(dist, fit_result, data, bins=None, range=None):
    """Perform a Chi^2 test for goodness of fit.

    Tests the H0 hypothesis if the distances between fit result and
    data are compatible  with random fluctuations.

    Args:
        dist:         A mle.Distribution instance
        fit_result:   The solution dict, returned by the Distribution.fit method
        data:         The data used in Distribution.fit
        bins:         Number of bins for the histogram (default: 1+log2(N))
        range:        Range for the histogram (default: min(data), max(data))
    Returns:
        chisquare:    the test statistic, chi^2/ndf
        p-value:      the p-value, probability that differences between dist
                      and data are compatible with random fluctuation
    """
    variables = dist.get_vars()
    if len(variables) > 1:
        raise ValueError("This is a 1d only chisquare test")
    var = variables[0]

    # rule of thumb for number if bins if not provided
    if bins is None:
        bins = np.ceil(2*len(data[var.name])**(1.0/3.0))

    entries, edges = np.histogram(data[var.name], bins=bins, range=range)

    # get expected frequencies from the cdf
    cdf = dist.cdf(edges, **fit_result["x"])
    exp_entries = np.round(len(data[var.name]) * (cdf[1:]-cdf[:-1]))

    # use only bins where more then 4 entries are expected
    mask = exp_entries >= 5

    chisq, pvalue = stats.chisquare(entries[mask], exp_entries[mask], ddof=len(fit_result["x"]))
    chisq = chisq/(np.sum(mask) - len(fit_result["x"]) - 1)
    return chisq, pvalue 
开发者ID:ibab,项目名称:python-mle,代码行数:40,代码来源:__init__.py

示例11: test_chisquaretest

# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import chisquare [as 别名]
def test_chisquaretest(self):
        chi_test = ChiSquareTest(self.obs, self.exp)
        sci_chi_test = chisquare(self.obs, self.exp)

        assert_almost_equal(chi_test.chi_square, sci_chi_test.statistic)
        assert_almost_equal(chi_test.p_value, sci_chi_test.pvalue)

        assert not chi_test.continuity_correction
        assert chi_test.degrees_of_freedom == len(self.obs) - 1 
开发者ID:aschleg,项目名称:hypothetical,代码行数:11,代码来源:test_normality.py

示例12: test_chisquaretest_arr

# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import chisquare [as 别名]
def test_chisquaretest_arr(self):
        chi_test = ChiSquareTest(np.array(self.obs), np.array(self.exp))
        sci_chi_test = chisquare(self.obs, self.exp)

        assert_almost_equal(chi_test.chi_square, sci_chi_test.statistic)
        assert_almost_equal(chi_test.p_value, sci_chi_test.pvalue)

        assert not chi_test.continuity_correction
        assert chi_test.degrees_of_freedom == len(self.obs) - 1 
开发者ID:aschleg,项目名称:hypothetical,代码行数:11,代码来源:test_normality.py

示例13: test_chisquare_no_exp

# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import chisquare [as 别名]
def test_chisquare_no_exp(self):
        chi_test = ChiSquareTest(self.obs)
        sci_chi_test = chisquare(self.obs, self.exp)

        assert_almost_equal(chi_test.chi_square, sci_chi_test.statistic)
        assert_almost_equal(chi_test.p_value, sci_chi_test.pvalue) 
开发者ID:aschleg,项目名称:hypothetical,代码行数:8,代码来源:test_normality.py

示例14: proportions_chisquare_allpairs

# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import chisquare [as 别名]
def proportions_chisquare_allpairs(count, nobs, multitest_method='hs'):
    '''chisquare test of proportions for all pairs of k samples

    Performs a chisquare test for proportions for all pairwise comparisons.
    The alternative is two-sided

    Parameters
    ----------
    count : integer or array_like
        the number of successes in nobs trials.
    nobs : integer
        the number of trials or observations.
    prop : float, optional
        The probability of success under the null hypothesis,
        `0 <= prop <= 1`. The default value is `prop = 0.5`
    multitest_method : string
        This chooses the method for the multiple testing p-value correction,
        that is used as default in the results.
        It can be any method that is available in  ``multipletesting``.
        The default is Holm-Sidak 'hs'.

    Returns
    -------
    result : AllPairsResults instance
        The returned results instance has several statistics, such as p-values,
        attached, and additional methods for using a non-default
        ``multitest_method``.

    Notes
    -----
    Yates continuity correction is not available.
    '''
    #all_pairs = lmap(list, lzip(*np.triu_indices(4, 1)))
    all_pairs = lzip(*np.triu_indices(len(count), 1))
    pvals = [proportions_chisquare(count[list(pair)], nobs[list(pair)])[1]
               for pair in all_pairs]
    return AllPairsResults(pvals, all_pairs, multitest_method=multitest_method) 
开发者ID:birforce,项目名称:vnpy_crypto,代码行数:39,代码来源:proportion.py

示例15: test_predict_prob

# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import chisquare [as 别名]
def test_predict_prob(self):
        res = self.res
        endog = res.model.endog
        freq = np.bincount(endog.astype(int))

        pr = res.predict(which='prob')
        pr2 = sm.distributions.genpoisson_p.pmf(np.arange(6)[:, None],
                                        res.predict(), res.params[-1], 1).T
        assert_allclose(pr, pr2, rtol=1e-10, atol=1e-10)

        from scipy import stats
        chi2 = stats.chisquare(freq, pr.sum(0))
        assert_allclose(chi2[:], (0.64628806058715882, 0.98578597726324468),
                        rtol=0.01) 
开发者ID:birforce,项目名称:vnpy_crypto,代码行数:16,代码来源:test_discrete.py


注:本文中的scipy.stats.chisquare方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。