Beispiel #1
0
def run_on_random(num_ts=5, n_points=70, prompt=False):
    n_plots_long = 5
    n_plots_wide = 1
    i = 0
    #timescale = (5, 'day')
    timescale = None
    params = {'beta_step': 1000,
              'beta_d1': 0.0,
              'beta_d2': 1.0
              }
    for seed in xrange(num_ts):
        print 'seed: %s' % seed
        t, y = make_random_ts(n_points, seed=seed)
        model = ZaggyModel(t, y, timescale=timescale, params=params)
        model.fit()

        if doplot:
            if i % (n_plots_long*n_plots_wide) == 0:
                plt.clf()
            n_plot = (i % (n_plots_long*n_plots_wide)) + 1
            plt.subplot(n_plots_long, n_plots_wide, n_plot)
            plt.plot(t, y)
            plt.plot(t, model.solution['model'])
            if prompt:
                ans = raw_input('ok?:')
                if ans == 'q':
                    return
        i += 1
Beispiel #2
0
def make_simple_plot(params=None):
    mock = get_mock_with_dates()
    plot_mock(mock)
    timescale = (1.0, 'month')
    model = ZaggyModel(mock['dates'], mock['y'], timescale=timescale,
                       params=params)
    model.fit()
    plot_model(model)
    plt.legend()
    return model
Beispiel #3
0
 def test_zaggy_predict_on_fitted_points(self):
     self.model = ZaggyModel(self.dates, self.y)
     self.model.fit()
     results = self.model.predict(self.dates)
     assert isinstance(results, ndarray)
     for expected, result in zip(self.model.solution['model'], results):
         self.assertEquals(expected, result)
Beispiel #4
0
    def test_zaggy_predict_is_zero_on_dates_before_first_data_point(self):
        self.model = ZaggyModel(self.dates, self.y)
        self.model.fit()

        old_dates = date_range(2013, 1, 2014, 12)
        results = self.model.predict(old_dates)
        for expected, result in zip(self.model.solution['model'], results):
            self.assertEquals(result, 0.0)
Beispiel #5
0
def make_extrapolatad_plot(params=None):
    mock = get_mock_with_dates()
    plot_mock(mock)
    timescale = (1.0, 'month')
    model = ZaggyModel(mock['dates'], mock['y'], timescale=timescale,
                       params=params)
    model.fit()
    plot_model(model)

    dates = date_range(2022, 1, 2024, 12)
    results = model.predict(dates)
    plt.plot(dates, results, color='orange', alpha=0.7, marker='d',
             label='predictions')

    dates = date_range(2014, 1, 2016, 1)
    results = model.predict(dates)
    plt.plot(dates, results, color='orange', alpha=0.7, marker='s',
             label='predictions (earlier)')

    plt.legend(loc='upper left')
    return model
Beispiel #6
0
 def test_zaggy_model_fit_runs(self):
     self.model = ZaggyModel(self.dates, self.y)
     self.assertIsNone(self.model.solution)
     self.model.fit()
     self.assertIsNotNone(self.model.solution)
     self.assertIsNotNone(self.model.slope)
     self.assertIsNotNone(self.model.offset)
     self.assertIsNotNone(self.model.seasonal)
     self.assertIsNotNone(self.model.interpolate)
     self.assertIsNotNone(self.model.extrapolate_without_seasonal)
     self.assertIsNotNone(self.model.seasonality_function)
     self.assertIsNotNone(self.model.date_to_seasonal_component)
     self.assertIsNotNone(self.model.compression_dict)
Beispiel #7
0
class TestZaggyModel(TestCase):
    def setUp(self):
        mock = make_mock()
        self.y = mock['y']
        num = len(self.y)
        self.dates = date_range(2015, 1, 2029, 12)[0: num]

    def test_zaggy_model_runs(self):
        self.model = ZaggyModel(self.dates, self.y)
        assert True

    def test_zaggy_model_fit_runs(self):
        self.model = ZaggyModel(self.dates, self.y)
        self.assertIsNone(self.model.solution)
        self.model.fit()
        self.assertIsNotNone(self.model.solution)
        self.assertIsNotNone(self.model.slope)
        self.assertIsNotNone(self.model.offset)
        self.assertIsNotNone(self.model.seasonal)
        self.assertIsNotNone(self.model.interpolate)
        self.assertIsNotNone(self.model.extrapolate_without_seasonal)
        self.assertIsNotNone(self.model.seasonality_function)
        self.assertIsNotNone(self.model.date_to_seasonal_component)
        self.assertIsNotNone(self.model.compression_dict)

    def test_zaggy_predict_on_fitted_points(self):
        self.model = ZaggyModel(self.dates, self.y)
        self.model.fit()
        results = self.model.predict(self.dates)
        assert isinstance(results, ndarray)
        for expected, result in zip(self.model.solution['model'], results):
            self.assertEquals(expected, result)

    def test_zaggy_predict_is_zero_on_dates_before_first_data_point(self):
        self.model = ZaggyModel(self.dates, self.y)
        self.model.fit()

        old_dates = date_range(2013, 1, 2014, 12)
        results = self.model.predict(old_dates)
        for expected, result in zip(self.model.solution['model'], results):
            self.assertEquals(result, 0.0)
Beispiel #8
0
 def test_zaggy_model_runs(self):
     self.model = ZaggyModel(self.dates, self.y)
     assert True