Skip to content

Commit

Permalink
poly and fft add steps
Browse files Browse the repository at this point in the history
  • Loading branch information
robfairh committed Aug 6, 2019
1 parent f2fb746 commit b4a8f07
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
24 changes: 11 additions & 13 deletions d3ploy/DO_solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import statsmodels.tsa.holtwinters as hw


def polyfit_regression(ts, back_steps=10, degree=1):
def polyfit_regression(ts, back_steps=10, degree=1, steps=1):
"""
Fits a polynomial to the entries in timeseries [ts]
to predict the next value.
Expand All @@ -28,11 +28,11 @@ def polyfit_regression(ts, back_steps=10, degree=1):
fit = np.polyfit(time[-back_steps:],
timeseries[-back_steps:], deg=degree)
eq = np.poly1d(fit)
x = eq(len(ts) + 1)
x = eq(len(ts) + steps)
return x


def exp_smoothing(ts, back_steps=10, degree=1):
def exp_smoothing(ts, back_steps=10, degree=1, steps=1):
"""
Predicts next value using simple exponential smoothing.
Parameters:
Expand All @@ -54,14 +54,14 @@ def exp_smoothing(ts, back_steps=10, degree=1):
# https://github.com/statsmodels/statsmodels/issues/4878
elif len(timeseries) == 5:
timeseries = np.append(np.mean(timeseries), timeseries)

#print(timeseries)
model = hw.SimpleExpSmoothing(timeseries)
model_fit = model.fit()
x = model_fit.predict(len(timeseries), len(timeseries))
return x[0]
x = model_fit.predict(len(timeseries), len(timeseries) + steps - 1)
return x[-1]


def holt_winters(ts, back_steps=10, degree=1):
def holt_winters(ts, back_steps=10, degree=1, steps=1):
"""
Predicts next value using triple exponential smoothing
(holt-winters method).
Expand All @@ -85,11 +85,11 @@ def holt_winters(ts, back_steps=10, degree=1):
timeseries = np.append(np.mean(timeseries), timeseries)
model = hw.ExponentialSmoothing(timeseries)
model_fit = model.fit()
x = model_fit.predict(len(timeseries), len(timeseries))
return x[0]
x = model_fit.predict(len(timeseries), len(timeseries) + steps - 1)
return x[-1]


def fft(ts, back_steps=1e6, degree=1):
def fft(ts, back_steps=10, degree=1, steps=1):
timeseries = np.array(list(ts.values()))
timeseries = timeseries[-back_steps:]
n = timeseries.size
Expand All @@ -105,13 +105,11 @@ def fft(ts, back_steps=1e6, degree=1):
indexes = list(range(n))
# sort indexes by frequency, lower -> higher
indexes.sort(key=lambda i: np.absolute(f[i]))

t = np.arange(0, n + 1)
t = np.arange(0, n + steps)
restored_sig = np.zeros(t.size)
for i in indexes[:1 + n_harm * 2]:
ampli = np.absolute(x_freqdom[i]) / n # amplitude
phase = np.angle(x_freqdom[i]) # phase
restored_sig += ampli * np.cos(2 * np.pi * f[i] * t + phase)
fft_fit = restored_sig + p[0] * t

return fft_fit[-1]
6 changes: 4 additions & 2 deletions d3ploy/demand_driven_deployment_inst.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,8 @@ def target(incommod):
elif self.calc_method in ['poly', 'exp_smoothing', 'holt_winters', 'fft']:
supply = CALC_METHODS[self.calc_method](target(commod),
back_steps=self.back_steps,
degree=self.degree)
degree=self.degree,
steps=self.steps)
elif self.calc_method in ['sw_seasonal']:
supply = CALC_METHODS[self.calc_method](
target(commod), period=self.degree)
Expand All @@ -371,7 +372,8 @@ def predict_demand(self, commod, time):
elif self.calc_method in ['poly', 'exp_smoothing', 'holt_winters', 'fft']:
demand = CALC_METHODS[self.calc_method](self.commodity_demand[commod],
back_steps=self.back_steps,
degree=self.degree)
degree=self.degree,
steps=self.steps)
elif self.calc_method in ['sw_seasonal']:
demand = CALC_METHODS[self.calc_method](
self.commodity_demand[commod], period=self.degree)
Expand Down
3 changes: 2 additions & 1 deletion d3ploy/supply_driven_deployment_inst.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,8 @@ def predict_supply(self, commod, time):
elif self.calc_method in ['poly', 'exp_smoothing', 'holt_winters', 'fft']:
supply = CALC_METHODS[self.calc_method](self.commodity_supply[commod],
back_steps=self.back_steps,
degree=self.degree)
degree=self.degree,
steps=self.steps)
elif self.calc_method in ['sw_seasonal']:
supply = CALC_METHODS[self.calc_method](
self.commodity_supply[commod], period=self.degree)
Expand Down

0 comments on commit b4a8f07

Please sign in to comment.