Sample draws of the future trend values. Vectorized version of sample_predictive_trend(). Parameters ---------- df: Prediction dataframe. iteration: Int sampling iteration to use parameters from. n_samples: Number of future paths of the trend to simulate.
(self, df: pd.DataFrame, n_samples: int, iteration: int = 0)
| 1632 | return trend * self.y_scale + df['floor'] |
| 1633 | |
| 1634 | def sample_predictive_trend_vectorized(self, df: pd.DataFrame, n_samples: int, iteration: int = 0) -> np.ndarray: |
| 1635 | """Sample draws of the future trend values. Vectorized version of sample_predictive_trend(). |
| 1636 | |
| 1637 | Parameters |
| 1638 | ---------- |
| 1639 | df: Prediction dataframe. |
| 1640 | iteration: Int sampling iteration to use parameters from. |
| 1641 | n_samples: Number of future paths of the trend to simulate. |
| 1642 | |
| 1643 | Returns |
| 1644 | ------- |
| 1645 | Draws of the trend values with shape (n_samples, len(df)). Values are on the scale of the original data. |
| 1646 | """ |
| 1647 | deltas = self.params["delta"][iteration] |
| 1648 | m = self.params["m"][iteration] |
| 1649 | k = self.params["k"][iteration] |
| 1650 | if self.growth == "linear": |
| 1651 | expected = self.piecewise_linear(df["t"].values, deltas, k, m, self.changepoints_t) |
| 1652 | elif self.growth == "logistic": |
| 1653 | expected = self.piecewise_logistic( |
| 1654 | df["t"].values, df["cap_scaled"].values, deltas, k, m, self.changepoints_t |
| 1655 | ) |
| 1656 | elif self.growth == "flat": |
| 1657 | expected = self.flat_trend(df["t"].values, m) |
| 1658 | else: |
| 1659 | raise NotImplementedError |
| 1660 | uncertainty = self._sample_uncertainty(df, n_samples, iteration) |
| 1661 | return ( |
| 1662 | (np.tile(expected, (n_samples, 1)) + uncertainty) * self.y_scale + |
| 1663 | np.tile(df["floor"].values, (n_samples, 1)) |
| 1664 | ) |
| 1665 | |
| 1666 | def _sample_uncertainty(self, df: pd.DataFrame, n_samples: int, iteration: int = 0) -> np.ndarray: |
| 1667 | """Sample draws of future trend changes, vectorizing as much as possible. |
no test coverage detected