<jupyter_start><jupyter_text>Simple demo example for learning the DLS model from simulated data:<jupyter_code>from __future__ import division
from helper import *
from embedding import *
from hawkes_embedding import HawkesEmbedding<jupyter_output><empty_output><jupyter_text>Simple simulated dataset:<jupyter_code>rand.seed(123)

B = 4  # Number of basis kernels
N = 5  # Number of nodes
D = 2  # Number of latent dimensions
T = 100  # End time

# Simulate parameters 
gamma = rand.uniform(low=0, high=.2)  # Base rate
beta = rand.uniform(low=0, high=.1)  # Jump size
xvec = rand.uniform(size=(N, B, D))  # Reciprocal latent features
zvec = rand.uniform(size=(N, D))   # Homophily latent features

# Initialize HawkesEmbedding and set parameters
hawkes = HawkesEmbedding(num_nodes=N, events=list(), end_time=T, num_dims=D)
assert_equal(B, hawkes.B)
hawkes.set_params(end_time=T, gamma=np.zeros((N, N)) + gamma,
                  beta=np.zeros((N, N)) + beta, xvec=xvec, zvec=zvec)
xi_true = hawkes.update_xi()

# Simulate Hawkes events
events = hawkes.simulate()
events<jupyter_output><empty_output><jupyter_text>Learn the DLS model parameters:<jupyter_code>hawkes_test = HawkesEmbedding(num_nodes=N, events=events,
                              end_time=T, num_dims=D)
hawkes_test.C_beta = 0.
hawkes_test.C_zvec = 1.  # Regularization on the magnitude of zvec's
hawkes_test.C_xvec1 = 1.  # Regularization on the magnitude of xvec's
hawkes_test.C_xvec2 = 1.  # Enforce similarity between the different basis-kernel dimensions for each node
hawkes_test.loglik(gamma=1, beta=beta, xvec=xvec, zvec=zvec)
gamma_mle, beta_mle, xvec_mle, zvec_mle = hawkes_test.mle()
# gamma_mle, beta_mle, xvec_mle = hawkes_test.mle(method='coord-ascent')<jupyter_output>1 [ 0.085  0.431  0.676  0.615  0.99   0.661  0.618  0.928  0.903  0.835
  0.407  0.698  0.956  0.24   0.487  0.758  0.598  0.784  0.264  0.131
  0.471  0.665  0.598  0.611  0.092  0.491  0.202  0.711  0.558  0.601
  0.596  0.305  0.083  0.412  0.298  0.57   0.223  0.223  0.324  0.172
  0.402  0.825  0.852  0.108  0.331  0.613  0.227  0.919  0.707  0.22
  0.107  0.074] [ 1384.161]
2 [ 0.047  0.3    0.676  0.615  0.991  0.662  0.621  0.938  0.909  0.836
  0.407  0.698  0.957  0.238  0.485  0.76   0.598  0.785  0.264  0.131
  0.47   0.666  0.6    0.614  0.088  0.489  0.202  0.711  0.558  0.601
  0.599  0.298  0.079  0.409  0.298  0.57   0.222  0.222  0.318  0.164
  0.403  0.827  0.853  0.108  0.331  0.614  0.226  0.921  0.707  0.22
  0.106  0.073] [ 1124.304]
3 [ 0.029  0.188  0.676  0.615  0.991  0.662  0.622  0.942  0.911  0.837
  0.407  0.698  0.957  0.238  0.484  0.761  0.599  0.785  0.264  0.131
  0.47   0.666  0.601  0.616  0.087  0.488  0.202  0.711  0.558  0.602
  0.601  0.294  0[...]<jupyter_text>These are the learned parameter values:<jupyter_code>gamma_mle
beta_mle
zvec_mle
xvec_mle
hawkes_test.loglik(gamma_mle, beta_mle, xvec_mle, zvec_mle)
hawkes_test.gamma[:] = gamma_mle
hawkes_test.beta[:] = beta_mle
hawkes_test.xvec[:] = xvec_mle
hawkes_test.zvec[:] = zvec_mle
xi = hawkes_test.update_xi()
xi[:, :, 1]<jupyter_output><empty_output><jupyter_text>View the learned node-similarity matrices:<jupyter_code>hawkes_test.dist_mat
# Similarity on the zvec's (homophily latent space)
hawkes_test.dist_mat[0]
# Similarity on the xvec's (B reciprocal latent space)
hawkes_test.dist_mat[1:]
hawkes_test.dist_mat
# See embedding.py and hawkes_embedding.dist_mat
plot_dist_heatmap(hawkes)  # Plots saved to directory. 
hawkes_test.zvec
hawkes_test.xvec
# Latent features for the first node:
hawkes_test.zvec[0]
hawkes_test.xvec[0]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11769570011025358, 'fraction_numerical': 0.1582138919514884, 'mean_word_length': 3.420219244823386, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '105214', 'license': 'MIT', 'n_tokens_mistral': 1915, 'n_tokens_neox': 1579, 'n_words': 358, 'path': 'Hawkes-DLS-demo.ipynb', 'repo_name': 'jiaseny/lspp', 'size': '3628'}