Skip to content

Optimize ARMA forecast loop to O(N) by using pre-allocated buffer - #33

Merged
OVVO-Financial merged 2 commits into
mainfrom
claude/serene-clarke-batq0o
Jun 15, 2026
Merged

Optimize ARMA forecast loop to O(N) by using pre-allocated buffer#33
OVVO-Financial merged 2 commits into
mainfrom
claude/serene-clarke-batq0o

Conversation

@OVVO-Financial

Copy link
Copy Markdown
Owner

Summary

Replaced the O(N²) array concatenation pattern in the ARMA forecast loop with a pre-allocated buffer and index-based writes, reducing time complexity to O(N).

Key Changes

  • Pre-allocate a fixed-size buffer (values.size + horizon) once before the forecast loop instead of reallocating on each iteration
  • Replace np.concatenate() calls with direct buffer writes using an index pointer (current_len)
  • Pass a view of the populated prefix (buffer[:current_len]) to helper functions to maintain identical mathematical behavior
  • Eliminate repeated full-array copies that occurred with the growing-array approach

Implementation Details

The original code used np.concatenate((current, np.array([estimate]))) at each iteration, which reallocates and copies the entire accumulated series. With a long horizon, this results in O(N²) total operations.

The optimized version:

  • Allocates buffer with capacity for the full history plus forecast horizon upfront
  • Initializes it with the input values
  • Advances current_len pointer as new estimates are computed
  • Provides helpers with a view of only the populated portion via slicing, preserving the original algorithm's logic

This change maintains numerical equivalence while dramatically improving performance for long-horizon forecasts.

https://claude.ai/code/session_01AN7vrBnRhxGd4eZv6A34VX

claude added 2 commits June 15, 2026 15:17
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.
The editable package version recorded in the lockfile was stale
(1.0.0a0); uv refreshed it to match pyproject.toml during a local
test run.
@OVVO-Financial
OVVO-Financial merged commit 3848dc1 into main Jun 15, 2026
4 checks passed
@OVVO-Financial
OVVO-Financial deleted the claude/serene-clarke-batq0o branch June 15, 2026 15:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants