Exemple #1
0
def _test_stats_mean_std(hi, ham, ma, n_chains):
    sampler = nk.sampler.MetropolisLocal(ma, n_chains=n_chains)

    n_samples = 16000
    num_samples_per_chain = n_samples // n_chains

    # Discard a few samples
    sampler.generate_samples(1000)

    samples = sampler.generate_samples(num_samples_per_chain)
    assert samples.shape == (num_samples_per_chain, n_chains, hi.size)

    eloc = np.empty((num_samples_per_chain, n_chains), dtype=np.complex128)
    for i in range(num_samples_per_chain):
        eloc[i] = local_values(ham, ma, samples[i])

    stats = statistics(eloc.T)

    # These tests only work for one MPI process
    assert nk.stats.MPI.COMM_WORLD.size == 1

    assert stats.mean == pytest.approx(np.mean(eloc))
    if n_chains > 1:

        # variance == average sample variance over chains
        assert stats.variance == pytest.approx(np.var(eloc))
        # R estimate
        B_over_n = stats.error_of_mean ** 2
        W = stats.variance
        assert stats.R_hat == pytest.approx(np.sqrt(1.0 + B_over_n / W), abs=1e-3)
Exemple #2
0
def _test_stats_mean_std(hi, ham, ma, n_chains):
    sampler = nk.sampler.MetropolisLocal(ma, n_chains=n_chains)

    n_samples = 16000
    num_samples_per_chain = n_samples // n_chains

    # Discard a few samples
    sampler.generate_samples(1000)

    samples = sampler.generate_samples(num_samples_per_chain)
    assert samples.shape == (num_samples_per_chain, n_chains, hi.size)

    eloc = local_values(ham, ma, samples)
    assert eloc.shape == (num_samples_per_chain, n_chains)

    stats = statistics(eloc)

    # These tests only work for one MPI process
    assert nk.MPI.size() == 1

    assert stats.mean == pytest.approx(np.mean(eloc))
    if n_chains > 1:
        # error of mean == stdev of sample mean between chains / sqrt(#chains)
        assert stats.error_of_mean == pytest.approx(
            eloc.mean(axis=0).std(ddof=0) / np.sqrt(n_chains)
        )
        # variance == average sample variance over chains
        assert stats.variance == pytest.approx(eloc.var(axis=0).mean())
        # R estimate
        B_over_n = stats.error_of_mean ** 2
        W = stats.variance
        assert stats.R == pytest.approx(
            np.sqrt((n_samples - 1.0) / n_samples + B_over_n / W), abs=1e-3
        )
Exemple #3
0
def compute_locals(n_times):
    for k in range(n_times):
        for i, sample in enumerate(samples):
            local_values(ha, ma, sample, out=loc[i])