Code Monkey home page Code Monkey logo

timeseers's Introduction

Unit Tests

TimeSeers

seers - (Noun) plural form of seer - A person who foretells future events by or as if by supernatural means

TimeSeers is an hierarchical Bayesian Time Series model based on Facebooks Prophet, written in PyMC3.

The goal of the TimeSeers project is to provide an easily extensible alternative to Prophet for timeseries modelling when multiple time series are expected to share parts of their parameters.

Usage

TimeSeers is designed as a language for building time series models. It offers a toolbox of various components which can be arranged in a formula. We can compose these components in various ways to best fit our problem.

TimeSeers strongly encourages using uncertainty estimates, and will by default use MCMC to get full posterior estimates.

from timeseers import LinearTrend, FourierSeasonality
import pandas as pd

model = LinearTrend() + FourierSeasonality(period=pd.Timedelta(days=365)) + FourierSeasonality(period=pd.Timedelta(days=365))
model.fit(data[['t']], data['value'])

Multiplicative seasonality

from timeseers import LinearTrend, FourierSeasonality
import pandas as pd

passengers = pd.read_csv('AirPassengers.csv').reset_index().assign(
    t=lambda d: pd.to_datetime(d['Month']),
    value=lambda d: d['#Passengers']
)

model = LinearTrend(n_changepoints=10) * FourierSeasonality(n=5, period=pd.Timedelta(days=365))
model.fit(passengers[['t']], passengers['value'], tune=2000)

model.plot_components(X_true=passengers, y_true=passengers['value']);
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
Multiprocess sampling (4 chains in 4 jobs)
NUTS: [sigma, beta, m, delta, k]
Sampling 4 chains, 0 divergences: 100%|██████████| 10000/10000 [00:57<00:00, 173.30draws/s]

png

Contributing

PR's and suggestions are always welcome. Please open an issue on the issue list before submitting though in order to avoid doing unnecessary work. I try to adhere to the scikit-learn style as much as possible. This means:

  • Fitted parameters have a trailing underscore
  • No parameter modification is done in __init__ methods of model components

timeseers's People

Contributors

jattenberg avatar kori73 avatar mbrouns avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

timeseers's Issues

PyMC3 is getting renamed to PyMC

Hi,
PyMC3 has been renamed to PyMC. If this affects you and you have questions let me know how I, or any of the PyMC devs can help.

Fix unit tests

I would like to help fix the unit tests if you don't have anything special in mind.

Add regressors to the model

Hi, Matthijs!

You’ve done a good job with your timeseers package! Thanks!
I’m a data scientist in a big retail company and we are now working on a new product in which we want to forecast sales. It seems like timeseers could be a great decision because we have lots of SKUs and it is really difficult to builld a model for each of them individually.
In our model we would like to use some exogenous regressors. It seems like timeseers already has a “Regressor” module. However, it is not very clear if it works and how it works. Could you please give some comments about it or some toy example how to use it? Does it work only with categorical features or continuous features can also be used? What “ON” argument is used for?

Thanks in advance for your reply!

Kate

Porting to PyMC4?

PyMC4 has now been released https://www.pymc.io/blog/v4_announcement.html#v4_announcement with various new features including better GPU support. Are there any plans to port this repo to PyMC4? Mostly this would involve just changing the imports from import pymc3 as pm to import pymc as pm but maybe also changing the way plots are handled https://www.pymc-labs.io/blog-posts/the-quickest-migration-guide-ever-from-pymc3-to-pymc-v40/ .

I noticed this closed issue #40 but I'm not sure that you actually changed anything in response to that?

Add RBFSeasonality

I have been working on this feature, and had chance to play with it on some highly seasonal series. If you think this would be a nice feature, I can wrap it up, open a PR and hopefully share some plots.

logo

lemme know if you'd like me to fix one.

Provide robustness to outliers

