File size: 22,752 Bytes
2b395f2 26a8ea5 2b395f2 26a8ea5 2b395f2 26a8ea5 2b395f2 26a8ea5 2b395f2 26a8ea5 2b395f2 26a8ea5 2b395f2 26a8ea5 2b395f2 26a8ea5 2b395f2 26a8ea5 2b395f2 26a8ea5 2b395f2 26a8ea5 2b395f2 26a8ea5 2b395f2 26a8ea5 2b395f2 26a8ea5 2b395f2 26a8ea5 2b395f2 26a8ea5 2b395f2 26a8ea5 2b395f2 26a8ea5 2b395f2 26a8ea5 2b395f2 26a8ea5 2b395f2 26a8ea5 2b395f2 26a8ea5 2b395f2 26a8ea5 2b395f2 26a8ea5 2b395f2 26a8ea5 2b395f2 26a8ea5 2b395f2 26a8ea5 2b395f2 26a8ea5 2b395f2 26a8ea5 2b395f2 26a8ea5 2b395f2 26a8ea5 2b395f2 26a8ea5 2b395f2 26a8ea5 2b395f2 26a8ea5 2b395f2 26a8ea5 2b395f2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 |
#!/usr/bin/env python3
"""
FRED ML - Enterprise Economic Analytics Platform
Professional think tank interface for comprehensive economic data analysis
"""
import streamlit as st
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import boto3
import json
from datetime import datetime, timedelta
import requests
import os
import sys
from typing import Dict, List, Optional
from pathlib import Path
# Add src to path for analytics modules
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'src'))
# Import analytics modules
try:
from src.analysis.comprehensive_analytics import ComprehensiveAnalytics
from src.core.enhanced_fred_client import EnhancedFREDClient
from config.settings import FRED_API_KEY
ANALYTICS_AVAILABLE = True
except ImportError:
ANALYTICS_AVAILABLE = False
st.warning("Advanced analytics modules not available. Running in basic mode.")
# Page configuration
st.set_page_config(
page_title="FRED ML - Economic Analytics Platform",
page_icon="๐๏ธ",
layout="wide",
initial_sidebar_state="expanded"
)
# Custom CSS for enterprise styling
st.markdown("""
<style>
/* Main styling */
.main-header {
background: linear-gradient(90deg, #1e3c72 0%, #2a5298 100%);
padding: 2rem;
border-radius: 10px;
margin-bottom: 2rem;
color: white;
}
.metric-card {
background: white;
padding: 1.5rem;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
border-left: 4px solid #1e3c72;
margin-bottom: 1rem;
}
.analysis-section {
background: #f8f9fa;
padding: 2rem;
border-radius: 10px;
margin: 1rem 0;
border: 1px solid #e9ecef;
}
.sidebar .sidebar-content {
background: #2c3e50;
}
.stButton > button {
background: linear-gradient(90deg, #1e3c72 0%, #2a5298 100%);
color: white;
border: none;
border-radius: 5px;
padding: 0.5rem 1rem;
font-weight: 600;
}
.stButton > button:hover {
background: linear-gradient(90deg, #2a5298 0%, #1e3c72 100%);
transform: translateY(-2px);
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}
.success-message {
background: #d4edda;
color: #155724;
padding: 1rem;
border-radius: 5px;
border: 1px solid #c3e6cb;
margin: 1rem 0;
}
.warning-message {
background: #fff3cd;
color: #856404;
padding: 1rem;
border-radius: 5px;
border: 1px solid #ffeaa7;
margin: 1rem 0;
}
.info-message {
background: #d1ecf1;
color: #0c5460;
padding: 1rem;
border-radius: 5px;
border: 1px solid #bee5eb;
margin: 1rem 0;
}
.chart-container {
background: white;
padding: 1rem;
border-radius: 10px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
margin: 1rem 0;
}
.tabs-container {
background: white;
border-radius: 10px;
padding: 1rem;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
</style>
""", unsafe_allow_html=True)
# Initialize AWS clients
@st.cache_resource
def init_aws_clients():
"""Initialize AWS clients for S3 and Lambda"""
try:
s3_client = boto3.client('s3')
lambda_client = boto3.client('lambda')
return s3_client, lambda_client
except Exception as e:
st.error(f"Failed to initialize AWS clients: {e}")
return None, None
# Load configuration
@st.cache_data
def load_config():
"""Load application configuration"""
return {
's3_bucket': os.getenv('S3_BUCKET', 'fredmlv1'),
'lambda_function': os.getenv('LAMBDA_FUNCTION', 'fred-ml-processor'),
'api_endpoint': os.getenv('API_ENDPOINT', 'http://localhost:8000')
}
def get_available_reports(s3_client, bucket_name: str) -> List[Dict]:
"""Get list of available reports from S3"""
try:
response = s3_client.list_objects_v2(
Bucket=bucket_name,
Prefix='reports/'
)
reports = []
if 'Contents' in response:
for obj in response['Contents']:
if obj['Key'].endswith('.json'):
reports.append({
'key': obj['Key'],
'last_modified': obj['LastModified'],
'size': obj['Size']
})
return sorted(reports, key=lambda x: x['last_modified'], reverse=True)
except Exception as e:
st.error(f"Failed to load reports: {e}")
return []
def get_report_data(s3_client, bucket_name: str, report_key: str) -> Optional[Dict]:
"""Get report data from S3"""
try:
response = s3_client.get_object(Bucket=bucket_name, Key=report_key)
data = json.loads(response['Body'].read().decode('utf-8'))
return data
except Exception as e:
st.error(f"Failed to load report data: {e}")
return None
def trigger_lambda_analysis(lambda_client, function_name: str, payload: Dict) -> bool:
"""Trigger Lambda function for analysis"""
try:
response = lambda_client.invoke(
FunctionName=function_name,
InvocationType='Event', # Asynchronous
Payload=json.dumps(payload)
)
return response['StatusCode'] == 202
except Exception as e:
st.error(f"Failed to trigger analysis: {e}")
return False
def create_time_series_plot(df: pd.DataFrame, title: str = "Economic Indicators"):
"""Create interactive time series plot"""
fig = go.Figure()
colors = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b']
for i, column in enumerate(df.columns):
if column != 'Date':
fig.add_trace(
go.Scatter(
x=df.index,
y=df[column],
mode='lines',
name=column,
line=dict(width=2, color=colors[i % len(colors)]),
hovertemplate='<b>%{x}</b><br>%{y:.2f}<extra></extra>'
)
)
fig.update_layout(
title=dict(text=title, x=0.5, font=dict(size=20)),
xaxis_title="Date",
yaxis_title="Value",
hovermode='x unified',
height=500,
plot_bgcolor='white',
paper_bgcolor='white',
font=dict(size=12)
)
return fig
def create_correlation_heatmap(df: pd.DataFrame):
"""Create correlation heatmap"""
corr_matrix = df.corr()
fig = px.imshow(
corr_matrix,
text_auto=True,
aspect="auto",
title="Correlation Matrix",
color_continuous_scale='RdBu_r',
center=0
)
fig.update_layout(
title=dict(x=0.5, font=dict(size=20)),
height=500,
plot_bgcolor='white',
paper_bgcolor='white'
)
return fig
def create_forecast_plot(historical_data, forecast_data, title="Forecast"):
"""Create forecast plot with confidence intervals"""
fig = go.Figure()
# Historical data
fig.add_trace(go.Scatter(
x=historical_data.index,
y=historical_data.values,
mode='lines',
name='Historical',
line=dict(color='#1f77b4', width=2)
))
# Forecast
if 'forecast' in forecast_data:
forecast_values = forecast_data['forecast']
forecast_index = pd.date_range(
start=historical_data.index[-1] + pd.DateOffset(months=3),
periods=len(forecast_values),
freq='Q'
)
fig.add_trace(go.Scatter(
x=forecast_index,
y=forecast_values,
mode='lines',
name='Forecast',
line=dict(color='#ff7f0e', width=2, dash='dash')
))
# Confidence intervals
if 'confidence_intervals' in forecast_data:
ci = forecast_data['confidence_intervals']
if 'lower' in ci.columns and 'upper' in ci.columns:
fig.add_trace(go.Scatter(
x=forecast_index,
y=ci['upper'],
mode='lines',
name='Upper CI',
line=dict(color='rgba(255,127,14,0.3)', width=1),
showlegend=False
))
fig.add_trace(go.Scatter(
x=forecast_index,
y=ci['lower'],
mode='lines',
fill='tonexty',
name='Confidence Interval',
line=dict(color='rgba(255,127,14,0.3)', width=1)
))
fig.update_layout(
title=dict(text=title, x=0.5, font=dict(size=20)),
xaxis_title="Date",
yaxis_title="Value",
height=500,
plot_bgcolor='white',
paper_bgcolor='white'
)
return fig
def main():
"""Main Streamlit application"""
# Initialize AWS clients
s3_client, lambda_client = init_aws_clients()
config = load_config()
# Sidebar
with st.sidebar:
st.markdown("""
<div style="text-align: center; padding: 1rem;">
<h2>๐๏ธ FRED ML</h2>
<p style="color: #666; font-size: 0.9rem;">Economic Analytics Platform</p>
</div>
""", unsafe_allow_html=True)
st.markdown("---")
# Navigation
page = st.selectbox(
"Navigation",
["๐ Executive Dashboard", "๐ฎ Advanced Analytics", "๐ Economic Indicators", "๐ Reports & Insights", "โ๏ธ Configuration"]
)
if page == "๐ Executive Dashboard":
show_executive_dashboard(s3_client, config)
elif page == "๐ฎ Advanced Analytics":
show_advanced_analytics_page(config)
elif page == "๐ Economic Indicators":
show_indicators_page(s3_client, config)
elif page == "๐ Reports & Insights":
show_reports_page(s3_client, config)
elif page == "โ๏ธ Configuration":
show_configuration_page(config)
def show_executive_dashboard(s3_client, config):
"""Show executive dashboard with key metrics"""
st.markdown("""
<div class="main-header">
<h1>๐ Executive Dashboard</h1>
<p>Comprehensive Economic Analytics & Insights</p>
</div>
""", unsafe_allow_html=True)
# Key metrics row
col1, col2, col3, col4 = st.columns(4)
with col1:
st.markdown("""
<div class="metric-card">
<h3>๐ GDP Growth</h3>
<h2>2.1%</h2>
<p>Q4 2024</p>
</div>
""", unsafe_allow_html=True)
with col2:
st.markdown("""
<div class="metric-card">
<h3>๐ญ Industrial Production</h3>
<h2>+0.8%</h2>
<p>Monthly Change</p>
</div>
""", unsafe_allow_html=True)
with col3:
st.markdown("""
<div class="metric-card">
<h3>๐ฐ Inflation Rate</h3>
<h2>3.2%</h2>
<p>Annual Rate</p>
</div>
""", unsafe_allow_html=True)
with col4:
st.markdown("""
<div class="metric-card">
<h3>๐ผ Unemployment</h3>
<h2>3.7%</h2>
<p>Current Rate</p>
</div>
""", unsafe_allow_html=True)
# Recent analysis section
st.markdown("""
<div class="analysis-section">
<h3>๐ Recent Analysis</h3>
</div>
""", unsafe_allow_html=True)
# Get latest report
reports = get_available_reports(s3_client, config['s3_bucket'])
if reports:
latest_report = reports[0]
report_data = get_report_data(s3_client, config['s3_bucket'], latest_report['key'])
if report_data:
# Show latest data visualization
if 'data' in report_data and report_data['data']:
df = pd.DataFrame(report_data['data'])
df['Date'] = pd.to_datetime(df['Date'])
df.set_index('Date', inplace=True)
col1, col2 = st.columns(2)
with col1:
st.markdown("""
<div class="chart-container">
<h4>Economic Indicators Trend</h4>
</div>
""", unsafe_allow_html=True)
fig = create_time_series_plot(df)
st.plotly_chart(fig, use_container_width=True)
with col2:
st.markdown("""
<div class="chart-container">
<h4>Correlation Analysis</h4>
</div>
""", unsafe_allow_html=True)
corr_fig = create_correlation_heatmap(df)
st.plotly_chart(corr_fig, use_container_width=True)
else:
st.warning("No report data available")
else:
st.info("No reports available. Run an analysis to generate reports.")
def show_advanced_analytics_page(config):
"""Show advanced analytics page with comprehensive analysis capabilities"""
st.markdown("""
<div class="main-header">
<h1>๐ฎ Advanced Analytics</h1>
<p>Comprehensive Economic Modeling & Forecasting</p>
</div>
""", unsafe_allow_html=True)
if not ANALYTICS_AVAILABLE:
st.error("Advanced analytics modules not available. Please install required dependencies.")
return
# Analysis configuration
st.markdown("""
<div class="analysis-section">
<h3>๐ Analysis Configuration</h3>
</div>
""", unsafe_allow_html=True)
col1, col2 = st.columns(2)
with col1:
# Economic indicators selection
indicators = [
"GDPC1", "INDPRO", "RSAFS", "CPIAUCSL", "FEDFUNDS", "DGS10",
"TCU", "PAYEMS", "PCE", "M2SL", "DEXUSEU", "UNRATE"
]
selected_indicators = st.multiselect(
"Select Economic Indicators",
indicators,
default=["GDPC1", "INDPRO", "RSAFS"]
)
# Date range
end_date = datetime.now()
start_date = end_date - timedelta(days=365*5) # 5 years
start_date_input = st.date_input(
"Start Date",
value=start_date,
max_value=end_date
)
end_date_input = st.date_input(
"End Date",
value=end_date,
max_value=end_date
)
with col2:
# Analysis options
forecast_periods = st.slider(
"Forecast Periods",
min_value=1,
max_value=12,
value=4,
help="Number of periods to forecast"
)
include_visualizations = st.checkbox(
"Generate Visualizations",
value=True,
help="Create charts and graphs"
)
analysis_type = st.selectbox(
"Analysis Type",
["Comprehensive", "Forecasting Only", "Segmentation Only", "Statistical Only"],
help="Type of analysis to perform"
)
# Run analysis button
if st.button("๐ Run Advanced Analysis", type="primary"):
if not selected_indicators:
st.error("Please select at least one economic indicator.")
return
if not FRED_API_KEY:
st.error("FRED API key not configured. Please set FRED_API_KEY environment variable.")
return
# Show progress
with st.spinner("Running comprehensive analysis..."):
try:
# Initialize analytics
analytics = ComprehensiveAnalytics(FRED_API_KEY, output_dir="data/exports/streamlit")
# Run analysis
results = analytics.run_complete_analysis(
indicators=selected_indicators,
start_date=start_date_input.strftime('%Y-%m-%d'),
end_date=end_date_input.strftime('%Y-%m-%d'),
forecast_periods=forecast_periods,
include_visualizations=include_visualizations
)
st.success("โ
Analysis completed successfully!")
# Display results
display_analysis_results(results)
except Exception as e:
st.error(f"โ Analysis failed: {e}")
def display_analysis_results(results):
"""Display comprehensive analysis results"""
st.markdown("""
<div class="analysis-section">
<h3>๐ Analysis Results</h3>
</div>
""", unsafe_allow_html=True)
# Create tabs for different result types
tab1, tab2, tab3, tab4 = st.tabs(["๐ฎ Forecasting", "๐ฏ Segmentation", "๐ Statistical", "๐ก Insights"])
with tab1:
if 'forecasting' in results:
st.subheader("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)
rmse = backtest.get('rmse', 0)
col1, col2 = st.columns(2)
with col1:
st.metric(f"{indicator} MAPE", f"{mape:.2f}%")
with col2:
st.metric(f"{indicator} RMSE", f"{rmse:.4f}")
with tab2:
if 'segmentation' in results:
st.subheader("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)
st.info(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)
st.info(f"Economic series clustered into {n_clusters} groups")
with tab3:
if 'statistical_modeling' in results:
st.subheader("Statistical Analysis Results")
stat_results = results['statistical_modeling']
if 'correlation' in stat_results:
corr_results = stat_results['correlation']
significant_correlations = corr_results.get('significant_correlations', [])
st.info(f"Found {len(significant_correlations)} significant correlations")
with tab4:
if 'insights' in results:
st.subheader("Key Insights")
insights = results['insights']
for finding in insights.get('key_findings', []):
st.write(f"โข {finding}")
def show_indicators_page(s3_client, config):
"""Show economic indicators page"""
st.markdown("""
<div class="main-header">
<h1>๐ Economic Indicators</h1>
<p>Real-time Economic Data & Analysis</p>
</div>
""", unsafe_allow_html=True)
# Indicators overview
indicators_info = {
"GDPC1": {"name": "Real GDP", "description": "Real Gross Domestic Product", "frequency": "Quarterly"},
"INDPRO": {"name": "Industrial Production", "description": "Industrial Production Index", "frequency": "Monthly"},
"RSAFS": {"name": "Retail Sales", "description": "Retail Sales", "frequency": "Monthly"},
"CPIAUCSL": {"name": "Consumer Price Index", "description": "Inflation measure", "frequency": "Monthly"},
"FEDFUNDS": {"name": "Federal Funds Rate", "description": "Target interest rate", "frequency": "Daily"},
"DGS10": {"name": "10-Year Treasury", "description": "Government bond yield", "frequency": "Daily"}
}
# Display indicators in cards
cols = st.columns(3)
for i, (code, info) in enumerate(indicators_info.items()):
with cols[i % 3]:
st.markdown(f"""
<div class="metric-card">
<h3>{info['name']}</h3>
<p><strong>Code:</strong> {code}</p>
<p><strong>Frequency:</strong> {info['frequency']}</p>
<p>{info['description']}</p>
</div>
""", unsafe_allow_html=True)
def show_reports_page(s3_client, config):
"""Show reports and insights page"""
st.markdown("""
<div class="main-header">
<h1>๐ Reports & Insights</h1>
<p>Comprehensive Analysis Reports</p>
</div>
""", unsafe_allow_html=True)
# Get available reports
reports = get_available_reports(s3_client, config['s3_bucket'])
if reports:
st.subheader("Available Reports")
for report in reports[:5]: # Show last 5 reports
with st.expander(f"Report: {report['key']} - {report['last_modified'].strftime('%Y-%m-%d %H:%M')}"):
report_data = get_report_data(s3_client, config['s3_bucket'], report['key'])
if report_data:
st.json(report_data)
else:
st.info("No reports available. Run an analysis to generate reports.")
def show_configuration_page(config):
"""Show configuration page"""
st.markdown("""
<div class="main-header">
<h1>โ๏ธ Configuration</h1>
<p>System Settings & Configuration</p>
</div>
""", unsafe_allow_html=True)
st.subheader("System Configuration")
col1, col2 = st.columns(2)
with col1:
st.write("**AWS Configuration**")
st.write(f"S3 Bucket: {config['s3_bucket']}")
st.write(f"Lambda Function: {config['lambda_function']}")
with col2:
st.write("**API Configuration**")
st.write(f"API Endpoint: {config['api_endpoint']}")
st.write(f"Analytics Available: {ANALYTICS_AVAILABLE}")
if __name__ == "__main__":
main() |