def test_gaussian_constraint(): param_vals = [5, 6, 3] observed = [3, 6.1, 4.3] sigma = [1, 0.3, 0.7] true_val = true_gauss_constr_value(x=observed, mu=param_vals, sigma=sigma) assert true_val == true_gauss_constr_value(x=param_vals, mu=observed, sigma=sigma) params = [zfit.Parameter(f"Param{i}", val) for i, val in enumerate(param_vals)] constr = GaussianConstraint(params=params, observation=observed, uncertainty=sigma) constr_np = constr.value().numpy() assert constr_np == pytest.approx(true_val) assert constr.get_cache_deps() == set(params) param_vals[0] = 2 params[0].set_value(param_vals[0]) constr2_np = constr.value().numpy() constr2_newtensor_np = constr.value().numpy() assert constr2_newtensor_np == pytest.approx(constr2_np) true_val2 = true_gauss_constr_value(x=param_vals, mu=observed, sigma=sigma) assert constr2_np == pytest.approx(true_val2) print(true_val2) constr.observation[0].set_value(5) observed[0] = 5 print("x: ", param_vals, [p.numpy() for p in params]) print("mu: ", observed, [p.numpy() for p in constr.observation]) print("sigma: ", sigma, np.sqrt([p for p in np.diag(constr.covariance)])) true_val3 = true_gauss_constr_value(x=param_vals, mu=observed, sigma=sigma) constr3_np = constr.value().numpy() assert constr3_np == pytest.approx(true_val3)
def test_gaussian_constraint_orderbug2( ): # as raised in #162, failed before fixing param1 = zfit.Parameter("param1", 1500) param5 = zfit.Parameter("param2", 0.5) param2 = zfit.Parameter("param3", 1.0) param3 = zfit.Parameter("param4", 1.0) param4 = zfit.Parameter("param5", 1.0) constraint = { "params": [param1, param2, param3, param4, param5], "observation": [1500, 1.0, 1.0, 1.0, 0.5], "uncertainty": [0.05 * 1500, 0.001, 0.01, 0.1, 0.05 * 0.5], } constr1 = GaussianConstraint(**constraint) # param_vals = [1500, 1.0, 1.0, 1.0, 0.5] constraint["x"] = [m.numpy() for m in constraint["params"]] true_val = true_gauss_constr_value(x=constraint["x"], mu=constraint["observation"], sigma=constraint["uncertainty"]) value_tensor = constr1.value() constr_np = value_tensor.numpy() assert constr_np == pytest.approx(true_val) assert true_val < 1000 assert true_val == pytest.approx( -8.592, abs=0.1) # if failing, change value. Hardcoded for additional layer
def test_gaussian_constraint_matrix(): param1 = zfit.Parameter("Param1", 5) param2 = zfit.Parameter("Param2", 6) params = [param1, param2] mu = [3., 6.1] sigma = np.array([[1, 0.3], [0.3, 0.5]]) constr = GaussianConstraint(params=params, mu=mu, sigma=sigma) constr_np = zfit.run(constr.value()) assert constr_np == pytest.approx(3.989638) assert constr.get_dependents() == set(params)
def test_gaussian_constraint(): param_vals = [5, 6, 3] mu = [3, 6.1, 4.3] sigma = [1, 0.3, 0.7] true_val = true_gauss_constr_value(param_vals, mu, sigma) params = [zfit.Parameter(f"Param{i}", val) for i, val in enumerate(param_vals)] constr = GaussianConstraint(params=params, mu=mu, sigma=sigma) constr_np = constr.value().numpy() assert constr_np == pytest.approx(true_val) assert constr.get_dependents() == set(params) param_vals[0] = 2 params[0].set_value(param_vals[0]) constr2_np = constr.value().numpy() constr2_newtensor_np = constr.value().numpy() assert constr2_newtensor_np == pytest.approx(constr2_np) true_val2 = true_gauss_constr_value(param_vals, mu, sigma) assert constr2_np == pytest.approx(true_val2)
def test_gaussian_constraint_orderbug(): # as raised in #162 observed = [1500, 1.0, 1.0, 1.0, 0.5] params = [zfit.Parameter(f"param{i}", val) for i, val in enumerate(observed)] sigma = [0.05 * 1500, 0.001, 0.01, 0.1, 0.05 * 0.5] true_val = true_gauss_constr_value(x=observed, mu=observed, sigma=sigma) constr1 = GaussianConstraint(params=params, observation=observed, uncertainty=sigma) value_tensor = constr1.value() constr_np = value_tensor.numpy() assert constr_np == pytest.approx(true_val) assert true_val < 10000
def test_gaussian_constraint_matrix(): param1 = zfit.Parameter("Param1", 5) param2 = zfit.Parameter("Param2", 6) params = [param1, param2] observed = [3., 6.1] sigma = np.array([[1, 0.3], [0.3, 0.5]]) trueval = true_multinormal_constr_value(x=zfit.run(params)[0], mean=observed, cov=sigma) constr = GaussianConstraint(params=params, observation=observed, uncertainty=sigma) constr_np = zfit.run(constr.value()) assert constr_np == pytest.approx(trueval) #assert constr_np == pytest.approx(3.989638) assert constr.get_cache_deps() == set(params)