Sometimes there is an unknown special effect that results in very high values in the time series. (let's say a hurricane). I might be okay not getting accurate predictions for those unknown special days. But the neighboring points might also be affected due to the Fourier seasonality. I think it could be nice to provide a model that is robust to outliers. One approach could be adding an option to use pm.StudentT for the observations.

PyMC3 docs has a nice example that includes other (more advanced) methods as well. GLM: Robust Regression example

Here is a motivating example:

from timeseers.utils import seasonal_data
from timeseers import FourierSeasonality, LinearTrend
import numpy as np
import pandas as pd

np.random.seed(42)

df, _ = seasonal_data(5)

df.loc[630, "value"] = 100.
df.loc[631, "value"] = 100.
df.loc[632, "value"] = 100.
df.loc[633, "value"] = 100.

model = FourierSeasonality(n=5, period=pd.Timedelta(days=365)) + LinearTrend(n_changepoints=1)
model.fit(df[["t"]], df["value"], tune=2000, cores=1, chains=1)
model.plot_components(X_true=df, y_true=df['value'])

Using normal distribution I get the following predictions.

image

Changing to StudentT (two line diff), I get:

image

Do you think this would be a useful addition to the library?

Afterthought:
There is a similar discussion on fbprophet too. See: #1500.

It might be nice if the users could define the likelihood in TimeSeriesModel as they please.

Add timeseers to Pypi

Hi there,

Would it be possible to add timeseers to Pypi so that one could use pip install timeseers to install it?

Thanks

Remove theano imports

Hey,

I was browsing through the codebase and found it very interesting, considering playing a bit more with it.

I saw then, that in some modules, there seems to be an unnecessary dependency on the theano.

import theano.tensor as tt

No big issue, but more accessible if this would be removed while being quite a quick fix. What do you think?

Settle on the API for hierarchical models

# 1) class option
model = LinearTrend() + FourierSeasonality()
model = HierarchicalLinearTrend(group='subject') + FourierSeasonality()
model = GroupedLinearTrend(group='subject') + FourierSeasonality()

# 2) metaclass option
model = LinearTrend() + FourierSeasonality()
model = Hierarchical(LinearTrend(), on='subject') + FourierSeasonality()
model = Distinct(LinearTrend(), on='subject') + FourierSeasonality()

# 3) seperate hierarchy object 1
partial_subject = Share(type='partially_shared', on='subject')
distinct_subject = Share(type='distinct', on='subject')

model = LinearTrend() + FourierSeasonality()
model = LinearTrend().share(partial_subject) + FourierSeasonality()
model = LinearTrend().share(distinct_subject) + FourierSeasonality()

# 4) seperate hierarchy object 2
shared_subject = Share(type='shared')  # default
partial_subject = Share(type='partially_shared', on='subject')
distinct_subject = Share(type='distinct', on='subject')

model = LinearTrend(share=shared_subject) + FourierSeasonality(share=shared_subject)
model = LinearTrend(share=partial_subject) + FourierSeasonality()
model = LinearTrend(share=distinct_subject) + FourierSeasonality()

# 5) keyword option
model = LinearTrend(shared='full') + FourierSeasonality()  #  shared='full' is default
model = LinearTrend(group='subject', shared='partial') + FourierSeasonality()
model = LinearTrend(group='subject', shared='none') + FourierSeasonality()

# Cannibalism in this option
model = Constant(range=(0, None), shared='none') + LinearTrend(shared='full') + ((FourierSeasonality(perios=365) + FourierSeasonality(period=7)) * Constant(range=[-1, 1], group='subject', shared='none'))
model = LinearTrend() + (FourierSeasonality(perios=365) * Constant(range=[-1, 1], group='subject', shared='none')) + FourierSeasonality(period=7)

# 6) keyword option
model = LinearTrend() + FourierSeasonality()
model = LinearTrend(partially_shared_on='subject') + FourierSeasonality()
model = LinearTrend(distinct_on='subject') + FourierSeasonality()

# 7) bitwise or Operator option
model = LinearTrend()                                         # single estimation for all subjects
model = (LinearTrend() | 'subject') + FourierSeasonality()    # hierarchical estimation
model = (LinearTrend() || ('subject') + FourierSeasonality()  # independent estimation

#return day well spent

LinearTrend() error if run without arguments

We noticed that if we run the a model with a LinearTrend without any arguments it errors with an unclear error message: " unsupported operand type(s) for +: 'NoneType' and 'int' "

Prediction method and on-going maintenance

Hi Matthijs ! Just watched your PyData talk from Oct 2020 and would love to try using this model package in a production codebase. It appears the main missing element is a robust prediction api. Is this still the case?

If so, would you be willing to invest some time either adding this functionality or guiding me to do it? I would also be happy to pitch in some level of on-going maintenance if so.

cannibalistic effects

Something like this?

LinearTrend() + (Constant(hierarchical_on='subgroup_col) * FourierSeasonality())

Does/can timeseers support external models for trend ?

Sorry for writing this question as an issue. One of the applications I deal with is forecasting short-mid life sku (2-10months). With prophet, you cannot model the trend for the product lifecycle with introduction-growth-maturity-decline phases. However, we can model trend with some kind of scikit/random forest type model based on other SKUs that have completed their lifecycle.

Is there any way to insert an external model for the trend component, for an application such as above?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.