Example #1
0
def run_mcmc(meas, x, nsamples, covm=None, scales=None):
    """
    Sample the likelihood space with a Markov Chain Monte Carlo.

    :param meas: TemplateMeasurement
        measurement whose spectrum likelihood space is to be probe
    :param x: [float]
        parameter values where to start the chain
    :param covm: [[float]]
        covariance matrix values if sampling transformed space
    :param scales: [float]
        parameter scales if not sampling transformed space
    :return: [float], [float], [float], pymcmc.MCMC
        posterior mean, lower CI, upper CI for each parameter, and the MCMC
        object used for sampling
    """
    mcmc = MCMC(meas.spec.npars)
    mcmc.set_values(x)

    if covm is not None and scales is None:
        mcmc.set_covm(covm)
    elif scales is not None:
        mcmc.set_scales(scales)
    else:
        raise ValueError("Must provide covariance OR scales")

    mcmc.rescale = 2  # good starting point
    mcmc.learn_scale(meas.spec.ll, 1000)

    mcmc.run(meas.spec.ll, nsamples)

    mean = list()
    mean_down = list()
    mean_up = list()

    for ipar in range(meas.spec.npars):
        mean.append(np.mean(mcmc.data[:, ipar]))
        low, high, _, _ = npinterval.interval(mcmc.data[:, ipar], 0.6827)
        mean_down.append(low-mean[-1])
        mean_up.append(high-mean[-1])

    return mean, mean_down, mean_up, mcmc
Example #2
0
def probe_results(scales, ntrials, ndims, mode):
    """
    Probe a grid of results at various scales.

    :param scales: iterable
        rescale values at which to run MCMCs
    :param ntrials: int
        number of trials at each scale
    :param ndims: int
        dimension of space to probe
    :param mode: str
        {var, over, pca}
        var: scale by the known variance of each parameter
        over: over-estimate the variance of each parameter
        pca: tranform to the PCA space
    """
    if mode not in {'var', 'over', 'pca'}:
        raise ValueError("Unknown probe mode: %s" % mode)

    results = list()

    for scale in scales:
        for _ in range(ntrials):
            norm = MultiNorm(ndims)
            mcmc = MCMC(norm._ndims)
            mcmc.verbose = False
            mcmc.rescale = scale

            if mode == 'var':
                # Use the know variance of each paramete as the proposal scale
                mcmc.set_scales(norm._sigs)

            elif mode == 'over':
                # Overestimate the variances by up to 50% (typical)
                mcmc.set_scales(
                    norm._sigs * 
                    (np.random.rand(norm._ndims)/2.+1))

            elif mode == 'pca':
                # Transform the proposal to a space of independent variables
                mcmc.set_covm(norm._cov)

            mcmc.run(norm.loglikelihood, 10000)
            data = mcmc.data[0::1]
            results.append((scale, performance(norm, data), mcmc.getrate()))

            print('%.1f: %.3f, %.2f' % results[-1])

    results = np.array(results)

    plt.xlabel(r'scale')
    plt.ylabel(r'$\langle |\Delta\sigma| / \sigma \rangle$')
    plt.plot(results[:, 0], results[:, 1], 'o')
    plt.savefig('%d_%s_scale.pdf' % (ndims, mode), format='pdf')
    plt.clf()

    plt.xlabel(r'acceptance rate')
    plt.ylabel(r'$\langle |\Delta\sigma| / \sigma \rangle$')
    plt.plot(results[:, 2], results[:, 1], 'o')
    plt.savefig('%d_%s_accept.pdf' % (ndims, mode), format='pdf')
    plt.clf()
Example #3
0
def run_mcmc(meas, x, nsamples, covm=None, scales=None):
    """
    Sample the likelihood space with a Markov Chain Monte Carlo.

    :param meas: TemplateMeasurement
        measurement whose spectrum likelihood space is to be probe
    :param x: [float]
        parameter values where to start the chain
    :param covm: [[float]]
        covariance matrix values if sampling transformed space
    :param scales: [float]
        parameter scales if not sampling transformed space
    :return: [float], [float], [float], pymcmc.MCMC
        posterior mean, lower CI, upper CI for each parameter, and the MCMC
        object used for sampling
    """
    mcmc = MCMC(meas.spec.npars)
    mcmc.set_values(x)

    if covm is not None and scales is None:
        mcmc.set_covm(covm)
    elif scales is not None:
        mcmc.set_scales(scales)
    else:
        raise ValueError("Must provide covariance OR scales")

    mcmc.rescale = 2  # good starting point
    mcmc.learn_scale(meas.spec.ll, 1000)

    mcmc.run(meas.spec.ll, nsamples)

    mean = list()
    mean_down = list()
    mean_up = list()

    for ipar in range(meas.spec.npars):
        mean.append(np.mean(mcmc.data[:, ipar]))
        low, high, _, _ = npinterval.interval(mcmc.data[:, ipar], 0.6827)
        mean_down.append(low - mean[-1])
        mean_up.append(high - mean[-1])

    return mean, mean_down, mean_up, mcmc
Example #4
0
    plt.ylabel(r'$\langle |\Delta\sigma| / \sigma \rangle$')
    plt.plot(results[:, 2], results[:, 1], 'o')
    plt.savefig('%d_%s_accept.pdf' % (ndims, mode), format='pdf')
    plt.clf()


# Set some formatting options for numpy and pyplot
np.set_printoptions(precision=2, suppress=True)
plt.rc('text', usetex=True)
plt.rc('font', family='serif')

rescale = 2.5

# 1D simple calse
norm = MultiNorm(1)
mcmc = MCMC(norm._ndims)
mcmc.rescale = rescale
mcmc.set_scales(norm._sigs)
mcmc.run(norm.loglikelihood, 10000)
data = mcmc.data[0::1]
draw_axis(norm, 0, data)
plt.savefig('axis_1D.pdf', format='pdf')
plt.clf()
trace_axis(norm, 0, data[:1000])
plt.savefig('trace_1D.pdf', format='pdf')
plt.clf()

# 1D asymmetric case
norm = MultiNorm(1, mus=[0], sigs=[1], cut=(float('-inf'), 0))
mcmc = MCMC(norm._ndims)
mcmc.rescale = rescale