Alpha Correlation Analysis

Lancer : Codespaces Colab

Objective: Find truly complementary alpha combinations for composite stratégies.

Problem Statement

Current composite stratégies often have Sharpe ratios that are linear combinations of underlying alphas. We need to find: - Uncorrelated return streams (différent market regimes, différent asset classes) - Asymmetric complementarity (one alpha profits when the other is flat/losing) - Synergistic combinations (combined Sharpe > weighted average)

Methodology

  1. Return Stream Collection: Gather daily returns from all alpha stratégies
  2. Correlation Matrix Analysis: Identify uncorrelated stratégies
  3. Regime Analysis: Map performance across market regimes (bull/bear/sideways, high/low vol)
  4. Complementarity Score: Rank pairs by asymmetric performance
  5. Downside Protection: Measure how well stratégies protect in drawdowns

Alpha Universe

Trend Following

  • EMA-Cross-Alpha: Tech stock EMA crossover (Sharpe ~1.0)
  • TrendStocks-Alpha: 5-stock trend following (Sharpe ~0.6)
  • Trend-Following: Competition EMA 50/200

Momentum

  • SectorMomentum: 9 sector ETF relative momentum
  • DualMomentumNoTLT: IEF/GLD/XLP dual momentum

Defensive/All-Weather

  • AllWeather: Permanent portfolio style allocation
  • MeanReversion: Short-term mean reversion

Factor-Based

  • FamaFrench: 5-factor model implementation

Pairs/Market Neutral

  • ETF-Pairs: Cointegration-based pairs trading
  • Options-VGT: Wheel strategy (income generation)
# QuantConnect Research Environment
from datetime import datetime, timedelta
try:
    from AlgorithmImports import *
except (ImportError, ModuleNotFoundError):
    pass
import pandas as pd
import numpy as np

# Initialize QuantBook
try:
    qb = QuantBook()
    # Set analysis period
    qb.SetStartDate(2020, 1, 1)
    qb.SetEndDate(2024, 12, 31)
    print(f"Analysis period: {qb.StartDate} to {qb.EndDate}")
except NameError:
    print("Local environment detected - yfinance will be used for data")
Local environment detected - yfinance will be used for data

