S = 2000
burn = 500
print('Running simulation...')
print('Parameters:\nN={:d}\nT={:.2f}\ndt_max={:.2f}\nS={:d}\n'.format(
    N, T, dt_max, S))

# Construct network
model = NetworkPoisson(N=N, dt_max=dt_max)
stable = False
while not stable:
    model.init_parameters()
    stable = model.check_stability()

# Generate data
print('Generating data...', end='')
data = model.generate_data(T=T)
print('(size={})'.format(len(data[0])))

# Plot the data
model.plot_data(data)

# Sample the posterior (cython extension)
lambda0_, W_, mu_, tau_ = model.sample_ext(data, T=T, size=S, method='cython')

# Plot the sample
plot_sample(lambda0_, model.lamb, 'lambda0')
plot_sample(W_, model.W, 'W')
plot_sample(mu_, model.mu, 'mu')
plot_sample(tau_, model.tau, 'tau')
model.plot_impulse(mu=np.median(mu_[0, 0, :]), tau=np.median(tau_[0, 0, :]))
Пример #2
0
        if len(events[0]) > max_events:
            print('Event count exceeds max_events; skipping\n')
            with open(
                    '/Users/colinswaney/Desktop/output_date={}_grp={}.txt'.
                    format(date, group), 'a') as fout:
                fout.write('{}\n'.format(name))
            events = None

        # Generate MCMC sample
        if (events is not None):
            # Create a network
            model = NetworkPoisson(N=N, dt_max=dt_max)
            # Gibbs sampling
            print("Beginning sampling... (M = {})".format(len(events[0])))
            start = time.time()
            lambda0, W, mu, tau = model.sample_ext(events, T=T, size=nsamples)
            print("Finished MCMC sampling.")
            # Save samples.
            with h5.File(write_path, 'a') as hdf:
                print('Writing sample to HDF5...')
                hdf.create_dataset(name='{}/{}/{}'.format(
                    name, date, 'lambda0'),
                                   data=lambda0)
                hdf.create_dataset(name='{}/{}/{}'.format(name, date, 'W'),
                                   data=W)
                hdf.create_dataset(name='{}/{}/{}'.format(name, date, 'mu'),
                                   data=mu)
                hdf.create_dataset(name='{}/{}/{}'.format(name, date, 'tau'),
                                   data=tau)
                print('Done.\n')