Pattern Matching (BNN & CORN)
Relevant source files
The following files were used as context for generating this wiki page:
Pattern matching algorithms in the universal-portfolios library are Nonparametric Learning approaches for Online Portfolio Selection (OLPS). These algorithms, specifically BNN (Nearest Neighbor) and CORN (Correlation-driven Nonparametric Learning), function by identifying historical price sequences that are similar to the current market state and using the subsequent returns of those historical matches to determine optimal portfolio weights for the next period.
Both algorithms inherit from the base Algo class and operate on "ratio" price types ($p_t / p_{t-1}$), aiming to maximize final wealth through historical comparison rather than explicit parametric modeling.
Sources: universal/algos/bnn.py:10-18, universal/algos/corn.py:17-29, README.md:44-55
Algorithm Architecture and Logic
The pattern matching strategy follows a general three-step workflow: sequence identification, similarity calculation (using Euclidean distance or Correlation), and portfolio optimization based on matched returns.
BNN: Nearest Neighbor Strategy
The BNN algorithm identifies a sequence of length k and looks for the l most similar sequences in the past. It calculates similarity using the squared Euclidean distance between the current sequence of returns and historical windows. Once the nearest neighbors are found, the algorithm extracts the returns that immediately followed those historical windows and calculates the Best Constant Rebalanced Portfolio (BCRP) weights for that subset of returns.
Sources: universal/algos/bnn.py:27-55
CORN: Correlation-driven Learning
CORN is a correlation-driven approach that searches history for windows of length window that have a correlation coefficient with the current window higher than a threshold rho. Unlike BNN, which picks a fixed number of neighbors, CORN identifies all historical points meeting the correlation criteria. If matching patterns are found, it optimizes weights using the opt_weights utility.
Sources: universal/algos/corn.py:46-107
Process Flow
The following diagram illustrates the logical flow for both pattern matching algorithms during a single time step.
The diagram shows the branching logic between BNN (distance-based) and CORN (correlation-based) to identify historical return sets for optimization.
Sources: universal/algos/bnn.py:40-66, universal/algos/corn.py:59-107
Implementation Details
Configuration Parameters
The behavior of these algorithms is controlled by window sizes and similarity thresholds.
| Algorithm | Parameter | Type | Default | Description |
|---|---|---|---|---|
| BNN | k | int | 5 | Length of the sequence to match. |
| BNN | l | int | 10 | Number of nearest neighbors to consider. |
| CORN | window | int | 5 | Length of the lookback window for correlation. |
| CORN | rho | float | 0.1 | Correlation coefficient threshold (Range: -1 to 1). |
| CORN | fast_version | bool | True | Enables vectorized correlation calculation for 2x speedup. |
| Sources: universal/algos/bnn.py:31-35, universal/algos/corn.py:40-45 |
Core Classes and Functions
BNN.find_nn(H, k, l)
This function implements an efficient search for neighbors. It shifts the history H by offsets up to k to align historical windows with the current tail and calculates the sum of squared differences.
Sources: universal/algos/bnn.py:57-69
CORN.step_fast vs CORN.step_slow
CORN implements two versions of its logic:
- step_slow: Iterates through history, calculating
np.corrcoeffor each possible window. - step_fast: Uses a flattened version of the history (
self.X_flat) created duringinit_stepviapd.concatwith shifts. This allows usingapplyover a DataFrame for vectorized correlation comparison.
Sources: universal/algos/corn.py:54-107
tools.bcrp_weights(J)
Utilized by BNN to find the Best Constant Rebalanced Portfolio for the set of identified returns J. This calls opt_weights with the "return" metric to find weights that would have been optimal in hindsight for those specific days.
Sources: universal/algos/bnn.py:55, universal/tools.py:202-205
Class Hierarchy
The pattern matching algorithms are part of the broader universal.algos ecosystem, inheriting core execution logic from the base Algo class.
This diagram represents the inheritance structure where specialized pattern matching logic is implemented in the step methods of the subclasses.
Sources: universal/algo.py:16-110, universal/algos/bnn.py:10-30, universal/algos/corn.py:17-48
Technical Summary
Pattern matching strategies in this repository provide a nonparametric alternative to mean-reversion or follow-the-winner strategies. By leveraging historical similarity via Euclidean distance (BNN) or Correlation (CORN), they attempt to capture complex market patterns that repeat over time. While BNN uses a fixed-count neighbor approach (l), CORN utilizes a threshold-based approach (rho) to filter the historical set used for weight optimization.
Sources: universal/algos/bnn.py:10-18, universal/algos/corn.py:17-29