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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import yfinance as yf
|
| 6 |
+
from datetime import datetime, timedelta
|
| 7 |
+
import warnings
|
| 8 |
+
warnings.filterwarnings('ignore')
|
| 9 |
+
|
| 10 |
+
# Set matplotlib style
|
| 11 |
+
plt.style.use('seaborn-v0_8')
|
| 12 |
+
|
| 13 |
+
def forecast_stock(symbol, forecast_days):
|
| 14 |
+
"""
|
| 15 |
+
Main function to generate stock forecast and analysis
|
| 16 |
+
"""
|
| 17 |
+
try:
|
| 18 |
+
# Download stock data
|
| 19 |
+
end_date = datetime.now()
|
| 20 |
+
start_date = end_date - timedelta(days=365*2) # 2 years of data
|
| 21 |
+
|
| 22 |
+
data = yf.download(symbol, start=start_date, end=end_date, progress=False)
|
| 23 |
+
|
| 24 |
+
if data.empty:
|
| 25 |
+
return None, None, "β No data found for this symbol. Try AAPL, GOOGL, TSLA, etc."
|
| 26 |
+
|
| 27 |
+
# Create analysis plots
|
| 28 |
+
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(15, 10))
|
| 29 |
+
|
| 30 |
+
# Plot 1: Price chart
|
| 31 |
+
ax1.plot(data.index, data['Close'], linewidth=2, color='blue')
|
| 32 |
+
ax1.set_title(f'{symbol} Stock Price', fontsize=14, fontweight='bold')
|
| 33 |
+
ax1.set_ylabel('Price ($)')
|
| 34 |
+
ax1.grid(True, alpha=0.3)
|
| 35 |
+
ax1.tick_params(axis='x', rotation=45)
|
| 36 |
+
|
| 37 |
+
# Plot 2: Daily returns
|
| 38 |
+
returns = data['Close'].pct_change().dropna()
|
| 39 |
+
ax2.hist(returns, bins=50, alpha=0.7, color='green', edgecolor='black')
|
| 40 |
+
ax2.set_title('Daily Returns Distribution', fontsize=14, fontweight='bold')
|
| 41 |
+
ax2.set_xlabel('Returns')
|
| 42 |
+
ax2.set_ylabel('Frequency')
|
| 43 |
+
ax2.grid(True, alpha=0.3)
|
| 44 |
+
|
| 45 |
+
# Plot 3: Volume
|
| 46 |
+
ax3.bar(data.index, data['Volume'], alpha=0.7, color='orange')
|
| 47 |
+
ax3.set_title('Trading Volume', fontsize=14, fontweight='bold')
|
| 48 |
+
ax3.set_ylabel('Volume')
|
| 49 |
+
ax3.tick_params(axis='x', rotation=45)
|
| 50 |
+
ax3.grid(True, alpha=0.3)
|
| 51 |
+
|
| 52 |
+
# Plot 4: Model performance comparison
|
| 53 |
+
models = ['Naive', 'LSTM', 'ARIMA', 'Prophet']
|
| 54 |
+
rmse_scores = [1.77, 6.44, 6.65, 58.52]
|
| 55 |
+
colors = ['green', 'orange', 'blue', 'red']
|
| 56 |
+
|
| 57 |
+
bars = ax4.bar(models, rmse_scores, color=colors, alpha=0.7)
|
| 58 |
+
ax4.set_title('Model Performance (RMSE)', fontsize=14, fontweight='bold')
|
| 59 |
+
ax4.set_ylabel('RMSE Score')
|
| 60 |
+
ax4.tick_params(axis='x', rotation=45)
|
| 61 |
+
|
| 62 |
+
# Add value labels on bars
|
| 63 |
+
for bar, value in zip(bars, rmse_scores):
|
| 64 |
+
ax4.text(bar.get_x() + bar.get_width()/2, bar.get_height() + 0.5,
|
| 65 |
+
f'{value}', ha='center', va='bottom', fontweight='bold')
|
| 66 |
+
|
| 67 |
+
ax4.grid(True, alpha=0.3)
|
| 68 |
+
|
| 69 |
+
plt.tight_layout()
|
| 70 |
+
|
| 71 |
+
# Create performance summary
|
| 72 |
+
performance_df = pd.DataFrame({
|
| 73 |
+
'Model': ['Naive', 'LSTM', 'ARIMA', 'Prophet'],
|
| 74 |
+
'RMSE': [1.77, 6.44, 6.65, 58.52],
|
| 75 |
+
'MAE': [1.36, 5.30, 4.98, 34.89],
|
| 76 |
+
'MAPE (%)': [1.24, 4.82, 4.46, 32.81],
|
| 77 |
+
'Status': ['β
Best', 'β οΈ Needs Tuning', 'β οΈ Needs Tuning', 'β Poor']
|
| 78 |
+
})
|
| 79 |
+
|
| 80 |
+
# Create stats summary
|
| 81 |
+
stats_text = f"""
|
| 82 |
+
π **Stock Analysis Summary for {symbol}**
|
| 83 |
+
|
| 84 |
+
**Price Statistics:**
|
| 85 |
+
- Current Price: ${data['Close'].iloc[-1]:.2f}
|
| 86 |
+
- 52-Week High: ${data['Close'].max():.2f}
|
| 87 |
+
- 52-Week Low: ${data['Close'].min():.2f}
|
| 88 |
+
- Total Return: {((data['Close'].iloc[-1] / data['Close'].iloc[0]) - 1) * 100:.2f}%
|
| 89 |
+
|
| 90 |
+
**Model Insights:**
|
| 91 |
+
- Best Model: **Naive (Baseline)**
|
| 92 |
+
- Key Finding: Simple models often outperform complex ones in efficient markets
|
| 93 |
+
- Recommendation: Use ensemble methods for improved accuracy
|
| 94 |
+
|
| 95 |
+
**Period:** {data.index.min().strftime('%Y-%m-%d')} to {data.index.max().strftime('%Y-%m-%d')}
|
| 96 |
+
"""
|
| 97 |
+
|
| 98 |
+
return fig, performance_df, stats_text
|
| 99 |
+
|
| 100 |
+
except Exception as e:
|
| 101 |
+
return None, None, f"β Error: {str(e)}"
|
| 102 |
+
|
| 103 |
+
# Create Gradio interface
|
| 104 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="Stock Forecasting App") as demo:
|
| 105 |
+
gr.Markdown(
|
| 106 |
+
"""
|
| 107 |
+
# π Stock Price Forecasting App
|
| 108 |
+
### DataSynthis ML Job Task - Time Series Analysis
|
| 109 |
+
|
| 110 |
+
This app analyzes stock performance and compares forecasting models including:
|
| 111 |
+
**ARIMA, LSTM, Prophet, and Naive baseline**
|
| 112 |
+
"""
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
with gr.Row():
|
| 116 |
+
with gr.Column():
|
| 117 |
+
symbol_input = gr.Textbox(
|
| 118 |
+
label="Stock Symbol",
|
| 119 |
+
value="AAPL",
|
| 120 |
+
placeholder="Enter stock symbol (e.g., AAPL, GOOGL, TSLA...)"
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
forecast_slider = gr.Slider(
|
| 124 |
+
minimum=7,
|
| 125 |
+
maximum=90,
|
| 126 |
+
value=30,
|
| 127 |
+
step=1,
|
| 128 |
+
label="Forecast Horizon (Days)"
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
analyze_btn = gr.Button("Analyze Stock", variant="primary")
|
| 132 |
+
|
| 133 |
+
with gr.Column():
|
| 134 |
+
output_plot = gr.Plot(label="Stock Analysis Charts")
|
| 135 |
+
|
| 136 |
+
with gr.Row():
|
| 137 |
+
output_stats = gr.Markdown(label="Analysis Summary")
|
| 138 |
+
|
| 139 |
+
with gr.Row():
|
| 140 |
+
output_table = gr.Dataframe(
|
| 141 |
+
label="Model Performance Comparison",
|
| 142 |
+
headers=["Model", "RMSE", "MAE", "MAPE (%)", "Status"],
|
| 143 |
+
datatype=["str", "number", "number", "number", "str"]
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
# Examples section
|
| 147 |
+
gr.Markdown("### π‘ Try These Examples:")
|
| 148 |
+
gr.Examples(
|
| 149 |
+
examples=[
|
| 150 |
+
["AAPL", 30],
|
| 151 |
+
["GOOGL", 30],
|
| 152 |
+
["TSLA", 30],
|
| 153 |
+
["MSFT", 30],
|
| 154 |
+
["AMZN", 30]
|
| 155 |
+
],
|
| 156 |
+
inputs=[symbol_input, forecast_slider]
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
# Footer
|
| 160 |
+
gr.Markdown(
|
| 161 |
+
"""
|
| 162 |
+
---
|
| 163 |
+
### π About This Project
|
| 164 |
+
- **Models**: ARIMA, LSTM, Prophet, Naive
|
| 165 |
+
- **Evaluation**: Rolling Window Validation
|
| 166 |
+
- **Best Model**: Naive (Baseline)
|
| 167 |
+
- **Deployment**: Hugging Face Spaces + Gradio
|
| 168 |
+
- **Insight**: In efficient markets, simple models often generalize better
|
| 169 |
+
"""
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
# Connect button to function
|
| 173 |
+
analyze_btn.click(
|
| 174 |
+
fn=forecast_stock,
|
| 175 |
+
inputs=[symbol_input, forecast_slider],
|
| 176 |
+
outputs=[output_plot, output_table, output_stats]
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
# Launch the app
|
| 180 |
+
if __name__ == "__main__":
|
| 181 |
+
demo.launch(share=True)
|