Beispiel #1
0
def test_importance_sampling():
    mu_sampler = 5.
    sigma_sampler = 4.
    mu_pdf = 4.
    sigma_pdf = 1.

    obs_sampler = zfit.Space(obs='obs1',
                             limits=(4.5, 5.5))  # smaller, so pdf is bigger
    obs_pdf = zfit.Space(obs='obs1', limits=(1, 7))

    gauss_sampler = GaussNoAnalyticSampling(mu=mu_sampler,
                                            sigma=sigma_sampler,
                                            obs=obs_sampler)
    gauss_pdf = GaussNoAnalyticSampling(mu=mu_pdf,
                                        sigma=sigma_pdf,
                                        obs=obs_pdf)

    importance_sampling_called = [False]

    class GaussianSampleAndWeights(SessionHolderMixin):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            # self.n_to_produce = tf.Variable(initial_value=-42, dtype=tf.int64, use_resource=True,
            #                                 trainable=False, validate_shape=False)
            # self.sess.run(self.n_to_produce.initializer)
            # self.dtype = dtype
            # self.limits = limits

        def __call__(self, n_to_produce, limits, dtype):
            importance_sampling_called[0] = True
            n_to_produce = tf.cast(n_to_produce, dtype=tf.int32)
            # assign_op = self.n_to_produce.assign(n_to_produce)
            # with tf.control_dependencies([assign_op]):
            gaussian_sample = gauss_sampler._create_sampler_tensor(
                n=n_to_produce, limits=limits, fixed_params=False,
                name='asdf')[2]
            weights = gauss_sampler.pdf(gaussian_sample)
            weights_max = None
            thresholds = tf.random.uniform(shape=(n_to_produce, ), dtype=dtype)
            return gaussian_sample, thresholds, weights, weights_max, n_to_produce

    sample = accept_reject_sample(prob=gauss_pdf.unnormalized_pdf,
                                  n=30000,
                                  limits=obs_pdf)
    gauss_pdf._sample_and_weights = GaussianSampleAndWeights
    sample2 = gauss_pdf.sample(n=30000, limits=obs_pdf)
    assert importance_sampling_called[0]
    sample_np, sample_np2 = zfit.run([sample, sample2])

    mean = np.mean(sample_np)
    mean2 = np.mean(sample_np2)
    std = np.std(sample_np)
    std2 = np.std(sample_np2)
    assert mean == pytest.approx(mu_pdf, rel=0.02)
    assert mean2 == pytest.approx(mu_pdf, rel=0.02)
    assert std == pytest.approx(sigma_pdf, rel=0.02)
    assert std2 == pytest.approx(sigma_pdf, rel=0.02)
Beispiel #2
0
def test_importance_sampling():
    mu_sampler = 5.
    sigma_sampler = 4.
    mu_pdf = 4.
    sigma_pdf = 1.

    obs_sampler = zfit.Space(obs='obs1',
                             limits=(4.5, 5.5))  # smaller, so pdf is bigger
    obs_pdf = zfit.Space(obs='obs1', limits=(1, 7))

    gauss_sampler = GaussNoAnalyticSampling(mu=mu_sampler,
                                            sigma=sigma_sampler,
                                            obs=obs_sampler)
    gauss_pdf = GaussNoAnalyticSampling(mu=mu_pdf,
                                        sigma=sigma_pdf,
                                        obs=obs_pdf)

    importance_sampling_called = [False]

    class GaussianSampleAndWeights:
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)

        @z.function
        def __call__(self, n_to_produce, limits, dtype):
            importance_sampling_called[0] = True
            n_to_produce = tf.cast(n_to_produce, dtype=tf.int32)
            gaussian_sample = gauss_sampler.sample(n=n_to_produce,
                                                   limits=limits,
                                                   name='asdf')
            weights = gauss_sampler.pdf(gaussian_sample)
            weights_max = None
            thresholds = tf.random.uniform(shape=(n_to_produce, ), dtype=dtype)
            return gaussian_sample, thresholds, weights, weights_max, n_to_produce

    sample = accept_reject_sample(prob=gauss_pdf.unnormalized_pdf,
                                  n=30000,
                                  limits=obs_pdf)
    gauss_pdf._sample_and_weights = GaussianSampleAndWeights
    sample2 = gauss_pdf.sample(n=30000, limits=obs_pdf)
    assert importance_sampling_called[0]
    sample_np, sample_np2 = [sample.numpy(), sample2.numpy()]

    mean = np.mean(sample_np)
    mean2 = np.mean(sample_np2)
    std = np.std(sample_np)
    std2 = np.std(sample_np2)
    assert mean == pytest.approx(mu_pdf, rel=0.02)
    assert mean2 == pytest.approx(mu_pdf, rel=0.02)
    assert std == pytest.approx(sigma_pdf, rel=0.02)
    assert std2 == pytest.approx(sigma_pdf, rel=0.02)
Beispiel #3
0
def test_importance_sampling():
    import tensorflow as tf

    mu_sampler = 5.
    sigma_sampler = 4.
    mu_pdf = 4.
    sigma_pdf = 1.

    obs_sampler = zfit.Space(obs='obs1',
                             limits=(4.5, 5.5))  # smaller, so pdf is bigger
    obs_pdf = zfit.Space(obs='obs1', limits=(1, 9))
    gauss_sampler = zfit.pdf.Gauss(mu=mu_sampler,
                                   sigma=sigma_sampler,
                                   obs=obs_sampler)
    gauss_pdf = zfit.pdf.Gauss(mu=mu_pdf, sigma=sigma_pdf, obs=obs_pdf)

    def gaussian_sample_and_weights(n_to_produce, limits, dtype):
        gaussian_sample = gauss_sampler.sample(n=n_to_produce, limits=limits)
        weights = gauss_sampler.pdf(gaussian_sample)
        weights_max = tf.reduce_max(weights) * 0.7
        thresholds = tf.random_uniform(shape=(n_to_produce, ))
        return gaussian_sample, thresholds, weights, weights_max, n_to_produce

    sample = accept_reject_sample(prob=gauss_pdf.unnormalized_pdf,
                                  n=30000,
                                  limits=obs_pdf)
    gauss_pdf._sample_and_weights = gaussian_sample_and_weights
    sample2 = gauss_pdf.sample(n=30000, limits=obs_pdf)
    sample_np, sample_np2 = zfit.run([sample, sample2])

    mean = np.mean(sample_np)
    mean2 = np.mean(sample_np2)
    std = np.std(sample_np)
    std2 = np.std(sample_np2)
    assert mean == pytest.approx(mu_pdf, rel=0.01)
    assert mean2 == pytest.approx(mu_pdf, rel=0.01)
    assert std == pytest.approx(sigma_pdf, rel=0.01)
    assert std2 == pytest.approx(sigma_pdf, rel=0.01)