Linear trend of time series.

class Trend[source]

Trend(breakpoints:int=None, date_range=None, mean=0, scale=1) :: Module

Broken Trend model, with breakpoints as defined by user. parameters:

  • breakpoints (optional): number of breaks in trend
  • date_range: Required if breakpoints is given. Start and stop date in days.
  • mean: mean of time
  • scale: standard deviation of time

Test with Dummy Data

N = 700
t = np.arange(N)

trend = 0.05*t + 0.1
trend[t>350] = -0.1*(t[t>350] - 350) + trend[t==350]

noise = np.random.randn(*trend.shape)
y = trend + noise
# y = np.clip(y, 1, 20)

plt.plot(t, y)
plt.plot(t, trend)
plt.show()
breakpoints = torch.Tensor([250, 500])
model = Trend(breakpoints)
learner = Learner(db, model, loss_func=F.mse_loss)
wd = 0
learner.lr_find(wd=wd)
learner.recorder.plot(skip_end=0)
LR Finder is complete, type {learner_name}.recorder.plot() to see the graph.
epochs = 10
learner.fit_one_cycle(epochs, 1e-1, wd=wd)
epoch train_loss valid_loss time
0 15006.965820 48516.570312 00:00
1 10705.563477 993.336182 00:00
2 7057.067871 29231.976562 00:00
3 5744.917969 11459.618164 00:00
4 4433.373047 556.789673 00:00
5 3629.159668 733.305664 00:00
6 2988.094727 189.605576 00:00
7 2479.688965 1147.893921 00:00
8 2095.290527 1420.154663 00:00
9 1794.216064 1420.845947 00:00
y2 = learner.model(torch.Tensor(t)[:,None])
plt.plot(t, y, label='actual')
plt.plot(t[:train_len], y[:train_len], label='train set')
plt.plot(t, y2, label='prediction')
plt.legend()
plt.show()