Example #1
0
def main():

    #load data    
    returns = data.get_data_google('SPY', start='2008-5-1', end='2009-12-1')['Close'].pct_change()
    returns.plot()
    plt.ylabel('daily returns in %');
    
    with pm.Model() as sp500_model:
        
        nu = pm.Exponential('nu', 1./10, testval=5.0)
        sigma = pm.Exponential('sigma', 1./0.02, testval=0.1)
        
        s = pm.GaussianRandomWalk('s', sigma**-2, shape=len(returns))                
        r = pm.StudentT('r', nu, lam=pm.math.exp(-2*s), observed=returns)
        
    
    with sp500_model:
        trace = pm.sample(2000)

    pm.traceplot(trace, [nu, sigma]);
    plt.show()
    
    plt.figure()
    returns.plot()
    plt.plot(returns.index, np.exp(trace['s',::5].T), 'r', alpha=.03)
    plt.legend(['S&P500', 'stochastic volatility process'])
    plt.show()
Example #2
0
def run( n=100000 ):
    with model:
        # initialize NUTS() with ADVI under the hood
        trace = pm.sample( n )

    # drop some first samples as burnin
    pm.traceplot( trace[1000:] )
Example #3
0
def run(n = 3000):
    if n == "short":
        n = 50
    with model:
        trace = pm.sample(n, step, start)

    pm.traceplot(trace);
Example #4
0
def run(n=1000):
    if n == "short":
        n = 50
    with model:
        trace = pm.sample(n)
    pm.traceplot(trace, varnames=['mu', 'r'],
                 lines={'mu': mu_r, 'r': corr_r[np.triu_indices(n_var, k=1)]})
Example #5
0
def run(n=2000):
    model = build_model()
    with model:
        # initialize NUTS() with ADVI under the hood
        trace = pm.sample(n, nuts_kwargs={'target_accept':.99})

    # drop some first samples as burnin
    pm.traceplot(trace[1000:])
Example #6
0
def run(n=3000):
    if n == "short":
        n = 500
    with model:
        trace = pm.sample(n, step)

    burn = n/10

    pm.traceplot(trace[burn:]);
    pm.plots.summary(trace[burn:])
Example #7
0
def run(n=1000):
    if n == "short":
        n = 50
    with arma_model:

        trace = sample(1000)

    burn = n/10

    traceplot(trace[burn:])
    plots.summary(trace[burn:])
def BayesianLearning(fig, path, measurements,
                     pos_min=-50, pos_max=50, subplot=133):
    with pm.Model() as model:
        # Compute bounds based on measurements
        pos_min_x, pos_max_x, pos_min_y, pos_max_y = boundsFromPath(path)
        minPos = min(pos_min_x, pos_min_y)
        maxPos = max(pos_max_x, pos_max_y)

        # Priors
        # See: http://stackoverflow.com/q/25342899
        thermal_position_x = pm.Uniform('thermal_position_x',
                                      lower=pos_min_x, upper=pos_max_x)
        thermal_position_y = pm.Uniform('thermal_position_y',
                                      lower=pos_min_y, upper=pos_max_y)
        thermal_amplitude = pm.Uniform('thermal_amplitude',
                                       lower=-10, upper=10)
        thermal_sd = pm.Uniform('sd', lower=0.1, upper=100)

        # When sampling, look at the values of the test thermal field at the points
        # we have taken measurements at.
        velocity = deterministicVelocity(path, measurements,
                                         thermal_position_x, thermal_position_y,
                                         thermal_amplitude, thermal_sd)

        # Observe the vertical velocities
        thermal_vert_vel = pm.Normal('thermal_vert_vel', mu=velocity,
                                     observed=measurements)

        # Sample this to find the posterior, note Metropolis works with discrete
        step = pm.Metropolis()
        start = pm.find_MAP(fmin=sp.optimize.fmin_powell)
        trace = pm.sample(2000, step=step, progressbar=True, start=start)

        # Find the most probable surface and plot that for comparison
        x = lameMAP(trace['thermal_position_x'])
        y = lameMAP(trace['thermal_position_y'])
        amp = lameMAP(trace['thermal_amplitude'])
        sd = lameMAP(trace['sd'])
        eq = thermalEq((x,y), amp, sd)

        # Plot it
        prev = plt.gca()
        visualizeThermalField([eq], path, measurements, trace, pos_min, pos_max,
                              only2d=False, fig=fig, subplot=subplot, lines=False,
                              limits=[prev.get_xlim(),prev.get_ylim(),prev.get_zlim()])

        # Really, we have more information than just this MAP estimate.
        # We have probability distributions over all the parameters.
        # It's hard to visualize this in one figure that we can directly
        # compare with the GPR though.
        pm.traceplot(trace, ['thermal_position_x','thermal_position_y',
                             'thermal_amplitude','sd'])
def mcmc_fit(xdata, ytime):

    print("length xdata:", len(xdata))
    print("length ytime:", len(ytime))
    base = 2
    params = [xdata[:, 1], xdata[:, 0], ytime, base]

    # creating mcmc model
    trace = mcmc_model(params)
    traceplot(trace)
    plt.close()
    # [y_cal, y_cal_upper, y_cal_lower, size]
    return trace
Example #10
0
def test_categorical():
    k = 3
    ndata = 5000

    v = np.random.randint(0, k, ndata)

    with pymc3.Model() as model:
        p = pymc3.Dirichlet(name='p', a=np.array([1., 1., 1.]), shape=k)
        category = pymc3.Categorical(name='category',
                                     p=p,
                                     shape=ndata,
                                     observed=v)
        trace = pymc3.sample(3000, step=pymc3.Slice())

    pymc3.traceplot(trace)
    plt.show()
def plot_traces_pymc(trcs, varnames=None):
    ''' Convenience fn: plot traces with overlaid means and values 
        Handle nested traces for hierarchical models
    '''

    nrows = len(trcs.varnames)
    if varnames is not None:
        nrows = len(varnames)
    
    ax = pm.traceplot(trcs, varnames=varnames, figsize=(12, nrows*1.4),
                      lines={k: v['mean'] for k, v in 
                                pm.df_summary(trcs,varnames=varnames).iterrows()},
                      combined=True)

    # don't label the nested traces (a bit clumsy this: consider tidying)
    dfmns = pm.df_summary(trcs, varnames=varnames)['mean'].reset_index()
    dfmns.rename(columns={'index':'featval'}, inplace=True)
    dfmns = dfmns.loc[dfmns['featval'].apply(lambda x: re.search('__[1-9]{1,}', x) is None)]
    dfmns['draw'] = dfmns['featval'].apply(lambda x: re.search('__0{1}$', x) is None)
    dfmns['pos'] = np.arange(dfmns.shape[0])
    dfmns.set_index('pos', inplace=True)

    for i, r in dfmns.iterrows():
        if r['draw']:
            ax[i,0].annotate('{:.2f}'.format(r['mean']), xy=(r['mean'],0)
                    ,xycoords='data', xytext=(5,10)
                    ,textcoords='offset points', rotation=90
                    ,va='bottom', fontsize='large', color='#AA0022') 
Example #12
0
def run(n=1500):
    if n == 'short':
        n = 50

    with m:
        trace = pm.sample(n)

    pm.traceplot(trace, varnames=['mu_hat'])

    print('Example observed data: ')
    print(y[:30, :].T)
    print('The true ranking is: ')
    print(yreal.flatten())
    print('The Latent mean is: ')
    latentmu = np.hstack(([0], pm.summary(trace, varnames=['mu_hat'])['mean'].values))
    print(np.round(latentmu, 2))
    print('The estimated ranking is: ')
    print(np.argsort(latentmu))
Example #13
0
    def plot_traces(self, fname=None, bbox_inches='tight', **kwargs):
        # Should plot the MCMC traces.
        axes = pm.traceplot(self.traces)
        fig = axes[0, 0].get_figure()
        if fname is not None:
            fig.tight_layout()
            fig.savefig(fname, bbox_inches=bbox_inches, **kwargs)

        return fig, axes
Example #14
0
def main():
  X, Y = generate_sample()

  with pm.Model() as model:
    alpha = pm.Normal('alpha', mu=0, sd=20)
    beta = pm.Normal('beta', mu=0, sd=20)
    sigma = pm.Uniform('sigma', lower=0)
    y = pm.Normal('y', mu=beta*X+alpha, sd=sigma, observed=Y)
    start = pm.find_MAP()
    step = pm.NUTS(state=start)

  with model:
    if (multicore):
      trace = pm.sample(itenum, step, start=start,
        njobs=chainnum, random_seed=range(chainnum), progressbar=progress)
    else:
      ts = [pm.sample(itenum, step, chain=i, progressbar=progress)
            for i in range(chainnum)]
      trace = merge_traces(ts)

    if (saveimage):
      pm.traceplot(trace).savefig("simple_linear_trace.png")
    print "Rhat = {0}".format(pm.gelman_rubin(trace))

  t1 = time.clock()
  print "elapsed time = {0}".format(t1 - t0)

  #trace
  if(not multicore):
  	trace=ts[0]
  with model:
  	pm.traceplot(trace,model.vars)

  pm.forestplot(trace)

  with open("simplelinearregression_model.pkl","w") as fpw:
  	pkl.dump(model,fpw)
  with open("simplelinearregression_trace.pkl","w") as fpw:
  	pkl.dump(trace,fpw)
  with open("simplelinearregression_model.pkl") as fp:
  	model=pkl.load(fp)
  with open("simplelinearregression_trace.pkl") as fp:
  	trace=pkl.load(fp)
