Spaces:
Sleeping
Sleeping
Commit
·
2f114de
1
Parent(s):
b0d3471
properly defined mape and sape
Browse files
app.py
CHANGED
@@ -65,20 +65,24 @@ def run_prediction():
|
|
65 |
})
|
66 |
|
67 |
# --- Calculate Metrics --- #
|
68 |
-
pred_df["% Error"] = ((pred_df["Predicted Close"] - pred_df["Actual Close"]) / pred_df["Actual Close"]) * 100
|
69 |
|
70 |
-
#
|
|
|
|
|
|
|
|
|
71 |
pred_df["Actual Close"] = pred_df["Actual Close"].round(2)
|
72 |
pred_df["Predicted Close"] = pred_df["Predicted Close"].round(2)
|
73 |
-
|
74 |
-
|
75 |
-
pred_df["% Error"] = pred_df["% Error"].apply(lambda x: f"$ {x:+.2f}")
|
76 |
|
77 |
# Add MAPE Range per row to table
|
78 |
pred_df["±MAPE Range"] = pred_df["Predicted Close"].apply(
|
79 |
lambda x: f"${x * (1 - mape/100):.2f} to ${x * (1 + mape/100):.2f}"
|
80 |
)
|
81 |
|
|
|
82 |
# --- Next Day Prediction --- #
|
83 |
latest_close = float(data["Close"].iloc[-1])
|
84 |
latest_date = data.index[-1].strftime("%Y-%m-%d")
|
|
|
65 |
})
|
66 |
|
67 |
# --- Calculate Metrics --- #
|
68 |
+
pred_df["% Error Raw"] = ((pred_df["Predicted Close"] - pred_df["Actual Close"]) / pred_df["Actual Close"]) * 100
|
69 |
|
70 |
+
# Compute MAPE and SAPE first
|
71 |
+
mape = np.mean(np.abs(pred_df["% Error Raw"]))
|
72 |
+
sape = np.std(np.abs(pred_df["% Error Raw"]))
|
73 |
+
|
74 |
+
# Format the columns
|
75 |
pred_df["Actual Close"] = pred_df["Actual Close"].round(2)
|
76 |
pred_df["Predicted Close"] = pred_df["Predicted Close"].round(2)
|
77 |
+
pred_df["% Error"] = pred_df["% Error Raw"].apply(lambda x: f"$ {x:+.2f}")
|
78 |
+
pred_df.drop(columns=["% Error Raw"], inplace=True)
|
|
|
79 |
|
80 |
# Add MAPE Range per row to table
|
81 |
pred_df["±MAPE Range"] = pred_df["Predicted Close"].apply(
|
82 |
lambda x: f"${x * (1 - mape/100):.2f} to ${x * (1 + mape/100):.2f}"
|
83 |
)
|
84 |
|
85 |
+
|
86 |
# --- Next Day Prediction --- #
|
87 |
latest_close = float(data["Close"].iloc[-1])
|
88 |
latest_date = data.index[-1].strftime("%Y-%m-%d")
|