facebook / prophet

Tool for producing high quality forecasts for time series data that has multiple seasonality with linear or non-linear growth.
https://facebook.github.io/prophet
MIT License
18.54k stars 4.54k forks source link

Manually calculating Fourier Series #1383

Closed shoaibkhanz closed 4 years ago

shoaibkhanz commented 4 years ago

Hi @bletham,

I am trying to better understand the calculation involved to get seasonality(I am going simple with Fourier order = 1). I have observed in the code that some scaling has been performed before fitting. I have provided a gist where I showcase what I am tying to achieve but not sure where I am wrong.

https://gist.github.com/shoaibkhanz/8e01b295ed2f938fb4ba9cd1a6646680

I am also following this blog post to calculate this and I believe I have got everything covered but in reality numbers don't match.
https://www.ritchievink.com/blog/2018/10/09/build-facebooks-prophet-in-pymc3-bayesian-time-series-analyis-with-generalized-additive-models/

bletham commented 4 years ago

The computation of the Fourier features happens here: https://github.com/facebook/prophet/blob/d9bea6196981bfcedae40525e702d424d26e7cd0/python/fbprophet/forecaster.py#L416

The t used is actually not the scaled time used elsewhere in the model (and in your gist), it is this: https://github.com/facebook/prophet/blob/d9bea6196981bfcedae40525e702d424d26e7cd0/python/fbprophet/forecaster.py#L430-L435

So basically this amounts to an offset of what you have. Does that make everything line up?

shoaibkhanz commented 4 years ago

Thanks @bletham, it matches now!