def expected_time_of_next_value(self, lagged_times): """ Rudimentary estimate of the next time a data point will arrive based on history :returns float, float expected_at epoch time dt typical time between data arrivals """ if len(lagged_times) > 5: # Compute or assume time between data points neg_dt = approx_mode(np.diff(lagged_times)) dt = -neg_dt if neg_dt is not None else None if dt is None: dt = self.DELAYS[-1] if dt < 0: pass if dt < 10: dt = 10 # Don't wait more than 6 hours if dt > (60 * 60 * 6): dt = 60 * 60 * 6 # Compute expected time of next data point, but if data is missing move it forward # num_intervals>1 is used when we skip prediction, as with most z-curves for example expected_at = lagged_times[0] + dt if expected_at < time.time() + 45: expected_at = time.time() + dt else: expected_at = time.time() + self.DELAYS[-1] dt = self.DELAYS[-1] return expected_at, dt
def sample(self, lagged_values, lagged_times=None, name=None, delay=None): """ Fit an echo-state machine to predict the mean, then use standard scattering for sampling """ dt = approx_mode(np.diff(lagged_times)) k = max(1, int(delay / dt)) num_lagged = len(lagged_values) inputs = np.ones(num_lagged) # Exogenous inputs outputs = np.array(list(reversed(lagged_values))) esn = ESN(n_inputs=1, n_outputs=1, **self.ecn_params) esn.fit(inputs=inputs, outputs=outputs) future = [v[0] for v in esn.predict(np.ones(k))] center = future[-1] offset = center - lagged_values[0] scenarios = differenced_bootstrap(lagged=lagged_values, decay=0.01, num=self.num_predictions) samples = [offset + s for s in scenarios] return samples