예제 #1
0
def band_plot(log_likelihood, x_min, x_max, y_min, y_max,
              n_sigma=1, steps=20, **kwargs):
    r"""This is an alias for `likelihood_contour` which is present for
    backward compatibility."""
    warnings.warn("The `band_plot` function has been replaced "
                  "by `likelihood_contour` (or "
                  "`likelihood_contour_data` in conjunction with `contour`) "
                  "and might be removed in the future. "
                  "Please update your code.", FutureWarning)
    valid_args = inspect.signature(likelihood_contour_data).parameters.keys()
    data_kwargs = {k:v for k,v in kwargs.items() if k in valid_args}
    if 'pre_calculated_z' not in kwargs:
        contour_kwargs = likelihood_contour_data(log_likelihood,
                      x_min, x_max, y_min, y_max,
                      n_sigma, steps, **data_kwargs)
    else:
        contour_kwargs = {}
        nx, ny = kwargs['pre_calculated_z'].shape
        _x = np.linspace(x_min, x_max, nx)
        _y = np.linspace(y_min, y_max, ny)
        x, y = np.meshgrid(_x, _y)
        contour_kwargs['x'] = x
        contour_kwargs['y'] = y
        contour_kwargs['z'] = kwargs['pre_calculated_z']
        if isinstance(n_sigma, Number):
            contour_kwargs['levels'] = [delta_chi2(n_sigma, dof=2)]
        else:
            contour_kwargs['levels'] = [delta_chi2(n, dof=2) for n in n_sigma]
    valid_args = inspect.signature(contour).parameters.keys()
    contour_kwargs.update({k:v for k,v in kwargs.items() if k in valid_args})
    contour(**contour_kwargs)
    return contour_kwargs['x'], contour_kwargs['y'], contour_kwargs['z']
예제 #2
0
def likelihood_contour_data(log_likelihood,
                            x_min,
                            x_max,
                            y_min,
                            y_max,
                            n_sigma=1,
                            steps=20,
                            threads=1,
                            pool=None):
    r"""Generate data required to plot coloured confidence contours (or bands)
    given a log likelihood function.

    Parameters:

    - `log_likelihood`: function returning the logarithm of the likelihood.
      Can e.g. be the method of the same name of a FastFit instance.
    - `x_min`, `x_max`, `y_min`, `y_max`: data boundaries
    - `n_sigma`: plot confidence level corresponding to this number of standard
      deviations. Either a number (defaults to 1) or a tuple to plot several
      contours.
    - `steps`: number of grid steps in each dimension (total computing time is
      this number squared times the computing time of one `log_likelihood` call!)
    - `threads`: number of threads, defaults to 1. If greater than one,
      computation of z values will be done in parallel.
    - `pool`: an instance of `multiprocessing.Pool` (or a compatible
    implementation, e.g. from `multiprocess` or `schwimmbad`). Overrides the
    `threads` argument.
    """
    _x = np.linspace(x_min, x_max, steps)
    _y = np.linspace(y_min, y_max, steps)
    x, y = np.meshgrid(_x, _y)
    if threads == 1:

        @np.vectorize
        def chi2_vect(x, y):  # needed for evaluation on meshgrid
            return -2 * log_likelihood([x, y])

        z = chi2_vect(x, y)
    else:
        xy = np.array([x, y]).reshape(2, steps**2).T
        pool = pool or Pool(threads)
        try:
            z = -2 * np.array(pool.map(log_likelihood, xy)).reshape(
                (steps, steps))
        except PicklingError:
            pool.close()
            raise PicklingError(
                "When using more than 1 thread, the "
                "log_likelihood function must be picklable; "
                "in particular, you cannot use lambda expressions.")
        pool.close()
        pool.join()
    z = z - np.min(z)  # subtract the best fit point (on the grid)

    # get the correct values for 2D confidence/credibility contours for n sigma
    if isinstance(n_sigma, Number):
        levels = [delta_chi2(n_sigma, dof=2)]
    else:
        levels = [delta_chi2(n, dof=2) for n in n_sigma]
    return {'x': x, 'y': y, 'z': z, 'levels': levels}
