コード例 #1
0
def test_prod_gauss_nd():
    test_values = np.random.random(size=(10, 3))
    lower = ((-5, -5, -5),)
    upper = ((4, 4, 4),)
    obs1 = ['a', 'b', 'c']
    norm_range_3d = Space(obs=obs1, limits=(lower, upper))
    test_values_data = Data.from_tensor(obs=obs1, tensor=test_values)
    product_pdf = product_gauss_3d()
    probs = product_pdf.pdf(x=test_values_data, norm_range=norm_range_3d)
    true_probs = np.prod(
        [gauss.pdf(test_values[:, i], norm_range=(-5, 4)) for i, gauss in enumerate(create_gaussians())], axis=0)
    probs_np = probs.numpy()
    np.testing.assert_allclose(true_probs, probs_np, rtol=1e-2)
コード例 #2
0
ファイル: test_pdfs.py プロジェクト: uiuc-arc/zfit
def test_prod_gauss_nd():
    test_values = np.random.random(size=(10, 3))
    test_values_data = Data.from_tensor(obs=obs1 * obs2 * obs3, tensor=test_values)
    product_pdf = product_gauss_3d()
    assert product_pdf.n_obs == 3
    probs = product_pdf.pdf(x=test_values_data)
    gaussians = create_gaussians()
    for gauss, space in zip(gaussians, [obs1, obs2, obs3]):
        gauss.set_norm_range(space.rect_limits)
    true_probs = np.prod(
        [gauss.pdf(test_values[:, i]) for i, gauss in enumerate(gaussians)], axis=0)
    probs_np = probs.numpy()
    np.testing.assert_allclose(true_probs, probs_np, rtol=1e-2)
コード例 #3
0
def run_no_nan(func, x):
    from zfit.core.data import Data

    value_with_nans = func(x=x)
    if value_with_nans.dtype in (tf.complex128, tf.complex64):
        value_with_nans = tf.math.real(value_with_nans) + tf.math.imag(value_with_nans)  # we care only about NaN or not
    finite_bools = tf.math.is_finite(tf.cast(value_with_nans, dtype=tf.float64))
    finite_indices = tf.where(finite_bools)
    new_x = tf.gather_nd(params=x, indices=finite_indices)
    new_x = Data.from_tensor(obs=x.obs, tensor=new_x)
    vals_no_nan = func(x=new_x)
    result = tf.scatter_nd(indices=finite_indices, updates=vals_no_nan,
                           shape=tf.shape(input=value_with_nans, out_type=finite_indices.dtype))
    return result
コード例 #4
0
def test_prod_gauss_nd():
    # return
    test_values = np.random.random(size=(10, 3))
    lower = ((-5, -5, -5), )
    upper = ((4, 4, 4), )
    obs1 = ['a', 'b', 'c']
    norm_range_3d = Space(obs=obs1, limits=(lower, upper))
    test_values_data = Data.from_tensor(obs=obs1, tensor=test_values)
    probs = prod_gauss_3d.pdf(x=test_values_data, norm_range=norm_range_3d)
    true_probs = np.prod([
        gauss.pdf(test_values[:, i], norm_range=(-5, 4))
        for i, gauss in enumerate(gauss_dists)
    ])
    probs_np = zfit.run(probs)
    np.testing.assert_allclose(zfit.run(true_probs), probs_np, rtol=1e-2)
