mirror of
https://github.com/saymrwulf/prophet.git
synced 2026-11-17 08:40:56 +00:00
Switch future changepoint generation to fully continuous model
This commit is contained in:
parent
5bcc1bc094
commit
cdc313584e
2 changed files with 15 additions and 33 deletions
|
|
@ -1447,30 +1447,18 @@ sample_predictive_trend <- function(model, df, iteration) {
|
||||||
t <- df$t
|
t <- df$t
|
||||||
T <- max(t)
|
T <- max(t)
|
||||||
|
|
||||||
|
# New changepoints from a Poisson process with rate S on [1, T]
|
||||||
if (T > 1) {
|
if (T > 1) {
|
||||||
# Get the time discretization of the history
|
|
||||||
dt <- diff(model$history$t)
|
|
||||||
dt <- min(dt[dt > 0])
|
|
||||||
# Number of time periods in the future
|
|
||||||
N <- ceiling((T - 1) / dt)
|
|
||||||
S <- length(model$changepoints.t)
|
S <- length(model$changepoints.t)
|
||||||
# The history had S split points, over t = [0, 1].
|
n.changes <- stats::rpois(1, S * (T - 1))
|
||||||
# The forecast is on [1, T], and should have the same average frequency of
|
} else {
|
||||||
# rate changes. Thus for N time periods in the future, we want an average
|
n.changes <- 0
|
||||||
# of S * (T - 1) changepoints in expectation.
|
}
|
||||||
prob.change <- min(1, (S * (T - 1)) / N)
|
if (n.changes > 0) {
|
||||||
# This calculation works for both history and df not uniformly spaced.
|
changepoint.ts.new <- 1 + stats::runif(n.changes) * (T - 1)
|
||||||
n.changes <- stats::rbinom(1, N, prob.change)
|
changepoint.ts.new <- sort(changepoint.ts.new)
|
||||||
|
|
||||||
# Sample ts
|
|
||||||
if (n.changes == 0) {
|
|
||||||
changepoint.ts.new <- c()
|
|
||||||
} else {
|
|
||||||
changepoint.ts.new <- sort(stats::runif(n.changes, min = 1, max = T))
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
changepoint.ts.new <- c()
|
changepoint.ts.new <- c()
|
||||||
n.changes <- 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Get the empirical scale of the deltas, plus epsilon to avoid NaNs.
|
# Get the empirical scale of the deltas, plus epsilon to avoid NaNs.
|
||||||
|
|
|
||||||
|
|
@ -1303,23 +1303,17 @@ class Prophet(object):
|
||||||
t = np.array(df['t'])
|
t = np.array(df['t'])
|
||||||
T = t.max()
|
T = t.max()
|
||||||
|
|
||||||
|
# New changepoints from a Poisson process with rate S on [1, T]
|
||||||
if T > 1:
|
if T > 1:
|
||||||
# Get the time discretization of the history
|
|
||||||
dt = np.diff(self.history['t'])
|
|
||||||
dt = np.min(dt[dt > 0])
|
|
||||||
# Number of time periods in the future
|
|
||||||
N = np.ceil((T - 1) / float(dt))
|
|
||||||
S = len(self.changepoints_t)
|
S = len(self.changepoints_t)
|
||||||
|
n_changes = np.random.poisson(S * (T - 1))
|
||||||
prob_change = min(1, (S * (T - 1)) / N)
|
|
||||||
n_changes = np.random.binomial(N, prob_change)
|
|
||||||
|
|
||||||
# Sample ts
|
|
||||||
changepoint_ts_new = sorted(np.random.uniform(1, T, n_changes))
|
|
||||||
else:
|
else:
|
||||||
# Case where we're not extrapolating.
|
|
||||||
changepoint_ts_new = []
|
|
||||||
n_changes = 0
|
n_changes = 0
|
||||||
|
if n_changes > 0:
|
||||||
|
changepoint_ts_new = 1 + np.random.rand(n_changes) * (T - 1)
|
||||||
|
changepoint_ts_new.sort()
|
||||||
|
else:
|
||||||
|
changepoint_ts_new = []
|
||||||
|
|
||||||
# Get the empirical scale of the deltas, plus epsilon to avoid NaNs.
|
# Get the empirical scale of the deltas, plus epsilon to avoid NaNs.
|
||||||
lambda_ = np.mean(np.abs(deltas)) + 1e-8
|
lambda_ = np.mean(np.abs(deltas)) + 1e-8
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue