To determine the dynamic correlation between two large arrays (each with >4M values) where the correlation varies over time, you need a sliding window approach that computes correlation over smaller segments. Here are the best strategies:
1. Sliding Window Pearson Correlation
- Concept: Compute Pearson correlation over a fixed-size moving window.
- Steps:
- Define a window size (e.g., 1000 points).
- Slide the window across both arrays, computing Pearson correlation for each segment.
- Store the correlation coefficients in a new array.
- Pros: Simple, widely used, detects linear correlation.
- Cons: Fixed window size may miss short-term changes.
Python Example:
import numpy as np
from scipy.stats import pearsonr
def sliding_pearson(arr1, arr2, window_size):
n = len(arr1)
correlations = np.zeros(n)
half_window = window_size // 2
for i in range(half_window, n - half_window):
window1 = arr1[i - half_window : i + half_window]
window2 = arr2[i - half_window : i + half_window]
r, _ = pearsonr(window1, window2)
correlations[i] = r
return correlations
corr_array = sliding_pearson(array1, array2, window_size=1000)
2. Dynamic Time Warping (DTW) for Local Similarity
- Concept: Measures similarity even if signals are out of phase.
- Use Case: If the curves are similar but shifted in time.
- Pros: Handles non-linear alignments.
- Cons: Computationally expensive (~O(n²)).
Python Example:
from dtaidistance import dtw
def compute_dtw_correlation(arr1, arr2, window_size):
n = len(arr1)
correlations = np.zeros(n)
for i in range(0, n, window_size):
window1 = arr1[i:i+window_size]
window2 = arr2[i:i+window_size]
distance = dtw.distance(window1, window2)
correlations[i:i+window_size] = 1 / (1 + distance) # Convert to similarity
return correlations
3. Rolling Spearman Rank Correlation (Non-linear)
- Concept: Like Pearson but for monotonic (not strictly linear) relationships.
- Use Case: If the relationship is non-linear but consistent in rank.
- Pros: Robust to outliers.
- Cons: Slightly slower than Pearson.
Python Example:
from scipy.stats import spearmanr
def rolling_spearman(arr1, arr2, window_size):
n = len(arr1)
correlations = np.zeros(n)
for i in range(n - window_size + 1):
window1 = arr1[i:i+window_size]
window2 = arr2[i:i+window_size]
r, _ = spearmanr(window1, window2)
correlations[i + window_size//2] = r
return correlations
4. Wavelet Coherence (Time-Frequency Analysis)
- Concept: Identifies regions where two signals are correlated in both time and frequency.
- Use Case: If correlation varies at different scales (e.g., high-frequency noise vs. low-frequency trends).
- Pros: Captures multi-scale dependencies.
- Cons: More complex, requires tuning.
Python Example (using pywt):
import pywt
def wavelet_coherence(arr1, arr2, scales=np.arange(1, 100)):
coherence, _ = pywt.coherence(arr1, arr2, scales=scales)
return coherence.mean(axis=0) # Average across scales
5. Cross-Correlation with Variable Lag
- Concept: Finds the best correlation by allowing a time shift.
- Use Case: If one signal leads/lags the other.
- Pros: Detects lagged relationships.
- Cons: Computationally intensive for large arrays.
Python Example:
from scipy.signal import correlate
def max_cross_corr(arr1, arr2, max_lag=100):
n = len(arr1)
correlations = np.zeros(n)
for i in range(max_lag, n - max_lag):
window1 = arr1[i - max_lag : i + max_lag]
window2 = arr2[i - max_lag : i + max_lag]
corr = correlate(window1, window2, mode='valid')
best_corr = np.max(corr)
correlations[i] = best_corr
return correlations
Which Method to Choose?
| Method | Best For | Speed | Complexity |
|---|
| Sliding Pearson | Linear correlation | Fast | Low |
| Rolling Spearman | Non-linear (monotonic) | Medium | Low |
| DTW | Phase-shifted signals | Slow | High |
| Wavelet Coherence | Multi-scale analysis | Medium | High |
| Cross-Correlation | Lagged relationships | Slow | Medium |
Optimization for Large Arrays
- Use Numba or Cython to speed up loops.
- Downsample data if high-frequency details are not critical.
- Parallelize computations (e.g., using
multiprocessing).
Would you like a specific implementation optimized for your use case?