Simulate the trend using the extrapolated generative model. Parameters ---------- df: Prediction dataframe. iteration: Int sampling iteration to use parameters from. Returns ------- np.array of simulated trend over df['t'].
(self, df, iteration)
| 1579 | return simulations |
| 1580 | |
| 1581 | def sample_predictive_trend(self, df, iteration): |
| 1582 | """Simulate the trend using the extrapolated generative model. |
| 1583 | |
| 1584 | Parameters |
| 1585 | ---------- |
| 1586 | df: Prediction dataframe. |
| 1587 | iteration: Int sampling iteration to use parameters from. |
| 1588 | |
| 1589 | Returns |
| 1590 | ------- |
| 1591 | np.array of simulated trend over df['t']. |
| 1592 | """ |
| 1593 | k = self.params['k'][iteration] |
| 1594 | m = self.params['m'][iteration] |
| 1595 | deltas = self.params['delta'][iteration] |
| 1596 | |
| 1597 | t = np.array(df['t']) |
| 1598 | T = t.max() |
| 1599 | |
| 1600 | # New changepoints from a Poisson process with rate S on [1, T] |
| 1601 | if T > 1: |
| 1602 | S = len(self.changepoints_t) |
| 1603 | n_changes = np.random.poisson(S * (T - 1)) |
| 1604 | else: |
| 1605 | n_changes = 0 |
| 1606 | if n_changes > 0: |
| 1607 | changepoint_ts_new = 1 + np.random.rand(n_changes) * (T - 1) |
| 1608 | changepoint_ts_new.sort() |
| 1609 | else: |
| 1610 | changepoint_ts_new = [] |
| 1611 | |
| 1612 | # Get the empirical scale of the deltas, plus epsilon to avoid NaNs. |
| 1613 | lambda_ = np.mean(np.abs(deltas)) + 1e-8 |
| 1614 | |
| 1615 | # Sample deltas |
| 1616 | deltas_new = np.random.laplace(0, lambda_, n_changes) |
| 1617 | |
| 1618 | # Prepend the times and deltas from the history |
| 1619 | changepoint_ts = np.concatenate((self.changepoints_t, |
| 1620 | changepoint_ts_new)) |
| 1621 | deltas = np.concatenate((deltas, deltas_new)) |
| 1622 | |
| 1623 | if self.growth == 'linear': |
| 1624 | trend = self.piecewise_linear(t, deltas, k, m, changepoint_ts) |
| 1625 | elif self.growth == 'logistic': |
| 1626 | cap = df['cap_scaled'] |
| 1627 | trend = self.piecewise_logistic(t, cap, deltas, k, m, |
| 1628 | changepoint_ts) |
| 1629 | elif self.growth == 'flat': |
| 1630 | trend = self.flat_trend(t, m) |
| 1631 | |
| 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(). |
no test coverage detected