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

Components of Predict not adding up #2512

Open GinoWoz1 opened 11 months ago

GinoWoz1 commented 11 months ago

Hello All,

Currently when I run multiplicative model with a regressor on my work data set, the sum of terms does not equal yhat. Here is reproducible example - the differences are small since the random numbers I am dealing with but as you can see they are not adding up. Any clue?

# Step 1: Setup and Installation
library(pacman)
p_load(prophet,dplyr)

# Data Preparation with Regressor
# Create a sequence of dates
ds <- seq(as.Date("2020-01-01"), as.Date("2022-01-01"), by="days")
# Create a dummy target variable
y <- rnorm(732, mean=20000, sd=5000)
# Create a dummy exogenous variable (e.g., marketing campaign: 1 for active days, 0 for others)
marketing_campaign <- rep(0, 732)
marketing_campaign[sample(1:732, 100)] <- 1  # Randomly activate the campaign on 100 days
df <- data.frame(ds, y, marketing_campaign)

# Initialize the Model with Multiplicative Seasonality
m <- prophet(seasonality.mode = "multiplicative")

# Add the Regressor to the Model
m <- add_regressor(m, 'marketing_campaign', mode = "multiplicative")

# Model Training
m <- fit.prophet(m, df)

# Forecasting with Regressor
future <- make_future_dataframe(m, periods = 365)
# Assume the marketing campaign is active on random 50 days in the future
future$marketing_campaign <- rep(0, nrow(future))
future$marketing_campaign[sample(1:nrow(future), 50)] <- 1

forecast <- predict(m, future)

forecast %>% 
  mutate(sum_of_terms = trend * 
                (1+ weekly) * 
                (1 + yearly) *
                (1 + extra_regressors_multiplicative)) %>%
  select( ds, yhat, sum_of_terms)
GinoWoz1 commented 11 months ago

image

GinoWoz1 commented 11 months ago

Seems off insignificantly however on my own data - its off by 5-10% (and has the same terms)

breadfan commented 8 months ago

Multiplicative model is y(t) = g(t) * (1+ s(t) + h(t))