Note sur l’environnement d’exécution (#8772) :

Ce notebook déclare en cell[1] la période 2020-01-01 → 2024-12-31 via qb.SetStartDate / qb.SetEndDate. Dans l’environnement qui a produit les sorties committées, QuantBook() lève NameError (QC Cloud / Lean indisponible) et la branche de repli yfinance prend le relais (mécanisme 2 de #8772).

Conséquences : - Les sorties ci-dessous proviennent du chemin local yfinance, pas d’un QuantBook. Ce repli est une source de données externe légitime (#7066), annoncée ici explicitement plutôt que silencieuse. - La cellule de chargement fige la fenêtre sur la période déclarée, avec les mêmes bornes sur les deux chemins : qb.History(symbole, début, fin) côté QuantBook, history(start=..., end=...) côté yfinance. Les sorties ne dépendent donc plus de la date d’exécution ; deux exécutions successives ne diffèrent qu’au-delà de la troisième décimale (arrondis de la source sur les prix ajustés). - Auparavant, le repli demandait les cinq dernières années (period="5y"), comptées depuis l’heure d’exécution : la fenêtre glissait d’une exécution à l’autre, et les chiffres cités en prose avec elle (#21836).

Dans un environnement Lean local dont les données equity s’arrêtent avant la fin de la période (#8734), le chemin QuantBook prolongerait la dernière barre en constante (fillDataForward=True), ce qui produit l’artefact de ligne plate de #8719. Vérifier la plage de dates imprimée par la cellule de chargement avant de lire les chiffres.

1. Define Alpha Stratégies

We’ll implement simplified versions of each alpha to generate return streams.

# Define tickers for our alpha universe
tickers = ['SPY', 'TLT', 'GLD', 'XLP', 'IEF', 'AAPL', 'MSFT', 'GOOGL', 'AMZN', 'NVDA',
           'XLK', 'XLE', 'XLF', 'XLV', 'XLY', 'XLB', 'XLRE', 'XLU']

# Register assets on QC Cloud
assets = {}
try:
    for t in tickers:
        assets[t] = qb.AddEquity(t, Resolution.DAILY).Symbol
    print(f"Added {len(assets)} assets to QuantBook")
except NameError:
    print(f"Using {len(tickers)} tickers (local environment)")
Using 18 tickers (local environment)

Pivot de la série ‘close’ en DataFrame large, avec remapping des colonnes Symbol → ticker pour Alpha-Correlation-Analysis.

# Fetch historical data per-ticker (avoids MultiIndex Symbol lookup issue)
# Fixed window = the period declared in cell[1], identical on both paths
# (end bound exclusive): the outputs no longer depend on the execution date.
WINDOW_START = datetime(2020, 1, 1)
WINDOW_END = datetime(2025, 1, 1)

data = {}
for ticker in tickers:
    loaded = False
    # Try QC Cloud per-ticker
    try:
        hist = qb.History(assets[ticker], WINDOW_START, WINDOW_END, Resolution.Daily)
        if hasattr(hist, 'shape') and len(hist) > 0:
            if isinstance(hist.index, pd.MultiIndex):
                try:
                    df_t = hist.loc[ticker]
                except KeyError:
                    df_t = hist.droplevel(0)
            else:
                df_t = hist.copy()
            if len(df_t) > 0:
                data[ticker] = df_t['close']
                loaded = True
    except (NameError, KeyError, TypeError):
        pass

    # yfinance fallback for this ticker, same fixed window
    if not loaded:
        try:
            import yfinance as yf
            raw = yf.Ticker(ticker).history(start=WINDOW_START.strftime("%Y-%m-%d"),
                                            end=WINDOW_END.strftime("%Y-%m-%d"),
                                            auto_adjust=True)
            if len(raw) > 0:
                series = raw['Close']
                series.index = series.index.tz_localize(None)
                data[ticker] = series
                loaded = True
        except Exception as e:
            print(f"  WARNING: {ticker} unavailable ({e})")

closes = pd.DataFrame(data)
# Forward-fill to align different ticker date ranges, drop entirely-empty columns
closes = closes.ffill().dropna(axis=1, how='all').dropna(how='all')

print(f"Data shape: {closes.shape}")
print(f"Tickers loaded: {len(closes.columns)}/{len(tickers)}")
print(f"Date range: {closes.index.min()} to {closes.index.max()}")
print(f"Columns: {list(closes.columns)}")
Data shape: (1258, 18)
Tickers loaded: 18/18
Date range: 2020-01-02 00:00:00 to 2024-12-31 00:00:00
Columns: ['SPY', 'TLT', 'GLD', 'XLP', 'IEF', 'AAPL', 'MSFT', 'GOOGL', 'AMZN', 'NVDA', 'XLK', 'XLE', 'XLF', 'XLV', 'XLY', 'XLB', 'XLRE', 'XLU']

2. Implement Alpha Stratégies

Define functions that generate signals/returns for each alpha type.

def calculate_ema(prices, period):
    """Calculate Exponential Moving Average."""
    return prices.ewm(span=period, adjust=False).mean()

def calculate_rsi(prices, period=14):
    """Calculate RSI."""
    delta = prices.diff()
    gain = (delta.where(delta > 0, 0)).rolling(window=period).mean()
    loss = (-delta.where(delta < 0, 0)).rolling(window=period).mean()
    rs = gain / loss
    return 100 - (100 / (1 + rs))

def calculate_returns(prices, period=1):
    """Calculate returns."""
    return prices.pct_change(period)

def calculate_volatility(prices, period=20):
    """Calculate rolling volatility."""
    returns = prices.pct_change()
    return returns.rolling(period).std() * np.sqrt(252)

print("Helper functions defined")
Helper functions defined

La moyenne mobile exponentielle est calculée pour identifier les signaux directionnels de la stratégie Alpha-Correlation-Analysis.

def ema_cross_alpha(prices, fast=20, slow=60, cooldown=3):
    """EMA Crossover Alpha (EMA-Cross-Alpha style).
    
    Long when fast EMA > slow EMA, with cooldown.
    """
    ema_fast = calculate_ema(prices, fast)
    ema_slow = calculate_ema(prices, slow)
    
    # Generate signals
    signals = pd.Series(0, index=prices.index)
    signals[ema_fast > ema_slow] = 1
    
    # Apply cooldown (no signal changes for N days after switch)
    signal_changes = signals.diff().abs()
    for i in range(1, len(signals)):
        if signal_changes.iloc[i] > 0:
            for j in range(i+1, min(i+cooldown, len(signals))):
                signals.iloc[j] = signals.iloc[i]
    
    # Calculate returns
    returns = prices.pct_change().shift(-1)  # Next day return
    strategy_returns = signals * returns
    
    return strategy_returns, signals

# Test on SPY
spy_returns, spy_signals = ema_cross_alpha(closes['SPY'])
print(f"EMA Cross SPY - Total Return: {spy_returns.sum():.2%}")
print(f"Signal distribution: {spy_signals.value_counts().to_dict()}")
EMA Cross SPY - Total Return: 54.58%
Signal distribution: {1: 961, 0: 297}

Calcul des métriques de performance (Sharpe, CAGR, drawdown) pour évaluer la qualité du backtest de la stratégie Alpha-Correlation-Analysis.

def momentum_alpha(prices, lookback=90):
    """Momentum Alpha (SectorMomentum style).
    
    Long when price > lookback-period ago price.
    """
    momentum = prices / prices.shift(lookback) - 1
    signals = (momentum > 0).astype(int)
    
    returns = prices.pct_change().shift(-1)
    strategy_returns = signals * returns
    
    return strategy_returns, signals

# Test on SPY
mom_returns, mom_signals = momentum_alpha(closes['SPY'])
print(f"Momentum SPY - Total Return: {mom_returns.sum():.2%}")
Momentum SPY - Total Return: 72.76%

Définition de dual_momentum_alpha() : fonction d’allocation ou de signal utilisée dans la comparaison des stratégies de Alpha-Correlation-Analysis.

def dual_momentum_alpha(spy_prices, tlt_prices, gld_prices, xlp_prices, lookback=126):
    """Dual Momentum Alpha (DualMomentumNoTLT style).
    
    Relative momentum: invest in best performing asset.
    """
    # Calculate momentum
    spy_mom = spy_prices / spy_prices.shift(lookback) - 1
    tlt_mom = tlt_prices / tlt_prices.shift(lookback) - 1
    gld_mom = gld_prices / gld_prices.shift(lookback) - 1
    xlp_mom = xlp_prices / xlp_prices.shift(lookback) - 1
    
    # Find best performer
    momentums = pd.DataFrame({
        'SPY': spy_mom,
        'TLT': tlt_mom,
        'GLD': gld_mom,
        'XLP': xlp_mom
    })
    
    # Defensive: if SPY momentum < 0, choose best defensive
    # fillna(0): during lookback warmup, treat momentum as neutral
    signals = pd.DataFrame(index=momentums.index)
    signals['asset'] = momentums.fillna(0).idxmax(axis=1)
    
    # Calculate returns
    spy_ret = spy_prices.pct_change().shift(-1)
    tlt_ret = tlt_prices.pct_change().shift(-1)
    gld_ret = gld_prices.pct_change().shift(-1)
    xlp_ret = xlp_prices.pct_change().shift(-1)
    
    strategy_returns = (
        (signals['asset'] == 'SPY') * spy_ret +
        (signals['asset'] == 'TLT') * tlt_ret +
        (signals['asset'] == 'GLD') * gld_ret +
        (signals['asset'] == 'XLP') * xlp_ret
    )
    
    return strategy_returns, signals

# Test
dm_returns, dm_signals = dual_momentum_alpha(
    closes['SPY'], closes['TLT'], closes['GLD'], closes['XLP']
)
print(f"Dual Momentum - Total Return: {dm_returns.sum():.2%}")
print(f"Asset allocation: {dm_signals['asset'].value_counts().to_dict()}")
Dual Momentum - Total Return: 57.11%
Asset allocation: {'SPY': 736, 'GLD': 316, 'XLP': 186, 'TLT': 20}

Calcul du Z-score du spread pour détecter les déviations statistiques et générer les signaux d’entrée/sortie dans Alpha-Correlation-Analysis.

def mean_reversion_alpha(prices, entry_z=2.0, exit_z=0.5, period=20):
    """Mean Reversion Alpha.
    
    Long when price is Z standard deviations below mean.
    """
    # Calculate Z-score
    rolling_mean = prices.rolling(period).mean()
    rolling_std = prices.rolling(period).std()
    z_score = (prices - rolling_mean) / rolling_std
    
    # Generate signals
    signals = pd.Series(0, index=prices.index)
    signals[z_score < -entry_z] = 1  # Long when oversold
    signals[z_score > exit_z] = 0    # Exit when reverted
    
    # Forward fill signals
    signals = signals.ffill().fillna(0)
    
    returns = prices.pct_change().shift(-1)
    strategy_returns = signals * returns
    
    return strategy_returns, signals, z_score

# Test on SPY
mr_returns, mr_signals, z_score = mean_reversion_alpha(closes['SPY'])
print(f"Mean Reversion SPY - Total Return: {mr_returns.sum():.2%}")
Mean Reversion SPY - Total Return: 26.30%

Définition de all_weather_allocation() : calcul des poids d’allocation statique pour le portefeuille Alpha-Correlation-Analysis.

def all_weather_allocation(spy_weight=0.3, tlt_weight=0.4, gld_weight=0.15, xlp_weight=0.15):
    """All-Weather-style static allocation.
    
    Permanent portfolio with balanced allocation.
    """
    spy_ret = closes['SPY'].pct_change().shift(-1)
    tlt_ret = closes['TLT'].pct_change().shift(-1)
    gld_ret = closes['GLD'].pct_change().shift(-1)
    xlp_ret = closes['XLP'].pct_change().shift(-1)
    
    strategy_returns = (
        spy_weight * spy_ret +
        tlt_weight * tlt_ret +
        gld_weight * gld_ret +
        xlp_weight * xlp_ret
    )
    
    return strategy_returns

aw_returns = all_weather_allocation()
print(f"All Weather - Total Return: {aw_returns.sum():.2%}")
All Weather - Total Return: 28.81%

3. Generate Return Streams for All Alphas

# Generate return streams for each alpha
alpha_returns = {}

# 1. EMA Cross on SPY
ema_returns, _ = ema_cross_alpha(closes['SPY'])
alpha_returns['EMA-Cross-SPY'] = ema_returns

# 2. EMA Cross on Tech (equal weight AAPL, MSFT, GOOGL, AMZN, NVDA)
tech_returns = (
    closes['AAPL'].pct_change() +
    closes['MSFT'].pct_change() +
    closes['GOOGL'].pct_change() +
    closes['AMZN'].pct_change() +
    closes['NVDA'].pct_change()
) / 5
ema_tech_returns, _ = ema_cross_alpha(closes['AAPL'] * closes['MSFT'] * closes['GOOGL'])  # Proxy
# Use equal-weight tech benchmark with EMA timing
tech_ema_signals = pd.Series(0, index=closes.index)
for stock in ['AAPL', 'MSFT', 'GOOGL', 'AMZN', 'NVDA']:
    stock_ema_returns, stock_signals = ema_cross_alpha(closes[stock])
    tech_ema_signals = tech_ema_signals.add(stock_signals, fill_value=0)
tech_ema_signals = (tech_ema_signals / 5).round()  # Average signal
alpha_returns['EMA-Cross-Tech'] = tech_ema_signals * tech_returns.shift(-1)

# 3. Momentum SPY
mom_returns, _ = momentum_alpha(closes['SPY'])
alpha_returns['Momentum-SPY'] = mom_returns

# 4. Dual Momentum
dm_returns, _ = dual_momentum_alpha(
    closes['SPY'], closes['TLT'], closes['GLD'], closes['XLP']
)
alpha_returns['Dual-Momentum'] = dm_returns

# 5. Mean Reversion SPY
mr_returns, _, _ = mean_reversion_alpha(closes['SPY'])
alpha_returns['Mean-Reversion'] = mr_returns

# 6. All Weather
alpha_returns['All-Weather'] = all_weather_allocation()

# 7. Trend Following (slower EMA)
trend_returns, _ = ema_cross_alpha(closes['SPY'], fast=50, slow=200)
alpha_returns['Trend-Following'] = trend_returns

# Create DataFrame
returns_df = pd.DataFrame(alpha_returns)
returns_df = returns_df.dropna()

print(f"Return streams shape: {returns_df.shape}")
print(f"\nTotal Returns:")
print(returns_df.sum().sort_values(ascending=False))
Return streams shape: (1257, 7)

Total Returns:
EMA-Cross-Tech     1.126858
Momentum-SPY       0.727611
Trend-Following    0.653459
Dual-Momentum      0.571071
EMA-Cross-SPY      0.545779
All-Weather        0.288053
Mean-Reversion     0.262988
dtype: float64

4. Correlation Matrix Analysis

# Calculate correlation matrix
corr_matrix = returns_df.corr()

# Plot heatmap
import matplotlib.pyplot as plt
import seaborn as sns

plt.figure(figsize=(12, 10))
sns.heatmap(corr_matrix, annot=True, cmap='RdYlGn', center=0,
            vmin=-1, vmax=1, fmt='.2f', linewidths=1)
plt.title('Alpha Return Correlation Matrix')
plt.tight_layout()
plt.show()

print("\nCorrelation Matrix:")
print(corr_matrix.round(3))


Correlation Matrix:
                 EMA-Cross-SPY  EMA-Cross-Tech  Momentum-SPY  Dual-Momentum  \
EMA-Cross-SPY            1.000           0.689         0.808          0.450   
EMA-Cross-Tech           0.689           1.000         0.618          0.434   
Momentum-SPY             0.808           0.618         1.000          0.367   
Dual-Momentum            0.450           0.434         0.367          1.000   
Mean-Reversion           0.128           0.152         0.082          0.283   
All-Weather              0.428           0.368         0.400          0.626   
Trend-Following          0.761           0.620         0.810          0.398   

                 Mean-Reversion  All-Weather  Trend-Following  
EMA-Cross-SPY             0.128        0.428            0.761  
EMA-Cross-Tech            0.152        0.368            0.620  
Momentum-SPY              0.082        0.400            0.810  
Dual-Momentum             0.283        0.626            0.398  
Mean-Reversion            1.000        0.152            0.194  
All-Weather               0.152        1.000            0.441  
Trend-Following           0.194        0.441            1.000  

Calcul de la matrice de corrélation entre les instruments pour l’analyse de diversification de la stratégie Alpha-Correlation-Analysis.

# Find least correlated pairs
pairs = []
for i, alpha1 in enumerate(returns_df.columns):
    for alpha2 in returns_df.columns[i+1:]:
        corr = corr_matrix.loc[alpha1, alpha2]
        pairs.append({
            'Pair': f"{alpha1} / {alpha2}",
            'Correlation': corr,
            'Abs_Corr': abs(corr)
        })

pairs_df = pd.DataFrame(pairs).sort_values('Abs_Corr')

print("Top 10 Least Correlated Pairs:")
print(pairs_df.head(10)[['Pair', 'Correlation']])
Top 10 Least Correlated Pairs:
                                Pair  Correlation
12     Momentum-SPY / Mean-Reversion     0.082199
3     EMA-Cross-SPY / Mean-Reversion     0.127844
18      Mean-Reversion / All-Weather     0.151632
8    EMA-Cross-Tech / Mean-Reversion     0.151862
19  Mean-Reversion / Trend-Following     0.193928
15    Dual-Momentum / Mean-Reversion     0.282696
11      Momentum-SPY / Dual-Momentum     0.366951
9       EMA-Cross-Tech / All-Weather     0.368242
17   Dual-Momentum / Trend-Following     0.397642
13        Momentum-SPY / All-Weather     0.400145

5. Regime Analysis

Classify market conditions and analyze alpha performance in each regime.

# Define market regimes
spy_returns = closes['SPY'].pct_change()
spy_volatility = spy_returns.rolling(20).std() * np.sqrt(252)
spy_trend = spy_returns.rolling(60).sum()

# Regime classifications
regimes = pd.DataFrame(index=returns_df.index)
regimes['vol_regime'] = pd.cut(
    spy_volatility,
    bins=[0, spy_volatility.quantile(0.33), spy_volatility.quantile(0.67), float('inf')],
    labels=['Low-Vol', 'Med-Vol', 'High-Vol']
)
regimes['trend_regime'] = pd.cut(
    spy_trend,
    bins=[-float('inf'), -0.05, 0.05, float('inf')],
    labels=['Bear', 'Sideways', 'Bull']
)

# Combine regimes
regimes['regime'] = regimes['trend_regime'].astype(str) + '-' + regimes['vol_regime'].astype(str)

print("Regime Distribution:")
print(regimes['regime'].value_counts())
Regime Distribution:
regime
Bull-Low-Vol         315
Bull-Med-Vol         201
Sideways-Med-Vol     189
Sideways-High-Vol    132
Bear-High-Vol        128
Bull-High-Vol        125
Sideways-Low-Vol      94
Bear-Med-Vol          13
Name: count, dtype: int64

Classification des régimes de marché (tendanciel / retour à la moyenne) pour adapter les paramètres de Alpha-Correlation-Analysis.

# Analyze alpha performance by regime
regime_performance = {}

for alpha in returns_df.columns:
    alpha_perf = []
    for regime in regimes['regime'].unique():
        mask = regimes['regime'] == regime
        if mask.sum() > 10:  # Need sufficient data
            avg_return = returns_df.loc[mask, alpha].mean() * 252  # Annualized
            sharpe = returns_df.loc[mask, alpha].mean() / returns_df.loc[mask, alpha].std() * np.sqrt(252) if returns_df.loc[mask, alpha].std() > 0 else 0
            alpha_perf.append({
                'Alpha': alpha,
                'Regime': regime,
                'Ann_Return': avg_return,
                'Sharpe': sharpe,
                'Days': mask.sum()
            })
    regime_performance[alpha] = pd.DataFrame(alpha_perf)

# Combine all
regime_df = pd.concat(regime_performance.values())
regime_pivot = regime_df.pivot(index='Alpha', columns='Regime', values='Sharpe')

plt.figure(figsize=(14, 8))
sns.heatmap(regime_pivot, annot=True, cmap='RdYlGn', center=0,
            fmt='.2f', linewidths=1)
plt.title('Sharpe Ratio by Market Regime')
plt.tight_layout()
plt.show()

print("\nSharpe by Regime:")
print(regime_pivot.round(2))


Sharpe by Regime:
Regime           Bear-High-Vol  Bear-Med-Vol  Bull-High-Vol  Bull-Low-Vol  \
Alpha                                                                       
All-Weather               2.05          4.79           2.59         -0.45   
Dual-Momentum             0.52          0.32           1.61          0.20   
EMA-Cross-SPY             0.00          0.00           1.95          0.76   
EMA-Cross-Tech            1.29         -4.91           1.96          1.49   
Mean-Reversion            2.74          2.49           0.00         -0.35   
Momentum-SPY              0.52         -4.40           1.90          0.76   
Trend-Following           2.22          2.10           1.75          0.76   

Regime           Bull-Med-Vol  Sideways-High-Vol  Sideways-Low-Vol  \
Alpha                                                                
All-Weather              0.94              -1.59              0.19   
Dual-Momentum            2.25               0.75              0.41   
EMA-Cross-SPY            1.10               2.57              1.27   
EMA-Cross-Tech           0.21              -0.52              1.96   
Mean-Reversion           0.92               0.38              0.17   
Momentum-SPY             1.42               2.14              1.02   
Trend-Following          1.46               0.38              1.18   

Regime           Sideways-Med-Vol  
Alpha                              
All-Weather                  0.61  
Dual-Momentum                1.47  
EMA-Cross-SPY                0.13  
EMA-Cross-Tech               1.90  
Mean-Reversion              -0.12  
Momentum-SPY                 1.56  
Trend-Following              1.07  

6. Complementarity Score

Calculate how well pairs complement each other: - Low correlation: Uncorrelated return streams - Regime diversification: One excels where the other struggles - Drawdown protection: One protects when the other draws down

def calculate_complementarity_score(alpha1_returns, alpha2_returns, regimes):
    """Calculate complementarity score for a pair of alphas.
    
    Score components:
    1. Correlation: Lower is better (0-1)
    2. Regime diversification: One excels where other struggles
    3. Downside protection: One protects when other draws down
    """
    # Remove NaN
    common_idx = alpha1_returns.dropna().index.intersection(alpha2_returns.dropna().index)
    r1 = alpha1_returns.loc[common_idx]
    r2 = alpha2_returns.loc[common_idx]
    regimes_common = regimes.loc[common_idx]
    
    # 1. Correlation score (lower correlation = higher score)
    corr = r1.corr(r2)
    corr_score = (1 - abs(corr))  # 0-1 scale
    
    # 2. Regime diversification
    regime_scores = []
    for regime in regimes_common['regime'].unique():
        mask = regimes_common['regime'] == regime
        if mask.sum() > 10:
            sharpe1 = r1.loc[mask].mean() / r1.loc[mask].std() * np.sqrt(252) if r1.loc[mask].std() > 0 else 0
            sharpe2 = r2.loc[mask].mean() / r2.loc[mask].std() * np.sqrt(252) if r2.loc[mask].std() > 0 else 0
            # Diversification benefit: one positive when other negative
            if (sharpe1 > 0 and sharpe2 < 0) or (sharpe1 < 0 and sharpe2 > 0):
                regime_scores.append(abs(sharpe1 - sharpe2) / 2)
            elif sharpe1 > 0 and sharpe2 > 0:
                regime_scores.append(max(sharpe1, sharpe2) - min(sharpe1, sharpe2))
    
    regime_div_score = np.mean(regime_scores) if regime_scores else 0
    
    # 3. Downside protection
    # When alpha1 is in drawdown, does alpha2 protect?
    cumret1 = (1 + r1).cumprod()
    cumret2 = (1 + r2).cumprod()
    
    # Find drawdown periods for alpha1
    drawdown_mask = (cumret1 < cumret1.cummax()) & (cumret1 < cumret1.iloc[0])
    
    if drawdown_mask.sum() > 10:
        # Alpha2's performance during alpha1's drawdowns
        dd_return2 = r2.loc[drawdown_mask].mean() * 252
        protection_score = max(0, dd_return2)  # Positive returns during drawdown = good
    else:
        protection_score = 0
    
    # Combined score (weighted)
    total_score = 0.4 * corr_score + 0.4 * min(regime_div_score, 1) + 0.2 * min(protection_score, 1)
    
    return {
        'correlation': corr,
        'corr_score': corr_score,
        'regime_diversification': regime_div_score,
        'downside_protection': protection_score,
        'total_score': total_score
    }

# Calculate scores for all pairs
complementarity = []
for i, alpha1 in enumerate(returns_df.columns):
    for alpha2 in returns_df.columns[i+1:]:
        scores = calculate_complementarity_score(
            returns_df[alpha1],
            returns_df[alpha2],
            regimes
        )
        complementarity.append({
            'Pair': f"{alpha1} / {alpha2}",
            **scores
        })

comp_df = pd.DataFrame(complementarity).sort_values('total_score', ascending=False)

print("Top 10 Most Complementary Pairs:")
print(comp_df.head(10)[['Pair', 'correlation', 'regime_diversification', 'downside_protection', 'total_score']].round(3))
Top 10 Most Complementary Pairs:
                               Pair  correlation  regime_diversification  \
8   EMA-Cross-Tech / Mean-Reversion        0.152                   1.433   
3    EMA-Cross-SPY / Mean-Reversion        0.128                   0.828   
12    Momentum-SPY / Mean-Reversion        0.082                   1.452   
15   Dual-Momentum / Mean-Reversion        0.283                   1.056   
9      EMA-Cross-Tech / All-Weather        0.368                   1.572   
13       Momentum-SPY / All-Weather        0.400                   1.446   
18     Mean-Reversion / All-Weather        0.152                   0.727   
7    EMA-Cross-Tech / Dual-Momentum        0.434                   1.210   
2     EMA-Cross-SPY / Dual-Momentum        0.450                   1.013   
4       EMA-Cross-SPY / All-Weather        0.428                   0.843   

    downside_protection  total_score  
8                 0.702        0.880  
3                 0.436        0.767  
12                0.000        0.767  
15                0.341        0.755  
9                 0.064        0.666  
13                0.000        0.640  
18                0.000        0.630  
7                 0.000        0.626  
2                 0.000        0.620  
4                 0.183        0.603  

7. Top Pairs Analysis

Deep dive into the most promising complementary pairs.

# Select top 3 pairs for detailed analysis
top_pairs = comp_df.head(3)['Pair'].tolist()

for pair_str in top_pairs:
    print(f"\n{'='*60}")
    print(f"Analysis: {pair_str}")
    print(f"{'='*60}")
    
    alpha1, alpha2 = [a.strip() for a in pair_str.split('/')]
    
    # Individual metrics
    print(f"\n{alpha1}:")
    print(f"  Annual Return: {returns_df[alpha1].mean() * 252:.2%}")
    print(f"  Volatility: {returns_df[alpha1].std() * np.sqrt(252):.2%}")
    print(f"  Sharpe: {returns_df[alpha1].mean() / returns_df[alpha1].std() * np.sqrt(252):.2f}")
    
    print(f"\n{alpha2}:")
    print(f"  Annual Return: {returns_df[alpha2].mean() * 252:.2%}")
    print(f"  Volatility: {returns_df[alpha2].std() * np.sqrt(252):.2%}")
    print(f"  Sharpe: {returns_df[alpha2].mean() / returns_df[alpha2].std() * np.sqrt(252):.2f}")
    
    # Combined 50/50
    combined = (returns_df[alpha1] + returns_df[alpha2]) / 2
    print(f"\nCombined (50/50):")
    print(f"  Annual Return: {combined.mean() * 252:.2%}")
    print(f"  Volatility: {combined.std() * np.sqrt(252):.2%}")
    print(f"  Sharpe: {combined.mean() / combined.std() * np.sqrt(252):.2f}")
    
    # Correlation
    corr = returns_df[alpha1].corr(returns_df[alpha2])
    print(f"  Correlation: {corr:.3f}")

============================================================
Analysis: EMA-Cross-Tech / Mean-Reversion
============================================================

EMA-Cross-Tech:
  Annual Return: 22.59%
  Volatility: 22.26%
  Sharpe: 1.01

Mean-Reversion:
  Annual Return: 5.27%
  Volatility: 6.62%
  Sharpe: 0.80

Combined (50/50):
  Annual Return: 13.93%
  Volatility: 12.09%
  Sharpe: 1.15
  Correlation: 0.152

============================================================
Analysis: EMA-Cross-SPY / Mean-Reversion
============================================================

EMA-Cross-SPY:
  Annual Return: 10.94%
  Volatility: 12.80%
  Sharpe: 0.85

Mean-Reversion:
  Annual Return: 5.27%
  Volatility: 6.62%
  Sharpe: 0.80

Combined (50/50):
  Annual Return: 8.11%
  Volatility: 7.57%
  Sharpe: 1.07
  Correlation: 0.128

============================================================
Analysis: Momentum-SPY / Mean-Reversion
============================================================

Momentum-SPY:
  Annual Return: 14.59%
  Volatility: 11.68%
  Sharpe: 1.25

Mean-Reversion:
  Annual Return: 5.27%
  Volatility: 6.62%
  Sharpe: 0.80

Combined (50/50):
  Annual Return: 9.93%
  Volatility: 6.95%
  Sharpe: 1.43
  Correlation: 0.082

8. Recommendations

Based on the analysis, here are the most promising complementary alpha combinations for composite stratégies.

# Generate recommendations
recommendations = []

for _, row in comp_df.head(10).iterrows():
    alpha1, alpha2 = [a.strip() for a in row['Pair'].split('/')]
    
    # Calculate expected combined Sharpe
    sharpe1 = returns_df[alpha1].mean() / returns_df[alpha1].std() * np.sqrt(252)
    sharpe2 = returns_df[alpha2].mean() / returns_df[alpha2].std() * np.sqrt(252)
    combined = (returns_df[alpha1] + returns_df[alpha2]) / 2
    combined_sharpe = combined.mean() / combined.std() * np.sqrt(252)
    
    recommendations.append({
        'Pair': row['Pair'],
        'Correlation': row['correlation'],
        'Sharpe_1': sharpe1,
        'Sharpe_2': sharpe2,
        'Combined_Sharpe': combined_sharpe,
        'Synergy': combined_sharpe - (sharpe1 + sharpe2) / 2,
        'Regime_Div': row['regime_diversification'],
        'DD_Protection': row['downside_protection']
    })

rec_df = pd.DataFrame(recommendations)

print("\nTop 10 Complementary Alpha Pairs for Composites:")
print(rec_df.round(3))

# Filter for synergistic pairs (combined Sharpe > average)
synergistic = rec_df[rec_df['Synergy'] > 0]
print(f"\nSynergistic Pairs ({len(synergistic)}):")
print(synergistic[['Pair', 'Combined_Sharpe', 'Synergy']].round(3))

Top 10 Complementary Alpha Pairs for Composites:
                              Pair  Correlation  Sharpe_1  Sharpe_2  \
0  EMA-Cross-Tech / Mean-Reversion        0.152     1.015     0.796   
1   EMA-Cross-SPY / Mean-Reversion        0.128     0.855     0.796   
2    Momentum-SPY / Mean-Reversion        0.082     1.249     0.796   
3   Dual-Momentum / Mean-Reversion        0.283     0.574     0.796   
4     EMA-Cross-Tech / All-Weather        0.368     1.015     0.517   
5       Momentum-SPY / All-Weather        0.400     1.249     0.517   
6     Mean-Reversion / All-Weather        0.152     0.796     0.517   
7   EMA-Cross-Tech / Dual-Momentum        0.434     1.015     0.574   
8    EMA-Cross-SPY / Dual-Momentum        0.450     0.855     0.574   
9      EMA-Cross-SPY / All-Weather        0.428     0.855     0.517   

   Combined_Sharpe  Synergy  Regime_Div  DD_Protection  
0            1.153    0.247       1.433          0.702  
1            1.071    0.245       0.828          0.436  
2            1.429    0.407       1.452          0.000  
3            0.735    0.050       1.056          0.341  
4            1.001    0.235       1.572          0.064  
5            1.065    0.182       1.446          0.000  
6            0.799    0.143       0.727          0.000  
7            0.951    0.157       1.210          0.000  
8            0.795    0.081       1.013          0.000  
9            0.825    0.139       0.843          0.183  

Synergistic Pairs (10):
                              Pair  Combined_Sharpe  Synergy
0  EMA-Cross-Tech / Mean-Reversion            1.153    0.247
1   EMA-Cross-SPY / Mean-Reversion            1.071    0.245
2    Momentum-SPY / Mean-Reversion            1.429    0.407
3   Dual-Momentum / Mean-Reversion            0.735    0.050
4     EMA-Cross-Tech / All-Weather            1.001    0.235
5       Momentum-SPY / All-Weather            1.065    0.182
6     Mean-Reversion / All-Weather            0.799    0.143
7   EMA-Cross-Tech / Dual-Momentum            0.951    0.157
8    EMA-Cross-SPY / Dual-Momentum            0.795    0.081
9      EMA-Cross-SPY / All-Weather            0.825    0.139

9. Walk-Forward Validation Framework

Template for validating pairs with out-of-sample testing.

def walk_forward_validation(returns_df, alpha1, alpha2, 
                           train_periods=252, test_periods=63):
    """Walk-forward validation for a pair.
    
    Args:
        returns_df: DataFrame with all returns
        alpha1, alpha2: Names of alphas to test
        train_periods: Training period in days
        test_periods: Test period in days
    """
    results = []
    
    for start in range(0, len(returns_df) - train_periods - test_periods, test_periods):
        train_end = start + train_periods
        test_end = train_end + test_periods
        
        if test_end > len(returns_df):
            break
        
        # Training period metrics
        train_corr = returns_df.iloc[start:train_end][alpha1].corr(
            returns_df.iloc[start:train_end][alpha2]
        )
        
        # Test period combined performance
        test_combined = (
            returns_df.iloc[train_end:test_end][alpha1] +
            returns_df.iloc[train_end:test_end][alpha2]
        ) / 2
        
        results.append({
            'Period': f"{returns_df.index[start].date()} to {returns_df.index[test_end-1].date()}",
            'Train_Corr': train_corr,
            'Test_Return': test_combined.mean() * 252,
            'Test_Sharpe': test_combined.mean() / test_combined.std() * np.sqrt(252) if test_combined.std() > 0 else 0
        })
    
    return pd.DataFrame(results)

# Example: Test top pair
top_pair = comp_df.iloc[0]['Pair']
alpha1, alpha2 = [a.strip() for a in top_pair.split('/')]

wf_results = walk_forward_validation(returns_df, alpha1, alpha2)
print(f"\nWalk-Forward Validation: {top_pair}")
print(wf_results)

print(f"\nAverage Test Sharpe: {wf_results['Test_Sharpe'].mean():.2f}")

Walk-Forward Validation: EMA-Cross-Tech / Mean-Reversion
                      Period  Train_Corr  Test_Return  Test_Sharpe
0   2020-01-02 to 2021-04-01    0.196052    -0.016206    -0.133264
1   2020-04-02 to 2021-07-01    0.070892     0.316264     3.309655
2   2020-07-02 to 2021-09-30    0.081561     0.093083     1.035006
3   2020-10-01 to 2021-12-30    0.129635     0.354952     2.752040
4   2020-12-31 to 2022-03-31    0.120971    -0.194254    -2.840700
5   2021-04-05 to 2022-07-01    0.079755    -0.081086    -1.138385
6   2021-07-02 to 2022-09-30    0.058772    -0.308550    -3.301252
7   2021-10-01 to 2022-12-30    0.033750    -0.014223    -1.651654
8   2021-12-31 to 2023-04-03    0.011628     0.246734     2.004357
9   2022-04-01 to 2023-07-05    0.009998     0.470357     3.948303
10  2022-07-05 to 2023-10-03    0.021766    -0.023141    -0.214532
11  2022-10-03 to 2024-01-03    0.031235    -0.115057    -1.345589
12  2023-01-03 to 2024-04-04    0.017079     0.441859     4.148449
13  2023-04-04 to 2024-07-05    0.022092     0.430853     3.962713
14  2023-07-06 to 2024-10-03    0.140989    -0.280142    -2.305780

Average Test Sharpe: 0.55

10. Next Steps

For Production Implementation:

  1. Build Composite Algorithms: Implement top pairs as QCAlphaModel composites
  2. Dynamic Weighting: Add regime-based allocation between alphas
  3. Risk Management: Add position sizing based on correlation regime
  4. Validation: Run full backtests on recommended pairs

Top Candidates for Composites:

Based on this analysis, prioritize: 1. Trend + Defensive: EMA-Cross + All-Weather 2. Momentum + Mean Reversion: Dual-Momentum + Mean-Reversion
3. Multi-Asset: Dual-Momentum + All-Weather 4. Regime Switching: Trend-Following + Mean-Reversion

Research Extensions:

  • Add sector-specific alphas (SectorMomentum)
  • Include options-based stratégies (Options-VGT)
  • Test with crypto assets (BTC, ETH)
  • Factor analysis: loadings to Size, Value, Momentum factors
Retour au sommet