Esempio n. 1
0
    def test_Divinity(self):
        """

        :return:
        """
        finished = False
        np.random.seed(123456)
        # now test the class and the new auto assigner
        N = 120
        Ntest = 100
        t = np.arange(N)
        y_test = 0.1 * t + np.sin(2 * np.pi / 20 * t) + np.random.randn(N) * 0.5
        t1 = time.time()
        dfc = dv.divinity(
            forecast_length=N - Ntest,
            seasonal_periods=list(np.arange(1, int(N / 2))),
            confidence_interval=70.0,
        )
        dfc.fit(y_test[:Ntest])
        y_forecast = dfc.predict()
        t2 = time.time()
        print("fit report")
        print("fit time...", t2 - t1)
        print("optimisation report...")
        print(str(len(dfc.input_features.columns)) + " input features")
        print(str(len(dfc.features.columns)) + " chosen features")
        print(dfc.features.columns)
        finished = True
        self.assertEqual(finished, True)
def divinity_univariate_factory(y: Y_TYPE,
                                s,
                                k: K_TYPE,
                                a=None,
                                t=None,
                                e=None,
                                max_buffer_len=1000,
                                n_warm=101,
                                model_params: dict = None):
    """ A partial wrapping of the divinity library with notable limitations:

         - Fits every invocation
         - Ignores exogenous variables
         - State is merely a buffer

    """
    y0 = wrap(y)[0]
    assert n_warm >= 101, ' You must use n_warm'

    if not s:
        s = dict(y=[])

    if y0 is None:
        return None, None, s  # Ignore suggestion to fit offline

    # Update buffer
    s['y'].append(y0)
    if len(s['y']) > max_buffer_len + 1000:
        s['y'] = s['y'][-max_buffer_len:]

    # Fit and predict, if warm, or just last value
    if len(s['y']) < max(n_warm, MIN_N_WARM):
        return [y0] * k, [abs(y0)] * k, s
    else:
        with no_stdout_stderr():
            kwargs = deepcopy(DIVINE_MODEL)
            if model_params:
                kwargs.update(**model_params)
            model = dv.divinity(forecast_length=k, **kwargs)
            model.fit(np.array(s['y']))
        x = list(model.predict())
        x_std = [1.0] * k  # TODO: fixme
        return x, x_std, s
Esempio n. 3
0
        N=120,
        trend_order=4,
        trend_amplitudes=None,
        seasonal_periods=list(np.arange(2, 50)),
        seasonal_amplitudes_sine=None,
        seasonal_amplitudes_cosine=None,
    )
    features = synthetic["features"]

    # now test the class and the new auto assigner
    N = 120
    Ntest = 100
    t = np.arange(N)
    y_test = 0.1 * t + np.sin(2 * np.pi / 20 * t) + np.random.randn(N) * 0.5
    dfc = dv.divinity(
        forecast_length=N - Ntest, seasonal_periods=[20], confidence_interval=70.0
    )
    dfc.fit(y_test[:Ntest])
    y_forecast = dfc.predict()
    fig = plt.figure()
    ax1 = fig.add_subplot(111)
    ax1.plot(t, y_test, label="True", color="k")
    ax1.plot(t[Ntest:], y_forecast, label="Forecast", color="b")
    ax1.fill_between(
        t[Ntest:],
        y_forecast - dfc.ystd,
        y_forecast + dfc.ystd,
        alpha=0.3,
        color="b",
        label=None,
    )
Esempio n. 4
0
import matplotlib.pylab as plt
import numpy as np
"""
Setup a simple synthetic dataset with a smooth increasing
trend and a 20-d periodic feature with gaussian random noise
"""
epochs = 100
forecast = 20
t = np.arange(epochs + forecast)
y_test = 0.1 * t + np.sin(
    2 * np.pi / 20 * t) + np.random.randn(epochs + forecast) * 0.5
"""
generate a 20 step forecast and
calculate 95pc confidence limits
"""
dfc = dv.divinity(forecast_length=forecast, confidence_interval=95.0)
dfc.fit(y_test[:epochs])
y_forecast = dfc.predict()
"""
visualise the results in matplotlib
"""
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(t, y_test, label="True", color="k")
ax1.plot(t[epochs:], y_forecast, label="Forecast", color="b")
ax1.fill_between(
    t[epochs:],
    y_forecast - dfc.ystd,
    y_forecast + dfc.ystd,
    alpha=0.3,
    color="b",
Esempio n. 5
0
import pandas as pd
import divinity as dv
import matplotlib.pyplot as plt
import time

if __name__ == "__main__":
    np.random.seed(123456)
    # now test the class and the new auto assigner
    N = 120
    Ntest = 100
    t = np.arange(N)
    y_test = 0.1 * t + np.sin(2 * np.pi / 20 * t) + np.random.randn(N) * 0.5
    t1 = time.time()
    dfc = dv.divinity(
        forecast_length=N - Ntest,
        seasonal_periods=list(np.arange(1, int(N / 2))),
        confidence_interval=70.0,
    )
    dfc.fit(y_test[:Ntest])
    y_forecast = dfc.predict()
    t2 = time.time()
    print("fit report")
    print("fit time...", t2 - t1)
    print("optimisation report...")
    print(str(len(dfc.input_features.columns)) + " input features")
    print(str(len(dfc.features.columns)) + " chosen features")
    print(dfc.features.columns)

    fig = plt.figure()
    ax1 = fig.add_subplot(111)
    ax1.plot(t, y_test, label="True", color="k")