Skip to content

Instantly share code, notes, and snippets.

@Vayel Vayel/
Last active Aug 10, 2018

What would you like to do?
import scipy.stats
import numpy as np
def ks_test(real_series, simulated_series):
# `real_series` and `simulated_series` are lists of series, i.e. lists of lists.
# All series have the same size but we don't need to have as many real series as
# simulated series.
real_series, simulated_series = map(np.asarray, (real_series, simulated_series))
n_steps = len(real_series[0])
p_values = [0] * n_steps
for t in range(n_steps):
real_samples = real_series[:,t]
simulated_samples = simulated_series[:,t]
_, p_values[t] = scipy.stats.ks_2samp(real_samples, simulated_samples)
return p_values
def sliding_ks_test(real_data, simulated_data, sliding_step=1):
# `real_series` and `simulated_series` are lists of series, i.e. lists of lists.
# All simulated series have the same length T_sim and all real series have the
# same length T_real, but T_sim can be different from T_real. The shorter series
# will be the ones we will make slide over time.
real_data, simulated_data = map(np.asarray, (real_data, simulated_data))
real_steps, simulated_steps = real_data.shape[1], simulated_data.shape[1]
delta_steps = abs(real_steps - simulated_steps)
if real_steps < simulated_steps:
data = simulated_data
pattern = real_data
data = real_data
pattern = simulated_data
p_values = []
first_step_compared = 0
while first_step_compared <= delta_steps:
window = slide(first_step_compared, first_step_compared+pattern.shape[1])
p_values.append(ks_test(data[:,window], pattern))
first_step_compared += sliding_step
return p_values
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.