예제 #3
0
def band_plot(log_likelihood, x_min, x_max, y_min, y_max,
              n_sigma=1, steps=20, **kwargs):
    r"""This is an alias for `likelihood_contour` which is present for
    backward compatibility."""
    warnings.warn("The `band_plot` function has been replaced "
                  "by `likelihood_contour` (or "
                  "`likelihood_contour_data` in conjunction with `contour`) "
                  "and might be removed in the future. "
                  "Please update your code.", FutureWarning)
    valid_args = inspect.signature(likelihood_contour_data).parameters.keys()
    data_kwargs = {k:v for k,v in kwargs.items() if k in valid_args}
    if 'pre_calculated_z' not in kwargs:
        contour_kwargs = likelihood_contour_data(log_likelihood,
                      x_min, x_max, y_min, y_max,
                      n_sigma, steps, **data_kwargs)
    else:
        contour_kwargs = {}
        nx, ny = kwargs['pre_calculated_z'].shape
        _x = np.linspace(x_min, x_max, nx)
        _y = np.linspace(y_min, y_max, ny)
        x, y = np.meshgrid(_x, _y)
        contour_kwargs['x'] = x
        contour_kwargs['y'] = y
        contour_kwargs['z'] = kwargs['pre_calculated_z']
        if isinstance(n_sigma, Number):
            contour_kwargs['levels'] = [delta_chi2(n_sigma, dof=2)]
        else:
            contour_kwargs['levels'] = [delta_chi2(n, dof=2) for n in n_sigma]
    valid_args = inspect.signature(contour).parameters.keys()
    contour_kwargs.update({k:v for k,v in kwargs.items() if k in valid_args})
    contour(**contour_kwargs)
    return contour_kwargs['x'], contour_kwargs['y'], contour_kwargs['z']
예제 #4
0
def likelihood_contour_data(log_likelihood, x_min, x_max, y_min, y_max,
              n_sigma=1, steps=20, threads=1, pool=None):
    r"""Generate data required to plot coloured confidence contours (or bands)
    given a log likelihood function.

    Parameters:

    - `log_likelihood`: function returning the logarithm of the likelihood.
      Can e.g. be the method of the same name of a FastFit instance.
    - `x_min`, `x_max`, `y_min`, `y_max`: data boundaries
    - `n_sigma`: plot confidence level corresponding to this number of standard
      deviations. Either a number (defaults to 1) or a tuple to plot several
      contours.
    - `steps`: number of grid steps in each dimension (total computing time is
      this number squared times the computing time of one `log_likelihood` call!)
    - `threads`: number of threads, defaults to 1. If greater than one,
      computation of z values will be done in parallel.
    - `pool`: an instance of `multiprocessing.Pool` (or a compatible
    implementation, e.g. from `multiprocess` or `schwimmbad`). Overrides the
    `threads` argument.
    """
    _x = np.linspace(x_min, x_max, steps)
    _y = np.linspace(y_min, y_max, steps)
    x, y = np.meshgrid(_x, _y)
    if threads == 1:
        @np.vectorize
        def chi2_vect(x, y): # needed for evaluation on meshgrid
            return -2*log_likelihood([x,y])
        z = chi2_vect(x, y)
    else:
        xy = np.array([x, y]).reshape(2, steps**2).T
        pool = pool or Pool(threads)
        try:
            z = -2*np.array(pool.map(log_likelihood, xy )).reshape((steps, steps))
        except PicklingError:
            pool.close()
            raise PicklingError("When using more than 1 thread, the "
                                "log_likelihood function must be picklable; "
                                "in particular, you cannot use lambda expressions.")
        pool.close()
        pool.join()
    z = z - np.min(z) # subtract the best fit point (on the grid)

    # get the correct values for 2D confidence/credibility contours for n sigma
    if isinstance(n_sigma, Number):
        levels = [delta_chi2(n_sigma, dof=2)]
    else:
        levels = [delta_chi2(n, dof=2) for n in n_sigma]
    return {'x': x, 'y': y, 'z': z, 'levels': levels}
