I’m trying to make an out-of-sample prediction using a model_factory setup. But I’m getting a shape error when using pymc5, but the same code works on pymc3. The error is due to the size deference of the “seasonality” random variable, which assumes it needs to have the same size as the training dataset. The prediction works when I increase the period to 60 months.
def model_factory(data, n_order):
store_idx, stores = data.store.factorize(sort=True)
item_idx, items = data.item.factorize(sort=True)
date_idx, date = data.date.factorize(sort=True)
t = date_to_timeindex(date)
fourier_features = pd.DataFrame(
{
f"{func}_order_{order}": getattr(np, func)(2 * np.pi * t * order)
for order in range(1, n_order + 1)
for func in ("sin", "cos")
}
)
print(fourier_features.shape)
COORDS = {
"obs": data.index,
"store": stores,
"item": items,
"date": date,
"fourier_features": np.arange(2 * n_order),
}
with pm.Model(coords=COORDS) as model:
_t = pm.MutableData("t", t, dims="date")
_date_idx = pm.MutableData("date_idx", date_idx, dims="obs")
_store_idx = pm.MutableData("store_idx", store_idx, dims="obs")
_item_idx = pm.MutableData("item_idx", item_idx, dims="obs")
sales = pm.MutableData("sales", data.sales, dims="obs")
baseline = pm.Normal(
"baseline", mu=np.log(1000), sigma=0.9, dims=["store", "item"]
)
trend = pm.Normal("trend", mu=0, sigma=0.05)
fourier_coefs = pm.Normal(
"fourier_coefs", mu=0, sigma=0.1, dims="fourier_features"
)
seasonality = pm.Deterministic(
"seasonality",
pm.math.dot(fourier_coefs, fourier_features.to_numpy().T),
dims="date",
)
def forecast_demand(idata,
forecasted_dates=pd.date_range("2018-01", "2019-01", freq="M"),):
n_order = len(idata.posterior.coords["fourier_features"]) // 2
print(n_order)
store = idata.posterior.coords["store"].values
item = idata.posterior.coords["item"].values
forecast_data_index = pd.DataFrame(
data=0,
index=pd.MultiIndex.from_product(
[forecasted_dates, store, item], names=["date", "store", "item"],
),
columns=["sales"],
)
print(forecasted_dates.shape)
forecast_data = forecast_data_index.reset_index()
print(forecast_data.shape)
print(forecast_data.head())
with model_factory(data=forecast_data, n_order=n_order) as forecast_model:
ppc = pm.sample_posterior_predictive(idata)
return ppc, forecast_data
241 raise TypeError(
242 "The numpy.ndarray object is not aligned."
243 " PyTensor C code does not support that.",
244 )
246 if not all(
247 ds == ts if ts is not None else True
248 for ds, ts in zip(data.shape, self.shape)
249 ):
--> 250 raise TypeError(
251 f"The type's shape ({self.shape}) is not compatible with the data's ({data.shape})"
252 )
254 if self.filter_checks_isfinite and not np.all(np.isfinite(data)):
255 raise ValueError("Non-finite elements not allowed")
TypeError: ("The type's shape ((12,)) is not compatible with the data's ((60,))", 'Container name "seasonality"')