docstr: ...Test score (ie. likelihood) function of Hawkes EM
          
docstr_tok: ["...","Test","score","(","ie",".","likelihood",")","function","of","Hawkes","EM"]
code: def test_hawkes_em_score(self):
      """...Test score (ie. likelihood) function of Hawkes EM"""
  
      def approximate_likelihood(em, events, end_times, precision=2):
          n_total_jumps = sum(map(len, events))
          kernels_func = [
              [
                  lambda t, i=i, j=j: em.get_kernel_values(
                      i, j, np.array([t])
                  )[0]
                  for j in range(n_nodes)
              ]
              for i in range(n_nodes)
          ]
          intensities = hawkes_intensities(
              events, em.baseline, kernels_func
          )
          return (
              hawkes_log_likelihood(
                  intensities, events, end_times, precision=precision
              )
              / n_total_jumps
          )
  
      # We use only 2 nodes otherwise integral approximation might be very
      # slow
      n_nodes = 2
      kernel_support = 1.0
      kernel_size = 3
      baseline = np.random.rand(n_nodes) + 0.2
      kernel = np.random.rand(n_nodes, n_nodes, kernel_size) + 0.4
  
      train_events = [
          np.cumsum(np.random.rand(2 + i)) for i in range(n_nodes)
      ]
  
      test_events = [
          2 + np.cumsum(np.random.rand(2 + i)) for i in range(n_nodes)
      ]
  
      # Test for 2 kind of discretization
      train_kwargs = [
          {"kernel_support": 1, "kernel_size": 3},
          {"kernel_discretization": np.array([0.0, 1.0, 1.5, 3.0])},
      ]
  
      # Test with and without fitting
      fits = [True, False]
  
      for kwargs, fit in zip(train_kwargs, fits):
          em = HawkesEM(**kwargs)
          end_times = max(map(max, train_events)) + 0.2 * kernel_support
  
          msg = "^You must either call `fit` before `score` or provide events"
          with self.assertRaisesRegex(ValueError, msg):
              em.score()
  
          if fit:
              em.fit(
                  train_events,
                  end_times=end_times,
                  baseline_start=baseline,
                  kernel_start=kernel,
              )
          else:
              em.baseline = baseline
              em.kernel = kernel
  
          # Score on em train data
          if fit:
              em_train_score = em.score()
          else:
              em_train_score = em.score(
                  train_events, end_times=end_times
              )
          self.assertAlmostEqual(
              em_train_score,
              approximate_likelihood(em, train_events, end_times, 2),
              delta=1e-1,
              msg="Failed on train for {}".format(kwargs),
          )
  
          # Score on test data
          em_test_score = em.score(events=test_events)
          test_end_times = max(map(max, test_events))
          self.assertAlmostEqual(
              em_test_score,
              approximate_likelihood(
                  em, test_events, test_end_times, 4
              ),
              delta=1e-3,
              msg="Failed on test for {}".format(kwargs),
          )
  
imports: import unittest
  import numpy
  from tick.hawkes.inference import HawkesEM
  from tick.hawkes.model.tests.model_hawkes_test_utils import hawkes_intensities, hawkes_log_likelihood
  from tick.hawkes import SimuHawkes, HawkesKernelTimeFunc, HawkesKernelExp, SimuHawkesExpKernels
  from tick.base import TimeFunction
label_desc: Use image node_id 6 for calling the Test obj's underlying member method code with example usage: obj.test_hawkes_em_score() and returns: unknown