def plot_intensities(events, lambda0, W, mu, tau, path): """Plot the intensity given event data and fitted parameters. lambda0: N W, mu, tau: N x N """ seconds = pd.date_range(start='01-02-2013 10:30:00', end='01-02-2013 14:59:59', freq='S') hours = pd.date_range(start='01-02-2013 10:30:00', end='01-02-2013 14:59:59', freq='H') def plot_intensity_pair(Y, pair, label): """Y is a T x N matrix of intensities.""" i, j = pair plt.fill_between(seconds, y1=0, y2=Y[:, i], alpha=0.5, color='C0') plt.fill_between(seconds, y1=0, y2=Y[:, j], alpha=0.5, color='C0') plt.ylabel(label, fontsize=8) plt.yticks(fontsize=8) plt.xticks(hours, [h.strftime('%H:%M:%S') for h in hours], fontsize=8) plt.xlim([seconds[0], seconds[-1]]) # Make a model N, = lambda0.shape params = {'lamb': lambda0, 'weights': W, 'mu': mu, 'tau': tau} model = NetworkPoisson(N=N, dt_max=dt_max, params=params) # Compute intensity grid = np.linspace(0, T, T + 1) # dt = 1 Lambda = model.compute_intensity(events, grid) # Plot plt.subplot(3, 2, 1) plot_intensity_pair(Lambda, (0, 3), label='ADDS, Level 0') plt.subplot(3, 2, 3) plot_intensity_pair(Lambda, (1, 4), label='ADDS, Level 1') plt.subplot(3, 2, 5) plot_intensity_pair(Lambda, (2, 5), label='ADDS, Level 2') plt.subplot(3, 2, 2) plot_intensity_pair(Lambda, (6, 7), label='CANCELS, Level 1') plt.subplot(3, 2, 4) plot_intensity_pair(Lambda, (8, 9), label='CANCELS, Level 2') plt.subplot(3, 2, 6) plot_intensity_pair(Lambda, (10, 11), label='EXECUTES') # Save figures plt.tight_layout() # plt.savefig(path + 'intensity_{}.eps'.format(date)) # plt.savefig(path + 'intensity_{}.pdf'.format(date)) plt.show() plt.clf()
def post_process(name, date, dt_max, burn=500): N = 12 model = NetworkPoisson(N, dt_max) sample_path = '/Volumes/datasets/ITCH/samples/large2007_dt_max={}.hdf5'.format( dt_max) # Import samples samples = import_samples(sample_path, name, date, burn) if samples is not None: # Unpack samples lambda0, W, mu, tau = samples # Compute point estimates lambda0 = np.median(lambda0, axis=1) W = np.median(W, axis=2).reshape(N * N) # row major mu = np.median(mu, axis=2).reshape(N * N) # row major tau = np.median(tau, axis=2).reshape(N * N) # row major estimates = [name, date] + list(np.concatenate([lambda0, W, mu, tau])) # Check stability model.lamb = lambda0 model.W = W.reshape((N, N)) model.mu = mu.reshape((N, N)) model.tau = tau.reshape((N, N)) _, maxeig = model.check_stability(return_value=True) eigenvalue = [name, date, maxeig] return estimates, eigenvalue
def check_stability(lambda0, W, mu, tau, dt_max): """Check if the model is stable for given parameter estimates.""" N, _ = W.shape model = NetworkPoisson(N=N, dt_max=dt_max) model.lamb = lambda0 model.W = W model.mu = mu model.tau = tau return model.check_stability(return_value=True)
from fawkes.models import NetworkPoisson import numpy as np verbose = False # Make a network model N = 2 T = 10.0 dt_max = 1.00 bias = np.array([1.0, 1.0]) weights = np.array([[0.5, 0.1], [0.1, 0.5]]) mu = 0.0 * np.ones((N,N)) tau = 1.0 * np.ones((N,N)) params = {'lamb': bias, 'weights': weights, 'mu': mu, 'tau': tau} model = NetworkPoisson(N=N, dt_max=dt_max, params=params) data = model.generate_data(T=T) # Generate some parents parents_cython = model.sample_parents_ext(data, bias, weights, mu, tau) parents = parents_cython + 1 # Compute sufficient statistics without Cython stats = model.model.calculate_stats(data, parents, T, dt_max) if verbose: print("M_0={}".format(stats[0])) print("M_m={}".format(stats[1])) print("M_nm={}".format(stats[2])) print("xbar_nm={}".format(stats[3])) print("nu_nm={}\n".format(stats[4])) # Compute sufficient statistics with cython
plt.tight_layout() plt.show() # Select parameters N = 2 T = 1000.0 dt_max = 1.0 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')
orders = [] n = 0 book = OrderBook() while n < N: order = random_limit_order() print('{}'.format(order)) if order.refno not in [order.refno for order in orders] and order.refno is not None: orders.append(order) if order.label == 'cancel': for o in orders: if o.refno == order.refno: orders.remove(o) book.update(order) n += 1 dt = 0.001 t_max = 60 N = 12 lambda0 = 0.4 * np.ones(N) W = 0.1 * np.eye(N) mu = -1.0 * np.ones((N,N)) tau = 1.0 * np.ones((N,N)) model = NetworkPoisson(N=N, dt_max=1.0, params={'lamb': lambda0, 'weights': W, 'mu': mu, 'tau': tau}) agent = Agent([None, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) simulator = Simulator(book, model, agent, dt) simulator.generate_events(t_max) simulator.plot(0, pause=True) simulator.run(t_max, pause=False)
times = times - t0 return (times, nodes), T # name = 'A' # name = 'LLY' name = 'SIG' mcmc_date = '072413' # test: '072513' event_date = '072513' norm = False log = True L = 100 # simulation size M = 180 # sample size N = 12 # network size dt_max = 60 model_net = NetworkPoisson(N=N, dt_max=dt_max) model_hom = HomogeneousPoisson(N=N) df = [] read_path = '/Volumes/datasets/ITCH/samples/large2007_dt_max=60.hdf5' write_path = '/Users/colinswaney/Desktop/threshold_name={}_date={}_M={}_norm={}.txt'.format( name, event_date, M, norm) with h5.File(read_path, 'r') as hdf: start = time.time() try: lambda0, W, mu, tau = import_samples(read_path, name, mcmc_date) except: print('Unable to import samples; skipping') for threshold in np.arange(-8, 1, .5): lambda0_, W_, mu_, tau_ = get_estimates((lambda0, W, mu, tau), threshold=threshold, log=log,
events = None if len(events[0]) == 0: print('Unable to find event data; skipping\n') events = None 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)
from fawkes.models import NetworkPoisson import numpy as np import time # Continuous-Time N = 2 T = 1000.0 dt_max = 1.00 bias = np.array([1.0, 1.0]) weights = np.array([[0.5, 0.2], [0.2, 0.5]]) mu = 0.0 * np.ones((N, N)) tau = 1.0 * np.ones((N, N)) params = {'lamb': bias, 'weights': weights, 'mu': mu, 'tau': tau} net = NetworkPoisson(N=N, dt_max=dt_max, params=params) data = net.generate_data(T=T) # Class Gibbs sampling # start = time.time() # sample = net.sample(data, T, size=10) # stop = time.time() # py_time = stop - start # print('elapsed time: {:.3f}\n'.format(py_time)) # # start = time.time() # sample = net.sample_ext(data, T, size=10) # stop = time.time() # cy_time = stop - start # print('elapsed time: {:.3f}'.format(cy_time)) # print('speed-up: {:.2f}\n'.format(py_time / cy_time))