Example #1
0
    def best_fit(self, **kwargs):
        r"""Compute the best fit point in the space of fit parameters and Wilson
        coefficients.

        Keyword arguments will be passed to `scipy.optimize.minimize_scalar` in
        the case of a single fit variable and to `flavio.math.optimize.minimize_robust`
        in the case of multiple fit variables.

        Returns a dictionary with the following keys:

        - 'x': position of the best fit point
        - 'log_likelihood': logarithm of the likelihood at the best fit point
        """
        n_fit_p = len(self.fit_parameters)
        n_wc = len(self.fit_wc_names)
        if n_fit_p + n_wc == 1:
            def f(x):
                return -self.log_likelihood([x])
            opt = scipy.optimize.minimize_scalar(f, **kwargs)
        else:
            def f(x):
                return -self.log_likelihood(x)
            if 'x0' not in kwargs:
                x0 = np.zeros(n_fit_p + n_wc)
                if n_fit_p > 1:
                    x0[:n_fit_p] = self.get_central_fit_parameters
                opt = minimize_robust(f, x0, **kwargs)
            else:
                opt = minimize_robust(f, **kwargs)
        if not opt.success:
            raise ValueError("Optimization failed.")
        else:
            return {'x': opt.x, 'log_likelihood': -opt.fun}
Example #2
0
def _best_fit_point(initial_guess, gl, wc_fct, scale_fct, array_input,
                    methods):
    llh_name, x0 = initial_guess
    llh = (gl.fast_likelihoods.get(llh_name) or gl.likelihoods.get(llh_name)
           or gl.custom_likelihoods.get(llh_name)
           or (_global_llh(gl) if llh_name == 'global' else None))
    llh_sm = (0 if llh_name == 'global' else gl.log_likelihood_sm[llh_name])
    if array_input:

        def chi2(x):
            w = gl.get_wilson(wc_fct(x), scale_fct(x))
            gp = gl.parameter_point(w)
            try:
                res = -2 * (llh.log_likelihood(gp.par_dict_np, w, delta=True) -
                            llh_sm)
            except ValueError as e:
                if ('The extraction of CKM elements failed.' in str(e)
                        or 'math domain error' in str(e)):
                    res = np.inf
                else:
                    raise
            return res
    else:

        def chi2(x):
            w = gl.get_wilson(wc_fct(*x), scale_fct(*x))
            gp = gl.parameter_point(w)
            try:
                res = -2 * (llh.log_likelihood(gp.par_dict_np, w, delta=True) -
                            llh_sm)
            except ValueError as e:
                if ('The extraction of CKM elements failed.' in str(e)
                        or 'math domain error' in str(e)):
                    res = np.inf
                else:
                    raise
            return res

    try:
        res = minimize_robust(chi2, x0, methods=methods)
        if not res.success:
            x = [np.nan] * len(x0)
            z = np.nan
            warnings.warn(
                "Optimization failed during computation of best-fit point of {}: {}"
                ''.format(llh_name, res.message))
        else:
            x = res.x
            z = res.fun
    except Exception as e:
        x = [np.nan] * len(x0)
        z = np.nan
        warnings.warn('\nERROR during computation of best-fit point of {}:\n{}'
                      ''.format(llh_name, e))
    return (llh_name, {'coords_min': np.array(x), 'z_min': z})
Example #3
0
    def best_fit(self, **kwargs):
        r"""Compute the best fit point in the space of fit parameters and Wilson
        coefficients.

        Keyword arguments will be passed to `scipy.optimize.minimize_scalar` in
        the case of a single fit variable and to `flavio.math.optimize.minimize_robust`
        in the case of multiple fit variables.

        Returns a dictionary with the following keys:

        - 'x': position of the best fit point
        - 'log_likelihood': logarithm of the likelihood at the best fit point
        """
        n_fit_p = len(self.fit_parameters)
        n_wc = len(self.fit_wc_names)
        if n_fit_p + n_wc == 1:

            def f(x):
                return -self.log_likelihood([x])

            opt = scipy.optimize.minimize_scalar(f, **kwargs)
        else:

            def f(x):
                return -self.log_likelihood(x)

            if 'x0' not in kwargs:
                x0 = np.zeros(n_fit_p + n_wc)
                if n_fit_p > 1:
                    x0[:n_fit_p] = self.get_central_fit_parameters
                opt = minimize_robust(f, x0, **kwargs)
            else:
                opt = minimize_robust(f, **kwargs)
        if not opt.success:
            raise ValueError("Optimization failed.")
        else:
            return {'x': opt.x, 'log_likelihood': -opt.fun}