def test_compare_recursive(show=False):
    k = 5
    n = 800
    y, a = hospital_with_exog(k=k, n=n + 2, offset=True)
    y = y[:n - k]
    a = a[:n]
    assert len(a) == len(y) + k, ' You need smaller n '
    x, x_std, forecast, m = prophet_iskater_factory(y=y,
                                                    k=k,
                                                    a=a,
                                                    freq='15min',
                                                    recursive=True)
    x1, x1_std, forecast1, m1 = prophet_iskater_factory(y=y,
                                                        k=k,
                                                        a=a,
                                                        freq='15min',
                                                        recursive=False)
    if show:
        m.plot(forecast)
        import matplotlib.pyplot as plt
        plt.title('Using exogenous')
        m1.plot(forecast1)
        plt.title('Not using exogenous')
        print(list(zip(x, x1)))
        plt.show()
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()
示例#3
0
    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 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 test_with_freq(show=False):
    k = 3
    n = 600
    y, a = hospital_with_exog(k=k, n=n + 2)
    y = y[:n - k]
    a = a[:n]
    x, x_std, forecast, m = prophet_iskater_factory(y=y,
                                                    k=k,
                                                    a=a,
                                                    freq='15min')
    assert len(x) == k
    if show:
        m.plot(forecast)
        import matplotlib.pyplot as plt
        plt.show()
def test_with_exog_and_advance_vars(show=False):
    k = 3
    n = 100
    y, a = hospital_with_exog(k=k, n=n)
    y = y[:-k]
    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, a=a)
    assert len(x) == k
    x1, x_std1, forecast1, m1 = prophet_fit_and_predict_with_exog_and_advance_vars(
        y=y, k=k, t=t, a=a)
    assert nearlysame(x1, x, 0.0001)
    if show:
        m.plot(forecast)
        m1.plot(forecast1)
        import matplotlib.pyplot as plt
        plt.show()
def fbprophet_skater_testor(y: Y_TYPE,
                            s: dict = None,
                            k: int = 1,
                            a: A_TYPE = None,
                            t: T_TYPE = None,
                            e: E_TYPE = None,
                            r: R_TYPE = None,
                            freq=None,
                            n_max=None):
    """ A default facebook prophet usage, with no hyper-parameters and no prediction parade """
    # For testing

    if freq is None:
        freq = PROPHET_META['freq']
    if n_max is None:
        n_max = PROPHET_META['n_max']

    y = wrap(y)
    a = wrap(a)

    if not s.get('y'):
        s = {'y': list(), 'a': list(), 'k': k}
    else:
        # Assert immutability of k, dimensions
        if s['y']:
            assert len(y) == len(s['y'][0])
            assert k == s['k']
        if s['a']:
            assert len(a) == len(s['a'][0])

    if y is None:
        return None, s, None
    else:
        s['y'].append(y)
        if a is not None:
            s['a'].append(a)
        if len(s['y']) > max(2 * k + 5, PROPHET_META['n_warm']):
            x, x_std, _, _ = prophet_iskater_factory(y=s['y'],
                                                     k=k,
                                                     a=s['a'],
                                                     freq=freq,
                                                     n_max=n_max)
        else:
            x = [y[0]] * k
            x_std = [1.0] * k
        return x, x_std, s
def test_with_exog_and_advance_vars_no_t(show=False):
    k = 3
    n = 100
    y, a = hospital_with_exog(k=k, n=n)
    y = y[:-k]
    freq = '15min'
    x, x_std, forecast, m = prophet_iskater_factory(y=y, k=k, freq=freq, a=a)
    assert len(x) == k
    x1, x_std1, forecast1, m1 = prophet_fit_and_predict_with_exog_and_advance_vars_no_t(
        y=y, k=k, freq=freq, a=a)
    if not nearlysame(x1, x, 0.0001):
        print(forecast.tail())
        print(forecast1.tail())
        pass
    if show:
        m.plot(forecast)
        m1.plot(forecast1)
        import matplotlib.pyplot as plt
        plt.show()
示例#9
0
def fbprophet_skater_factory(y: Y_TYPE, s: dict, k: int, a: A_TYPE = None,
                             t: T_TYPE = None, e: E_TYPE = None,
                             emp_mass: float = 0.0, emp_std_mass: float = 0.0,
                             freq=None, recursive: bool = False,
                             model_params: dict = None,
                             n_max: int = None) -> ([float], Any, Any):
    """ Prophet skater with running prediction error moments
        Hyper-parameters are explicit here, whereas they are determined from r in actual skaters.
        Params of note:

             a: value of known-in-advance vars k step in advance (not contemporaneous with y)

    """

    assert 0 <= emp_mass <= 1
    assert 0 <= emp_std_mass <= 1

    if freq is None:
        freq = PROPHET_META['freq']
    if n_max is None:
        n_max = PROPHET_META['n_max']

    y = wrap(y)
    a = wrap(a)

    if not s.get('y'):
        s = {'p': {},     # parade
             'y': list(), # historical y
             'a': list(), # list of a known k steps in advance
             't': list(),
             'k': k}
    else:
        # Assert immutability of k, dimensions of y,a
        if s['y']:
            assert len(y) == len(s['y'][0])
            assert k == s['k']
        if s['a']:
            assert len(a) == len(s['a'][0])

    if y is None:
        return None, s, None
    else:
        s['y'].append(y)
        if a is not None:
            s['a'].append(a)
        if t is not None:
            assert isinstance(t,float), 'epoch time please'
            s['t'].append(t)

        if len(s['y']) > max(2 * k + 5, PROPHET_META['n_warm']):
            # Offset y, t, a are supplied to prophet interface
            t_arg = s['t'][k:] if t is not None else None
            a_arg = s['a']
            y_arg = s['y'][k:]
            x, x_std, forecast, model = prophet_iskater_factory(y=y_arg, k=k, a=a_arg, t=t_arg,
                                                                freq=freq, n_max=n_max,
                                                                recursive=recursive, model_params=model_params)
            s['m'] = True # Flag indicating a model has been fit (there is no point keeping the model itself, however)
        else:
            x = [y[0]] * k
            x_std = None

        # Get running mean prediction errors from the prediction parade
        x_resid, x_resid_std, s['p'] = parade(p=s['p'], x=x, y=y[0])
        x_resid = nonecast(x_resid,y[0])
        x_resid_std = nonecast(x_resid_std,1.0)

        # Compute center of mass between bias-corrected and uncorrected predictions
        x_corrected = np.array(x_resid) + np.array(x)
        x_center = nonecenter(m=[emp_mass, 1 - emp_mass], x=[x_corrected, x])
        x_std_center = nonecenter(m=[emp_std_mass, 1 - emp_std_mass], x=[x_resid_std, x_std])

        return x_center, x_std_center, s