def test_not_allowed(): param1 = Parameter("param1", 1.0) param2 = Parameter("param2", 2.0) param3 = Parameter("param3", 3.0, floating=False) def func1_pure(self, x): return param1 * x def func2_pure(self, x): return param2 * x + param3 func1 = SimpleFuncV1(func=func1_pure, obs=obs1, p1=param1) pdf1 = SimplePDF(func=lambda self, x: x * param1, obs=obs1) pdf2 = SimplePDF(func=lambda self, x: x * param2, obs=obs1) with pytest.raises(BreakingAPIChangeError): pdf1 + pdf2 ext_pdf1 = pdf1.create_extended(param1) with pytest.raises(BreakingAPIChangeError): ext_pdf1 + pdf2 with pytest.raises(BreakingAPIChangeError): param2 * pdf1 with pytest.raises(NotImplementedError): param1 + func1 with pytest.raises(NotImplementedError): func1 + param1 with pytest.raises(ModelIncompatibleError): func1 * pdf2 with pytest.raises(ModelIncompatibleError): pdf1 * func1
def test_projection_pdf(): return # HACK(Mayou36) check again on projection and dimensions. Why was there an expand_dims? # x = zfit.Space("x", limits=(-5, 5)) # y = zfit.Space("y", limits=(-5, 5)) # gauss_x = Gauss(mu=mu, sigma=sigma, obs=x, name="gauss_x") # gauss_y = Gauss(mu=mu, sigma=sigma, obs=y, name="gauss_x") # gauss_xy = ProductPDF([gauss_x, gauss_y]) x = zfit.Space("x", limits=(-1, 1)) y = zfit.Space("y", limits=(-0.1, 1)) gauss_x = Gauss(mu=mu, sigma=sigma, obs=x, name="gauss_x") gauss_y = Gauss(mu=mu, sigma=sigma, obs=y, name="gauss_x") # gauss_xy = ProductPDF([gauss_x, gauss_y]) import tensorflow as tf def correlated_func(self, x): x, y = x.unstack_x() value = 1 / (tf.abs(x - y) + 0.1) ** 2 return value gauss_xy = SimplePDF(func=correlated_func, obs=x * y) assert gauss_xy.create_projection_pdf(limits_to_integrate=y).norm_range == x proj_pdf = gauss_xy.create_projection_pdf(limits_to_integrate=y) test_values = np.array([-0.95603563, -0.84636306, -0.83895759, 2.62608006, 1.02336499, -0.99631608, -1.22185623, 0.83838586, 2.77894762, -2.48259488, 1.5440374, 0.1109899, 0.20873491, -2.45271623, 2.04510553, 0.31566277, -1.55696965, 0.36304538, 0.77765786, 3.92630088]) true_probs = np.array([0.0198562, 0.02369336, 0.02399382, 0.00799939, 0.2583654, 0.01868723, 0.01375734, 0.53971816, 0.00697152, 0.00438797, 0.03473656, 0.55963737, 0.58296759, 0.00447878, 0.01517764, 0.59553535, 0.00943439, 0.59838703, 0.56315399, 0.00312496]) probs = proj_pdf.pdf(x=test_values) probs = zfit.run(probs) np.testing.assert_allclose(probs, true_probs, rtol=1e-5) # MC normalization
def test_not_allowed(): param1 = Parameter('param1', 1.) param2 = Parameter('param2', 2.) param3 = Parameter('param3', 3., floating=False) param4 = Parameter('param4', 4.) def func1_pure(self, x): return param1 * x def func2_pure(self, x): return param2 * x + param3 func1 = SimpleFunc(func=func1_pure, obs=obs1, p1=param1) func2 = SimpleFunc(func=func2_pure, obs=obs1, p2=param2, p3=param3) pdf1 = SimplePDF(func=lambda self, x: x * param1, obs=obs1) pdf2 = SimplePDF(func=lambda self, x: x * param2, obs=obs1) with pytest.raises(ModelIncompatibleError): pdf1 + pdf2 with pytest.raises(NotImplementedError): param1 + func1 with pytest.raises(NotImplementedError): func1 + param1 with pytest.raises(ModelIncompatibleError): func1 * pdf2 with pytest.raises(ModelIncompatibleError): pdf1 * func1
def test_implicit_extended(): # return # TODO(Mayou36): deps: impl_copy, param1 = Parameter('param1', 12.) yield1 = Parameter('yield1', 21.) param2 = Parameter('param2', 13., floating=False) yield2 = Parameter('yield2', 31., floating=False) pdf1 = SimplePDF(func=lambda self, x: x * param1, obs=obs1) pdf2 = SimplePDF(func=lambda self, x: x * param2, obs=obs1) extended_pdf = yield1 * pdf1 + yield2 * pdf2 with pytest.raises(ModelIncompatibleError): true_extended_pdf = SumPDF(pdfs=[pdf1, pdf2], obs=obs1) assert isinstance(extended_pdf, SumPDF) assert extended_pdf.is_extended
def test_projection_pdf(test_values): import numpy as np import zfit x = zfit.Space("x", limits=(-1, 1)) y = zfit.Space("y", limits=(-1, 1)) def correlated_func(self, x): x, y = x.unstack_x() value = (((x - y**3)**2) + 0.1) return value def correlated_func_integrate_x(y, limits): lower, upper = limits.rect_limits def integ(x, y): return 0.333333333333333 * x**3 - 1.0 * x**2 * y**3 + x * ( 1.0 * y**6 + 0.1) return integ(y, upper) - integ(y, lower) def correlated_func_integrate_y(x, limits): lower, upper = limits.rect_limits def integ(x, y): return -0.5 * x * y**4 + 0.142857142857143 * y**7 + y * ( 1.0 * x**2 + 0.1) return (integ(x, upper) - integ(x, lower))[0] obs = x * y from zfit.models.special import SimplePDF gauss_xy = SimplePDF(func=correlated_func, obs=obs) assert gauss_xy.create_projection_pdf( limits_to_integrate=y).norm_range == x proj_pdf = gauss_xy.create_projection_pdf(limits_to_integrate=y) test_values = np.array([ -0.95603563, -0.84636306, -0.83895759, 2.62608006, 1.02336499, -0.99631608, -1.22185623, 0.83838586, 2.77894762, -2.48259488, 1.5440374, 0.1109899, 0.20873491, -2.45271623, 2.04510553, 0.31566277, -1.55696965, 0.36304538, 0.77765786, 3.92630088 ]) true_probs = correlated_func_integrate_y( test_values, y) / gauss_xy.integrate(limits=obs, norm_range=False) probs = proj_pdf.pdf(x=test_values) probs = probs.numpy() np.testing.assert_allclose(probs, true_probs, rtol=1e-3) # MC normalization
def test_param_pdf(): # return # TODO(Mayou36): deps: impl_copy, param1 = Parameter('param1', 12.) param2 = Parameter('param2', 22.) yield1 = Parameter('yield1', 21.) yield2 = Parameter('yield2', 22.) pdf1 = SimplePDF(func=lambda self, x: x * param1, obs=obs1) pdf2 = SimplePDF(func=lambda self, x: x * param2, obs=obs1) assert not pdf1.is_extended extended_pdf = yield1 * pdf1 assert extended_pdf.is_extended with pytest.raises(ModelIncompatibleError): _ = pdf2 * yield2 with pytest.raises(AlreadyExtendedPDFError): _ = yield2 * extended_pdf
def test_implicit_sumpdf(): # return # TODO(Mayou36): deps: impl_copy, (mostly for Simple{PDF,Func}) # tf.reset_default_graph() norm_range = (-5.7, 13.6) param1 = Parameter('param13s', 1.1) frac1 = 0.11 frac1_param = Parameter('frac13s', frac1) frac2 = 0.56 frac2_param = Parameter('frac23s', frac2) frac3 = 1 - frac1 - frac2 # -frac1_param -frac2_param param2 = Parameter('param23s', 1.5, floating=False) param3 = Parameter('param33s', 0.4, floating=False) pdf1 = SimplePDF(func=lambda self, x: x * param1**2, obs=obs1) pdf2 = SimplePDF(func=lambda self, x: x * param2, obs=obs1) pdf3 = SimplePDF(func=lambda self, x: x * 2 + param3, obs=obs1) # sugar 1 # sum_pdf = frac1_param * pdf1 + frac2_param * pdf2 + pdf3 # TODO(Mayou36): deps, correct copy sum_pdf = zfit.pdf.SumPDF(pdfs=[pdf1, pdf2, pdf3], fracs=[frac1_param, frac2_param]) true_values = pdf1.pdf(rnd_test_values, norm_range=norm_range) true_values *= frac1_param true_values += pdf2.pdf(rnd_test_values, norm_range=norm_range) * frac2_param true_values += pdf3.pdf(rnd_test_values, norm_range=norm_range) * ztf.constant(frac3) assert isinstance(sum_pdf, SumPDF) assert not sum_pdf.is_extended assert zfit.run(sum(sum_pdf.fracs)) == 1. true_values = zfit.run(true_values) test_values = zfit.run(sum_pdf.pdf(rnd_test_values, norm_range=norm_range)) np.testing.assert_allclose(true_values, test_values, rtol=5e-2) # it's MC normalized # sugar 2 sum_pdf2_part1 = frac1 * pdf1 + frac2 * pdf3