PERF: Optimize VECM memory/speed by avoiding O(T^2) projection matrix
#9720
+8
−7
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Title:
PERF: Optimize VECM memory/speed by avoiding O(T^2) projection matrixDescription:
Problem$M$ :$(T \times T)$ .
The current implementation of
_r_matricesin VECM explicitly constructs the projection/annihilator matrix$$ M = I_T - X' (X X')^{-1} X $$
This matrix has dimensions
MemoryErroror severe swapping.Solution$M$ :$O(T)$ (for storing inputs/outputs) and computational complexity significantly, as intermediate matrices are now $(K \times N)$ or $(N \times N)$ where $N, K \ll T$ .
Refactored
_r_matricesto calculate residuals directly using the associativity of matrix multiplication, without forming$$ R = Y - (Y X') (X X')^{-1} X $$
This reduces the memory complexity to
Benchmark Results$T$ increases, with ~113x speedup for $T=30,000$ .
Comparison against the current main branch across various scenarios (using
time.time()).Significant speedups are observed as
Click to see full comprehensive benchmark table
The following table compares the original implementation vs. this PR.
z_alphaetc. refer to the t-stats. Differences are within machine precision (float64 associativity noise).Numerical Check: All coefficient differences are$< 10^{-14}$ . LLF differences are $< 10^{-11}$ (acceptable for large cumulative sums).
Mathematical Equivalence$(Y M) = Y (I - P) = Y - YP$ , where $P$ is the projection matrix onto $\Delta X$ .
The change relies on the identity