Created
March 30, 2026 03:53
-
-
Save niculistana/f714196d9ecd38f065a5df8b04a0371d to your computer and use it in GitHub Desktop.
ARIMA sample code for Notebook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "# Rice Price Prediction - Philippines\n", | |
| "This notebook uses an **ARIMA** model to forecast retail rice prices for the next 6 months." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import pandas as pd\n", | |
| "import matplotlib.pyplot as plt\n", | |
| "from statsmodels.tsa.arima.model import ARIMA\n", | |
| "import io\n", | |
| "\n", | |
| "# 1. Create Mock Data (Replace this with your CSV upload)\n", | |
| "csv_data = '''Date,Price\n", | |
| "2024-01-01,50.20\n", | |
| "2024-02-01,51.50\n", | |
| "2024-03-01,52.80\n", | |
| "2024-04-01,54.10\n", | |
| "2024-05-01,55.00\n", | |
| "2024-06-01,54.50\n", | |
| "2024-07-01,53.80\n", | |
| "2024-08-01,53.20\n", | |
| "2024-09-01,52.90\n", | |
| "2024-10-01,52.50\n", | |
| "2024-11-01,51.80\n", | |
| "2024-12-01,52.20\n", | |
| "2025-01-01,52.80\n", | |
| "2025-02-01,53.50\n", | |
| "2025-03-01,54.20'''\n", | |
| "\n", | |
| "df = pd.read_csv(io.StringIO(csv_data))\n", | |
| "df['Date'] = pd.to_datetime(df['Date'])\n", | |
| "df.set_index('Date', inplace=True)\n", | |
| "df = df.asfreq('MS')\n", | |
| "\n", | |
| "# 2. Build ARIMA Model (p=1, d=1, q=1)\n", | |
| "model = ARIMA(df['Price'], order=(1, 1, 1))\n", | |
| "model_fit = model.fit()\n", | |
| "\n", | |
| "# 3. Forecast\n", | |
| "forecast_steps = 6\n", | |
| "forecast_res = model_fit.get_forecast(steps=forecast_steps)\n", | |
| "forecast_df = forecast_res.summary_frame()\n", | |
| "\n", | |
| "# 4. Plot\n", | |
| "plt.figure(figsize=(12, 6))\n", | |
| "plt.plot(df.index, df['Price'], label='Actual Price', marker='o')\n", | |
| "plt.plot(forecast_df.index, forecast_df['mean'], label='6-Month Forecast', color='red', linestyle='--')\n", | |
| "plt.fill_between(forecast_df.index, forecast_df['mean_ci_lower'], forecast_df['mean_ci_upper'], color='pink', alpha=0.3)\n", | |
| "plt.title('Rice Price Forecast (PHP/kg)')\n", | |
| "plt.xlabel('Date')\n", | |
| "plt.ylabel('Price')\n", | |
| "plt.legend()\n", | |
| "plt.grid(True)\n", | |
| "plt.show()\n", | |
| "\n", | |
| "print(\"Forecasted Prices for next 6 months:\")\n", | |
| "print(forecast_df['mean'])\n" | |
| ] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 3", | |
| "language": "python", | |
| "name": "python3" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 3 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_light_renderer": "inkpot", | |
| "version": "3.10.0" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 4 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment