def fit_network_hawkes_gibbs(S, K, C, dt, dt_max, output_path, standard_model=None): # Check for existing Gibbs results if os.path.exists(output_path + ".gibbs.pkl"): with open(output_path + ".gibbs.pkl", 'r') as f: print "Loading Gibbs results from ", (output_path + ".gibbs.pkl") (samples, timestamps) = cPickle.load(f) else: print "Fitting the data with a network Hawkes model using Gibbs sampling" # Make a new model for inference # test_model = DiscreteTimeNetworkHawkesModelGammaMixture(C=C, K=K, dt=dt, dt_max=dt_max, B=B, # alpha=1.0, beta=1.0/20.0) test_basis = IdentityBasis(dt, dt_max, allow_instantaneous=True) network_hypers = {'C': C, 'alpha': 1.0, 'beta': 1.0/10.0, 'tau1': 1.0, 'tau0': 10.0, 'allow_self_connections': False} test_model = DiscreteTimeNetworkHawkesModelSpikeAndSlab(K=K, dt=dt, dt_max=dt_max, basis=test_basis, network_hypers=network_hypers) test_model.add_data(S) # Initialize with the standard model parameters if standard_model is not None: test_model.initialize_with_standard_model(standard_model) plt.ion() im = plot_network(test_model.weight_model.A, test_model.weight_model.W, vmax=0.5) plt.pause(0.001) # Gibbs sample N_samples = 100 samples = [] lps = [test_model.log_probability()] timestamps = [] for itr in xrange(N_samples): if itr % 1 == 0: print "Iteration ", itr, "\tLL: ", lps[-1] im.set_data(test_model.weight_model.W_effective) plt.pause(0.001) # lps.append(test_model.log_probability()) lps.append(test_model.log_probability()) samples.append(test_model.resample_and_copy()) timestamps.append(time.clock()) # Save this sample with open(output_path + ".gibbs.itr%04d.pkl" % itr, 'w') as f: cPickle.dump(samples[-1], f, protocol=-1) # Save the Gibbs samples with open(output_path + ".gibbs.pkl", 'w') as f: print "Saving Gibbs samples to ", (output_path + ".gibbs.pkl") cPickle.dump((samples, timestamps), f, protocol=-1) return samples, timestamps
def fit_network_hawkes_gibbs_ss(S, K, C, B, dt, dt_max, output_path, p, standard_model=None): samples_and_timestamps = load_partial_results(output_path, typ="gibbs_ss") if samples_and_timestamps is not None: samples, timestamps = samples_and_timestamps else: print "Fitting the data with a spike and slab network Hawkes model using Gibbs sampling" # Make a new model for inference network_hypers = {'C': C, 'alpha': 1.0, 'beta': 1.0/20.0, 'p': p, 'v': 5.0,'c': np.arange(C).repeat((K // C))} test_model = DiscreteTimeNetworkHawkesModelSpikeAndSlab(K=K, dt=dt, dt_max=dt_max, B=B, network_hypers=network_hypers) test_model.add_data(S) # Initialize with the standard model parameters if standard_model is not None: test_model.initialize_with_standard_model(standard_model) # Gibbs sample N_samples = 1000 samples = [] lps = [] timestamps = [time.clock()] for itr in xrange(N_samples): lps.append(test_model.log_probability()) samples.append(test_model.resample_and_copy()) timestamps.append(time.clock()) print test_model.network.v if itr % 1 == 0: print "Iteration ", itr, "\t LP: ", lps[-1] # Save this sample with open(output_path + ".gibbs_ss.itr%04d.pkl" % itr, 'w') as f: cPickle.dump((samples[-1], timestamps[-1]-timestamps[0]), f, protocol=-1) # Save the Gibbs timestamps timestamps = np.array(timestamps) with open(output_path + ".gibbs_ss.timestamps.pkl", 'w') as f: print "Saving spike and slab Gibbs samples to ", (output_path + ".gibbs_ss.timestamps.pkl") cPickle.dump(timestamps, f, protocol=-1) # Save the Gibbs samples with open(output_path + ".gibbs_ss.pkl", 'w') as f: print "Saving Gibbs samples to ", (output_path + ".gibbs_ss.pkl") cPickle.dump((samples, timestamps[1:] - timestamps[0]), f, protocol=-1) return samples, timestamps
def fit_spikeslab_network_hawkes_gibbs(S, S_test, dt, dt_max, output_path, model_args={}, standard_model=None, N_samples=100, time_limit=8 * 60 * 60): T, K = S.shape # Check for existing Gibbs results if os.path.exists(output_path): with gzip.open(output_path, 'r') as f: print("Loading Gibbs results from ", output_path) results = pickle.load(f) else: print( "Fitting the data with a network Hawkes model using Gibbs sampling" ) test_model = DiscreteTimeNetworkHawkesModelSpikeAndSlab(K=K, dt=dt, dt_max=dt_max, **model_args) test_model.add_data(S) # Initialize with the standard model parameters if standard_model is not None: test_model.initialize_with_standard_model(standard_model) # TODO: Precompute F_test F_test = test_model.basis.convolve_with_basis(S_test) # Gibbs sample samples = [] lps = [test_model.log_probability()] hlls = [test_model.heldout_log_likelihood(S_test)] times = [0] for _ in progprint_xrange(N_samples, perline=10): # Update the model tic = time.time() test_model.resample_model() samples.append(copy.deepcopy(test_model.get_parameters())) times.append(time.time() - tic) # Compute log probability and heldout log likelihood # lps.append(test_model.log_probability()) hlls.append(test_model.heldout_log_likelihood(S_test, F=F_test)) # # Save this sample # with open(output_path + ".gibbs.itr%04d.pkl" % itr, 'w') as f: # cPickle.dump(samples[-1], f, protocol=-1) # Check if time limit has been exceeded if np.sum(times) > time_limit: break # Get cumulative timestamps timestamps = np.cumsum(times) lps = np.array(lps) hlls = np.array(hlls) # Make results object results = Results(samples, timestamps, lps, hlls) # Save the Gibbs samples with gzip.open(output_path, 'w') as f: print("Saving Gibbs samples to ", output_path) pickle.dump(results, f, protocol=-1) return results
def demo(seed=None): """ Create a discrete time Hawkes model and generate from it. :return: """ if seed is None: seed = np.random.randint(2**32) print("Setting seed to ", seed) np.random.seed(seed) ########################################################### # Load some example data. # See data/synthetic/generate.py to create more. ########################################################### data_path = os.path.join("data", "synthetic", "synthetic_K20_C4_T10000.pkl.gz") with gzip.open(data_path, 'r') as f: S, true_model = pickle.load(f) T = S.shape[0] K = true_model.K B = true_model.B dt = true_model.dt dt_max = true_model.dt_max ########################################################### # Initialize with MAP estimation on a standard Hawkes model ########################################################### init_with_map = True if init_with_map: init_len = T print("Initializing with BFGS on first ", init_len, " time bins.") init_model = DiscreteTimeStandardHawkesModel(K=K, dt=dt, dt_max=dt_max, B=B, alpha=1.0, beta=1.0) init_model.add_data(S[:init_len, :]) init_model.initialize_to_background_rate() init_model.fit_with_bfgs() else: init_model = None ########################################################### # Create a test spike and slab model ########################################################### # Copy the network hypers. # Give the test model p, but not c, v, or m network_hypers = true_model.network_hypers.copy() network_hypers['c'] = None network_hypers['v'] = None network_hypers['m'] = None test_network = StochasticBlockModel(K=K, **network_hypers) test_model = DiscreteTimeNetworkHawkesModelSpikeAndSlab( K=K, dt=dt, dt_max=dt_max, B=B, basis_hypers=true_model.basis_hypers, bkgd_hypers=true_model.bkgd_hypers, impulse_hypers=true_model.impulse_hypers, weight_hypers=true_model.weight_hypers, network=test_network) test_model.add_data(S) # F_test = test_model.basis.convolve_with_basis(S_test) # Initialize with the standard model parameters if init_model is not None: test_model.initialize_with_standard_model(init_model) # Initialize plots ln, im_net, im_clus = initialize_plots(true_model, test_model, S) ########################################################### # Fit the test model with Gibbs sampling ########################################################### N_samples = 50 samples = [] lps = [] # plls = [] for itr in range(N_samples): lps.append(test_model.log_probability()) # plls.append(test_model.heldout_log_likelihood(S_test, F=F_test)) samples.append(test_model.copy_sample()) print("") print("Gibbs iteration ", itr) print("LP: ", lps[-1]) test_model.resample_model() # Update plot if itr % 1 == 0: update_plots(itr, test_model, S, ln, im_clus, im_net) ########################################################### # Analyze the samples ########################################################### analyze_samples(true_model, init_model, samples, lps)
def execute_toy(self,mode="discrete",dt_max=3,N_samples=1000,network_priors={"p": 1.0, "allow_self_connections": False}): #np.random.seed(0) if mode == 'discrete': test_model1 = DiscreteTimeNetworkHawkesModelSpikeAndSlab(K=self.K, dt_max=dt_max, network_hypers=network_priors) test_model1.add_data(self.data) test_model1.initialize_with_standard_model(None) elif mode == 'continuous': test_model = ContinuousTimeNetworkHawkesModel(self.K, dt_max=dt_max, network_hypers=network_hypers) test_model.add_data(self.data,self.labels) ########################################################### # Fit the test model with Gibbs sampling ########################################################### samples = [] lps = [] #for itr in xrange(N_samples): # test_model1.resample_model() # lps.append(test_model1.log_probability()) # samples.append(test_model1.copy_sample()) test_model = DiscreteTimeStandardHawkesModel(K=self.K, dt_max=dt_max, allow_self_connections= False) #test_model.initialize_with_gibbs_model(test_model1) test_model.add_data(self.data) test_model.fit_with_bfgs() impulse = test_model1.impulse_model.impulses responses = {} #for i in range(3): # responses[str(i)] = [] # for j in range(3): # responses[str(i)].append({"key":"response: process "+str(i)+" to "+str(j),"values":[{"x":idx,"y":k} for idx,k in enumerate(impulse[:,i,j])]}) # with open('/Users/PauKung/hawkes_demo/webapp/static/data/response'+str(i)+'.json','w') as outfile: # json.dump({"out":responses[str(i)]},outfile) # calculate convolved basis rr = test_model.basis.convolve_with_basis(np.ones((dt_max*2,self.K))) impulse = np.sum(rr, axis=2) impulse[dt_max:,:] = 0 for i in range(3): responses[str(i)] = {"key":"response: process "+str(i),"values":[{"x":idx,"y":k} for idx,k in enumerate(impulse[:,i])]} with open('/Users/PauKung/hawkes_demo/webapp/static/data/response'+str(i)+'.json','w') as outfile: json.dump({"out":responses[str(i)]},outfile) rates = test_model.compute_rate()#self.compute_rate(test_model,mode,dt_max) inferred_rate = {} S,F = test_model.data_list[0] print F for i in range(3): inferred_rate[str(i)] = [] inferred_rate[str(i)].append({"key":"background", "values":[[j,test_model.bias[i]] for j in range(self.T)]}) #"values":[[j,test_model1.bias_model.lambda0[i]] for j in range(self.T)]}) for i in range(3): inferred_rate[str(i)].append({"key":"influence: process"+str(i), "values":[[idx,j-test_model.bias[i]] for idx,j in enumerate(rates[:,i])]}) with open('/Users/PauKung/hawkes_demo/webapp/static/data/infer'+str(i)+'.json','w') as outfile: json.dump({"out":inferred_rate[str(i)]},outfile) # output response function diagram (K x K timeseries) #plt.subplot(3,3,1) #for i in range(3): # for j in range(3): # plt.subplot(3,3,3*i+(j+1)) # plt.plot(np.arange(4),impulse[:,i,j],color="#377eb8", lw=2) #plt.savefig(fpath+"response_fun.png",transparent=True) # output background bias diagram (K x 1 timeseries) #plt.subplot(3,1,1) #for i in range(3): # plt.subplot(3,1,i+1) # plt.plot(np.arange(4),[test_model.bias_model.lambda0[i] for j in range(4)],color="#333333",lw=2) #plt.savefig(fpath+"bias.png",transparent=True) # output inferred rate diagram (K x 1 timeseries) #test_figure, test_handles = test_model.plot(color="#e41a1c", T_slice=(0,self.T)) #plt.savefig(fpath+"inferred_rate.png",transparent=True) print test_model.W return test_model.W, inferred_rate, responses
def fit_network_hawkes_gibbs(S, K, C, dt, dt_max, output_path, standard_model=None): # Check for existing Gibbs results if os.path.exists(output_path + ".gibbs.pkl"): with open(output_path + ".gibbs.pkl", "r") as f: print "Loading Gibbs results from ", (output_path + ".gibbs.pkl") (samples, timestamps) = cPickle.load(f) else: print "Fitting the data with a network Hawkes model using Gibbs sampling" # Make a new model for inference # test_model = DiscreteTimeNetworkHawkesModelGammaMixture(C=C, K=K, dt=dt, dt_max=dt_max, B=B, # alpha=1.0, beta=1.0/20.0) test_basis = IdentityBasis(dt, dt_max, allow_instantaneous=True) # Set the network prior such that E[W] ~= 0.01 # W ~ Gamma(kappa, v) for kappa = 1.25 => v ~ 125 # v ~ Gamma(alpha, beta) for alpha = 10, beta = 10 / 125 E_W = 0.01 kappa = 10.0 E_v = kappa / E_W alpha = 10.0 beta = alpha / E_v network_hypers = { "C": 2, "kappa": kappa, "alpha": alpha, "beta": beta, "p": 0.8, "allow_self_connections": False, } test_model = DiscreteTimeNetworkHawkesModelSpikeAndSlab( K=K, dt=dt, dt_max=dt_max, basis=test_basis, network_hypers=network_hypers ) test_model.add_data(S) # Initialize with the standard model parameters if standard_model is not None: test_model.initialize_with_standard_model(standard_model) plt.ion() im = plot_network(test_model.weight_model.A, test_model.weight_model.W, vmax=0.5) plt.pause(0.001) # Gibbs sample N_samples = 100 samples = [] lps = [test_model.log_probability()] timestamps = [] for itr in xrange(N_samples): if itr % 1 == 0: print "Iteration ", itr, "\tLL: ", lps[-1] im.set_data(test_model.weight_model.W_effective) plt.pause(0.001) # lps.append(test_model.log_probability()) lps.append(test_model.log_probability()) samples.append(test_model.resample_and_copy()) timestamps.append(time.clock()) # Save this sample with open(output_path + ".gibbs.itr%04d.pkl" % itr, "w") as f: cPickle.dump(samples[-1], f, protocol=-1) # Save the Gibbs samples with open(output_path + ".gibbs.pkl", "w") as f: print "Saving Gibbs samples to ", (output_path + ".gibbs.pkl") cPickle.dump((samples, timestamps), f, protocol=-1) return samples, timestamps
def fit_spikeslab_network_hawkes_gibbs(S, S_test, dt, dt_max, output_path, model_args={}, standard_model=None, N_samples=100, time_limit=8*60*60): T,K = S.shape # Check for existing Gibbs results if os.path.exists(output_path): with gzip.open(output_path, 'r') as f: print "Loading Gibbs results from ", output_path results = cPickle.load(f) else: print "Fitting the data with a network Hawkes model using Gibbs sampling" test_model = DiscreteTimeNetworkHawkesModelSpikeAndSlab(K=K, dt=dt, dt_max=dt_max, **model_args) test_model.add_data(S) # Initialize with the standard model parameters if standard_model is not None: test_model.initialize_with_standard_model(standard_model) # TODO: Precompute F_test F_test = test_model.basis.convolve_with_basis(S_test) # Gibbs sample samples = [] lps = [test_model.log_probability()] hlls = [test_model.heldout_log_likelihood(S_test)] times = [0] for _ in progprint_xrange(N_samples, perline=10): # Update the model tic = time.time() test_model.resample_model() samples.append(copy.deepcopy(test_model.get_parameters())) times.append(time.time() - tic) # Compute log probability and heldout log likelihood # lps.append(test_model.log_probability()) hlls.append(test_model.heldout_log_likelihood(S_test, F=F_test)) # # Save this sample # with open(output_path + ".gibbs.itr%04d.pkl" % itr, 'w') as f: # cPickle.dump(samples[-1], f, protocol=-1) # Check if time limit has been exceeded if np.sum(times) > time_limit: break # Get cumulative timestamps timestamps = np.cumsum(times) lps = np.array(lps) hlls = np.array(hlls) # Make results object results = Results(samples, timestamps, lps, hlls) # Save the Gibbs samples with gzip.open(output_path, 'w') as f: print "Saving Gibbs samples to ", output_path cPickle.dump(results, f, protocol=-1) return results
def demo(seed=None): """ Create a discrete time Hawkes model and generate from it. :return: """ if seed is None: seed = np.random.randint(2**32) print "Setting seed to ", seed np.random.seed(seed) ########################################################### # Load some example data. # See data/synthetic/generate.py to create more. ########################################################### data_path = os.path.join("data", "synthetic", "synthetic_K20_C4_T10000.pkl.gz") with gzip.open(data_path, 'r') as f: S, true_model = cPickle.load(f) T = S.shape[0] K = true_model.K B = true_model.B dt = true_model.dt dt_max = true_model.dt_max ########################################################### # Initialize with MAP estimation on a standard Hawkes model ########################################################### init_with_map = True if init_with_map: init_len = T print "Initializing with BFGS on first ", init_len, " time bins." init_model = DiscreteTimeStandardHawkesModel(K=K, dt=dt, dt_max=dt_max, B=B, alpha=1.0, beta=1.0) init_model.add_data(S[:init_len, :]) init_model.initialize_to_background_rate() init_model.fit_with_bfgs() else: init_model = None ########################################################### # Create a test spike and slab model ########################################################### # Copy the network hypers. # Give the test model p, but not c, v, or m network_hypers = true_model.network_hypers.copy() network_hypers['c'] = None network_hypers['v'] = None network_hypers['m'] = None test_network = StochasticBlockModel(K=K, **network_hypers) test_model = DiscreteTimeNetworkHawkesModelSpikeAndSlab(K=K, dt=dt, dt_max=dt_max, B=B, basis_hypers=true_model.basis_hypers, bkgd_hypers=true_model.bkgd_hypers, impulse_hypers=true_model.impulse_hypers, weight_hypers=true_model.weight_hypers, network=test_network) test_model.add_data(S) # F_test = test_model.basis.convolve_with_basis(S_test) # Initialize with the standard model parameters if init_model is not None: test_model.initialize_with_standard_model(init_model) # Initialize plots ln, im_net, im_clus = initialize_plots(true_model, test_model, S) ########################################################### # Fit the test model with Gibbs sampling ########################################################### N_samples = 50 samples = [] lps = [] # plls = [] for itr in xrange(N_samples): lps.append(test_model.log_probability()) # plls.append(test_model.heldout_log_likelihood(S_test, F=F_test)) samples.append(test_model.copy_sample()) print "" print "Gibbs iteration ", itr print "LP: ", lps[-1] test_model.resample_model() # Update plot if itr % 1 == 0: update_plots(itr, test_model, S, ln, im_clus, im_net) ########################################################### # Analyze the samples ########################################################### analyze_samples(true_model, init_model, samples, lps)