Spaces:
Running
Running
File size: 2,225 Bytes
be39557 eb34a96 f92cf9d eb34a96 f92cf9d eb34a96 f92cf9d eb34a96 | 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 | # utils/visualization.py
import plotly.graph_objects as go
from statsmodels.tsa.seasonal import seasonal_decompose
import pandas as pd
def plot_decomposition(data):
# If data has MultiIndex columns, select the first level
if isinstance(data.columns, pd.MultiIndex):
data.columns = data.columns.get_level_values(0)
decomposition = seasonal_decompose(data['Close'], period=30)
fig = go.Figure()
fig.add_trace(go.Scatter(
x=data.index,
y=decomposition.trend,
name="Trend"
))
fig.add_trace(go.Scatter(
x=data.index,
y=decomposition.seasonal,
name="Seasonal"
))
fig.add_trace(go.Scatter(
x=data.index,
y=decomposition.resid,
name="Residual"
))
fig.update_layout(
title="Time Series Decomposition",
xaxis_title="Date",
yaxis_title="Price",
height=800
)
return fig
def plot_forecast(historical_data, forecast, model_name):
# If historical_data has MultiIndex columns, select the first level
if isinstance(historical_data.columns, pd.MultiIndex):
historical_data.columns = historical_data.columns.get_level_values(0)
fig = go.Figure()
# Plot historical data
fig.add_trace(go.Scatter(
x=historical_data.index,
y=historical_data['Close'],
name="Historical"
))
# Plot forecast
fig.add_trace(go.Scatter(
x=forecast['ds'],
y=forecast['yhat'],
name="Forecast",
line=dict(dash='dash')
))
# Add confidence intervals
fig.add_trace(go.Scatter(
x=forecast['ds'],
y=forecast['yhat_upper'],
fill=None,
mode='lines',
line_color='rgba(0,100,80,0.2)',
name='Upper Bound'
))
fig.add_trace(go.Scatter(
x=forecast['ds'],
y=forecast['yhat_lower'],
fill='tonexty',
mode='lines',
line_color='rgba(0,100,80,0.2)',
name='Lower Bound'
))
fig.update_layout(
title=f"{model_name} Forecast",
xaxis_title="Date",
yaxis_title="Price",
height=600
)
return fig |