コード例 #5
0
def test_prod_gauss_nd_mixed():
    norm_range = (-5, 4)
    low, high = norm_range
    test_values = np.random.uniform(low=low, high=high, size=(1000, 4))

    obs4d = ['a', 'b', 'c', 'd']
    test_values_data = Data.from_tensor(obs=obs4d, tensor=test_values)
    # prod_gauss_4d.update_integration_options(draws_per_dim=10)
    limits_4d = Space(limits=(((-5, ) * 4, ), ((4, ) * 4, )), obs=obs4d)
    prod_gauss_4d = product_gauss_4d()
    prod_gauss_4d.set_norm_range(limits_4d)
    probs = prod_gauss_4d.pdf(x=test_values_data, norm_range=limits_4d)
    gausses = create_gaussians()

    for gauss in (gausses):
        gauss.set_norm_range(norm_range)
    gauss1, gauss2, gauss3 = gausses
    prod_gauss_3d = product_gauss_3d()

    def probs_4d(values):
        true_prob = [gauss1.pdf(values[:, 3])]
        true_prob += [gauss2.pdf(values[:, 0])]
        true_prob += [gauss3.pdf(values[:, 2])]
        true_prob += [
            prod_gauss_3d.pdf(values[:, (0, 1, 2)],
                              norm_range=Space(limits=(((-5, ) * 3, ),
                                                       ((4, ) * 3, )),
                                               obs=['a', 'b', 'c']))
        ]
        return np.prod(true_prob, axis=0)

    true_unnormalized_probs = probs_4d(values=test_values)

    normalization_probs = limits_4d.area() * probs_4d(
        np.random.uniform(low=low, high=high, size=(40**4, 4)))
    true_probs = true_unnormalized_probs / tf.reduce_mean(
        input_tensor=normalization_probs)
    grad = tf.gradients(ys=probs, xs=list(prod_gauss_4d.get_dependents()))
    probs_np = zfit.run(probs)
    grad_np = zfit.run(grad)
    print("Gradients", grad_np)
    print(np.average(probs_np))
    true_probs_np = zfit.run(true_probs)
    assert np.average(probs_np * limits_4d.area()) == pytest.approx(
        1., rel=0.33)  # low n mc
    np.testing.assert_allclose(true_probs_np, probs_np, rtol=2e-2)
コード例 #6
0
ファイル: test_pdfs.py プロジェクト: zfit/zfit
def test_prod_gauss_nd_mixed():
    norm = (-5, 4)
    low, high = norm
    test_values = np.random.uniform(low=low, high=high, size=(1000, 4))

    obs4d = ["obs1", "obs2", "obs3", "obs4"]
    test_values_data = Data.from_tensor(obs=obs4d, tensor=test_values)
    limits_4d = Space(limits=(((-5, ) * 4, ), ((4, ) * 4, )), obs=obs4d)
    prod_gauss_4d = product_gauss_4d()
    prod_gauss_4d.set_norm_range(limits_4d)
    probs = prod_gauss_4d.pdf(x=test_values_data, norm=limits_4d)
    gausses = create_gaussians()

    for gauss in gausses:
        gauss.set_norm_range(norm)
    gauss1, gauss2, gauss3 = gausses
    prod_gauss_3d = product_gauss_3d()

    def probs_4d(values):
        true_prob = [gauss1.pdf(values[:, 3])]
        true_prob += [gauss2.pdf(values[:, 0])]
        true_prob += [gauss3.pdf(values[:, 2])]
        true_prob += [
            prod_gauss_3d.pdf(
                values[:, 0:3],
                norm=Space(limits=(((-5, ) * 3, ), ((4, ) * 3, )),
                           obs=["obs1", "obs2", "obs3"]),
            )
        ]
        return tf.math.reduce_prod(true_prob, axis=0)

    true_unnormalized_probs = probs_4d(values=test_values)

    normalization_probs = limits_4d.area() * probs_4d(
        tf.random.uniform(minval=low, maxval=high, shape=(40**4, 4)))
    true_probs = true_unnormalized_probs / tf.reduce_mean(
        input_tensor=normalization_probs)
    probs_np = probs.numpy()
    true_probs_np = true_probs.numpy()
    assert np.average(probs_np * limits_4d.area()) == pytest.approx(
        1.0, rel=0.33)  # low n mc
    np.testing.assert_allclose(true_probs_np, probs_np, rtol=2e-2)