Example #15
0
def test_bernoulli():
    data = [random.randint(0,1) for i in range(200)]

    model = pymc3.Model()

    with model:
        p = pymc3.Uniform(lower=0,upper=1, name='p')
        X = pymc3.Bernoulli(p=p, name='X', observed=data)

        start = pymc3.find_MAP()

        # instantiate sampler
        step = pymc3.NUTS(scaling=start)

        # draw 500 posterior samples
        trace = pymc3.sample(10000, step, start=start)

    pymc3.traceplot(trace)
    plt.show()
Example #16
0
    def __init__(self,X_train,y_train,n_hidden,lam=1):
        n_train = y_train.shape[0]
        n_dim = X_train.shape[1]
        print X_train.shape
        with pm.Model() as rbfnn:
            C = pm.Normal('C',mu=0,sd=10,shape=(n_hidden))
            #beta = pm.Gamma('beta',1,1)
            w = pm.Normal('w',mu=0,sd=10,shape=(n_hidden+1))
            
            #component, updates = theano.scan(fn=lambda x: T.sum(C-x)**2,sequences=[X_train])
            y_out=[]
            for x in X_train:
                #rbf_out =  T.exp(-lam*T.sum((C-x)**2,axis=1)) 
                #1d speed up
                rbf_out =  T.exp(-lam*(C-x)**2)
                #rbf_out = theano.printing.Print(rbf_out)                 
                rbf_out_biased = \
                        T.concatenate([ rbf_out, T.alloc(1,1) ], 0)
                y_out.append(T.dot(w,rbf_out_biased))
            
            y = pm.Normal('y',mu=y_out,sd=0.01,observed=y_train)
            
            start = pm.find_MAP(fmin=scipy.optimize.fmin_l_bfgs_b)
            print start
            step = pm.NUTS(scaling=start)
            trace = pm.sample(2000, step, progressbar=False)
            step = pm.NUTS(scaling=trace[-1])
            trace = pm.sample(20000,step,start=trace[-1])
            

            print summary(trace, vars=['C', 'w'])

            vars = trace.varnames   
            for i, v in enumerate(vars):
                for d in trace.get_values(v, combine=False, squeeze=False):
                    d=np.squeeze(d)
                    with open(str(v)+".txt","w+") as thefile:
                        for item in d:
                            print>>thefile, item

            traceplot(trace)
            plt.show()
Example #17
0
def mcmc_fit(xdata, ytime, eq_used):

    print("length xdata:", len(xdata))
    print("length ytime:", len(ytime))

    for equation in eq_used:
        params = [xdata[:, 1], xdata[:, 0], ytime, equation]

        # creating mcmc model
        trace = mcmc_model(params)

        # save the learned mode in pickle file
        pickle.dump(trace, open("results/model" + str(equation) + ".pickle", "wb"), protocol=-1)

        traceplot(trace)
        # move plot about learned param values to results folder
        os.rename("mcmc.png", "results/mcmc_" + str(equation) + ".png")
        plt.close()
    print("mcmc_fit finish")
    # [y_cal, y_cal_upper, y_cal_lower, size]
    return trace
def groundTruthTraceplot(truths,trace,var,sigma=0.01,scale=None,jitter=0.01,ymax=None,show=True,**kwargs):
    ax = traceplot(trace=trace,vars=[var],**kwargs)
#Reset color cycle to match colors properly
    ax[0][0].set_color_cycle(None)
    #ax[0][0].set_line_style('dashed')
    truths = truths.flatten()
    xmin = min(truths.min(),trace[var].min())
    xmax = max(truths.max(),trace[var].max())
    x = np.linspace(xmin,xmax,int(1000*(xmax-xmin)))
    if scale is None:
        scale = len(truths)
    for truth in truths:
        randj = (np.random.rand()-0.5)*jitter
        ax[0][0].plot(x,mlab.normpdf(x,truth+randj,sigma),'--')
    if ymax is not None:
        ax[0][0].set_ylim((0,ymax))
    if show:
        plt.show()
        start_MAP = pm.find_MAP(fmin=optimize.fmin_powell)
        t2 = time.time()
        print("Found MAP, took %f seconds" % (t2 - t1))

        ## take samples
        t1 = time.time()
        traces_ols = pm.sample(2000, start=start_MAP, step=pm.NUTS(), progressbar=True)
        print()
        t2 = time.time()
        print("Done sampling, took %f seconds" % (t2 - t1))

    pm.summary(traces_ols)
    ## plot the samples and the marginal distributions
    _ = pm.traceplot(
        traces_ols,
        figsize=(12, len(traces_ols.varnames) * 1.5),
        lines={k: v["mean"] for k, v in pm.df_summary(traces_ols).iterrows()},
    )
    plt.show()


do_tstudent = False

if do_tstudent:

    print("Robust Student-t analysis...")

    t1 = time.time()
    with pm.Model() as mdl_studentt:

        ## Define weakly informative Normal priors to give Ridge regression
Example #20
0
with model1:
    s = shared(pm.floatX(1))
    inference = pm.ADVI(cost_part_grad_scale=s)
    # ADVI has nearly converged
    inference.fit(n=20000)
    # It is time to set `s` to zero
    s.set_value(0)
    approx = inference.fit(n=10000)
    trace = approx.sample(3000, include_transformed=True)
    elbos1 = -inference.hist

chain = trace[2000:]
varnames2 = ['beta', 'beta1', 'beta2', 'beta3']
# # pm.plot_posterior(chain2, varnames2, ref_val=0)
pm.traceplot(chain)
plt.show()
pm.traceplot(chain, varnames2)
plt.show()

# 需要着重分析:'beta', 'beta1', 'beta2', 'beta3'
varnames2 = ['beta', 'beta1', 'beta2', 'beta3']
tmp = pm.df_summary(chain, varnames2)
betaMAP = tmp['mean'][np.arange(companiesABC)]
beta1MAP = tmp['mean'][np.arange(companiesABC) + companiesABC]
beta2MAP = tmp['mean'][2 * companiesABC]
beta3MAP = tmp['mean'][2 * companiesABC + 1]

from matplotlib import gridspec

plt.figure(figsize=(12, 5))
Example #21
0
def make_dataframe(n, s):
    df = pd.DataFrame({
        'success': [0] * (n * 4),
        'country': ['Canada'] * (2 * n) + ['China'] * (2 * n),
        'treated': [0] * n + [1] * n + [0] * n + [1] * n
    })
 
    for i, successes in zip([n, n*2, n*3, n*4], s):
        df.loc[i - n:i - n + successes - 1, 'success'] = 1
 
    return df
 
# n, ss = 200, [60, 100, 110, 120]
n, ss = 100, [30, 50, 55, 60]
 
df = make_dataframe(n, ss)

le = preprocessing.LabelEncoder()
country_idx = le.fit_transform(df['country'])
n_countries = len(set(country_idx))



pm.traceplot(trace_h)





