Skip to content

Commit 09dd7f9

Browse files
committed
Pre-allocate nns_arma recursive forecast buffer
Replace the per-step np.concatenate in the nns_arma recursive forecast loop with a pre-allocated buffer and a length pointer. The previous approach reallocated and copied the entire series on every horizon step (O(N^2) over the horizon); writing into a fixed buffer and passing a view of the populated prefix to the helpers is O(1) per step and leaves the forecast math unchanged.
1 parent d8df069 commit 09dd7f9

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

src/nns/arma.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,9 +433,18 @@ def _finish(
433433
)
434434
return _finish(estimates)
435435

436-
current = values
436+
# Pre-allocate the full history-plus-horizon buffer once and fill it by
437+
# index. Appending the recursive estimate via np.concatenate at every step
438+
# reallocates and copies the entire series each iteration, which is O(N^2)
439+
# over a long horizon; writing into a fixed buffer and advancing a length
440+
# pointer is O(1) per step. The helpers receive a view of the populated
441+
# prefix, so the math is identical to the growing-array version.
442+
buffer = np.empty(values.size + horizon, dtype=np.float64)
443+
buffer[: values.size] = values
444+
current_len = values.size
437445
lin_regression_estimates = np.array([], dtype=np.float64)
438446
for index in range(horizon):
447+
current = buffer[:current_len]
439448
if dynamic:
440449
lags, lag_weights = _resolve_lags_and_weights(
441450
current,
@@ -496,7 +505,8 @@ def _finish(
496505
estimate = 0.0
497506

498507
estimates[index] = estimate
499-
current = np.concatenate((current, np.array([estimate], dtype=np.float64)))
508+
buffer[current_len] = estimate
509+
current_len += 1
500510

501511
lin_resid = 0.0
502512
if pred_int is not None and method_l != "means" and lin_regression_estimates.size:

0 commit comments

Comments
 (0)