generate_test_data.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import numpy as np
  2. def generate_ground_truth_data(length):
  3. """
  4. Generate and save test data using the parameters from Ding et al. 2006
  5. section 5.1 Example 1 (p. 18).
  6. Parameters
  7. ----------
  8. length : int
  9. Length of generated time series data
  10. References
  11. ----------
  12. Ding, M., Chen, Y., & Bressler, S. L. (2006). Granger Causality: Basic
  13. Theory and Application to Neuroscience. arXiv preprint q-bio/0608035.
  14. """
  15. np.random.seed(123)
  16. order = 2
  17. time_series = np.zeros((2, length + order))
  18. weights_1 = np.array([[0.9, 0], [0.16, 0.8]])
  19. weights_2 = np.array([[-0.5, 0], [-0.2, -0.5]])
  20. weights = np.stack((weights_1, weights_2))
  21. noise_covariance = np.array([[1., 0.4], [0.4, 0.7]])
  22. for i in range(length):
  23. for lag in range(order):
  24. time_series[:, i + order] += np.dot(weights[lag],
  25. time_series[:, i + 1 - lag])
  26. rnd_var = np.random.multivariate_normal([0, 0],
  27. noise_covariance)
  28. time_series[:, i + order] += rnd_var
  29. time_series = time_series[:, 2:]
  30. # Save the generated data
  31. data_folder = '../data/'
  32. if length <= 30000:
  33. time_series_filename = 'time_series_small.npy'
  34. else:
  35. time_series_filename = 'time_series.npy'
  36. np.save(data_folder + time_series_filename, time_series)
  37. np.save(data_folder + 'weights.npy', weights)
  38. np.save(data_folder + 'noise_covariance.npy', noise_covariance)
  39. if __name__ == '__main__':
  40. generate_ground_truth_data(30000)
  41. generate_ground_truth_data(300000)