Example #22
0
    """
    def __init__(self):
        self.count = 0

    def __call__(self, shape):
        self.count += 1

        regularization = pm.HalfNormal('reg_hyper%d' % self.count, sd=1)

        return pm.Normal('w%d' % self.count, mu=0, sd=regularization,
                         testval=np.random.normal(size=shape),
                         shape=shape)


if __name__ == "__main__":

    print("Loading data...")
    X_train, y_train, X_val, y_val, X_test, y_test = load_dataset()

    # Building a theano.shared variable
    input_var = theano.shared(X_train[:500, ...].astype(np.float64))
    target_var = theano.shared(y_train[:500, ...].astype(np.float64))

    with pm.Model() as neural_network_hier:
        likelihood = build_ann(GaussWeightsHierarchicalRegularization())
        v_params, trace, ppc, y_pred = run_advi(likelihood, X_train, y_train, input_var, X_test, y_test, target_var)

    print('Accuracy on test data = {}%'.format(accuracy_score(y_test, y_pred) * 100))
    pm.traceplot(trace, varnames=['reg_hyper1', 'reg_hyper2', 'reg_hyper3', 'reg_hyper4', 'reg_hyper5', 'reg_hyper6'])

Example #23
0
with pm.Model() as NonCentered_eight:
    tau = pm.HalfCauchy('tau', beta=5)
    mu = pm.Normal('mu', mu=0, sigma=5)
    theta_tilde = pm.Normal('theta_t', mu=0, sigma=1, shape=N)
    theta = pm.Deterministic('theta', mu + tau * theta_tilde)
    y = pm.Normal('obs', mu=theta, sigma=sigma_observed, observed=y_observed)

with Centered_eight:
    centered_trace = pm.sample(5000, chains=2, tune=2000, target_accept=.99)

with NonCentered_eight:
    noncentered_trace = pm.sample(5000, chains=2, tune=1000, target_accept=.80)

print('Centered')
print(pm.summary(centered_trace).round(2))
pm.traceplot(centered_trace)

print('Non Centered')
print(pm.summary(noncentered_trace).round(2))
pm.traceplot(noncentered_trace)

plt.figure()
plt.scatter(np.log(centered_trace['tau'][2000:]),
            centered_trace['theta'][:, 0][2000:],
            color='orange')
plt.scatter(np.log(noncentered_trace['tau'][2000:]),
            noncentered_trace['theta'][:, 0][2000:],
            color='green')
plt.legend(['Centered', 'Non-Centered'])

plt.show()
print('--- Model Specified ---')

#%% Model Fitting

# Maximum a posteriori (MAP) estimate for a model, is the mode of the posterior
# distribution and is generally found using numerical optimization methods
map_estimate = pm.find_MAP(model=basic_model)

map_estimate

# In summary, while PyMC3 provides the function find_MAP(), at this point mostly
# for historical reasons, this function is of little use in most scenarios.
# If you want a point estimate you should get it from the posterior.
# In the next section we will see how to get a posterior using sampling methods.

print('--- Model Fit ---')

#%% Sampling Methods

with basic_model:
    # draw 500 posterior samples
    trace = pm.sample(500)

print('--- Model Sampling ---')
#%% Posterior Analysis

# PyMC3 provides plotting and summarization functions for inspecting the sampling
# output. A simple posterior plot can be created using traceplot.
pm.traceplot(trace)

pm.summary(trace).round(2)
Example #25
0
# default
k1, n1 = 5, 10
k2, n2 = 7, 10
## Exercise 3.3.1
#k1, n1 = 14, 20
#k2, n2 = 16, 20
## Exercise 3.3.2
#k1, n1 =  0, 10
#k2, n2 = 10, 10
## Exercise 3.3.3
#k1, n1 =  5, 10
#k2, n2 =  5, 10

model = pm.Model()

with model:
    # Prior on single rate
    theta = pm.Beta('theta', alpha=1, beta=1)
    # Observed Counts
    k1 = pm.Binomial('k1', p=theta, n=n1, observed=k1)
    k2 = pm.Binomial('k2', p=theta, n=n2, observed=k2)
    # instantiate Metropolis-Hastings sampler
    stepFunc = pm.Metropolis()
    # draw 5,000 posterior samples (in 4 parallel running chains)
    Nsample = 5000
    Nchains = 4
    traces = pm.sample(Nsample, step=stepFunc, njobs=Nchains)

axs = pm.traceplot(traces, vars=['theta'], combined=False)
axs[0][0].set_xlim([0,1]) # manually set x-limits for comparisons
Example #26
0
plt.subplot(211)
plt.title(r'Trace of $\alpha$')
plt.plot(alpha_samples, color='darkred')
plt.xlabel('Samples')
plt.ylabel('Parameter')

# Plot beta trace
plt.subplot(212)
plt.title(r'Trace of $\beta$')
plt.plot(beta_samples, color='b')
plt.xlabel('Samples')
plt.ylabel('Parameter')
plt.tight_layout(h_pad=0.8)

figsize(20, 12)
pm.traceplot(sleep_trace, ['alpha', 'beta'])

# %%

pm.autocorrplot(sleep_trace, ['alpha', 'beta'])

# Sort the values by time offset
wake_data.sort_values('time_offset', inplace=True)

# Time is the time offset
time = np.array(wake_data.loc[:, 'time_offset'])

# Observations are the indicator
wake_obs = np.array(wake_data.loc[:, 'indicator'])

with pm.Model() as wake_model:
Example #27
0
"""
plt.scatter(x_2, y_2)
plt.xlabel('$x$', fontsize=16)
plt.ylabel('$y$', fontsize=16, rotation=0)
plt.savefig('img422.png')
"""

with pm.Model() as model_poly:
    alpha = pm.Normal('alpha', mu=0, sd=10)
    beta1 = pm.Normal('beta1', mu=0, sd=1)
    beta2 = pm.Normal('beta2', mu=0, sd=1)
    epsilon = pm.HalfCauchy('epsilon', 5)
    mu = alpha + beta1 * x_2 + beta2 * x_2**2
    y_pred = pm.Normal('y_pred', mu=mu, sd=epsilon, observed=y_2)
    trace_poly = pm.sample(2000, njobs=1)

pm.traceplot(trace_poly)
plt.savefig('img423.png')
pm.autocorrplot(trace_poly)
plt.savefig('img4232.png')

plt.clf()
x_p = np.linspace(-6, 6)
y_p = trace_poly['alpha'].mean(
) + trace_poly['beta1'].mean() * x_p + trace_poly['beta2'].mean() * x_p**2
plt.scatter(x_2, y_2)
plt.xlabel('$x$', fontsize=16)
plt.ylabel('$y$', fontsize=16, rotation=0)
plt.plot(x_p, y_p, c='k')
plt.savefig('img424.png')
Example #28
0
    # Hyperpriors
    mu_a = pm.Normal('mu_alpha', mu=clothm_mean, sd=10)
    sigma_a = pm.HalfCauchy('sigma_alpha', beta=2)
    mu_b = pm.Normal('mu_beta', mu=0., sd=10)
    sigma_b = pm.HalfCauchy('sigma_beta', beta=2)

    # Intercept for each county, distributed around group mean mu_a
    a = pm.Normal('alpha', mu=mu_a, sd=sigma_a, shape=len(Data.wavecode.unique()))
    # Intercept for each county, distributed around group mean mu_a
    b = pm.Normal('beta', mu=mu_b, sd=sigma_b, shape=len(Data.wavecode.unique()))

    # Error standard deviation
    eps = pm.HalfCauchy('eps', beta=2)

    # Expected value
    w_est = a[wave_idx] + b[wave_idx] * Data.ltotR

    # Data likelihood
    w_like = pm.Normal('w_like', mu=w_est, sd=eps, observed=Data.w_clothm)

with hierarchical_model:
    hierarchical_trace = pm.sample(draws=5000, tune=1000, njobs=4)[1000:]

x = pd.Series(hierarchical_trace['sigma_alpha_log'], name='sigma alpha log')
y = pd.Series(hierarchical_trace['eps_log'], name='sigma eps log')

sns.jointplot(x, y);


pm.traceplot(hierarchical_trace)
Example #29
0
def PLD(tpf,
        planet_mask=None,
        aperture=None,
        return_soln=False,
        return_quick_corrected=False,
        sigma=5,
        trim=0,
        ndraws=1000):
    ''' Use exoplanet, pymc3 and theano to perform PLD correction

    Parameters
    ----------
    tpf : lk.TargetPixelFile
        Target Pixel File to Correct
    planet_mask : np.ndarray
        Boolean array. Cadences where planet_mask is False will be excluded from the
        PLD correction. Use this to mask out planet transits.
    '''
    if planet_mask is None:
        planet_mask = np.ones(len(tpf.time), dtype=bool)
    if aperture is None:
        aperture = tpf.pipeline_mask

    time = np.asarray(tpf.time, np.float64)
    if trim > 0:
        flux = np.asarray(tpf.flux[:, trim:-trim, trim:-trim], np.float64)
        flux_err = np.asarray(tpf.flux_err[:, trim:-trim, trim:-trim],
                              np.float64)
        aper = np.asarray(aperture, bool)[trim:-trim, trim:-trim]
    else:
        flux = np.asarray(tpf.flux, np.float64)
        flux_err = np.asarray(tpf.flux_err, np.float64)
        aper = np.asarray(aperture, bool)

    raw_flux = np.asarray(np.nansum(flux[:, aper], axis=(1)), np.float64)
    raw_flux_err = np.asarray(
        np.nansum(flux_err[:, aper]**2, axis=(1))**0.5, np.float64)

    raw_flux_err /= np.median(raw_flux)
    raw_flux /= np.median(raw_flux)
    raw_flux -= 1

    # Setting to Parts Per Thousand keeps us from hitting machine precision errors...
    raw_flux *= 1e3
    raw_flux_err *= 1e3

    # Build the first order PLD basis
    #    X_pld = np.reshape(flux[:, aper], (len(flux), -1))
    saturation = (np.nanpercentile(flux, 100, axis=0) > 175000)
    X_pld = np.reshape(flux[:, aper & ~saturation], (len(tpf.flux), -1))

    extra_pld = np.zeros((len(time), np.any(saturation, axis=0).sum()))
    idx = 0
    for column in saturation.T:
        if column.any():
            extra_pld[:, idx] = np.sum(flux[:, column, :], axis=(1, 2))
            idx += 1
    X_pld = np.hstack([X_pld, extra_pld])

    # Remove NaN pixels
    X_pld = X_pld[:, ~((~np.isfinite(X_pld)).all(axis=0))]
    X_pld = X_pld / np.sum(flux[:, aper], axis=-1)[:, None]

    # Build the second order PLD basis and run PCA to reduce the number of dimensions
    X2_pld = np.reshape(X_pld[:, None, :] * X_pld[:, :, None], (len(flux), -1))
    # Remove NaN pixels
    X2_pld = X2_pld[:, ~((~np.isfinite(X2_pld)).all(axis=0))]
    U, _, _ = np.linalg.svd(X2_pld, full_matrices=False)
    X2_pld = U[:, :X_pld.shape[1]]

    # Construct the design matrix and fit for the PLD model
    X_pld = np.concatenate((np.ones((len(flux), 1)), X_pld, X2_pld), axis=-1)

    # Create a matrix with small numbers along diagonal to ensure that
    # X.T * sigma^-1 * X is not singular, and prevent it from being non-invertable
    diag = np.diag((1e-8 * np.ones(X_pld.shape[1])))

    if (~np.isfinite(X_pld)).any():
        raise ValueError('NaNs in components.')
    if (np.any(raw_flux_err == 0)):
        raise ValueError('Zeros in raw_flux_err.')

    def build_model(mask=None, start=None):
        ''' Build a PYMC3 model

        Parameters
        ----------
        mask : np.ndarray
            Boolean array to mask cadences. Cadences that are False will be excluded
            from the model fit
        start : dict
            MAP Solution from exoplanet

        Returns
        -------
        model : pymc3.model.Model
            A pymc3 model
        map_soln : dict
            Best fit solution
        '''

        if mask is None:
            mask = np.ones(len(time), dtype=bool)

        with pm.Model() as model:
            # GP
            # --------
            logs2 = pm.Normal("logs2", mu=np.log(np.var(raw_flux[mask])), sd=4)
            #            pm.Potential("logs2_prior1", tt.switch(logs2 < -3, -np.inf, 0.0))

            logsigma = pm.Normal("logsigma",
                                 mu=np.log(np.std(raw_flux[mask])),
                                 sd=4)
            logrho = pm.Normal("logrho", mu=np.log(150), sd=4)
            kernel = xo.gp.terms.Matern32Term(log_rho=logrho,
                                              log_sigma=logsigma)
            gp = xo.gp.GP(kernel, time[mask],
                          tt.exp(logs2) + raw_flux_err[mask]**2)

            # Motion model
            #------------------
            A = tt.dot(X_pld[mask].T, gp.apply_inverse(X_pld[mask]))
            A = A + diag  # Add small numbers to diagonal to prevent it from being singular
            B = tt.dot(X_pld[mask].T, gp.apply_inverse(raw_flux[mask, None]))
            C = tt.slinalg.solve(A, B)
            motion_model = pm.Deterministic("motion_model",
                                            tt.dot(X_pld[mask], C)[:, 0])

            # Likelihood
            #------------------
            pm.Potential("obs",
                         gp.log_likelihood(raw_flux[mask] - motion_model))

            # gp predicted flux
            gp_pred = gp.predict()
            pm.Deterministic("gp_pred", gp_pred)
            pm.Deterministic("weights", C)

            # Optimize
            #------------------
            if start is None:
                start = model.test_point
            map_soln = xo.optimize(start=start, vars=[logsigma])
            map_soln = xo.optimize(start=start, vars=[logrho, logsigma])
            map_soln = xo.optimize(start=start, vars=[logsigma])
            map_soln = xo.optimize(start=start, vars=[logrho, logsigma])
            map_soln = xo.optimize(start=map_soln, vars=[logs2])
            map_soln = xo.optimize(start=map_soln,
                                   vars=[logrho, logsigma, logs2])
            return model, map_soln, gp

    # First rough correction
    log.info('Optimizing roughly')
    with silence():
        model0, map_soln0, gp = build_model(mask=planet_mask)

    # Remove outliers, make sure to remove a few nearby points incase of flares.
    with model0:
        motion = np.dot(X_pld, map_soln0['weights']).reshape(-1)
        stellar = xo.eval_in_model(gp.predict(time), map_soln0)
        corrected = raw_flux - motion - stellar
        mask = ~sigma_clip(corrected, sigma=sigma).mask
        mask = ~(convolve(mask, Box1DKernel(3), fill_value=1) != 1)
        mask &= planet_mask

    # Optimize PLD
    log.info('Optimizing without outliers')
    with silence():
        model, map_soln, gp = build_model(mask, map_soln0)
    lc_fig = _plot_light_curve(map_soln, model, mask, time, raw_flux,
                               raw_flux_err, X_pld, gp)

    if return_soln:
        motion = np.dot(X_pld, map_soln['weights']).reshape(-1)
        with model:
            stellar = xo.eval_in_model(gp.predict(time), map_soln)
        return model, map_soln, motion, stellar

    if return_quick_corrected:
        raw_lc = tpf.to_lightcurve()
        clc = lk.KeplerLightCurve(
            time=time,
            flux=(raw_flux - stellar - motion) * 1e-3 + 1,
            flux_err=(raw_flux_err) * 1e-3,
            time_format=raw_lc.time_format,
            centroid_col=tpf.estimate_centroids()[0],
            centroid_row=tpf.estimate_centroids()[0],
            quality=raw_lc.quality,
            channel=raw_lc.channel,
            campaign=raw_lc.campaign,
            quarter=raw_lc.quarter,
            mission=raw_lc.mission,
            cadenceno=raw_lc.cadenceno,
            targetid=raw_lc.targetid,
            ra=raw_lc.ra,
            dec=raw_lc.dec,
            label='{} PLD Corrected'.format(raw_lc.targetid))
        return clc

    # Burn in
    sampler = xo.PyMC3Sampler()
    with model:
        burnin = sampler.tune(tune=np.max([int(ndraws * 0.3), 150]),
                              start=map_soln,
                              step_kwargs=dict(target_accept=0.9),
                              chains=4)
    # Sample
    with model:
        trace = sampler.sample(draws=ndraws, chains=4)

    varnames = ["logrho", "logsigma", "logs2"]
    pm.traceplot(trace, varnames=varnames)

    samples = pm.trace_to_dataframe(trace, varnames=varnames)
    corner.corner(samples)

    # Generate 50 realizations of the prediction sampling randomly from the chain
    N_pred = 50
    pred_mu = np.empty((N_pred, len(time)))
    pred_motion = np.empty((N_pred, len(time)))
    with model:
        pred = gp.predict(time)
        for i, sample in enumerate(
                tqdm(xo.get_samples_from_trace(trace, size=N_pred),
                     total=N_pred)):
            pred_mu[i] = xo.eval_in_model(pred, sample)
            pred_motion[i, :] = np.dot(X_pld, sample['weights']).reshape(-1)

    star_model = np.mean(pred_mu + pred_motion, axis=0)
    star_model_err = np.std(pred_mu + pred_motion, axis=0)

    raw_lc = tpf.to_lightcurve()

    meta = {
        'samples': samples,
        'trace': trace,
        'pred_mu': pred_mu,
        'pred_motion': pred_motion
    }

    clc = lk.KeplerLightCurve(
        time=time,
        flux=(raw_flux - star_model) * 1e-3 + 1,
        flux_err=((raw_flux_err**2 + star_model_err**2)**0.5) * 1e-3,
        time_format=raw_lc.time_format,
        centroid_col=tpf.estimate_centroids()[0],
        centroid_row=tpf.estimate_centroids()[0],
        quality=raw_lc.quality,
        channel=raw_lc.channel,
        campaign=raw_lc.campaign,
        quarter=raw_lc.quarter,
        mission=raw_lc.mission,
        cadenceno=raw_lc.cadenceno,
        targetid=raw_lc.targetid,
        ra=raw_lc.ra,
        dec=raw_lc.dec,
        label='{} PLD Corrected'.format(raw_lc.targetid),
        meta=meta)
    return clc
Example #30
0
 def evaluate_fit(self, show_feats):
     return pm.traceplot(self.trace_, varnames=show_feats)
Example #31
0
with pm.Model() as model:
    sigma = pm.HalfCauchy('sigma', beta=10, testval=1.0)
    intercept = pm.Normal('Intercept', 0, sd=20)
    x_coeff = pm.Normal('x', 0, sd=20)

    likelihood = pm.Normal('y',
                           mu=intercept + x_coeff * x,
                           sd=sigma,
                           observed=y)

    start = pm.find_MAP()
    step = pm.NUTS(scaling=start)
    trace = pm.sample(2000, step, start=start, progressbar=True)

plt.figure(figsize=(7, 7))
pm.traceplot(trace[100:])
plt.tight_layout()
plt.savefig("figures/glm-02.png")

plt.figure(figsize=(7, 7))
plt.plot(x, y, 'x', label='data')
pm.glm.plot_posterior_predictive(trace,
                                 samples=100,
                                 label='posterior predictive regression lines')
plt.plot(x, true_regression_line, label='true regression line', lw=3.0, c='y')
plt.title('Posterior predictive regression lines')
plt.legend(loc=0)
plt.xlabel('x')
plt.ylabel('y')
plt.savefig("figures/glm-03.png")
plt.show()
Example #32
0
 def posterior_traceplot(self, **kwargs):
     return pm.traceplot(self.posterior_, **kwargs)
Example #33
0
def mcmc_changepoint(dates,
                     ratings,
                     mcmc_iter=1000,
                     discrete=0,
                     plot_result=1):
    """This function models Yelp reviews as coming from two normal distributions
    with a switch point somewhere between them. When left of the switch point then
    reviews are drawn from the first normal distribution. To the right of the
    switch point reviews are drawn from the second normal distribution. Normal
    distributions are used if the reviews have been normalized to the user's
    average rating; otherwise if analyzing in terms of 1-5 stars set discrete=1
    and the function will do the same estimation on Poisson distributions. This
    function then finds the most likely distribution for where the switchpoint is
    and the most likely parameters for the two generator distributions by using
    Metropolis-Hastings sampling and Hamiltonian Monte Carlo."""

    # dates: Array of dates when the reviews were posted
    # ratings: Array of the ratings given by each review
    # mcmc_iter: How many iterations of the MCMC to run?
    # discrete: Should I use Normal or Poisson distributions to model the ratings?
    # (i.e. are the user-averaged or 1-5 stars)
    # plot_result: Should the function output a plot?

    number_of_ratings = np.arange(0, len(ratings))

    if discrete == 0:
        with Model() as switch_model:
            switchpoint = DiscreteUniform('switchpoint',
                                          lower=0,
                                          upper=len(dates))

            before_intensity = Normal('before_intensity', mu=0, sd=1)
            after_intensity = Normal('after_intensity', mu=0, sd=1)

            intensity = switch(switchpoint >= number_of_ratings,
                               before_intensity, after_intensity)
            sigma = HalfNormal('sigma', sd=1)

            rating = Normal('rating', mu=intensity, sd=sigma, observed=ratings)

    elif discrete == 1:
        with Model() as switch_model:
            switchpoint = DiscreteUniform('switchpoint',
                                          lower=0,
                                          upper=len(dates))

            before_intensity = Exponential('before_intensity', 1)
            after_intensity = Exponential('after_intensity', 1)

            intensity = switch(switchpoint >= number_of_ratings,
                               before_intensity, after_intensity)

            rating = Poisson('rating', intensity, observed=ratings)

    with switch_model:
        trace = sample(mcmc_iter)

    if plot_result == 1:
        traceplot(trace)
        plt.show()

    switch_posterior = trace['switchpoint']
    N_MCs = switch_posterior.shape[0]

    before_intensity_posterior = trace['before_intensity']
    after_intensity_posterior = trace['after_intensity']

    expected_stars = np.zeros(len(ratings))
    for a_rating in number_of_ratings:
        where_switch = a_rating < switch_posterior
        expected_stars[a_rating] = (
            before_intensity_posterior[where_switch].sum() +
            after_intensity_posterior[~where_switch].sum()) / N_MCs

    if plot_result == 1:
        plt.plot(dates, ratings, 'o')
        plt.plot(dates, expected_stars, 'b-')
        plt.show()

    # Return the mode and it's frequency / mcmc_iter
    b_mean, b_count = scipy.stats.mode(trace['before_intensity'])
    a_mean, a_count = scipy.stats.mode(trace['after_intensity'])
    modal_switch, count = scipy.stats.mode(trace['switchpoint'])
    sigma_est, sigma_count = scipy.stats.mode(trace['sigma'])
    differential = b_mean - a_mean
    return differential, modal_switch, expected_stars, sigma_est, switch_posterior
Example #34
0
%config InlineBackend.figure_format = 'retina'
%matplotlib inline
plt.rcParams["figure.figsize"] = (10, 5)
np.random.seed(42)

# Prepare the data
Ns = [90, 50, 80]
mus = [4, 10, 20]
sds = [0.5, 4, 1.5]

mix = np.array([])
for N, mu, sd in zip(Ns, mus, sds):
    mix = np.append(mix, norm(mu, sd).rvs(N))

# Sampling
n = len(Ns)
with pm.Model() as model:
    p = pm.Dirichlet("p", np.ones(n))
    k = pm.Categorical("k", p=p, shape=sum(Ns))
    means = pm.Normal("means", mu=[10, 10, 10], sd=10, shape=n)
    sigmas = pm.HalfCauchy("sigmas", 5, shape=n)
    y = pm.Normal("y", mu=means[k], sd=sigmas[k], observed=mix)
    trace = pm.sample(5000, tune=1000)
    
# Plot
pm.traceplot(
    trace, 
    varnames=["means", "p", "sigmas"],
    lines={"means": mus, "sigmas":sds}
)
plt.savefig("./results/4-23-mixture-model.png")
np.random.seed(314)
N = 100
alfa_real = 2.5
beta_real = 0.9
eps_real = np.random.normal(0, 0.5, size=N)

x = np.random.normal(10, 1, N)
y_real = alfa_real + beta_real * x
y = y_real + eps_real

with pm.Model() as model_n:
    alpha = pm.Normal('alpha', mu=0, sd=10)
    beta = pm.Normal('beta', mu=0, sd=1)
    epsilon = pm.HalfCauchy('epsilon', 5)

    mu = alpha + beta * x
    y_pred = pm.Normal('y_pred', mu=mu, sd=epsilon, observed=y)

    rb = pm.Deterministic('rb', (beta * x.std() / y.std())**2)

    y_mean = y.mean()
    ss_reg = pm.math.sum((mu - y_mean)**2)
    ss_tot = pm.math.sum((y - y_mean)**2)
    rss = pm.Deterministic('rss', ss_reg / ss_tot)

    start = pm.find_MAP()
    step = pm.NUTS()
    trace_n = pm.sample(2000, step=step, start=start)
pm.traceplot(trace_n)
plt.show()
Example #36
0
    with pm.Model() as model_t:
        alpha = pm.Normal('alpha', mu=0, sd=100)
        beta = pm.Normal('beta', mu=0, sd=1)
        epsilon = pm.HalfCauchy('epsilon', 5)
        nu = pm.Deterministic('nu', pm.Exponential('nu_', 1 / 29) + 1)

        y_pred = pm.StudentT('y_pred',
                             mu=alpha + beta * x_3,
                             sd=epsilon,
                             nu=nu,
                             observed=y_3)

        start = pm.find_MAP()
        step = pm.Metropolis()
        trace_t = pm.sample(2000, step=step, start=start)
    pm.traceplot(trace_t)

    plt.figure()
    beta_c, alpha_c = stats.linregress(x_3, y_3)[:2]

    plt.plot(x_3, (alpha_c + beta_c * x_3), 'k', label='non-robust', alpha=0.5)
    plt.plot(x_3, y_3, 'bo')
    alpha_m = trace_t['alpha'].mean()
    beta_m = trace_t['beta'].mean()
    plt.plot(x_3, alpha_m + beta_m * x_3, c='k', label='robust')

    plt.xlabel('$x$', fontsize=16)
    plt.ylabel('$y$', rotation=0, fontsize=16)
    plt.legend(loc=2, fontsize=12)
    plt.tight_layout()
Example #37
0
    def traceplot(self):
        """Generate a traceplot for MCMC diagnostics."""

        pm.traceplot(self.trace)
    y = pm.Binomial('y', p=theta, n=N, observed=z)
    trace = pm.sample(5000, step=pm.NUTS(), progressbar=False)

## Check the results.
burnin = 0  # posterior samples to discard

## Print summary for each trace
#pm.df_summary(trace[burnin:])
#pm.df_summary(trace)

## Check for mixing and autocorrelation
#pm.autocorrplot(trace, varnames=['mu', 'kappa'])

## Plot KDE and sampled values for each parameter.
#pm.traceplot(trace[burnin:])
pm.traceplot(trace)

# Create arrays with the posterior sample
mu1_sample = trace['mu'][:,0][burnin:]
mu2_sample = trace['mu'][:,1][burnin:]
mu3_sample = trace['mu'][:,2][burnin:]
mu4_sample = trace['mu'][:,3][burnin:]


# Plot differences among filtrations experiments
fig, ax = plt.subplots(1, 3, figsize=(15, 6))
pm.plot_posterior((mu1_sample-mu2_sample), ax=ax[0], ref_val=0, color='skyblue')
ax[0].set_xlabel(r'$\mu1-\mu2$')

# Plot differences among condensation experiments
pm.plot_posterior((mu3_sample-mu4_sample), ax=ax[1], ref_val=0, color='skyblue')
Example #39
0
    ObservedA = pm.Normal('ObservedA',
                          mu=theta,
                          sd=sigma,
                          observed=elec_faults)  # 观测值
    ObservedB = pm.Normal('ObservedB',
                          mu=thetaB,
                          sd=sigma,
                          observed=elec_faultsB)  # 观测值

    start = pm.find_MAP()
    # step = pm.Metropolis()
    # trace2 = pm.sample(nuts_kwargs={'target_accept': 0.95})
    trace2 = pm.sample(3000, tune=1000)
chain2 = trace2
varnames1 = ['σ_a', 'σ_aB', 'theta1', 'theta1B']
pm.traceplot(chain2, varnames1)
plt.show()
varnames1 = ['a0', 'sigma', 'δ', 'δB', 'Δ_a', 'Δ_aB']
pm.traceplot(chain2, varnames1)
plt.show()

plt.plot(trace2['step_size_bar'])
plt.show()

pm.energyplot(chain2)
plt.show()
# 画出自相关曲线
varnames1 = ['σ_a', 'a0', 'δ', 'σ_aB', 'Δ_a', 'δB']
pm.autocorrplot(chain2, varnames1)
plt.show()
Example #40
0
def bayesian_t(df, val_col, grp_col='regulated',
               sig_fac=2, unif_l=0, unif_u=20,
               exp_mn=30, 
               plot_trace=False, plot_ppc=False,
               plot_vars=False, plot_diffs=True,
               steps=2000, mcmc='metropolis'):
    """ Simple Bayesian test for differences between two groups.
    
    Args:
        df         Dataframe. Must have a column containing values
                   and a categorical 'regulated' column that is [0, 1]
                   to define the two groups
        val_col    Name of the values column
        grp_col    Name of the categorical column defining the groups
        sig_fac    Factor applied to std. dev. of pooled data to define
                   prior std. dev. for group means
        unif_l     Lower bound for uniform prior on std. dev. of group
                   means
        unif_u     Upper bound for uniform prior on std. dev. of group
                   means
        exp_mn     Mean of exponential prior for v in Student-T 
                   distribution
        plot_trace Whether to plot the MCMC traces
        plot_ppc   Whether to perform and plot the Posterior Predictive
                   Check 
        plot_vars  Whether to plot posteriors for variables
        plot_diffs Whether to plot posteriors for differences
        steps      Number of steps to take in MCMC chains
        mcmc       Sampler to use: ['metropolis', 'slice', 'nuts']
    
    Returns:
        Creates plots showing the distribution of differences in 
        means and variances, plus optional diagnostics. Returns the 
        MCMC trace
    """
    import numpy as np
    import pymc3 as pm
    import pandas as pd
    import seaborn as sn
    import matplotlib.pyplot as plt

    # Get overall means and s.d.
    mean_all = df[val_col].mean()
    std_all = df[val_col].std()

    # Group data
    grpd = df.groupby(grp_col)
    
    # Separate groups
    reg_data = grpd.get_group(1)[val_col].values
    ureg_data = grpd.get_group(0)[val_col].values   

    # Setup model
    with pm.Model() as model:
        # Priors for means of Student-T dists
        reg_mean = pm.Normal('regulated_mean', mu=mean_all, sd=std_all*sig_fac)
        ureg_mean = pm.Normal('unregulated_mean', mu=mean_all, sd=std_all*sig_fac)

        # Priors for std. dev. of Student-T dists
        reg_std = pm.Uniform('regulated_std', lower=unif_l, upper=unif_u)
        ureg_std = pm.Uniform('unregulated_std', lower=unif_l, upper=unif_u)

        # Prior for v of Student-T dists
        nu = pm.Exponential('v_minus_one', 1./29.) + 1

        # Define Student-T dists
        # PyMC3 uses precision = 1 / (sd^2) to define dists rather than std. dev.
        reg_lam = reg_std**-2
        ureg_lam = ureg_std**-2

        reg = pm.StudentT('regulated', nu=nu, mu=reg_mean, lam=reg_lam, observed=reg_data)
        ureg = pm.StudentT('unregulated', nu=nu, mu=ureg_mean, lam=ureg_lam, observed=ureg_data)

        # Quantities of interest (difference of means and std. devs.)
        diff_of_means = pm.Deterministic('difference_of_means', reg_mean - ureg_mean)
        diff_of_stds = pm.Deterministic('difference_of_stds', reg_std - ureg_std)
        
        # Run sampler to approximate posterior
        if mcmc == 'metropolis':
            trace = pm.sample(steps, step=pm.Metropolis())
        elif mcmc == 'slice':
            trace = pm.sample(steps, step=pm.Slice())
        elif mcmc == 'nuts':
            trace = pm.sample(steps)
        else:
            raise ValueError("mcmc must be one of ['metropolis', 'slice', 'nuts']")

    # Plot results
    # Raw data
    fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(14,4))
    
    for name, grp in grpd:
        sn.distplot(grp[val_col].values, ax=axes[name], kde=False)
        axes[name].set_title('Regulated = %s' % name)        

    # Traces
    if plot_trace:
        pm.traceplot(trace)
    
    # Posteriors for variables
    if plot_vars:
        pm.plot_posterior(trace[1000:],
                          varnames=['regulated_mean', 'unregulated_mean', 
                                    'regulated_std', 'unregulated_std'],
                          alpha=0.3)

    # Posteriors for differences
    if plot_diffs:
        pm.plot_posterior(trace[1000:],
                          varnames=['difference_of_means', 'difference_of_stds'],
                          ref_val=0,
                          alpha=0.3)
        
    # Posterior predictive check
    if plot_ppc:
        ppc = pm.sample_ppc(trace, samples=500, model=model, size=100)

        fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(14,4))

        sn.distplot([n.mean() for n in ppc['unregulated']], ax=axes[0])
        axes[0].axvline(ureg_data.mean(), c='k')
        axes[0].set(title='Posterior predictive of the mean (unregulated)', 
                    xlabel='Mean', 
                    ylabel='Frequency')

        sn.distplot([n.mean() for n in ppc['regulated']], ax=axes[1])
        axes[1].axvline(reg_data.mean(), c='k')
        axes[1].set(title='Posterior predictive of the mean (regulated)', 
                    xlabel='Mean', 
                    ylabel='Frequency')
    
    return trace
Example #41
0
    beta2 = pm.Normal('beta2', mu_2, sd_2, shape=companiesABC)
    beta1 = pm.Normal('beta1', mu_1, sd_1, shape=companiesABC)
    beta = pm.Normal('beta', 0, 100)
    u = pm.Normal('u', 0, 0.01)

    beta_mu = pm.Deterministic('beta_mu', tt.exp(u + beta + \
                                             (beta1[Num_shared] * xs_year + beta2[Num_shared] * xs_char1 +\
                                              beta3[Num_shared] * xs_char2 + beta4[Num_shared] * xs_year * xs_year)))

    Observed = pm.Weibull("Observed",
                          alpha=alpha,
                          beta=beta_mu,
                          observed=ys_faults)  # 观测值
    trace_1 = pm.sample(10000, init='advi+adapt_diag')

pm.traceplot(trace_1,
             varnames=['beta', 'beta1', 'beta2', 'beta3', 'beta4', 'u'])
plt.show()

burnin = 9000
chain = trace_1[burnin:]
# get MAP estimate
varnames2 = ['beta', 'beta1', 'beta2', 'beta3', 'beta4', 'u']
tmp = pm.df_summary(chain, varnames2)
betaMAP = tmp['mean'][0]
beta1MAP = tmp['mean'][np.arange(companiesABC) + 1]
beta2MAP = tmp['mean'][np.arange(companiesABC) + 1 * companiesABC + 1]
beta3MAP = tmp['mean'][np.arange(companiesABC) + 2 * companiesABC + 1]
beta4MAP = tmp['mean'][np.arange(companiesABC) + 3 * companiesABC + 1]
uMAP = tmp['mean'][4 * companiesABC + 1]
# am0MAP = tmp['mean'][4*companiesABC+2]
# am1MAP = tmp['mean'][4*companiesABC+3]
    steps = pm.Metropolis()
    trace = pm.sample(20000, steps, start=start, progressbar=True)

# EXAMINE THE RESULTS
burnin = 2000
thin = 50

# Print summary for each trace
#pm.summary(trace[burnin::thin])
#pm.summary(trace)

# Check for mixing and autocorrelation
#pm.autocorrplot(trace[burnin::thin], vars=model.unobserved_RVs[:-1])

## Plot KDE and sampled values for each parameter.
pm.traceplot(trace[burnin::thin])
#pm.traceplot(trace)


# Extract values of 'a'
a0_sample = trace['a0'][burnin::thin]
b1_sample = trace['b1'][burnin::thin]
b2_sample = trace['b2'][burnin::thin]
b1b2_sample = trace['b1b2'][burnin::thin]

b0_sample = a0_sample * np.std(y) + np.mean(y)
b1_sample = b1_sample * np.std(y)
b2_sample = b2_sample * np.std(y)
b1b2_sample = b1b2_sample * np.std(y)

Example #43
0
    theta_real = 0.35  # unkwon value in a real experiment
    alpha_prior = 1
    beta_prior = 1
    data = stats.bernoulli.rvs(p=theta_real, size=n_experiments)
    data_sum = data.sum()

    with pm.Model() as our_first_model:
        # a priori
        theta = pm.Beta('theta', alpha=1, beta=1)
        # likelihood
        y = pm.Bernoulli('y', p=theta, observed=data)
        #y = pm.Binomial('theta',n=n_experimentos, p=theta, observed=sum(datos))
        start = pm.find_MAP()
        step = pm.Metropolis()
        trace = pm.sample(2000, step=step, start=start, nchains=1)
        burnin = 0  # no burnin
        chain = trace[burnin:]
        pm.traceplot(chain, lines={'theta': theta_real})

    plt.figure()
    x = np.linspace(.2, .6, 1000)
    func = stats.beta(a=alpha_prior + data_sum,
                      b=beta_prior + n_experiments - data_sum)
    y = func.pdf(x)

    plt.plot(x, y, 'r-', lw=3, label='True distribution')
    plt.hist(chain['theta'],
             bins=30,
             normed=True,
             label='Estimated posterior distribution')
Example #44
0
from data.data_generation import generate_data
from visualizing.plot_kde import plot_kde
from visualizing.predictive_uncertaity import plot_predictive_uncertainty

# HOW TO USE THE MODEL
if __name__ == '__main__':
    n = 500
    d = 5

    # generating data example
    train_X, test_X, train_y, test_y = generate_data(n, d, 0.2)
    train_X, test_X = _adding_intecept(train_X), _adding_intecept(test_X)

    # using model example
    model = Model(n_draws=1000, init_model=None)
    model.fit(train_X, train_y)
    pred, err = model.predict(test_X, with_error=True)
    pm.traceplot(model.trace)

    # plotting uncertainty plots example
    plot_predictive_uncertainty(test_X, test_y, pred, err)

    # plotting kde for given samples example
    fig, ax = plt.subplots()
    coef = model.trace['w'].T
    for w in coef:
        plot_kde(w, ax=ax)

    # display the plots
    plt.show()
Example #45
0
     )
assert_array_almost_equal(
    hierarchical_trace['hyper_alpha_mu'][-5:],
    [ 2.72175 ,  2.72175 ,  2.72175 ,  2.533818,  2.533818]
    )
assert_array_almost_equal(
    hierarchical_trace['hyper_beta_mu'][-5:],
    [ 0.243719,  0.237223,  0.237223,  0.237223,  0.206227]
)


# ## Model Checking

# In[15]:

pm.traceplot(hierarchical_trace[25000:], varnames=['mu', 'hyper_alpha_mu', 'hyper_beta_mu']);


# In[16]:

x_lim = 60
n_burn = 25000

# we discard burn-in and use every 1000th trace
y_pred = hierarchical_trace.get_values('y_pred')[n_burn::1000].ravel()

sns.set_style('white')
fig, ax = plt.subplots(2, sharex=True, figsize=(12,6))

ax[0].hist(y_pred, range=[0, x_lim], bins=x_lim, histtype='stepfilled')   
ax[0].set_xlim(1, x_lim)
Example #46
0
assert_almost_equal(trace['sigma'][1002], 13.8586428904)


# In[9]:

assert_almost_equal(trace['alpha'][1125:1130], [ 0.53256295, -0.57260872,  1.68859471,  0.31409282, -1.54722002])
assert_almost_equal(trace['beta'][145:150], [ 0.14140093,  0.14140093,  0.13782623,  0.13112726,  0.13281072])
assert_almost_equal(trace['sigma'][670:675], [ 18.09469578,  21.51386513,  13.30334073,  14.80010898,  19.32803484])


# # Visualizing

# In[10]:

sns.set_style('darkgrid')
pm.traceplot(trace[-1000:], ['alpha', 'beta', 'sigma'])
plt.show()


# In[11]:

n_samples = 1000

x = local.Distance.values
y = local.AirTime.values

sns.set_style('white')

fig, ax = plt.subplots()

ax.scatter(x, y, c=sns.xkcd_rgb['denim blue'], label = 'Sample Data')
Example #47
0
def run(n=2000):
    model = build_model()
    with model:
        trace = pm.sample(n, nuts_kwargs={'target_accept':.99})

    pm.traceplot(trace)
Example #48
0
import matplotlib.pyplot as plt
from pymc_fit import ReadData

curr_dir = os.path.dirname(os.path.realpath(__file__))
trained_file_paths = [
    os.path.abspath(fp) for fp in glob.glob(os.path.join(curr_dir, '*.pkl'))
]
result_dir = os.path.dirname(curr_dir, 'result')
if not os.path.isdir(result_dir): os.mkdir(result_dir)

for trained_file_path in trained_file_paths:
    model_name = os.path.basename(trained_file_path)[:-4]
    with open(trained_file_path, 'rb') as trained_file_obj:
        trained_trace = pickle.load(trained_file_obj)

    axes = pm.traceplot(trace=trained_trace)
    axes[0][0].figure.savefig(
        os.path.join(result_dir, 'parameters_plot_{}.png'.format(model_name)))

    plt.figure()
    plt.title('Log volatility')
    plt.plot(trained_trace['s'].T, 'b', alpha=.03)
    plt.xlabel('Time')
    plt.ylabel('Log volatility')
    plt.title('Fig 2. ln(volatility)')
    plt.savefig(
        os.path.join(result_dir, 'log_volatility_{}.png'.format(model_name)))

    returns = ReadData().train['vwretd'].as_matrix()
    plt.figure()
    plt.plot(np.abs(returns))
Example #49
0
                                                      s['p_logodds'],
                                                      map_est['p_logodds'],
                                                      model.logp(map_est)))

# By default `basinhopping` uses a gradient minimization technique,
# `fmin_bfgs`, resulting in inaccurate predictions many times. If we force
# `basinhoping` to use a non-gradient technique we get much better results

with model:
    for i in range(n+1):
        s = {'p_logodds': 0.0, 'surv_sim': i}
        map_est = mc.find_MAP(start=s, vars=model.vars,
                              fmin=bh, minimizer_kwargs={"method": "Powell"})
        print('surv_sim: %i->%i, p: %f->%f, LogP:%f'%(s['surv_sim'],
                                                      map_est['surv_sim'],
                                                      s['p_logodds'],
                                                      map_est['p_logodds'],
                                                      model.logp(map_est)))

# Confident in our MAP estimate we can sample from the posterior, making sure
# we use the `Metropolis` method for our discrete variables.

with model:
    step1 = mc.step_methods.HamiltonianMC(vars=[p])
    step2 = mc.step_methods.Metropolis(vars=[surv_sim])

with model:
    trace = mc.sample(25000, [step1, step2], start=map_est)

mc.traceplot(trace);
Example #50
0
def run(n=1000):
    if n == "short":
        n = 50
    with model:
        trace = pm.sample(n)
    pm.traceplot(trace, varnames=["x"])
Example #51
0
# and the other indexes the shape of the parameter. Thus for this example:

# In[8]:

trace[y].shape


# Out[8]:

#     (3000, 2, 1)

# `traceplot` is a summary plotting function for a trace.

# In[9]:

pm.traceplot(trace);


# Out[9]:

# image file:

# ## PyMC Internals
#
# ### Model
#
# The `Model` class has very simple internals: just a list of unobserved variables (`Model.vars`) and a list of factors which go into computing the posterior density (`Model.factors`) (see model.py for more).
#
# A Python "`with model:`" block has `model` as the current model. Many functions, like `find_MAP` and `sample`, must be in such a block to work correctly by default. They look in the current context for a model to use. You may also explicitly specify the model for them to use. This allows us to treat the current model as an implicit parameter to these functions.
#
# ### Distribution Classes
Example #52
0
    # similar notation to PyMC2 can be used for simple distributions
    @theano.compile.ops.as_op(itypes=[t.lscalar, t.dscalar, t.dscalar],
                              otypes=[t.dvector])
    def rate(switchpoint, early_mean, late_mean):
        out = np.empty(len(data))
        out[:switchpoint] = early_mean
        out[switchpoint:] = late_mean
        return out.flatten()

    # need to explicitly define inputs for "rate" to run
    accidents = pm.Poisson('accidents',
                           mu=rate(switchpoint, early_mean, late_mean),
                           observed=data)

    # no support for dag in PyMC3
    # we do it with theano instead
    # install pydotprint for python 3: ' pip3 install pydotprint '
    # install graphviz
    # pydotprint(inferAccidents_Model.logpt)

    # define iteration start
    start = pm.find_MAP()

    # MCMC in PyMC3
    step = pm.Metropolis()
    trace = pm.sample(1e4, start=start, step=step, model=inferAccidents_Model)

# show our amazing results
pm.traceplot(trace[0:])
plt.show()
Example #53
0
from pymc3 import find_MAP 
map_estimate = find_MAP(model=basic_model) 
print(map_estimate)

from scipy import optimize
map_estimate = find_MAP(model=basic_model, fmin=optimize.fmin_powell)

print(map_estimate)

from pymc3 import NUTS, sample

with basic_model:
	# obtain starting values via MAP 
	start = find_MAP(fmin=optimize.fmin_powell)

	# instantiate sampler 
	step = NUTS(scaling=start)

	# draw 2000 posterior samples 
	trace = sample(2000, step, start=start)

trace['alpha'][-5:]

from pymc3 import traceplot

traceplot(trace)

from pymc3 import summary

summary(trace['alpha'])
data = np.array([51.06, 55.12, 53.73, 50.24, 52.05, 56.40, 48.45, 52.34, 55.65, 51.49, 51.86, 63.43, 53.00, 56.09, 51.93, 52.31, 52.33, 57.48, 57.44, 55.14, 53.93, 54.62, 56.09, 68.58, 51.36, 55.47, 50.73, 51.94, 54.95, 50.39, 52.91, 51.5, 52.68, 47.72, 49.73, 51.82, 54.99, 52.84, 53.19, 54.52, 51.46, 53.73, 51.61, 49.81, 52.42, 54.3, 53.84, 53.16])
quantile = np.percentile(data, [25, 75])
iqr = quantile[1] - quantile[0]
upper = quantile[1] + iqr * 1.5
lower = quantile[0] - iqr * 1.5
clean_data = data[(data > lower) & (data < upper)]
sns.kdeplot(data)
plt.xlabel('$x$', fontsize=16)

with pm.Model() as Gaussian_model:
    mu = pm.Uniform('mu', lower=40, upper=70)
    sigma = pm.HalfNormal('sigma', sd=10)
    y = pm.Normal('y', mu=mu, sd=sigma, observed=data)
    trace = pm.sample(1100)
chain = trace[100:]
pm.traceplot(chain)

# summarize
pm.summary(chain)

# predictive sample
y_pred = pm.sample_ppc(chain, 100, Gaussian_model, size=len(data))
sns.kdeplot(data, color='b')
for draw in y_pred['y']:
    sns.kdeplot(draw.flatten(), color='r', alpha=0.1)
plt.title('Gaussian model', fontsize=16)
plt.xlabel('$x$', fontsize=16)

>>>>>>>>>>>>>>>>>>  Gaussian robust inferences  <<<<<<<<<<<<<<<<<<<<< #
plt.figure(figsize=(8, 6))
x = np.linspace(-10, 10, 200)
Example #55
0
                trace = mc.sample(nsamples, step=step, start=start, njobs=self.njobs, trace=backend)
        return trace




if __name__ == "__main__":
    def real_func():
        x = np.linspace(0.01, 1.0, 10)
        f = x + np.random.randn(len(x))*0.01
        return f
        
    def model_func(beta):
        x = np.linspace(0.01, 1.0, 10)
        f = beta
        return f

    data = real_func()
    tau_obs = np.eye(10)/.01**2
    tau_prior = np.eye(10)/1.0**2
    beta_prior = np.ones_like(data)*1.0
    beta_map = np.linspace(0.01, 1.0, 10) + np.random.randn(10)*0.1
    sampler = MCMCSampler(model_func, data, tau_obs, beta_prior, tau_prior, beta_map, is_cov=False, method=None)
    trace = sampler.sample(2000)
    mc.summary(trace)
    mc.traceplot(trace)
    plt.figure()
    plt.plot(beta_map, label='ACTUAL')
    plt.plot(np.mean(trace['beta'][:,:], axis=0), label='MCMC')
    plt.show()
Example #56
0
def run(n=2000):
    model = build_model()
    with model:
        trace = pm.sample(n, nuts_kwargs={'target_accept': .99})

    pm.traceplot(trace)
Example #57
0
def robust_lin_reg(df, var_map, 
                   steps=2000, mcmc='metropolis',
                   plot_trace=True, plot_vars=True):
    """ Robust Bayesian linear regression.
    
    Args:
        df         Dataframe. Must have a column containing values
                   and a categorical 'regulated' column that is [0, 1]
                   to define the two groups
        val_map    Dict specifying x and y vars: {'x':'expl_var',
                                                  'y':'resp_var'}
        steps      Number of steps to take in MCMC chains
        mcmc       Sampler to use: ['metropolis', 'slice', 'nuts']
        plot_trace Whether to plot the MCMC traces
        plot_vars  Whether to plot posteriors for variables
    
    Returns:
        Creates plots showing the distribution of differences in 
        means and variances, plus optional diagnostics. Returns the 
        MCMC trace
    """
    import pymc3 as pm
    import pandas as pd
    import matplotlib.pyplot as plt
    import numpy as np
    import theano 
    
    # Get cols
    df = df[var_map.values()]
    
    # Swap keys and values
    var_map_rev = dict((v,k) for k,v in var_map.iteritems())
    
    # Convert df columns to x and y
    df.columns = [var_map_rev[i] for i in df.columns]

    with pm.Model() as model:
        # Priors
        nu = pm.Exponential('v_minus_one', 1./29.) + 1
        
        # The patsy string below automatically assumes mu=0 and estimates
        # lam = (1/s.d.**2), so don't need to add these. Do need to add 
        # prior for nu though.
        family = pm.glm.families.StudentT(nu=nu)
        
        # Define model
        pm.glm.glm('y ~ x', df, family=family)
        
        # Find MAP as starting point
        start = pm.find_MAP()

        # Run sampler to approximate posterior
        if mcmc == 'metropolis':
            step = pm.Metropolis()
            trace = pm.sample(steps, step, start=start)
        elif mcmc == 'slice':
            step = pm.Slice()
            trace = pm.sample(steps, step, start=start)
        elif mcmc == 'nuts':
            step = pm.NUTS(scaling=start)
            trace = pm.sample(steps, step)
        else:
            raise ValueError("mcmc must be one of ['metropolis', 'slice', 'nuts']")

    # Traces
    if plot_trace:
        pm.traceplot(trace)
    
    # Posteriors for variables
    if plot_vars:
        pm.plot_posterior(trace[-1000:],
                          varnames=['v_minus_one', 'lam'],
                          alpha=0.3)

        pm.plot_posterior(trace[1000:],
                          varnames=['x', 'Intercept'],
                          ref_val=0,
                          alpha=0.3)
        
    # PPC
    fig = plt.figure(figsize=(10, 10))
    ax = fig.add_subplot(111, xlabel=var_map['x'], ylabel=var_map['y'])
    ax.scatter(df.x, df.y, marker='o', label='Data')
    pm.glm.plot_posterior_predictive(trace, samples=50, eval=df.x,
                                     label='PPC', alpha=0.3)
    
    return trace
Example #58
0
        'mu',
        tt.exp(beta[companyABC] + beta1[companyABC] * elec_year +
               beta2 * elec_tem))

    # Observed_pred = pm.Weibull("Observed_pred",  alpha=mu, beta=sigma, shape=elec_faults.shape)  # 观测值
    Observed = pm.Weibull("Observed",
                          alpha=mu,
                          beta=sigma,
                          observed=elec_faults)  # 观测值

    # start = pm.find_MAP()
    # step = pm.Metropolis([Observed])
    trace2 = pm.sample(1000)
chain2 = trace2
varnames1 = ['sigma', 'mu']
pm.traceplot(chain2)
plt.show()
print(pm.dic(trace2, unpooled_model))

print(pm.df_summary(trace2, varnames1))
# com_pred = chain2.get_values('Observed_pred')[:].ravel()
# plt.hist(com_pred,  range=[0, 2], bins=130, histtype='stepfilled')
# plt.axvline(elec_faults.mean(), color='r', ls='--', label='True mean')
# plt.show()
# # 画出自相关曲线
# pm.autocorrplot(chain2, varnames2)
# plt.show()
#
with unpooled_model:
    post_pred = pm.sample_ppc(trace2, samples=1000)
plt.figure()
	N_samples = [30, 30, 30]  # total number of each groups
	G_samples = [18, 18, 18]  # record of the number of good-quality samples

	group_idx = np.repeat(np.arange(len(N_samples)), N_samples)
	data = []
	for i in range(0, len(N_samples)):
		data.extend(np.repeat([1, 0], [G_samples[i], N_samples[i]-G_samples[i]]))
	print(group_idx, data)

	base_name = os.path.basename(__file__)[:-3]
	with pm.Model() as model_h,\
			matplotlib.backends.backend_pdf.PdfPages('%s.pdf' % base_name) as pdf_all:
		# prior
		alpha = pm.HalfCauchy('alpha', beta=10)
		beta = pm.HalfCauchy('beta', beta=10)
		theta = pm.Beta('theta', alpha, beta, shape=len(N_samples))

		# likehood
		y = pm.Bernoulli('y', p=theta[group_idx], observed=data)

		trace = pm.sample(2000, njobs=1)

		chain = trace[200:]
		fig = plt.figure()
		pm.traceplot(chain)
		pdf_all.savefig()
		
		# mean, standard deviation, and the HPD intervals
		print(pm.summary(trace))

Example #60
0
    alpha = pm.Normal('alpha', mu=0, sd=10)
    beta = pm.Normal('beta', mu=0, sd=10)

    mu = alpha + pm.math.dot(x_0, beta)
    theta = pm.Deterministic('theta', 1 / (1 + pm.math.exp(-mu)))
    bd = pm.Deterministic('bd', -alpha / beta)

    yl = pm.Bernoulli('yl', theta, observed=y_0)

    start = pm.find_MAP()
    step = pm.NUTS()
    trace_0 = pm.sample(5000, step, start)

chain_0 = trace_0[100:]
varnames = ['alpha', 'beta', 'bd']
pm.traceplot(chain_0, varnames)
plt.show()

theta = chain_0['theta'].mean(axis=0)
idx = np.argsort(x_0)
plt.plot(x_0[idx], theta[idx], color='b', lw=3)
plt.axvline(chain_0['bd'].mean(), ymax=1, color='r')
bd_hpd = pm.hpd(chain_0['bd'])
plt.fill_betweenx([0, 1], bd_hpd[0], bd_hpd[1], color='r', alpha=0.5)

plt.plot(x_0, y_0, 'o', color='k')
theta_hpd = pm.hpd(chain_0['theta'])[idx]
plt.fill_between(x_0[idx],
                 theta_hpd[:, 0],
                 theta_hpd[:, 1],
                 color='b',