def test_add(): param1 = zfit.Parameter("param1", 1.) param2 = zfit.Parameter("param2", 2.) param3 = zfit.Parameter("param3", 2.) pdfs = [0] * 4 pdfs[0] = Gauss(param1, 4, obs=obs1) pdfs[1] = Gauss(param2, 5, obs=obs1) pdfs[2] = Gauss(3, 6, obs=obs1) pdfs[3] = Gauss(4, 7, obs=obs1) datas = [0] * 4 datas[0] = zfit.Data.from_tensor(obs=obs1, tensor=z.constant(1.)) datas[1] = zfit.Data.from_tensor(obs=obs1, tensor=z.constant(2.)) datas[2] = zfit.Data.from_tensor(obs=obs1, tensor=z.constant(3.)) datas[3] = zfit.Data.from_tensor(obs=obs1, tensor=z.constant(4.)) ranges = [0] * 4 ranges[0] = (1, 4) ranges[1] = Space(limits=(2, 5), obs=obs1) ranges[2] = Space(limits=(3, 6), obs=obs1) ranges[3] = Space(limits=(4, 7), obs=obs1) constraint1 = zfit.constraint.nll_gaussian(params=param1, observation=1., uncertainty=0.5) constraint2 = zfit.constraint.nll_gaussian(params=param3, observation=2., uncertainty=0.25) merged_contraints = [constraint1, constraint2] nll1 = UnbinnedNLL(model=pdfs[0], data=datas[0], fit_range=ranges[0], constraints=constraint1) nll2 = UnbinnedNLL(model=pdfs[1], data=datas[1], fit_range=ranges[1], constraints=constraint2) nll3 = UnbinnedNLL(model=[pdfs[2], pdfs[3]], data=[datas[2], datas[3]], fit_range=[ranges[2], ranges[3]]) simult_nll = nll1 + nll2 + nll3 assert simult_nll.model == pdfs assert simult_nll.data == datas ranges[0] = Space(limits=ranges[0], obs='obs1', axes=(0,)) # for comparison, Space can only compare with Space ranges[1].coords._axes = (0,) ranges[2].coords._axes = (0,) ranges[3].coords._axes = (0,) assert simult_nll.fit_range == ranges def eval_constraint(constraints): return z.reduce_sum([c.value() for c in constraints]).numpy() assert eval_constraint(simult_nll.constraints) == eval_constraint(merged_contraints) assert set(simult_nll.get_params()) == {param1, param2, param3}
def test_gradients(chunksize): from numdifftools import Gradient zfit.run.chunking.active = True zfit.run.chunking.max_n_points = chunksize initial1 = 1.0 initial2 = 2 param1 = zfit.Parameter("param1", initial1) param2 = zfit.Parameter("param2", initial2) gauss1 = Gauss(param1, 4, obs=obs1) gauss1.set_norm_range((-5, 5)) gauss2 = Gauss(param2, 5, obs=obs1) gauss2.set_norm_range((-5, 5)) data1 = zfit.Data.from_tensor(obs=obs1, tensor=z.constant(1.0, shape=(100, ))) data1.set_data_range((-5, 5)) data2 = zfit.Data.from_tensor(obs=obs1, tensor=z.constant(1.0, shape=(100, ))) data2.set_data_range((-5, 5)) nll = UnbinnedNLL(model=[gauss1, gauss2], data=[data1, data2]) def loss_func(values): for val, param in zip(values, nll.get_cache_deps(only_floating=True)): param.set_value(val) return nll.value().numpy() # theoretical, numerical = tf.test.compute_gradient(loss_func, list(params)) gradient1 = nll.gradient(params=param1) gradient_func = Gradient(loss_func) # gradient_func = lambda *args, **kwargs: list(gradient_func_numpy(*args, **kwargs)) assert gradient1[0].numpy() == pytest.approx( gradient_func([param1.numpy()])) param1.set_value(initial1) param2.set_value(initial2) params = [param2, param1] gradient2 = nll.gradient(params=params) both_gradients_true = list( reversed(list(gradient_func([initial1, initial2 ])))) # because param2, then param1 assert [g.numpy() for g in gradient2] == pytest.approx(both_gradients_true) param1.set_value(initial1) param2.set_value(initial2) gradient3 = nll.gradient() assert frozenset(g.numpy() for g in gradient3) == pytest.approx( frozenset(both_gradients_true))
def create_gauss3ext(): mu, sigma, yield3 = create_params3() gaussian3 = Gauss(mu, sigma, obs=obs1, name="gaussian3") gaussian3 = gaussian3.create_extended(yield3) return gaussian3, mu, sigma, yield3
def create_gauss2(): mu, sigma = create_params2() return Gauss(mu, sigma, obs=obs1, name="gaussian2"), mu, sigma