metadata
license: mit
Apple Stock Price Forecasting
This repository contains models for forecasting Apple stock prices using ARIMA and LSTM.
Inference Instructions
You can either navigate to the specific model folder and open the provided notebook, or run the inference code directly below.
ARIMA Model Inference
# Install required packages
!pip install --quiet yfinance joblib pmdarima huggingface_hub
# Import Libraries
from huggingface_hub import hf_hub_download
import joblib
import numpy as np
import pandas as pd
import yfinance as yf
HF_TOKEN = "your_own_hf_token"
# Load ARIMA model and Box-Cox transformer
arima_model_path = hf_hub_download(
repo_id="EsferSami/DataSynthis_ML_JobTask",
filename="Apple-Stock-Price-Forecasting-ARIMA-Model/apple_stock_arima.pkl",
token=HF_TOKEN
)
bct_path = hf_hub_download(
repo_id="EsferSami/DataSynthis_ML_JobTask",
filename="Apple-Stock-Price-Forecasting-ARIMA-Model/boxcox_transformer.pkl",
token=HF_TOKEN
)
arima_model = joblib.load(arima_model_path)
bct = joblib.load(bct_path)
# Download recent data
data = yf.download("AAPL", period="3mo", auto_adjust=False)
recent_prices = data['Adj Close'].values.astype(float)
# Transform and forecast
y_trans, _ = bct.transform(recent_prices)
resid_std = np.std(arima_model.resid()) if hasattr(arima_model, "resid") else np.std(y_trans - np.mean(y_trans))
predictions_trans = []
current_series = y_trans.copy()
for day in range(7):
try:
pred = arima_model.predict(n_periods=1)[0]
except Exception:
pred = current_series[-1]
pred = current_series[-1] + np.random.normal(0.0, resid_std*0.3)
predictions_trans.append(pred)
current_series = np.append(current_series, pred)
predictions_price, _ = bct.inverse_transform(np.array(predictions_trans))
prediction_dates = pd.date_range(start=data.index[-1] + pd.Timedelta(days=1), periods=7)
arima_results_df = pd.DataFrame({'Date': prediction_dates, 'Predicted_Price': predictions_price})
print("\nARIMA - 7-Day Forecast")
print("="*60)
print(arima_results_df.to_string(index=False))
# Install required packages
!pip install --quiet yfinance joblib tensorflow huggingface_hub scikit-learn
# Import Libraries
from huggingface_hub import hf_hub_download
import tensorflow as tf
import joblib
import numpy as np
import pandas as pd
import yfinance as yf
from sklearn.preprocessing import MinMaxScaler
HF_TOKEN = "your_own_hf_token"
# Load model and scaler
model_path = hf_hub_download(
repo_id="EsferSami/DataSynthis_ML_JobTask",
filename="Apple-Stock-Price-Forecasting-LSTM-Model/apple_stock_lstm.h5",
token=HF_TOKEN
)
scaler_path = hf_hub_download(
repo_id="EsferSami/DataSynthis_ML_JobTask",
filename="Apple-Stock-Price-Forecasting-LSTM-Model/scaler.joblib",
token=HF_TOKEN
)
model = tf.keras.models.load_model(model_path)
scaler = joblib.load(scaler_path)
# Download recent data
data = yf.download("AAPL", period="3mo", auto_adjust=False)
recent_prices = data['Adj Close'].values.astype(float)
# Prepare input
last_60_days = recent_prices[-60:].reshape(-1, 1)
last_60_scaled = scaler.transform(last_60_days)
predictions = []
current_seq = last_60_scaled.copy()
last_price = last_60_days[-1][0]
MAX_DAILY_CHANGE = 0.02
for day in range(7):
input_data = current_seq.reshape(1, 60, 1)
pred_scaled = model.predict(input_data, verbose=0)
pred_price_raw = scaler.inverse_transform(pred_scaled)[0][0]
change = pred_price_raw - last_price
change = np.clip(change, -MAX_DAILY_CHANGE*last_price, MAX_DAILY_CHANGE*last_price)
anchored_price = last_price + change
predictions.append(anchored_price)
pred_scaled_reshaped = scaler.transform(np.array([[anchored_price]]))
current_seq = np.append(current_seq[1:], pred_scaled_reshaped, axis=0)
last_price = anchored_price
prediction_dates = pd.date_range(start=data.index[-1] + pd.Timedelta(days=1), periods=7)
results_df = pd.DataFrame({'Date': prediction_dates, 'Predicted_Price': np.round(predictions, 2)})
print("\nLSTM - 7-Day Forecast")
print("="*50)
print(results_df.to_string(index=False))