File size: 4,109 Bytes
01513bb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
{
"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"
]
}
]
} |