Skip to content

Commit

Permalink
#315 Clamp smoothed values to minimum of 0
Browse files Browse the repository at this point in the history
  • Loading branch information
IanGrimstead authored and IanGrimstead committed Sep 3, 2019
1 parent 988cf6f commit f24870f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
4 changes: 3 additions & 1 deletion scripts/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pickle
from os import makedirs, path

import numpy as np
from pandas import read_pickle
from scipy.signal import savgol_filter
from tqdm import tqdm
Expand Down Expand Up @@ -208,10 +209,11 @@ def pickle_object(short_name, obj):

self.__timeseries_quarterly.append(quarterly_values)
smooth_series = savgol_filter(quarterly_values, 9, 2, mode='nearest')
smooth_series_no_negatives = np.clip(smooth_series, a_min=0, a_max=None)

# _, _1, smooth_series_s, _2 = SteadyStateModel(quarterly_values).run_smoothing()
# smooth_series = smooth_series_s[0].tolist()[0]
self.__timeseries_quarterly_smoothed.append(smooth_series)
self.__timeseries_quarterly_smoothed.append(smooth_series_no_negatives)

em = Emergence(all_quarterly_values)
for term_index in tqdm(range(self.__term_counts_per_week.shape[1]), unit='term', desc='Calculating eScore',
Expand Down
62 changes: 62 additions & 0 deletions tests/algorithms/test_holt_winters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from sys import platform as sys_pf

from scripts.algorithms.holtwinters_predictor import HoltWintersPredictor

if sys_pf == 'darwin':
import matplotlib

matplotlib.use("TkAgg")

import unittest
import numpy.testing as np_test

import platform;

print(platform.platform())
import sys;

print("Python", sys.version)
import numpy as np;

print("NumPy", np.__version__)
import scipy;

print("SciPy", scipy.__version__)
import sklearn;

print("Scikit-Learn", sklearn.__version__)
import statsmodels;

print("Statsmodels", statsmodels.__version__)


class HoltWintersTests(unittest.TestCase):

def test_negatives_in_sequence(self):
time_series = [1, 1, -1, 1, 1]
num_predicted_periods = 3

with self.assertRaises(NotImplementedError) as nie:
HoltWintersPredictor(time_series, num_predicted_periods)

self.assertEqual(nie.exception.args[0], 'Unable to correct for negative or zero values')

def test_zeros_in_sequence(self):
time_series = [1, 1, 0, 1, 1]
num_predicted_periods = 3
expected_prediction = [0.8] * num_predicted_periods
hw = HoltWintersPredictor(time_series, num_predicted_periods)

actual_prediction = hw.predict_counts()

np_test.assert_almost_equal(actual_prediction, expected_prediction, decimal=4)

def test_static_sequence(self):
time_series = [1.0, 1.0, 1.0, 1.0, 1.0]
num_predicted_periods = 3
expected_prediction = [1] * num_predicted_periods
hw = HoltWintersPredictor(time_series, num_predicted_periods)

actual_prediction = hw.predict_counts()

np_test.assert_almost_equal(actual_prediction, expected_prediction, decimal=4)

0 comments on commit f24870f

Please sign in to comment.