def hospital_mean_square_error(f, k: int = 1, n=120, n_burn=30, r=None) -> float: """ Useful for a quick test of a skater, univariate and random hyper-param """ y = hospital()[:n] return evaluate_mean_squared_error(f=f, y=y, k=k, r=r, n_burn=n_burn)
def test_univariate_without_time(show=False): k = 3 n = 100 y = hospital(n=n) x, x_std, forecast, m = prophet_iskater_factory(y=y, k=k) assert len(x) == k x1, x_std1, forecast1, m1 = prophet_fit_and_predict_simple(y=y, k=k) assert nearlysame(x1, x, 0.0001) if show: m.plot(forecast) m1.plot(forecast1) import matplotlib.pyplot as plt plt.show()
def test_univariate_with_time_and_advance_time(show=False): k = 3 n = 100 y = hospital(n=n) t = [i * 15 * 50 for i in range(len(y) + k)] x, x_std, forecast, m = prophet_iskater_factory(y=y, k=k, t=t) assert len(x) == k x1, x_std1, forecast1, m1 = prophet_fit_and_predict_with_time_and_advance_time( y=y, k=k, t=t) assert nearlysame(x1, x, 0.0001) if show: m.plot(forecast) m1.plot(forecast1) import matplotlib.pyplot as plt plt.show()
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.data.real 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