def stream_data(name: str, n_obs: int): """ values and times for a univariate stream """ mr = MicroReader() lagged_values, lagged_times = mr.get_lagged_values_and_times( name=name, count=n_obs) y, t = list(reversed(lagged_values)), list(reversed(lagged_times)) return y, t
def next_opinionated_forecast(n_train, k, n_recent, multiple, name=None): while True: try: from microprediction import MicroReader n_obs = 0 while n_obs < 1000: mr = MicroReader() if name is None: names = mr.get_stream_names() random_name = random.choice(names) else: random_name = name lag_values, lag_times = mr.get_lagged_values_and_times( name=random_name, count=2000) y = list(reversed(lag_values)) t = list(reversed(lag_times)) n_obs = len(y) except ImportError: from timemachines.skatertools.data import hospital y = hospital() t = [15 * 60 * i for i in range(len(y))] name = 'hospital' for i in [100 * j for j in range(10)]: print('Looking at ' + random_name + ' ' + str(i) + '/1000') if len(y) > i + 2 * k + n_train: y_fit = y[i:i + n_train] t_fit = t[i:i + n_train] y_hats, _, forecast, m = prophet_iskater_factory(y=y_fit, k=k, t=t_fit) if is_opinonated(y=y_fit, forecast=forecast, k=k, n_recent=n_recent, multiple=multiple): y_3avg = np.mean( y[i + n_train - 3:i + n_train]) # avg of last three points y_true_mean = np.mean( y[i + n_train + k - 1:i + n_train + k + 1]) # avg of 3 future points error = (y_hats[-1] - y_true_mean) / abs(0.01 + y_3avg) avg_error = (y_3avg - y_true_mean) / abs(0.01 + y_3avg) return forecast, m, random_name, error, avg_error, y[ i + n_train:i + n_train + k] print(random_name + ' is okay.') return None, None, None
def optimal_r_for_stream( f, name: str, k: int, evaluator=None, optimizer=None, n_trials=None, n_dim=None, n_burn: int = None, test_objective_first=True ) -> (float, float, dict): # best_r, best_val, info """ Find the best hyper-parameters for a univariate skater using live from www.microprediction.org :param f: :param name: Choose from https://www.microprediction.org/browse_streams.html but add '.json' to the end :param k: :param evaluator: :param optimizer: :param n_trials: :param n_dim: :param n_burn: :param test_objective_first: :return: best_r, best_value, info """ mr = MicroReader() lagged_values, lagged_times = mr.get_lagged_values_and_times(name=name) t = list(reversed(lagged_times)) y = list(reversed(lagged_values)) return optimal_r(f=f, y=y, k=k, a=None, t=t, e=None, evaluator=evaluator, optimizer=optimizer, n_trials=n_trials, n_dim=n_dim, n_burn=n_burn, test_objective_first=test_objective_first)