Simulate observations from the extrapolated generative model. Vectorized version of sample_model(). Parameters ---------- df: Prediction dataframe. seasonal_features: pd.DataFrame of seasonal features. iteration: Int sampling iteration to use parameters from.
(
self,
df: pd.DataFrame,
seasonal_features: pd.DataFrame,
iteration: int,
s_a: np.ndarray,
s_m: np.ndarray,
n_samples: int,
)
| 1537 | } |
| 1538 | |
| 1539 | def sample_model_vectorized( |
| 1540 | self, |
| 1541 | df: pd.DataFrame, |
| 1542 | seasonal_features: pd.DataFrame, |
| 1543 | iteration: int, |
| 1544 | s_a: np.ndarray, |
| 1545 | s_m: np.ndarray, |
| 1546 | n_samples: int, |
| 1547 | ) -> List[Dict[str, np.ndarray]]: |
| 1548 | """Simulate observations from the extrapolated generative model. Vectorized version of sample_model(). |
| 1549 | |
| 1550 | Parameters |
| 1551 | ---------- |
| 1552 | df: Prediction dataframe. |
| 1553 | seasonal_features: pd.DataFrame of seasonal features. |
| 1554 | iteration: Int sampling iteration to use parameters from. |
| 1555 | s_a: Indicator vector for additive components. |
| 1556 | s_m: Indicator vector for multiplicative components. |
| 1557 | n_samples: Number of future paths of the trend to simulate. |
| 1558 | |
| 1559 | Returns |
| 1560 | ------- |
| 1561 | List (length n_samples) of dictionaries with arrays for trend and yhat, each ordered like df['t']. |
| 1562 | """ |
| 1563 | # Get the seasonality and regressor components, which are deterministic per iteration |
| 1564 | beta = self.params['beta'][iteration] |
| 1565 | Xb_a = np.matmul(seasonal_features.values, |
| 1566 | beta * s_a.values) * self.y_scale |
| 1567 | Xb_m = np.matmul(seasonal_features.values, beta * s_m.values) |
| 1568 | # Get the future trend, which is stochastic per iteration |
| 1569 | trends = self.sample_predictive_trend_vectorized(df, n_samples, iteration) # already on the same scale as the actual data |
| 1570 | sigma = self.params['sigma_obs'][iteration] |
| 1571 | noise_terms = np.random.normal(0, sigma, trends.shape) * self.y_scale |
| 1572 | |
| 1573 | simulations = [] |
| 1574 | for trend, noise in zip(trends, noise_terms): |
| 1575 | simulations.append({ |
| 1576 | 'yhat': trend * (1 + Xb_m) + Xb_a + noise, |
| 1577 | 'trend': trend |
| 1578 | }) |
| 1579 | return simulations |
| 1580 | |
| 1581 | def sample_predictive_trend(self, df, iteration): |
| 1582 | """Simulate the trend using the extrapolated generative model. |
no test coverage detected