Update README.md
Browse files
README.md
CHANGED
|
@@ -6,129 +6,42 @@ license: mit
|
|
| 6 |
|
| 7 |
This repository contains models for forecasting Apple stock prices using ARIMA and LSTM.
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
## Inference Instructions
|
| 10 |
|
| 11 |
-
You can
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
<summary>ARIMA Model Inference</summary>
|
| 17 |
-
|
| 18 |
-
```python
|
| 19 |
-
# Install required packages
|
| 20 |
-
!pip install --quiet yfinance joblib pmdarima huggingface_hub
|
| 21 |
-
|
| 22 |
-
# Import Libraries
|
| 23 |
-
from huggingface_hub import hf_hub_download
|
| 24 |
-
import joblib
|
| 25 |
-
import numpy as np
|
| 26 |
-
import pandas as pd
|
| 27 |
-
import yfinance as yf
|
| 28 |
-
|
| 29 |
-
HF_TOKEN = "your_own_hf_token"
|
| 30 |
-
|
| 31 |
-
# Load ARIMA model and Box-Cox transformer
|
| 32 |
-
arima_model_path = hf_hub_download(
|
| 33 |
-
repo_id="EsferSami/DataSynthis_ML_JobTask",
|
| 34 |
-
filename="Apple-Stock-Price-Forecasting-ARIMA-Model/apple_stock_arima.pkl",
|
| 35 |
-
token=HF_TOKEN
|
| 36 |
-
)
|
| 37 |
-
bct_path = hf_hub_download(
|
| 38 |
-
repo_id="EsferSami/DataSynthis_ML_JobTask",
|
| 39 |
-
filename="Apple-Stock-Price-Forecasting-ARIMA-Model/boxcox_transformer.pkl",
|
| 40 |
-
token=HF_TOKEN
|
| 41 |
-
)
|
| 42 |
-
|
| 43 |
-
arima_model = joblib.load(arima_model_path)
|
| 44 |
-
bct = joblib.load(bct_path)
|
| 45 |
-
|
| 46 |
-
# Download recent data
|
| 47 |
-
data = yf.download("AAPL", period="3mo", auto_adjust=False)
|
| 48 |
-
recent_prices = data['Adj Close'].values.astype(float)
|
| 49 |
-
|
| 50 |
-
# Transform and forecast
|
| 51 |
-
y_trans, _ = bct.transform(recent_prices)
|
| 52 |
-
resid_std = np.std(arima_model.resid()) if hasattr(arima_model, "resid") else np.std(y_trans - np.mean(y_trans))
|
| 53 |
-
|
| 54 |
-
predictions_trans = []
|
| 55 |
-
current_series = y_trans.copy()
|
| 56 |
-
for day in range(7):
|
| 57 |
-
try:
|
| 58 |
-
pred = arima_model.predict(n_periods=1)[0]
|
| 59 |
-
except Exception:
|
| 60 |
-
pred = current_series[-1]
|
| 61 |
-
pred = current_series[-1] + np.random.normal(0.0, resid_std*0.3)
|
| 62 |
-
predictions_trans.append(pred)
|
| 63 |
-
current_series = np.append(current_series, pred)
|
| 64 |
-
|
| 65 |
-
predictions_price, _ = bct.inverse_transform(np.array(predictions_trans))
|
| 66 |
-
prediction_dates = pd.date_range(start=data.index[-1] + pd.Timedelta(days=1), periods=7)
|
| 67 |
-
arima_results_df = pd.DataFrame({'Date': prediction_dates, 'Predicted_Price': predictions_price})
|
| 68 |
-
|
| 69 |
-
print("\nARIMA - 7-Day Forecast")
|
| 70 |
-
print("="*60)
|
| 71 |
-
print(arima_results_df.to_string(index=False))
|
| 72 |
-
|
| 73 |
-
# Install required packages
|
| 74 |
-
!pip install --quiet yfinance joblib tensorflow huggingface_hub scikit-learn
|
| 75 |
-
|
| 76 |
-
# Import Libraries
|
| 77 |
-
from huggingface_hub import hf_hub_download
|
| 78 |
-
import tensorflow as tf
|
| 79 |
-
import joblib
|
| 80 |
-
import numpy as np
|
| 81 |
-
import pandas as pd
|
| 82 |
-
import yfinance as yf
|
| 83 |
-
from sklearn.preprocessing import MinMaxScaler
|
| 84 |
-
|
| 85 |
-
HF_TOKEN = "your_own_hf_token"
|
| 86 |
-
|
| 87 |
-
# Load model and scaler
|
| 88 |
-
model_path = hf_hub_download(
|
| 89 |
-
repo_id="EsferSami/DataSynthis_ML_JobTask",
|
| 90 |
-
filename="Apple-Stock-Price-Forecasting-LSTM-Model/apple_stock_lstm.h5",
|
| 91 |
-
token=HF_TOKEN
|
| 92 |
-
)
|
| 93 |
-
scaler_path = hf_hub_download(
|
| 94 |
-
repo_id="EsferSami/DataSynthis_ML_JobTask",
|
| 95 |
-
filename="Apple-Stock-Price-Forecasting-LSTM-Model/scaler.joblib",
|
| 96 |
-
token=HF_TOKEN
|
| 97 |
-
)
|
| 98 |
-
|
| 99 |
-
model = tf.keras.models.load_model(model_path)
|
| 100 |
-
scaler = joblib.load(scaler_path)
|
| 101 |
-
|
| 102 |
-
# Download recent data
|
| 103 |
-
data = yf.download("AAPL", period="3mo", auto_adjust=False)
|
| 104 |
-
recent_prices = data['Adj Close'].values.astype(float)
|
| 105 |
-
|
| 106 |
-
# Prepare input
|
| 107 |
-
last_60_days = recent_prices[-60:].reshape(-1, 1)
|
| 108 |
-
last_60_scaled = scaler.transform(last_60_days)
|
| 109 |
-
|
| 110 |
-
predictions = []
|
| 111 |
-
current_seq = last_60_scaled.copy()
|
| 112 |
-
last_price = last_60_days[-1][0]
|
| 113 |
-
MAX_DAILY_CHANGE = 0.02
|
| 114 |
-
|
| 115 |
-
for day in range(7):
|
| 116 |
-
input_data = current_seq.reshape(1, 60, 1)
|
| 117 |
-
pred_scaled = model.predict(input_data, verbose=0)
|
| 118 |
-
pred_price_raw = scaler.inverse_transform(pred_scaled)[0][0]
|
| 119 |
-
|
| 120 |
-
change = pred_price_raw - last_price
|
| 121 |
-
change = np.clip(change, -MAX_DAILY_CHANGE*last_price, MAX_DAILY_CHANGE*last_price)
|
| 122 |
-
anchored_price = last_price + change
|
| 123 |
-
predictions.append(anchored_price)
|
| 124 |
-
|
| 125 |
-
pred_scaled_reshaped = scaler.transform(np.array([[anchored_price]]))
|
| 126 |
-
current_seq = np.append(current_seq[1:], pred_scaled_reshaped, axis=0)
|
| 127 |
-
last_price = anchored_price
|
| 128 |
-
|
| 129 |
-
prediction_dates = pd.date_range(start=data.index[-1] + pd.Timedelta(days=1), periods=7)
|
| 130 |
-
results_df = pd.DataFrame({'Date': prediction_dates, 'Predicted_Price': np.round(predictions, 2)})
|
| 131 |
-
|
| 132 |
-
print("\nLSTM - 7-Day Forecast")
|
| 133 |
-
print("="*50)
|
| 134 |
-
print(results_df.to_string(index=False))
|
|
|
|
| 6 |
|
| 7 |
This repository contains models for forecasting Apple stock prices using ARIMA and LSTM.
|
| 8 |
|
| 9 |
+
## Overview
|
| 10 |
+
|
| 11 |
+
This project provides pre-trained models for predicting Apple (AAPL) stock prices:
|
| 12 |
+
|
| 13 |
+
- **ARIMA Model** – Classical time series forecasting using ARIMA with Box-Cox transformation.
|
| 14 |
+
- **LSTM Model** – Deep learning based forecasting using a trained LSTM network with a scaler.
|
| 15 |
+
|
| 16 |
+
Both models use the last 3 months of stock data to generate a 7-day forecast.
|
| 17 |
+
|
| 18 |
## Inference Instructions
|
| 19 |
|
| 20 |
+
You can perform inference in one of two ways:
|
| 21 |
|
| 22 |
+
1. **Run the provided inference notebooks**
|
| 23 |
+
|
| 24 |
+
Each model folder contains a ready-to-run notebook along with the pre-trained model files:
|
| 25 |
+
|
| 26 |
+
- **ARIMA Model**:
|
| 27 |
+
Folder: `Apple-Stock-Price-Forecasting-ARIMA-Model`
|
| 28 |
+
Notebook: `inference.ipynb` (includes loading the ARIMA model and Box-Cox transformer, downloading recent AAPL data, and generating a 7-day forecast)
|
| 29 |
+
|
| 30 |
+
- **LSTM Model**:
|
| 31 |
+
Folder: `Apple-Stock-Price-Forecasting-LSTM-Model`
|
| 32 |
+
Notebook: `inference.ipynb` (includes loading the LSTM model and scaler, preparing the last 60 days of data, and generating a 7-day forecast)
|
| 33 |
+
|
| 34 |
+
2. **Use the code from the notebooks directly in your Python environment**
|
| 35 |
+
|
| 36 |
+
Each notebook contains **fully commented code** showing how to:
|
| 37 |
+
- Download recent stock data (`yfinance`)
|
| 38 |
+
- Load the pre-trained model from Hugging Face Hub
|
| 39 |
+
- Preprocess data (Box-Cox for ARIMA, scaling for LSTM)
|
| 40 |
+
- Run 7-day predictions
|
| 41 |
+
- Generate a results table with forecasted prices
|
| 42 |
+
|
| 43 |
+
> Note: If you want to directly run inference without notebooks, you can copy the code from the `inference.ipynb` files in each model folder. The notebooks also include instructions for installing required packages and setting your Hugging Face token.
|
| 44 |
+
|
| 45 |
+
## License
|
| 46 |
|
| 47 |
+
This project is licensed under the MIT License.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|