{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "id": "WB9guq4AguTq" }, "outputs": [], "source": [ "\n", "# Install required packages\n", "\n", "!pip install --quiet yfinance joblib pmdarima huggingface_hub\n", "\n", "# Import Libraries\n", "\n", "from huggingface_hub import hf_hub_download\n", "import joblib\n", "import numpy as np\n", "import pandas as pd\n", "import yfinance as yf\n", "\n", "# Hugging Face Token\n", "\n", "HF_TOKEN = \"your_own_hf_token\" # Replace with your token\n", "\n", "\n", "# Load ARIMA Model & Box-Cox Transformer\n", "\n", "arima_model_path = hf_hub_download(\n", " repo_id=\"EsferSami/DataSynthis_ML_JobTask\",\n", " filename=\"Apple-Stock-Price-Forecasting-ARIMA-Model/apple_stock_arima.pkl\",\n", " token=HF_TOKEN\n", ")\n", "\n", "bct_path = hf_hub_download(\n", " repo_id=\"EsferSami/DataSynthis_ML_JobTask\",\n", " filename=\"Apple-Stock-Price-Forecasting-ARIMA-Model/boxcox_transformer.pkl\",\n", " token=HF_TOKEN\n", ")\n", "\n", "arima_model = joblib.load(arima_model_path)\n", "bct = joblib.load(bct_path)\n", "\n", "print(\"ARIMA model loaded from Hugging Face Hub!\")\n", "\n", "\n", "# Download Recent AAPL Data\n", "\n", "data = yf.download(\"AAPL\", period=\"3mo\", auto_adjust=False)\n", "recent_prices = data['Adj Close'].values.astype(float)\n", "\n", "\n", "\n", "# Transform Data using Box-Cox\n", "\n", "y_trans, _ = bct.transform(recent_prices)\n", "\n", "\n", "# Compute residuals standard deviation for small fluctuations\n", "\n", "try:\n", " residuals = arima_model.resid()\n", " resid_std = np.std(residuals)\n", "except Exception:\n", " resid_std = np.std(y_trans - np.mean(y_trans)) # fallback\n", "print(f\"Residual std used for fluctuation: {resid_std:.4f}\")\n", "\n", "\n", "# 7-Day Forecast with Small Fluctuations\n", "\n", "predictions_trans = []\n", "current_series = y_trans.copy()\n", "\n", "for day in range(7):\n", " # Predict next step using ARIMA\n", " try:\n", " pred = arima_model.predict(n_periods=1)[0]\n", " except Exception:\n", " pred = current_series[-1]\n", "\n", " # Anchor to last actual price in Box-Cox space\n", " last_price_bc = current_series[-1]\n", "\n", " # Add small random fluctuation (±30% of residual std)\n", " pred = last_price_bc + np.random.normal(loc=0.0, scale=resid_std * 0.3)\n", "\n", " predictions_trans.append(pred)\n", " current_series = np.append(current_series, pred)\n", "\n", "\n", "# Inverse Box-Cox to Price Space\n", "\n", "predictions_price, _ = bct.inverse_transform(np.array(predictions_trans))\n", "\n", "\n", "# Prepare Results Table\n", "\n", "prediction_dates = pd.date_range(start=data.index[-1] + pd.Timedelta(days=1), periods=7)\n", "arima_results_df = pd.DataFrame({\n", " 'Date': prediction_dates,\n", " 'Predicted_Price': predictions_price\n", "})\n", "\n", "print(\"\\nARIMA - 7-Day Forecast\")\n", "print(\"=\"*60)\n", "print(arima_results_df.to_string(index=False))\n" ] } ] }