|
|
|
""" |
|
Comprehensive Economic Analytics Demo |
|
Demonstrates advanced analytics capabilities including forecasting, segmentation, and statistical modeling |
|
""" |
|
|
|
import logging |
|
import os |
|
import sys |
|
from datetime import datetime |
|
from pathlib import Path |
|
|
|
|
|
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'src')) |
|
|
|
from src.analysis.comprehensive_analytics import ComprehensiveAnalytics |
|
from src.core.enhanced_fred_client import EnhancedFREDClient |
|
from config.settings import FRED_API_KEY |
|
|
|
def setup_logging(): |
|
"""Setup logging for demo""" |
|
logging.basicConfig( |
|
level=logging.INFO, |
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' |
|
) |
|
|
|
def run_basic_demo(): |
|
"""Run basic demo with key economic indicators""" |
|
print("=" * 80) |
|
print("ECONOMIC ANALYTICS DEMO - BASIC ANALYSIS") |
|
print("=" * 80) |
|
|
|
|
|
client = EnhancedFREDClient(FRED_API_KEY) |
|
|
|
|
|
indicators = ['GDPC1', 'INDPRO', 'RSAFS'] |
|
print(f"\n๐ Fetching data for indicators: {indicators}") |
|
|
|
try: |
|
data = client.fetch_economic_data( |
|
indicators=indicators, |
|
start_date='2010-01-01', |
|
end_date='2024-01-01' |
|
) |
|
|
|
print(f"โ
Successfully fetched {len(data)} observations") |
|
print(f"๐
Date range: {data.index.min().strftime('%Y-%m')} to {data.index.max().strftime('%Y-%m')}") |
|
|
|
|
|
quality_report = client.validate_data_quality(data) |
|
print(f"\n๐ Data Quality Summary:") |
|
for series, metrics in quality_report['missing_data'].items(): |
|
print(f" โข {series}: {metrics['completeness']:.1f}% complete") |
|
|
|
return data |
|
|
|
except Exception as e: |
|
print(f"โ Error fetching data: {e}") |
|
return None |
|
|
|
def run_forecasting_demo(data): |
|
"""Run forecasting demo""" |
|
print("\n" + "=" * 80) |
|
print("FORECASTING DEMO") |
|
print("=" * 80) |
|
|
|
from src.analysis.economic_forecasting import EconomicForecaster |
|
|
|
forecaster = EconomicForecaster(data) |
|
|
|
|
|
indicators = ['GDPC1', 'INDPRO', 'RSAFS'] |
|
available_indicators = [ind for ind in indicators if ind in data.columns] |
|
|
|
print(f"๐ฎ Forecasting indicators: {available_indicators}") |
|
|
|
for indicator in available_indicators: |
|
try: |
|
|
|
series = forecaster.prepare_data(indicator) |
|
|
|
|
|
stationarity = forecaster.check_stationarity(series) |
|
print(f"\n๐ {indicator} Stationarity Test:") |
|
print(f" โข ADF Statistic: {stationarity['adf_statistic']:.4f}") |
|
print(f" โข P-value: {stationarity['p_value']:.4f}") |
|
print(f" โข Is Stationary: {stationarity['is_stationary']}") |
|
|
|
|
|
forecast_result = forecaster.forecast_series(series, forecast_periods=4) |
|
print(f"๐ฎ {indicator} Forecast:") |
|
print(f" โข Model: {forecast_result['model_type'].upper()}") |
|
if forecast_result['aic']: |
|
print(f" โข AIC: {forecast_result['aic']:.4f}") |
|
|
|
|
|
backtest_result = forecaster.backtest_forecast(series) |
|
if 'error' not in backtest_result: |
|
print(f" โข Backtest MAPE: {backtest_result['mape']:.2f}%") |
|
print(f" โข Backtest RMSE: {backtest_result['rmse']:.4f}") |
|
|
|
except Exception as e: |
|
print(f"โ Error forecasting {indicator}: {e}") |
|
|
|
def run_segmentation_demo(data): |
|
"""Run segmentation demo""" |
|
print("\n" + "=" * 80) |
|
print("SEGMENTATION DEMO") |
|
print("=" * 80) |
|
|
|
from src.analysis.economic_segmentation import EconomicSegmentation |
|
|
|
segmentation = EconomicSegmentation(data) |
|
|
|
|
|
print("๐ฏ Clustering time periods...") |
|
try: |
|
time_clusters = segmentation.cluster_time_periods( |
|
indicators=['GDPC1', 'INDPRO', 'RSAFS'], |
|
method='kmeans' |
|
) |
|
|
|
if 'error' not in time_clusters: |
|
n_clusters = time_clusters['n_clusters'] |
|
print(f"โ
Time periods clustered into {n_clusters} economic regimes") |
|
|
|
|
|
cluster_analysis = time_clusters['cluster_analysis'] |
|
for cluster_id, analysis in cluster_analysis.items(): |
|
print(f" โข Cluster {cluster_id}: {analysis['size']} periods ({analysis['percentage']:.1f}%)") |
|
|
|
except Exception as e: |
|
print(f"โ Error in time period clustering: {e}") |
|
|
|
|
|
print("\n๐ฏ Clustering economic series...") |
|
try: |
|
series_clusters = segmentation.cluster_economic_series( |
|
indicators=['GDPC1', 'INDPRO', 'RSAFS', 'CPIAUCSL', 'FEDFUNDS', 'DGS10'], |
|
method='kmeans' |
|
) |
|
|
|
if 'error' not in series_clusters: |
|
n_clusters = series_clusters['n_clusters'] |
|
print(f"โ
Economic series clustered into {n_clusters} groups") |
|
|
|
|
|
cluster_analysis = series_clusters['cluster_analysis'] |
|
for cluster_id, analysis in cluster_analysis.items(): |
|
print(f" โข Cluster {cluster_id}: {analysis['size']} series ({analysis['percentage']:.1f}%)") |
|
|
|
except Exception as e: |
|
print(f"โ Error in series clustering: {e}") |
|
|
|
def run_statistical_demo(data): |
|
"""Run statistical modeling demo""" |
|
print("\n" + "=" * 80) |
|
print("STATISTICAL MODELING DEMO") |
|
print("=" * 80) |
|
|
|
from src.analysis.statistical_modeling import StatisticalModeling |
|
|
|
modeling = StatisticalModeling(data) |
|
|
|
|
|
print("๐ Performing correlation analysis...") |
|
try: |
|
corr_results = modeling.analyze_correlations() |
|
significant_correlations = corr_results['significant_correlations'] |
|
print(f"โ
Found {len(significant_correlations)} significant correlations") |
|
|
|
|
|
print("\n๐ Top 3 Strongest Correlations:") |
|
for i, corr in enumerate(significant_correlations[:3]): |
|
print(f" โข {corr['variable1']} โ {corr['variable2']}: {corr['correlation']:.3f} ({corr['strength']})") |
|
|
|
except Exception as e: |
|
print(f"โ Error in correlation analysis: {e}") |
|
|
|
|
|
print("\n๐ Performing regression analysis...") |
|
key_indicators = ['GDPC1', 'INDPRO', 'RSAFS'] |
|
|
|
for target in key_indicators: |
|
if target in data.columns: |
|
try: |
|
regression_result = modeling.fit_regression_model( |
|
target=target, |
|
lag_periods=4 |
|
) |
|
|
|
performance = regression_result['performance'] |
|
print(f"โ
{target} Regression Model:") |
|
print(f" โข Rยฒ: {performance['r2']:.4f}") |
|
print(f" โข RMSE: {performance['rmse']:.4f}") |
|
print(f" โข MAE: {performance['mae']:.4f}") |
|
|
|
|
|
coefficients = regression_result['coefficients'] |
|
print(f" โข Top 3 Variables:") |
|
for i, row in coefficients.head(3).iterrows(): |
|
print(f" - {row['variable']}: {row['coefficient']:.4f}") |
|
|
|
except Exception as e: |
|
print(f"โ Error in regression for {target}: {e}") |
|
|
|
def run_comprehensive_demo(): |
|
"""Run comprehensive analytics demo""" |
|
print("=" * 80) |
|
print("COMPREHENSIVE ECONOMIC ANALYTICS DEMO") |
|
print("=" * 80) |
|
|
|
|
|
analytics = ComprehensiveAnalytics(FRED_API_KEY, output_dir="data/exports/demo") |
|
|
|
|
|
print("\n๐ Running comprehensive analysis...") |
|
try: |
|
results = analytics.run_complete_analysis( |
|
indicators=['GDPC1', 'INDPRO', 'RSAFS', 'CPIAUCSL', 'FEDFUNDS', 'DGS10'], |
|
start_date='2010-01-01', |
|
end_date='2024-01-01', |
|
forecast_periods=4, |
|
include_visualizations=True |
|
) |
|
|
|
print("โ
Comprehensive analysis completed successfully!") |
|
|
|
|
|
if 'insights' in results: |
|
insights = results['insights'] |
|
print("\n๐ฏ KEY INSIGHTS:") |
|
for finding in insights.get('key_findings', []): |
|
print(f" โข {finding}") |
|
|
|
|
|
if 'forecasting' in results: |
|
print("\n๐ฎ FORECASTING RESULTS:") |
|
forecasting_results = results['forecasting'] |
|
for indicator, result in forecasting_results.items(): |
|
if 'error' not in result: |
|
backtest = result.get('backtest', {}) |
|
if 'error' not in backtest: |
|
mape = backtest.get('mape', 0) |
|
print(f" โข {indicator}: MAPE = {mape:.2f}%") |
|
|
|
|
|
if 'segmentation' in results: |
|
print("\n๐ฏ SEGMENTATION RESULTS:") |
|
segmentation_results = results['segmentation'] |
|
|
|
if 'time_period_clusters' in segmentation_results: |
|
time_clusters = segmentation_results['time_period_clusters'] |
|
if 'error' not in time_clusters: |
|
n_clusters = time_clusters.get('n_clusters', 0) |
|
print(f" โข Time periods clustered into {n_clusters} economic regimes") |
|
|
|
if 'series_clusters' in segmentation_results: |
|
series_clusters = segmentation_results['series_clusters'] |
|
if 'error' not in series_clusters: |
|
n_clusters = series_clusters.get('n_clusters', 0) |
|
print(f" โข Economic series clustered into {n_clusters} groups") |
|
|
|
print(f"\n๐ Results saved to: data/exports/demo") |
|
|
|
except Exception as e: |
|
print(f"โ Error in comprehensive analysis: {e}") |
|
|
|
def main(): |
|
"""Main demo function""" |
|
setup_logging() |
|
|
|
print("๐ฏ ECONOMIC ANALYTICS DEMO") |
|
print("This demo showcases advanced analytics capabilities including:") |
|
print(" โข Economic data collection and quality assessment") |
|
print(" โข Time series forecasting with ARIMA/ETS models") |
|
print(" โข Economic segmentation (time periods and series)") |
|
print(" โข Statistical modeling and correlation analysis") |
|
print(" โข Comprehensive insights extraction") |
|
|
|
|
|
if not FRED_API_KEY: |
|
print("\nโ FRED API key not found. Please set FRED_API_KEY environment variable.") |
|
return |
|
|
|
|
|
data = run_basic_demo() |
|
if data is None: |
|
return |
|
|
|
|
|
run_forecasting_demo(data) |
|
run_segmentation_demo(data) |
|
run_statistical_demo(data) |
|
|
|
|
|
run_comprehensive_demo() |
|
|
|
print("\n" + "=" * 80) |
|
print("DEMO COMPLETED!") |
|
print("=" * 80) |
|
print("Generated outputs:") |
|
print(" ๐ data/exports/demo/ - Comprehensive analysis results") |
|
print(" ๐ Visualizations and reports") |
|
print(" ๐ Statistical diagnostics") |
|
print(" ๐ฎ Forecasting results") |
|
print(" ๐ฏ Segmentation analysis") |
|
|
|
if __name__ == "__main__": |
|
main() |