예제 #5
0
def plot(fin, fout, x0=None):
    '''
	Read data from file and plot it in flavio-style
	'''
    f = open(fin, 'rt')
    _x = []
    _y = []
    for l in f.readlines():
        ls = l.split('\t')
        _x.append(float(ls[1]))
        _y.append(float(ls[2]))
    f.close()
    stepx = float('Inf')
    stepy = float('Inf')
    minx = min(_x)
    miny = min(_y)
    maxx = max(_x)
    maxy = max(_y)
    for i in range(0, len(_x)):
        if _x[i] != minx:
            stepx = min(stepx, _x[i] - minx)
    for i in range(0, len(_y)):
        if _y[i] != miny:
            stepy = min(stepy, _y[i] - miny)
    x, y = np.meshgrid(np.arange(minx, maxx, stepx),
                       np.arange(miny, maxy, stepy))
    shape1, shape2 = x.shape
    f = open(fin, 'rt')
    i = 0
    zbs = np.zeros(x.shape)
    zDMs = np.zeros(x.shape)
    zACP = np.zeros(x.shape)
    zglob = np.zeros(x.shape)
    for l in f.readlines():
        i1 = i % shape1
        i2 = i // shape1
        i += 1
        ls = l.split('\t')
        zbs[i1, i2] = float(ls[-4])
        zACP[i1, i2] = float(ls[-2])
        zDMs[i1, i2] = float(ls[-3])
        zglob[i1, i2] = float(ls[-1])
    f.close()
    zbs = zbs - np.min(zbs)
    zDMs = zDMs - np.min(zDMs)
    zACP = zACP - np.min(zACP)
    zglob = zglob - np.min(zglob)
    levels = [delta_chi2(n, dof=2) for n in (1, 2)]
    plotbs = {
        'x': x,
        'y': y,
        'z': zbs,
        'levels': levels,
        'interpolation_factor': 5,
        'col': 0,
        'label': r'$b \to s \mu^+ \mu^-$'
    }
    plotDMs = {
        'x': x,
        'y': y,
        'z': zDMs,
        'levels': levels,
        'interpolation_factor': 5,
        'col': 1,
        'label': r'$\Delta B_s$'
    }
    plotACP = {
        'x': x,
        'y': y,
        'z': zACP,
        'levels': levels,
        'interpolation_factor': 5,
        'col': 2,
        'label': r'$A_{CP}^{\mathrm{mix}}$'
    }
    plotglob = {
        'x': x,
        'y': y,
        'z': zglob,
        'levels': levels,
        'interpolation_factor': 5,
        'col': 3,
        'label': 'Global'
    }
    fig = texfig.figure()
    #fig = plt.figure()
    plt.xlim([-0.15, 0.15])
    plt.ylim([-0.15, 0.15])
    flavio.plots.contour(**plotbs)
    flavio.plots.contour(**plotDMs)
    flavio.plots.contour(**plotACP)
    flavio.plots.contour(**plotglob)
    plt.axhline(0, c='k', lw=0.2)
    plt.axvline(0, c='k', lw=0.2)
    if x0 is not None:
        plt.plot(x0[0], x0[1], marker='x', c='k')
    plt.xlabel(r'$\mathrm{Re}\ y^{QL}_{32} y^{QL*}_{22}$')
    plt.ylabel(r'$\mathrm{Im}\ y^{QL}_{32} y^{QL*}_{22}$')
    #plt.xlabel(r'$\mathrm{Re}\ \lambda^Q_{23}$')
    #plt.ylabel(r'$\mathrm{Im}\ \lambda^Q_{23}$')
    plt.legend(loc=2, bbox_to_anchor=(1.05, 1))
    texfig.savefig(fout)