Exemplo n.º 1
0
def deriv(Q, R, dR=None, width=5, degree=2):
    from bumps.wsolve import wpolyfit
    d = numpy.empty_like(Q)
    if dR is None: dR = numpy.ones_like(Q)
    k = (width - 1) / 2
    p = wpolyfit(Q[:width], R[:width], dy=dR[:width], degree=degree)
    d[:k + 1] = p.der(Q[:k + 1])
    for i in range(k + 1, len(Q) - k - 1):
        idx = slice(i - k, i + k + 1)
        p = wpolyfit(Q[idx], R[idx], dy=dR[idx], degree=degree)
        d[i] = p.der(Q[i])
    p = wpolyfit(Q[-width:], R[-width:], dy=dR[-width:], degree=degree)
    d[-k - 1:] = p.der(Q[-k - 1:])
    return d
Exemplo n.º 2
0
def deriv(Q, R, dR=None, width=5, degree=2):
    from bumps.wsolve import wpolyfit
    d = np.empty_like(Q)
    if dR is None:
        dR = np.ones_like(Q)
    k = (width-1)/2
    p = wpolyfit(Q[:width], R[:width], dy=dR[:width], degree=degree)
    d[:k+1] = p.der(Q[:k+1])
    for i in range(k+1, len(Q)-k-1):
        idx = slice(i-k, i+k+1)
        p = wpolyfit(Q[idx], R[idx], dy=dR[idx], degree=degree)
        d[i] = p.der(Q[i])
    p = wpolyfit(Q[-width:], R[-width:], dy=dR[-width:], degree=degree)
    d[-k-1:] = p.der(Q[-k-1:])
    return d
Exemplo n.º 3
0
def plot_logp(state, portion=None):
    from pylab import axes, title
    from scipy.stats import chi2, kstest
    from matplotlib.ticker import NullFormatter

    # Plot log likelihoods
    draw, logp = state.logp()
    start = int((1 - portion) * len(draw)) if portion else 0
    genid = arange(state.generation - len(draw) + start, state.generation) + 1
    width, height, margin, delta = 0.7, 0.75, 0.1, 0.01
    trace = axes([margin, 0.1, width, height])
    trace.plot(genid, logp[start:], ',', markersize=1)
    trace.set_xlabel('Generation number')
    trace.set_ylabel('Log likelihood at x[k]')
    title('Log Likelihood History')

    # Plot log likelihood trend line
    from bumps.wsolve import wpolyfit
    from .formatnum import format_uncertainty
    x = np.arange(start, logp.shape[0]) + state.generation - state.Ngen + 1
    y = np.mean(logp[start:], axis=1)
    dy = np.std(logp[start:], axis=1, ddof=1)
    p = wpolyfit(x, y, dy=dy, degree=1)
    px, dpx = p.ci(x, 1.)
    trace.plot(x, px, 'k-', x, px + dpx, 'k-.', x, px - dpx, 'k-.')
    trace.text(x[0],
               y[0],
               "slope=" + format_uncertainty(p.coeff[0], p.std[0]),
               va='top',
               ha='left')

    # Plot long likelihood histogram
    data = logp[start:].flatten()
    hist = axes(
        [margin + width + delta, 0.1, 1 - 2 * margin - width - delta, height])
    hist.hist(data, bins=40, orientation='horizontal', density=True)
    hist.set_ylim(trace.get_ylim())
    null_formatter = NullFormatter()
    hist.xaxis.set_major_formatter(null_formatter)
    hist.yaxis.set_major_formatter(null_formatter)

    # Plot chisq fit to log likelihood histogram
    float_df, loc, scale = chi2.fit(-data, f0=state.Nvar)
    df = int(float_df + 0.5)
    pval = kstest(-data, lambda x: chi2.cdf(x, df, loc, scale))
    #with open("/tmp/chi", "a") as fd:
    #    print("chi2 pars for llf", float_df, loc, scale, pval, file=fd)
    xmin, xmax = trace.get_ylim()
    x = np.linspace(xmin, xmax, 200)
    hist.plot(chi2.pdf(-x, df, loc, scale), x, 'r')
Exemplo n.º 4
0
bounds = (-20, -inf), (40, inf)
sigma = 1
data = fn((1, 1)) + randn(*x.shape) * sigma  # Fake data

# Sample from the posterior density function
n = 2
model = Simulation(
    f=fn, data=data, sigma=sigma, bounds=bounds, labels=["x", "y"])
sampler = Dream(
    model=model,
    population=randn(5 * n, 4, n),
    thinning=1,
    draws=20000,
)
mc = sampler.sample()
mc.title = 'Strong anti-correlation'

# Create a derived parameter without the correlation
mc.derive_vars(lambda p: (p[0] + p[1]), labels=['x+y'])

# Compare the MCMC estimate for the derived parameter to a least squares fit
from bumps.wsolve import wpolyfit
poly = wpolyfit(x, data, degree=1, origin=True)
print("x+y from linear fit", poly.coeff[0], poly.std[0])
points, logp = mc.sample(portion=0.5)
print("x+y from MCMC", mean(points[:, 2]), std(points[:, 2], ddof=1))

# Plot the samples
plot_all(mc, portion=0.5)
show()
Exemplo n.º 5
0
# Sample from the posterior density function
n = 2
model = Simulation(f=fn,
                   data=data,
                   sigma=sigma,
                   bounds=bounds,
                   labels=["x", "y"])
sampler = Dream(
    model=model,
    population=randn(5 * n, 4, n),
    thinning=1,
    draws=20000,
)
mc = sampler.sample()
mc.title = 'Strong anti-correlation'

# Create a derived parameter without the correlation
mc.derive_vars(lambda p: (p[0] + p[1]), labels=['x+y'])

# Compare the MCMC estimate for the derived parameter to a least squares fit
from bumps.wsolve import wpolyfit
poly = wpolyfit(x, data, degree=1, origin=True)
print("x+y from linear fit", poly.coeff[0], poly.std[0])
points, logp = mc.sample(portion=0.5)
print("x+y from MCMC", mean(points[:, 2]), std(points[:, 2], ddof=1))

# Plot the samples
plot_all(mc, portion=0.5)
show()