Spaces:
Sleeping
Sleeping
Update stock_analysis.py
Browse files- stock_analysis.py +16 -6
stock_analysis.py
CHANGED
|
@@ -20,14 +20,12 @@ def forecast_series(series, model="ARIMA", forecast_period=FORECAST_PERIOD):
|
|
| 20 |
|
| 21 |
# Get confidence intervals
|
| 22 |
conf_int = model_fit.get_forecast(steps=forecast_period).conf_int()
|
| 23 |
-
lower_ci = conf_int.iloc[:, 0] if isinstance(conf_int, pd.DataFrame) else conf_int[:, 0]
|
| 24 |
-
upper_ci = conf_int.iloc[:, 1] if isinstance(conf_int, pd.DataFrame) else conf_int[:, 1]
|
| 25 |
|
| 26 |
# Ensure all arrays have the same length
|
| 27 |
-
min_length = min(len(forecast),
|
| 28 |
predictions = forecast[:min_length]
|
| 29 |
-
lower_ci =
|
| 30 |
-
upper_ci =
|
| 31 |
|
| 32 |
elif model == "Prophet":
|
| 33 |
# Implement Prophet forecasting method
|
|
@@ -38,6 +36,12 @@ def forecast_series(series, model="ARIMA", forecast_period=FORECAST_PERIOD):
|
|
| 38 |
else:
|
| 39 |
raise ValueError(f"Unsupported model: {model}")
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
return predictions, pd.DataFrame({'Lower_CI': lower_ci, 'Upper_CI': upper_ci})
|
| 42 |
|
| 43 |
def get_stock_graph_and_info(idx, stock, interval, graph_type, forecast_method, start_date, end_date):
|
|
@@ -53,7 +57,13 @@ def get_stock_graph_and_info(idx, stock, interval, graph_type, forecast_method,
|
|
| 53 |
|
| 54 |
last_date = pd.to_datetime(series['Date'].values[-1])
|
| 55 |
forecast_dates = pd.date_range(start=last_date + timedelta(days=1), periods=len(predictions))
|
| 56 |
-
forecast_dates = [date for date in forecast_dates if is_business_day(date)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
forecast = pd.DataFrame({
|
| 59 |
"Date": forecast_dates,
|
|
|
|
| 20 |
|
| 21 |
# Get confidence intervals
|
| 22 |
conf_int = model_fit.get_forecast(steps=forecast_period).conf_int()
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# Ensure all arrays have the same length
|
| 25 |
+
min_length = min(len(forecast), conf_int.shape[0])
|
| 26 |
predictions = forecast[:min_length]
|
| 27 |
+
lower_ci = conf_int.iloc[:min_length, 0] if isinstance(conf_int, pd.DataFrame) else conf_int[:min_length, 0]
|
| 28 |
+
upper_ci = conf_int.iloc[:min_length, 1] if isinstance(conf_int, pd.DataFrame) else conf_int[:min_length, 1]
|
| 29 |
|
| 30 |
elif model == "Prophet":
|
| 31 |
# Implement Prophet forecasting method
|
|
|
|
| 36 |
else:
|
| 37 |
raise ValueError(f"Unsupported model: {model}")
|
| 38 |
|
| 39 |
+
# Ensure all arrays are of the same length
|
| 40 |
+
min_length = min(len(predictions), len(lower_ci), len(upper_ci))
|
| 41 |
+
predictions = predictions[:min_length]
|
| 42 |
+
lower_ci = lower_ci[:min_length]
|
| 43 |
+
upper_ci = upper_ci[:min_length]
|
| 44 |
+
|
| 45 |
return predictions, pd.DataFrame({'Lower_CI': lower_ci, 'Upper_CI': upper_ci})
|
| 46 |
|
| 47 |
def get_stock_graph_and_info(idx, stock, interval, graph_type, forecast_method, start_date, end_date):
|
|
|
|
| 57 |
|
| 58 |
last_date = pd.to_datetime(series['Date'].values[-1])
|
| 59 |
forecast_dates = pd.date_range(start=last_date + timedelta(days=1), periods=len(predictions))
|
| 60 |
+
forecast_dates = [date for date in forecast_dates if is_business_day(date)]
|
| 61 |
+
|
| 62 |
+
# Ensure all data has the same length
|
| 63 |
+
min_length = min(len(predictions), len(forecast_dates), len(confidence_intervals))
|
| 64 |
+
predictions = predictions[:min_length]
|
| 65 |
+
forecast_dates = forecast_dates[:min_length]
|
| 66 |
+
confidence_intervals = confidence_intervals.iloc[:min_length]
|
| 67 |
|
| 68 |
forecast = pd.DataFrame({
|
| 69 |
"Date": forecast_dates,
|