Edwin Salguero commited on
Commit ·
94e5687
1
Parent(s): 712bf79
Remove all demo data and update project to use only real FRED API data
Browse filesFiles changed (2)
- frontend/app.py+161-445
- frontend/demo_data.pyDeleted-288
36 unmodified lines3738394040414246 unmodified lines89909193949596979899100101102103104105929394316 unmodified lines41141241342842943043141441541641741843741942042142242342440 unmodified lines46546646746846947047142 unmodified lines51451551653253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458551751858758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162251952052152252373 unmodified lines59759859970270360060160260360460560618 unmodified lines62562662762862963063144 unmodified lines67667767867968068168268368472 unmodified lines757758759856857858859860861862863864760761866762763764765766123 unmodified lines89089189299699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111089389489565 unmodified lines961962963964965966967189 unmodified lines11571158115913771378137911601161116211631164116511661167138511681387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143011691170117111721173117411751176117711781432143314341435143614371179118011811182118311841439144014411442144314441445144614471448144914501451118511861187118811894 unmodified lines11941195119611971198119912001201120212031204101 unmodified lines13061307130815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612130913101311131213131614161516161617161816191620162116221623162416251626162716281629131413151316163116321633163416351636163716381639164016411642164316441645164616471648164916501651131713181319165316541655165616571658165913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135216651353135413551356135716691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393170517061707170817091710171117121394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914202 unmodified lines1423142414251726172717281729142614271428142914301431143214331434143514361437173117321438143914401441144214431444144514461447144814491734145014511452145348 unmodified lines150215031504178917901505150615071508150934 unmodified lines1544154515461831154715481549155036 unmodified lines return requests
# Initialize flagsDEMO_MODE = FalseANALYTICS_AVAILABLE = FalseFRED_API_AVAILABLE = FalseCONFIG_AVAILABLE = False46 unmodified lines REAL_DATA_MODE = FRED_API_KEY and FRED_API_KEY != 'your-fred-api-key-here' return False
# Lazy load demo datadef load_demo_data(): """Load demo data only when needed""" global DEMO_MODE try: from demo_data import get_demo_data DEMO_DATA = get_demo_data() DEMO_MODE = True return DEMO_DATA except ImportError: DEMO_MODE = False return None
# Custom CSS for enterprise stylingst.markdown("""<style>316 unmodified lines # Initialize AWS clients s3_client, lambda_client = init_aws_clients() config = load_config() # Load demo data if needed if not REAL_DATA_MODE: demo_data = load_demo_data() # Show data mode info if REAL_DATA_MODE: st.success("🎯 Using real FRED API data for live economic insights.") else: st.info("📊 Using demo data for demonstration. Get a free FRED API key for real data.") st.error("❌ FRED API key not configured. Please set FRED_API_KEY environment variable.") st.info("Get a free FRED API key at: https://fred.stlouisfed.org/docs/api/api_key.html") return # Sidebar with st.sidebar:40 unmodified lines if REAL_DATA_MODE and FRED_API_AVAILABLE: # Get real insights from FRED API try: load_fred_client() insights = generate_real_insights(FRED_API_KEY) with col1:42 unmodified lines except Exception as e: st.error(f"Failed to fetch real data: {e}") # Fallback to demo data if DEMO_MODE: insights = DEMO_DATA['insights'] # ... demo data display else: # Static fallback pass elif DEMO_MODE: insights = DEMO_DATA['insights'] with col1: gdp_insight = insights['GDPC1'] st.markdown(f""" <div class="metric-card"> <h3>📈 GDP Growth</h3> <h2>{gdp_insight['growth_rate']}</h2> <p>{gdp_insight['current_value']}</p> <small>{gdp_insight['trend']}</small> </div> """, unsafe_allow_html=True) with col2: indpro_insight = insights['INDPRO'] st.markdown(f""" <div class="metric-card"> <h3>🏭 Industrial Production</h3> <h2>{indpro_insight['growth_rate']}</h2> <p>{indpro_insight['current_value']}</p> <small>{indpro_insight['trend']}</small> </div> """, unsafe_allow_html=True) with col3: cpi_insight = insights['CPIAUCSL'] st.markdown(f""" <div class="metric-card"> <h3>💰 Inflation Rate</h3> <h2>{cpi_insight['growth_rate']}</h2> <p>{cpi_insight['current_value']}</p> <small>{cpi_insight['trend']}</small> </div> """, unsafe_allow_html=True) with col4: unrate_insight = insights['UNRATE'] st.markdown(f""" <div class="metric-card"> <h3>💼 Unemployment</h3> <h2>{unrate_insight['current_value']}</h2> <p>{unrate_insight['growth_rate']}</p> <small>{unrate_insight['trend']}</small> </div> """, unsafe_allow_html=True) st.info("Please check your FRED API key configuration.") else: # Fallback to static data 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) st.error("❌ FRED API not available. Please configure your FRED API key.") st.info("Get a free FRED API key at: https://fred.stlouisfed.org/docs/api/api_key.html") # Recent analysis section st.markdown("""73 unmodified lines </div> """, unsafe_allow_html=True) if DEMO_MODE: st.info("🎯 Running in demo mode with realistic economic data and insights.") if not REAL_DATA_MODE: st.error("❌ FRED API key not configured. Please set FRED_API_KEY environment variable.") st.info("Get a free FRED API key at: https://fred.stlouisfed.org/docs/api/api_key.html") return # Analysis configuration st.markdown("""18 unmodified lines ) # Date range from datetime import datetime, timedelta end_date = datetime.now() start_date = end_date - timedelta(days=365*5) # 5 years 44 unmodified lines # Run real analysis with FRED API data with st.spinner(analysis_message): try: # Load FRED client load_fred_client() # Get real economic data real_data = get_real_economic_data(FRED_API_KEY, start_date_input.strftime('%Y-%m-%d'),72 unmodified lines except Exception as e: st.error(f"❌ Real data analysis failed: {e}") st.info("Falling back to demo analysis...") # Fallback to demo analysis if DEMO_MODE: run_demo_analysis(analysis_type, selected_indicators) elif DEMO_MODE: # Run demo analysis run_demo_analysis(analysis_type, selected_indicators) st.info("Please check your FRED API key and try again.") else: st.error("No data sources available. Please configure FRED API key or use demo mode.") st.error("❌ FRED API not available. Please configure your FRED API key.") st.info("Get a free FRED API key at: https://fred.stlouisfed.org/docs/api/api_key.html")
def generate_analysis_results(analysis_type, real_data, selected_indicators): """Generate analysis results based on the selected analysis type"""123 unmodified lines return {}
def run_demo_analysis(analysis_type, selected_indicators): """Run demo analysis based on selected type""" with st.spinner(f"Running {analysis_type.lower()} analysis with demo data..."): try: # Simulate analysis with demo data import time time.sleep(2) # Simulate processing time # Generate demo results based on analysis type if analysis_type == "Comprehensive": demo_results = { 'forecasting': { 'GDPC1': { 'backtest': {'mape': 2.1, 'rmse': 0.045}, 'forecast': [21847, 22123, 22401, 22682] }, 'INDPRO': { 'backtest': {'mape': 1.8, 'rmse': 0.032}, 'forecast': [102.4, 103.1, 103.8, 104.5] }, 'RSAFS': { 'backtest': {'mape': 2.5, 'rmse': 0.078}, 'forecast': [579.2, 584.7, 590.3, 595.9] } }, 'segmentation': { 'time_period_clusters': {'n_clusters': 3}, 'series_clusters': {'n_clusters': 4} }, 'statistical_modeling': { 'correlation': { 'significant_correlations': [ 'GDPC1-INDPRO: 0.85', 'GDPC1-RSAFS: 0.78', 'CPIAUCSL-FEDFUNDS: 0.65' ] } }, 'insights': { 'key_findings': [ 'Strong correlation between GDP and Industrial Production (0.85)', 'Inflation showing signs of moderation', 'Federal Reserve policy rate at 22-year high', 'Labor market remains tight with low unemployment', 'Consumer spending resilient despite inflation' ] } } elif analysis_type == "Forecasting Only": demo_results = { 'forecasting': { 'GDPC1': { 'backtest': {'mape': 2.1, 'rmse': 0.045}, 'forecast': [21847, 22123, 22401, 22682] }, 'INDPRO': { 'backtest': {'mape': 1.8, 'rmse': 0.032}, 'forecast': [102.4, 103.1, 103.8, 104.5] } }, 'insights': { 'key_findings': [ 'Forecasting analysis completed successfully', 'Time series models applied to selected indicators', 'Forecast accuracy metrics calculated', 'Confidence intervals generated' ] } } elif analysis_type == "Segmentation Only": demo_results = { 'segmentation': { 'time_period_clusters': {'n_clusters': 3}, 'series_clusters': {'n_clusters': 4} }, 'insights': { 'key_findings': [ 'Segmentation analysis completed successfully', 'Economic regimes identified', 'Series clustering performed', 'Pattern recognition applied' ] } } elif analysis_type == "Statistical Only": demo_results = { 'statistical_modeling': { 'correlation': { 'significant_correlations': [ 'GDPC1-INDPRO: 0.85', 'GDPC1-RSAFS: 0.78', 'CPIAUCSL-FEDFUNDS: 0.65' ] } }, 'insights': { 'key_findings': [ 'Statistical analysis completed successfully', 'Correlation analysis performed', 'Significance testing completed', 'Statistical models validated' ] } } else: demo_results = {} st.success(f"✅ Demo {analysis_type.lower()} analysis completed successfully!") # Display results display_analysis_results(demo_results) except Exception as e: st.error(f"❌ Demo analysis failed: {e}")
def display_analysis_results(results): """Display comprehensive analysis results with download options""" st.markdown("""65 unmodified lines # Generate downloadable reports import json import io from datetime import datetime # Create JSON report report_data = {189 unmodified lines # Check if AWS clients are available and test bucket access if s3_client is None: st.subheader("Demo Reports & Insights") st.info("📊 Showing demo reports (AWS not configured)") show_demo_reports = True st.error("❌ AWS S3 not configured. Please configure AWS credentials to access reports.") st.info("Reports are stored in AWS S3. Configure your AWS credentials to access them.") return else: # Test if we can actually access the S3 bucket try: s3_client.head_bucket(Bucket=config['s3_bucket']) st.success(f"✅ Connected to S3 bucket: {config['s3_bucket']}") show_demo_reports = False except Exception as e: st.warning(f"⚠️ AWS connected but bucket '{config['s3_bucket']}' not accessible: {str(e)}") st.info("📊 Showing demo reports (S3 bucket not accessible)") show_demo_reports = True # Show demo reports if needed if show_demo_reports: demo_reports = [ { 'title': 'Economic Outlook Q4 2024', 'date': '2024-12-15', 'summary': 'Comprehensive analysis of economic indicators and forecasts', 'insights': [ 'GDP growth expected to moderate to 2.1% in Q4', 'Inflation continuing to moderate from peak levels', 'Federal Reserve likely to maintain current policy stance', 'Labor market remains tight with strong job creation', 'Consumer spending resilient despite inflation pressures' ] }, { 'title': 'Monetary Policy Analysis', 'date': '2024-12-10', 'summary': 'Analysis of Federal Reserve policy and market implications', 'insights': [ 'Federal Funds Rate at 22-year high of 5.25%', 'Yield curve inversion persists, signaling economic uncertainty', 'Inflation expectations well-anchored around 2%', 'Financial conditions tightening as intended', 'Policy normalization expected to begin in 2025' ] }, { 'title': 'Labor Market Trends', 'date': '2024-12-05', 'summary': 'Analysis of employment and wage trends', 'insights': [ 'Unemployment rate at 3.7%, near historic lows', 'Nonfarm payrolls growing at steady pace', 'Wage growth moderating but still above pre-pandemic levels', 'Labor force participation improving gradually', 'Skills mismatch remains a challenge in certain sectors' ] } ] st.error(f"❌ Cannot access S3 bucket '{config['s3_bucket']}': {str(e)}") st.info("Please check your AWS credentials and bucket configuration.") return # Try to get real reports from S3 reports = get_available_reports(s3_client, config['s3_bucket']) if reports: st.subheader("Available Reports") for i, report in enumerate(demo_reports): with st.expander(f"📊 {report['title']} - {report['date']}"): st.markdown(f"**Summary:** {report['summary']}") st.markdown("**Key Insights:**") for insight in report['insights']: st.markdown(f"• {insight}") for report in reports[:10]: # Show last 10 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: # Try to get real reports from S3 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.") st.info("No reports available. Run an analysis to generate reports.") st.info("Reports will be automatically generated when you run advanced analytics.")
def show_downloads_page(s3_client, config): """Show comprehensive downloads page with reports and visualizations"""4 unmodified lines </div> """, unsafe_allow_html=True) if not REAL_DATA_MODE: st.error("❌ FRED API key not configured. Please set FRED_API_KEY environment variable.") st.info("Get a free FRED API key at: https://fred.stlouisfed.org/docs/api/api_key.html") return # Create tabs for different download types tab1, tab2, tab3, tab4 = st.tabs(["📊 Visualizations", "📄 Reports", "📈 Analysis Data", "📦 Bulk Downloads"]) 101 unmodified lines st.subheader("📄 Analysis Reports") st.info("Download comprehensive analysis reports in various formats") # Generate sample reports for download import json import io from datetime import datetime # Sample analysis report sample_report = { 'analysis_timestamp': datetime.now().isoformat(), 'summary': { 'gdp_growth': '2.1%', 'inflation_rate': '3.2%', 'unemployment_rate': '3.7%', 'industrial_production': '+0.8%' }, 'key_findings': [ 'GDP growth remains steady at 2.1%', 'Inflation continues to moderate from peak levels', 'Labor market remains tight with strong job creation', 'Industrial production shows positive momentum' ], 'risk_factors': [ 'Geopolitical tensions affecting supply chains', 'Federal Reserve policy uncertainty', 'Consumer spending patterns changing' ], 'opportunities': [ 'Strong domestic manufacturing growth', 'Technology sector expansion', 'Green energy transition investments' ] } col1, col2, col3 = st.columns(3) with col1: # JSON Report json_report = json.dumps(sample_report, indent=2) st.download_button( label="📄 Download JSON Report", data=json_report, file_name=f"economic_analysis_report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json", mime="application/json" ) st.write("Comprehensive analysis data in JSON format") if s3_client is None: st.error("❌ AWS S3 not configured. Reports are stored in AWS S3.") st.info("Configure your AWS credentials to access reports.") return with col2: # CSV Summary csv_data = io.StringIO() csv_data.write("Metric,Value\n") csv_data.write(f"GDP Growth,{sample_report['summary']['gdp_growth']}\n") csv_data.write(f"Inflation Rate,{sample_report['summary']['inflation_rate']}\n") csv_data.write(f"Unemployment Rate,{sample_report['summary']['unemployment_rate']}\n") csv_data.write(f"Industrial Production,{sample_report['summary']['industrial_production']}\n") st.download_button( label="📊 Download CSV Summary", data=csv_data.getvalue(), file_name=f"economic_summary_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv", mime="text/csv" ) st.write("Key metrics in spreadsheet format") # Try to get real reports from S3 reports = get_available_reports(s3_client, config['s3_bucket']) with col3: # Text Report text_report = f"""ECONOMIC ANALYSIS REPORTGenerated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
SUMMARY METRICS:- GDP Growth: {sample_report['summary']['gdp_growth']}- Inflation Rate: {sample_report['summary']['inflation_rate']}- Unemployment Rate: {sample_report['summary']['unemployment_rate']}- Industrial Production: {sample_report['summary']['industrial_production']}
KEY FINDINGS:{chr(10).join([f"• {finding}" for finding in sample_report['key_findings']])}
RISK FACTORS:{chr(10).join([f"• {risk}" for risk in sample_report['risk_factors']])}
OPPORTUNITIES:{chr(10).join([f"• {opp}" for opp in sample_report['opportunities']])}""" if reports: st.success(f"✅ Found {len(reports)} reports available for download") st.download_button( label="📝 Download Text Report", data=text_report, file_name=f"economic_report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.txt", mime="text/plain" ) st.write("Human-readable analysis report") for i, report in enumerate(reports[:10]): # Show last 10 reports col1, col2 = st.columns([3, 1]) with col1: st.write(f"**{report['key']}**") st.write(f"Size: {report['size']:,} bytes | Modified: {report['last_modified'].strftime('%Y-%m-%d %H:%M')}") with col2: try: report_data = get_report_data(s3_client, config['s3_bucket'], report['key']) if report_data: import json json_data = json.dumps(report_data, indent=2) st.download_button( label="📥 Download", data=json_data, file_name=f"{report['key']}.json", mime="application/json", key=f"report_{i}" ) except Exception as e: st.error("❌ Download failed") else: st.info("No reports available. Run an analysis to generate reports.") with tab3: st.subheader("📈 Analysis Data") st.info("Download raw data and analysis results for further processing") if not REAL_DATA_MODE: st.error("❌ No real data available. Please configure your FRED API key.") return # Generate sample data files # Generate real economic data files import pandas as pd import numpy as np from datetime import datetime, timedelta # Sample economic data dates = pd.date_range('2020-01-01', periods=100, freq='D') economic_data = pd.DataFrame({ 'GDP': np.random.normal(100, 5, 100).cumsum(), 'Inflation': np.random.normal(2, 0.5, 100), 'Unemployment': np.random.normal(5, 1, 100), 'Industrial_Production': np.random.normal(50, 3, 100) }, index=dates) col1, col2 = st.columns(2) with col1: # CSV Data csv_data = economic_data.to_csv() st.download_button( label="📊 Download CSV Data", data=csv_data, file_name=f"economic_data_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv", mime="text/csv" ) st.write("Raw economic time series data") with col2: # Excel Data excel_buffer = io.BytesIO() with pd.ExcelWriter(excel_buffer, engine='openpyxl') as writer: economic_data.to_excel(writer, sheet_name='Economic_Data') # Add summary sheet summary_df = pd.DataFrame({ 'Metric': ['Mean', 'Std', 'Min', 'Max'], 'GDP': [economic_data['GDP'].mean(), economic_data['GDP'].std(), economic_data['GDP'].min(), economic_data['GDP'].max()], 'Inflation': [economic_data['Inflation'].mean(), economic_data['Inflation'].std(), economic_data['Inflation'].min(), economic_data['Inflation'].max()], 'Unemployment': [economic_data['Unemployment'].mean(), economic_data['Unemployment'].std(), economic_data['Unemployment'].min(), economic_data['Unemployment'].max()] }) summary_df.to_excel(writer, sheet_name='Summary', index=False) try: # Load FRED client and get real data load_fred_client() real_data = get_real_economic_data(FRED_API_KEY, (datetime.now() - timedelta(days=365)).strftime('%Y-%m-%d'), datetime.now().strftime('%Y-%m-%d')) # Convert to DataFrame if real_data and 'data' in real_data: economic_data = pd.DataFrame(real_data['data']) col1, col2 = st.columns(2) with col1: # CSV Data csv_data = economic_data.to_csv() st.download_button( label="📊 Download CSV Data", data=csv_data, file_name=f"fred_economic_data_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv", mime="text/csv" ) st.write("Raw FRED economic time series data") with col2: # Excel Data excel_buffer = io.BytesIO() with pd.ExcelWriter(excel_buffer, engine='openpyxl') as writer: economic_data.to_excel(writer, sheet_name='Economic_Data') # Add summary sheet summary_df = pd.DataFrame({ 'Metric': ['Mean', 'Std', 'Min', 'Max'], 'Value': [economic_data.mean().mean(), economic_data.std().mean(), economic_data.min().min(), economic_data.max().max()] }) summary_df.to_excel(writer, sheet_name='Summary', index=False) excel_buffer.seek(0) st.download_button( label="📈 Download Excel Data", data=excel_buffer.getvalue(), file_name=f"economic_analysis_{datetime.now().strftime('%Y%m%d_%H%M%S')}.xlsx", mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ) st.write("Multi-sheet Excel workbook with data and summary") excel_buffer.seek(0) st.download_button( label="📈 Download Excel Data", data=excel_buffer.getvalue(), file_name=f"fred_economic_analysis_{datetime.now().strftime('%Y%m%d_%H%M%S')}.xlsx", mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ) st.write("Multi-sheet Excel workbook with FRED data and summary") else: st.error("❌ Could not retrieve real economic data.") st.info("Please check your FRED API key and try again.") except Exception as e: st.error(f"❌ Failed to generate data files: {e}") st.info("Please check your FRED API key and try again.") with tab4: st.subheader("📦 Bulk Downloads") st.info("Download all available files in one package") if not REAL_DATA_MODE: st.error("❌ No real data available for bulk download.") return # Create a zip file with all available data import zipfile import tempfile2 unmodified lines zip_buffer = io.BytesIO() with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zip_file: # Add sample reports zip_file.writestr('reports/economic_analysis.json', json.dumps(sample_report, indent=2)) zip_file.writestr('reports/economic_summary.csv', csv_data) zip_file.writestr('reports/economic_report.txt', text_report) # Add real reports if available if s3_client: reports = get_available_reports(s3_client, config['s3_bucket']) for i, report in enumerate(reports[:5]): # Add first 5 reports try: report_data = get_report_data(s3_client, config['s3_bucket'], report['key']) if report_data: import json zip_file.writestr(f'reports/{report["key"]}.json', json.dumps(report_data, indent=2)) except Exception: continue # Add sample data zip_file.writestr('data/economic_data.csv', economic_data.to_csv()) # Add real data if available try: load_fred_client() real_data = get_real_economic_data(FRED_API_KEY, (datetime.now() - timedelta(days=365)).strftime('%Y-%m-%d'), datetime.now().strftime('%Y-%m-%d')) if real_data and 'data' in real_data: economic_data = pd.DataFrame(real_data['data']) zip_file.writestr('data/fred_economic_data.csv', economic_data.to_csv()) except Exception: pass # Add sample visualizations (if available) # Add visualizations if available try: charts = chart_gen.list_available_charts() for i, chart in enumerate(charts[:5]): # Add first 5 charts48 unmodified lines st.success("✅ FRED API Key Configured") st.info("🎯 Real economic data is being used for analysis.") else: st.warning("⚠️ FRED API Key Not Configured") st.info("📊 Demo data is being used for demonstration.") st.error("❌ FRED API Key Not Configured") st.info("📊 Please configure your FRED API key to access real economic data.") # Setup instructions with st.expander("🔧 How to Set Up FRED API"):34 unmodified lines st.write(f"API Endpoint: {config['api_endpoint']}") st.write(f"Analytics Available: {ANALYTICS_AVAILABLE}") st.write(f"Real Data Mode: {REAL_DATA_MODE}") st.write(f"Demo Mode: {DEMO_MODE}") st.write(f"FRED API Available: {FRED_API_AVAILABLE}") # Data Source Information st.subheader("Data Sources")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288"""FRED ML - Demo Data GeneratorProvides realistic economic data and senior data scientist insights"""
import pandas as pdimport numpy as npfrom datetime import datetime, timedeltaimport random
def generate_economic_data(): """Generate realistic economic data for demonstration""" # Generate date range (last 5 years) end_date = datetime.now() start_date = end_date - timedelta(days=365*5) dates = pd.date_range(start=start_date, end=end_date, freq='ME') # Base values and trends for realistic economic data base_values = { 'GDPC1': 20000, # Real GDP in billions 'INDPRO': 100, # Industrial Production Index 'RSAFS': 500, # Retail Sales in billions 'CPIAUCSL': 250, # Consumer Price Index 'FEDFUNDS': 2.5, # Federal Funds Rate 'DGS10': 3.0, # 10-Year Treasury Rate 'UNRATE': 4.0, # Unemployment Rate 'PAYEMS': 150000, # Total Nonfarm Payrolls (thousands) 'PCE': 18000, # Personal Consumption Expenditures 'M2SL': 21000, # M2 Money Stock 'TCU': 75, # Capacity Utilization 'DEXUSEU': 1.1 # US/Euro Exchange Rate } # Growth rates and volatility for realistic trends growth_rates = { 'GDPC1': 0.02, # 2% annual growth 'INDPRO': 0.015, # 1.5% annual growth 'RSAFS': 0.03, # 3% annual growth 'CPIAUCSL': 0.025, # 2.5% annual inflation 'FEDFUNDS': 0.0, # Policy rate 'DGS10': 0.0, # Market rate 'UNRATE': 0.0, # Unemployment 'PAYEMS': 0.015, # Employment growth 'PCE': 0.025, # Consumption growth 'M2SL': 0.04, # Money supply growth 'TCU': 0.005, # Capacity utilization 'DEXUSEU': 0.0 # Exchange rate } # Generate realistic data data = {'Date': dates} for indicator, base_value in base_values.items(): # Create trend with realistic economic cycles trend = np.linspace(0, len(dates) * growth_rates[indicator], len(dates)) # Add business cycle effects cycle = 0.05 * np.sin(2 * np.pi * np.arange(len(dates)) / 48) # 4-year cycle # Add random noise noise = np.random.normal(0, 0.02, len(dates)) # Combine components values = base_value * (1 + trend + cycle + noise) # Ensure realistic bounds if indicator in ['UNRATE', 'FEDFUNDS', 'DGS10']: values = np.clip(values, 0, 20) elif indicator in ['CPIAUCSL']: values = np.clip(values, 200, 350) elif indicator in ['TCU']: values = np.clip(values, 60, 90) data[indicator] = values return pd.DataFrame(data)
def generate_insights(): """Generate senior data scientist insights""" insights = { 'GDPC1': { 'current_value': '$21,847.2B', 'growth_rate': '+2.1%', 'trend': 'Moderate growth', 'forecast': '+2.3% next quarter', 'key_insight': 'GDP growth remains resilient despite monetary tightening, supported by strong consumer spending and business investment.', 'risk_factors': ['Inflation persistence', 'Geopolitical tensions', 'Supply chain disruptions'], 'opportunities': ['Technology sector expansion', 'Infrastructure investment', 'Green energy transition'] }, 'INDPRO': { 'current_value': '102.4', 'growth_rate': '+0.8%', 'trend': 'Recovery phase', 'forecast': '+0.6% next month', 'key_insight': 'Industrial production shows signs of recovery, with manufacturing leading the rebound. Capacity utilization improving.', 'risk_factors': ['Supply chain bottlenecks', 'Labor shortages', 'Energy price volatility'], 'opportunities': ['Advanced manufacturing', 'Automation adoption', 'Reshoring initiatives'] }, 'RSAFS': { 'current_value': '$579.2B', 'growth_rate': '+3.2%', 'trend': 'Strong consumer spending', 'forecast': '+2.8% next month', 'key_insight': 'Retail sales demonstrate robust consumer confidence, with e-commerce continuing to gain market share.', 'risk_factors': ['Inflation impact on purchasing power', 'Interest rate sensitivity', 'Supply chain issues'], 'opportunities': ['Digital transformation', 'Omnichannel retail', 'Personalization'] }, 'CPIAUCSL': { 'current_value': '312.3', 'growth_rate': '+3.2%', 'trend': 'Moderating inflation', 'forecast': '+2.9% next month', 'key_insight': 'Inflation continues to moderate from peak levels, with core CPI showing signs of stabilization.', 'risk_factors': ['Energy price volatility', 'Wage pressure', 'Supply chain costs'], 'opportunities': ['Productivity improvements', 'Technology adoption', 'Supply chain optimization'] }, 'FEDFUNDS': { 'current_value': '5.25%', 'growth_rate': '0%', 'trend': 'Stable policy rate', 'forecast': '5.25% next meeting', 'key_insight': 'Federal Reserve maintains restrictive stance to combat inflation, with policy rate at 22-year high.', 'risk_factors': ['Inflation persistence', 'Economic slowdown', 'Financial stability'], 'opportunities': ['Policy normalization', 'Inflation targeting', 'Financial regulation'] }, 'DGS10': { 'current_value': '4.12%', 'growth_rate': '-0.15%', 'trend': 'Declining yields', 'forecast': '4.05% next week', 'key_insight': '10-year Treasury yields declining on economic uncertainty and flight to quality. Yield curve inversion persists.', 'risk_factors': ['Economic recession', 'Inflation expectations', 'Geopolitical risks'], 'opportunities': ['Bond market opportunities', 'Portfolio diversification', 'Interest rate hedging'] }, 'UNRATE': { 'current_value': '3.7%', 'growth_rate': '0%', 'trend': 'Stable employment', 'forecast': '3.6% next month', 'key_insight': 'Unemployment rate remains near historic lows, indicating tight labor market conditions.', 'risk_factors': ['Labor force participation', 'Skills mismatch', 'Economic slowdown'], 'opportunities': ['Workforce development', 'Technology training', 'Remote work adoption'] }, 'PAYEMS': { 'current_value': '156,847K', 'growth_rate': '+1.2%', 'trend': 'Steady job growth', 'forecast': '+0.8% next month', 'key_insight': 'Nonfarm payrolls continue steady growth, with healthcare and technology sectors leading job creation.', 'risk_factors': ['Labor shortages', 'Wage pressure', 'Economic uncertainty'], 'opportunities': ['Skills development', 'Industry partnerships', 'Immigration policy'] }, 'PCE': { 'current_value': '$19,847B', 'growth_rate': '+2.8%', 'trend': 'Strong consumption', 'forecast': '+2.5% next quarter', 'key_insight': 'Personal consumption expenditures show resilience, supported by strong labor market and wage growth.', 'risk_factors': ['Inflation impact', 'Interest rate sensitivity', 'Consumer confidence'], 'opportunities': ['Digital commerce', 'Experience economy', 'Sustainable consumption'] }, 'M2SL': { 'current_value': '$20,847B', 'growth_rate': '+2.1%', 'trend': 'Moderate growth', 'forecast': '+1.8% next month', 'key_insight': 'Money supply growth moderating as Federal Reserve tightens monetary policy to combat inflation.', 'risk_factors': ['Inflation expectations', 'Financial stability', 'Economic growth'], 'opportunities': ['Digital payments', 'Financial innovation', 'Monetary policy'] }, 'TCU': { 'current_value': '78.4%', 'growth_rate': '+0.3%', 'trend': 'Improving utilization', 'forecast': '78.7% next quarter', 'key_insight': 'Capacity utilization improving as supply chain issues resolve and demand remains strong.', 'risk_factors': ['Supply chain disruptions', 'Labor shortages', 'Energy constraints'], 'opportunities': ['Efficiency improvements', 'Technology adoption', 'Process optimization'] }, 'DEXUSEU': { 'current_value': '1.087', 'growth_rate': '+0.2%', 'trend': 'Stable exchange rate', 'forecast': '1.085 next week', 'key_insight': 'US dollar remains strong against euro, supported by relative economic performance and interest rate differentials.', 'risk_factors': ['Economic divergence', 'Geopolitical tensions', 'Trade policies'], 'opportunities': ['Currency hedging', 'International trade', 'Investment diversification'] } } return insights
def generate_forecast_data(): """Generate forecast data with confidence intervals""" # Generate future dates (next 4 quarters) last_date = datetime.now() future_dates = pd.date_range(start=last_date + timedelta(days=90), periods=4, freq='QE') forecasts = {} # Realistic forecast scenarios forecast_scenarios = { 'GDPC1': {'growth': 0.02, 'volatility': 0.01}, # 2% quarterly growth 'INDPRO': {'growth': 0.015, 'volatility': 0.008}, # 1.5% monthly growth 'RSAFS': {'growth': 0.025, 'volatility': 0.012}, # 2.5% monthly growth 'CPIAUCSL': {'growth': 0.006, 'volatility': 0.003}, # 0.6% monthly inflation 'FEDFUNDS': {'growth': 0.0, 'volatility': 0.25}, # Stable policy rate 'DGS10': {'growth': -0.001, 'volatility': 0.15}, # Slight decline 'UNRATE': {'growth': -0.001, 'volatility': 0.1}, # Slight decline 'PAYEMS': {'growth': 0.008, 'volatility': 0.005}, # 0.8% monthly growth 'PCE': {'growth': 0.02, 'volatility': 0.01}, # 2% quarterly growth 'M2SL': {'growth': 0.015, 'volatility': 0.008}, # 1.5% monthly growth 'TCU': {'growth': 0.003, 'volatility': 0.002}, # 0.3% quarterly growth 'DEXUSEU': {'growth': -0.001, 'volatility': 0.02} # Slight decline } for indicator, scenario in forecast_scenarios.items(): base_value = 100 # Normalized base value # Generate forecast values forecast_values = [] confidence_intervals = [] for i in range(4): # Add trend and noise value = base_value * (1 + scenario['growth'] * (i + 1) + np.random.normal(0, scenario['volatility'])) # Generate confidence interval lower = value * (1 - 0.05 - np.random.uniform(0, 0.03)) upper = value * (1 + 0.05 + np.random.uniform(0, 0.03)) forecast_values.append(value) confidence_intervals.append({'lower': lower, 'upper': upper}) forecasts[indicator] = { 'forecast': forecast_values, 'confidence_intervals': pd.DataFrame(confidence_intervals), 'dates': future_dates } return forecasts
def generate_correlation_matrix(): """Generate realistic correlation matrix""" # Define realistic correlations between economic indicators correlations = { 'GDPC1': {'INDPRO': 0.85, 'RSAFS': 0.78, 'CPIAUCSL': 0.45, 'FEDFUNDS': -0.32, 'DGS10': -0.28}, 'INDPRO': {'RSAFS': 0.72, 'CPIAUCSL': 0.38, 'FEDFUNDS': -0.25, 'DGS10': -0.22}, 'RSAFS': {'CPIAUCSL': 0.42, 'FEDFUNDS': -0.28, 'DGS10': -0.25}, 'CPIAUCSL': {'FEDFUNDS': 0.65, 'DGS10': 0.58}, 'FEDFUNDS': {'DGS10': 0.82} } # Create correlation matrix indicators = ['GDPC1', 'INDPRO', 'RSAFS', 'CPIAUCSL', 'FEDFUNDS', 'DGS10', 'UNRATE', 'PAYEMS', 'PCE', 'M2SL', 'TCU', 'DEXUSEU'] corr_matrix = pd.DataFrame(index=indicators, columns=indicators) # Fill diagonal with 1 for indicator in indicators: corr_matrix.loc[indicator, indicator] = 1.0 # Fill with realistic correlations for i, indicator1 in enumerate(indicators): for j, indicator2 in enumerate(indicators): if i != j: if indicator1 in correlations and indicator2 in correlations[indicator1]: corr_matrix.loc[indicator1, indicator2] = correlations[indicator1][indicator2] elif indicator2 in correlations and indicator1 in correlations[indicator2]: corr_matrix.loc[indicator1, indicator2] = correlations[indicator2][indicator1] else: # Generate random correlation between -0.3 and 0.3 corr_matrix.loc[indicator1, indicator2] = np.random.uniform(-0.3, 0.3) return corr_matrix
def get_demo_data(): """Get comprehensive demo data""" return { 'economic_data': generate_economic_data(), 'insights': generate_insights(), 'forecasts': generate_forecast_data(), 'correlation_matrix': generate_correlation_matrix() } frontend/app.py+161-445
frontend/demo_data.pyDeleted-288