def test_unbinned_simultaneous_nll(): test_values = tf.constant(test_values_np) test_values = zfit.Data.from_tensor(obs=obs1, tensor=test_values) test_values2 = tf.constant(test_values_np2) test_values2 = zfit.Data.from_tensor(obs=obs1, tensor=test_values2) gaussian1, mu1, sigma1 = create_gauss1() gaussian2, mu2, sigma2 = create_gauss2() gaussian2 = gaussian2.create_extended(zfit.Parameter('yield_gauss2', 5)) nll = zfit.loss.UnbinnedNLL( model=[gaussian1, gaussian2], data=[test_values, test_values2], ) minimizer = Minuit(tolerance=1e-5) status = minimizer.minimize(loss=nll, params=[mu1, sigma1, mu2, sigma2]) params = status.params assert set(nll.get_params()) == {mu1, mu2, sigma1, sigma2} assert params[mu1]['value'] == pytest.approx(np.mean(test_values_np), rel=0.007) assert params[mu2]['value'] == pytest.approx(np.mean(test_values_np2), rel=0.007) assert params[sigma1]['value'] == pytest.approx(np.std(test_values_np), rel=0.007) assert params[sigma2]['value'] == pytest.approx(np.std(test_values_np2), rel=0.007)
def from_minuit( cls, loss: ZfitLoss, params: Iterable[ZfitParameter], result: iminuit.util.MigradResult, minimizer: Union[ZfitMinimizer, iminuit.Minuit]) -> 'FitResult': """Create a `FitResult` from a :py:class:~`iminuit.util.MigradResult` returned by :py:meth:`iminuit.Minuit.migrad` and a iminuit :py:class:~`iminuit.Minuit` instance with the corresponding zfit objects. Args: loss: zfit Loss that was minimized. params: Iterable of the zfit parameters that were floating during the minimization. result: Return value of the iminuit migrad command. minimizer: Instance of the iminuit Minuit that was used to minimize the loss. Returns: A `FitResult` as if zfit Minuit was used. """ from .minimizer_minuit import Minuit if not isinstance(minimizer, Minuit): if isinstance(minimizer, iminuit.Minuit): minimizer_new = Minuit() minimizer_new._minuit_minimizer = minimizer minimizer = minimizer_new else: raise ValueError( f"Minimizer {minimizer} not supported. Use `Minuit` from zfit or from iminuit." ) params_result = [p_dict for p_dict in result[1]] result_vals = [res["value"] for res in params_result] set_values(params, values=result_vals) info = { 'n_eval': result[0]['nfcn'], 'n_iter': result[0]['ncalls'], # 'grad': result['jac'], # 'message': result['message'], 'original': result[0] } edm = result[0]['edm'] fmin = result[0]['fval'] status = -999 converged = result[0]['is_valid'] params = OrderedDict( (p, res['value']) for p, res in zip(params, params_result)) return cls(params=params, edm=edm, fmin=fmin, info=info, loss=loss, status=status, converged=converged, minimizer=minimizer)
def test_extended_unbinned_nll(): test_values = ztf.constant(test_values_np) test_values = zfit.Data.from_tensor(obs=obs1, tensor=test_values) gaussian3, mu3, sigma3, yield3 = create_gauss3ext() nll_object = zfit.loss.ExtendedUnbinnedNLL(model=gaussian3, data=test_values, fit_range=(-20, 20)) minimizer = Minuit() status = minimizer.minimize(loss=nll_object) params = status.params assert params[mu3]['value'] == pytest.approx(np.mean(test_values_np), rel=0.005) assert params[sigma3]['value'] == pytest.approx(np.std(test_values_np), rel=0.005) assert params[yield3]['value'] == pytest.approx(yield_true, rel=0.005)
def test_unbinned_simultaneous_nll(): test_values = tf.constant(test_values_np) test_values = zfit.Data.from_tensor(obs=obs1, tensor=test_values) test_values2 = tf.constant(test_values_np2) test_values2 = zfit.Data.from_tensor(obs=obs1, tensor=test_values2) gaussian1, mu1, sigma1 = create_gauss1() gaussian2, mu2, sigma2 = create_gauss2() nll_object = zfit.loss.UnbinnedNLL(model=[gaussian1, gaussian2], data=[test_values, test_values2], fit_range=[(-np.infty, np.infty), (-np.infty, np.infty)] ) minimizer = Minuit() status = minimizer.minimize(loss=nll_object, params=[mu1, sigma1, mu2, sigma2]) params = status.params assert params[mu1]['value'] == pytest.approx(np.mean(test_values_np), rel=0.005) assert params[mu2]['value'] == pytest.approx(np.mean(test_values_np2), rel=0.005) assert params[sigma1]['value'] == pytest.approx(np.std(test_values_np), rel=0.005) assert params[sigma2]['value'] == pytest.approx(np.std(test_values_np2), rel=0.005)
def test_unbinned_nll(weights, sigma): gaussian1, mu1, sigma1 = create_gauss1() gaussian2, mu2, sigma2 = create_gauss2() test_values = tf.constant(test_values_np) test_values = zfit.Data.from_tensor(obs=obs1, tensor=test_values, weights=weights) nll_object = zfit.loss.UnbinnedNLL(model=gaussian1, data=test_values) minimizer = Minuit(tolerance=1e-5) status = minimizer.minimize(loss=nll_object, params=[mu1, sigma1]) params = status.params rel_error = 0.005 if weights is None else 0.1 # more fluctuating with weights assert params[mu1]['value'] == pytest.approx(np.mean(test_values_np), rel=rel_error) assert params[sigma1]['value'] == pytest.approx(np.std(test_values_np), rel=rel_error) constraints = zfit.constraint.nll_gaussian( params=[mu2, sigma2], observation=[mu_constr[0], sigma_constr[0]], uncertainty=sigma()) nll_object = UnbinnedNLL(model=gaussian2, data=test_values, constraints=constraints) minimizer = Minuit(tolerance=1e-4) status = minimizer.minimize(loss=nll_object, params=[mu2, sigma2]) params = status.params if weights is None: assert params[mu2]['value'] > np.average(test_values_np, weights=weights) assert params[sigma2]['value'] < np.std(test_values_np)