Analyzes multi-contract correlations and generates hedge recommendations. Use when managing a portfolio of correlated assets and needing to mitigate risk. Trigger with "analyze correlations",...
Identifies correlations between multiple contracts and generates hedging strategies for portfolio risk management.
Analyzes relationships between assets in a portfolio to suggest hedging strategies. Takes CSV data with multiple time series, calculates correlation matrix, identifies significant relationships, and outputs hedge recommendations with visualizations. Generates correlation heatmap, rolling correlation plots, and hedge effectiveness charts.
Tools: Read, Write, Bash, Glob, Grep
Environment: None required (optional: NIXTLA_TIMEGPT_API_KEY for forecasted correlations)
Packages:
pip install pandas numpy scipy matplotlib seaborn
Input Format: CSV with columns: unique_id (contract identifier), ds (date), y (price/value)
Load multi-series contract data and calculate returns. Uses {baseDir}/scripts/prepare_data.py.
python scripts/prepare_data.py contracts.csv --method log --output-dir results/
Output: prices_wide.csv, returns.csv
Calculate correlation matrix and identify significant pairs. Uses {baseDir}/scripts/correlation_analysis.py.
python scripts/correlation_analysis.py \
--returns results/returns.csv \
--method pearson \
--threshold 0.5 \
--rolling-window 30 \
--output-dir results/
Output: correlation_matrix.csv, correlation_pvalues.csv, high_correlations.json, rolling_correlations.csv
Calculate optimal hedge ratios using regression or minimum variance methods. Uses {baseDir}/scripts/hedge_recommendations.py.
python scripts/hedge_recommendations.py \
--returns results/returns.csv \
--correlation results/correlation_matrix.csv \
--method ols \
--top-n 10 \
--portfolio-value 100000 \
--output-dir results/
Output: hedge_recommendations.csv, hedge_recommendations.json, hedged_portfolio.csv
Generate correlation heatmap, rolling correlation plot, and hedge effectiveness chart. Uses {baseDir}/scripts/visualize.py.
python scripts/visualize.py \
--correlation results/correlation_matrix.csv \
--rolling results/rolling_correlations.csv \
--recommendations results/hedge_recommendations.json \
--output-dir results/ \
--top-n 5
Output: correlation_heatmap.png, rolling_correlation.png, hedge_effectiveness.png
Create comprehensive markdown report with all analysis results. Uses {baseDir}/scripts/generate_report.py.
python scripts/generate_report.py \
--correlation results/correlation_matrix.csv \
--high-correlations results/high_correlations.json \
--recommendations results/hedge_recommendations.json \
--output results/correlation_report.md
Output: correlation_report.md
Error: Input file not found
ls -laError: Missing required columns
unique_id, ds, y columnsError: Insufficient data points
Error: Invalid data format
y values are numeric (not strings)Error: Insufficient contracts
unique_id column has multiple distinct valuesInput (portfolio.csv):
unique_id,ds,y
BTC,2024-01-01,42000
ETH,2024-01-01,2200
BTC,2024-01-02,42500
ETH,2024-01-02,2250
Workflow:
python scripts/prepare_data.py portfolio.csv
python scripts/correlation_analysis.py
python scripts/hedge_recommendations.py
python scripts/visualize.py
python scripts/generate_report.py
Result: Correlation 0.85 between BTC-ETH, hedge ratio -0.95, variance reduction 72%
Input: 5 election-related prediction market contracts
Command:
python scripts/prepare_data.py elections.csv --output-dir election_analysis/
python scripts/correlation_analysis.py --threshold 0.7 --output-dir election_analysis/
python scripts/hedge_recommendations.py --top-n 5 --output-dir election_analysis/
python scripts/visualize.py --output-dir election_analysis/
python scripts/generate_report.py --output election_analysis/report.md
Result: Identified 3 pairs with correlation > 0.7, top hedge reduces variance by 62%
Scripts: All analysis scripts located in {baseDir}/scripts/
prepare_data.py: Data loading, pivoting, returns calculationcorrelation_analysis.py: Correlation matrix, p-values, rolling correlationshedge_recommendations.py: Hedge ratios, portfolio allocationvisualize.py: Heatmaps, rolling plots, effectiveness chartsgenerate_report.py: Comprehensive markdown reportCorrelation Methods: Pearson (linear), Spearman (rank-based), Kendall (concordance)
Hedge Methods: OLS regression (standard), Minimum variance (risk-minimizing)
Interpretation: