dibend commited on
Commit
41c3b0f
1 Parent(s): 5dada9c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -0
app.py CHANGED
@@ -52,6 +52,14 @@ def plot_and_predict(zip, start_year, start_month, prediction_months):
52
  future_months = np.array([last_month_index + i for i in range(1, prediction_months + 1)]).reshape(-1, 1)
53
  predicted_prices = model.predict(future_months)
54
 
 
 
 
 
 
 
 
 
55
  # Prepare data for plotting
56
  historical_prices_trace = go.Scatter(
57
  x=df['Date'],
@@ -67,6 +75,12 @@ def plot_and_predict(zip, start_year, start_month, prediction_months):
67
  name="Predicted Prices"
68
  )
69
 
 
 
 
 
 
 
70
  # Plot data
71
  fig = go.Figure()
72
  fig.add_trace(historical_prices_trace)
 
52
  future_months = np.array([last_month_index + i for i in range(1, prediction_months + 1)]).reshape(-1, 1)
53
  predicted_prices = model.predict(future_months)
54
 
55
+ # Calculate standard error and prediction intervals
56
+ se = np.sqrt(np.sum((model.predict(X) - y) ** 2) / (len(y) - 2))
57
+ t = stats.t.ppf(0.975, len(y) - 2) # 95% prediction interval
58
+ prediction_interval = t * se * np.sqrt(1 + 1/len(y) + (future_months - np.mean(X))**2 / np.sum((X - np.mean(X))**2))
59
+
60
+ upper_bound = predicted_prices + prediction_interval
61
+ lower_bound = predicted_prices - prediction_interval
62
+
63
  # Prepare data for plotting
64
  historical_prices_trace = go.Scatter(
65
  x=df['Date'],
 
75
  name="Predicted Prices"
76
  )
77
 
78
+ # Plot prediction intervals
79
+ fig.add_traces([
80
+ go.Scatter(x=future_dates, y=upper_bound.flatten(), mode='lines', name='Upper Bound', line=dict(width=0)),
81
+ go.Scatter(x=future_dates, y=lower_bound.flatten(), mode='lines', name='Lower Bound', line=dict(width=0), fill='tonexty')
82
+ ])
83
+
84
  # Plot data
85
  fig = go.Figure()
86
  fig.add_trace(historical_prices_trace)