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.36k stars 4.52k forks source link

Different params on prophets model (windows != unix ?) Why? #1855

Closed clonyjr closed 3 years ago

clonyjr commented 3 years ago

Hi guys, how you doing? Hope everyone is alright! In the image below you will see the model.params printed on a windows based system:

pastedImage

And these are the same params returned from the same object (prophet model) but on a unix based system:

Captura de tela de 2021-03-26 09-20-41

As you can see the image shows different params from the ones returned on unix based system, ok, it is true that prophet's object does not represent the same data, still this should not be the reason for this. In fact I am assuming that this difference resides on the system based, since the fit method on CmdStanPyBackend class inside models.py, uses a method call prepare_data which is not used on unix based system on class PyStanBackend. I do not have window system, but discussing about Prophet with my teacher she show me that her model object has different params from mine, and I wish to know why is that and if it is correct the assumption based on the operating system.

Best regards to everyone.

bletham commented 3 years ago

There aren't any changes to the logic inside the prophet package depending on the operating system. There are some changes depending on the backend (cmdstanpy or pystan) but you can use either on either OS, so for instance if you use the pystan backend in Windows it will have the same logic as in Unix.

(I will note as a minor contradiction to what I said, Windows and Unix do use different stan model files: https://github.com/facebook/prophet/tree/master/python/stan . But they are functionally equivalent, there were just some Stan functions that didn't work in windows. There is no difference in the python code by OS.).

Even though the model and code is the same, it isn't uncommon to see small differences in fitted parameters across different systems. This is usually because of differences in the linear algebra libraries across systems, which produce differences in the optimizers and can cause, even for the same dataset, the optimization to terminate in slightly different places. This can especially be an issue on very small datasets or datasets without a lot of structure, where the likelihood surface is very flat and so the optimizer converges poorly.

There's some discussion of this in #1077 and #253.

The differences in parameters that you report do seem larger than I've usually seen with this issue, so if possible I'd be very interested to see the output of m.plot() on both systems to compare them and see if it does look like numerical issues in the optimizer is the source of the discrepancy.

clonyjr commented 3 years ago

Thanks for your answer. The differences that you see and turns your attention to, probably is due two different datasets, as I mentioned before "it is true that prophet's object does not represent the same data" . Here is the models params using the same dataset:

Captura de tela de 2021-04-05 15-39-59

I think that's the problem (so I won't upload the image because I think it is not necessary and I have to ask my teacher to print it from her computer, but let me know it you still want to see it, though I'll upload it for you)

Maybe I didn't understand your answer so excuse me if I'll be repeating myself. I asked why using windows (cmdstanpy), Prophet models params was:

and when using Prophet on unix based system, the models params (pystan) was:

I want to understand the absence of two params (beta_m and beta_a) on Pystan backend model.

Thanks again.

bletham commented 3 years ago

Oh, I'm sorry, I totally misunderstood the question. I had thought it was about why the values are different (a question that comes up periodically) and didn't even notice that the list of parameters itself is different.

I actually hadn't ever noticed this until you just pointed it out. This difference does come from the fact that Windows and Unix are using different Stan models. In Windows, these are the parameters defined in the Stan model: https://github.com/facebook/prophet/blob/cd8a24eddd6bebb9888f2e0ae2388f209047b02d/python/stan/win/prophet.stan#L131-L143 As you see, these are all extracted and attached to the Prophet model m.params. Then the Stan model itself is very simple, https://github.com/facebook/prophet/blob/cd8a24eddd6bebb9888f2e0ae2388f209047b02d/python/stan/win/prophet.stan#L174

In Unix, the parameters Y, beta_m, and beta_a are left out: https://github.com/facebook/prophet/blob/cd8a24eddd6bebb9888f2e0ae2388f209047b02d/python/stan/unix/prophet.stan#L108-L117

Instead, rather than having that computation done as a derived parameter, it is just done directly in the model component: https://github.com/facebook/prophet/blob/cd8a24eddd6bebb9888f2e0ae2388f209047b02d/python/stan/unix/prophet.stan#L136-L141

The two Stan models have diverged from the very beginning, because the vector/matrix operations used in the Unix model code above failed in Windows; there is discussion of that here: https://github.com/facebook/prophet/pull/96 , which is when the separate Windows model was introduced. Without having the vector/matrix operations you have to do things in a slightly more circuitous route, that involves creating some extra transformed parameters.

So that's what's happening and why it's happening. These extra parameters Y, beta_m, and beta_a are not used in the python code anywhere, they are just returned from Stan because they had to be defined in Stan and we currently extract out everything that Stan returns. So there isn't anything different in the actual models that is causing the parameter list to be different.