PK [N example_simple_fit.ipynb{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Simple auto_arima model\n\n\n\nThis is a simple example of how we can fit an ARIMA model in several lines\nwithout knowing anything about our data or optimal hyper parameters.\n\n.. raw:: html\n\n
\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print(__doc__)\n\n# Author: Taylor Smith \n\nimport pmdarima as pm\nimport numpy as np\nfrom matplotlib import pyplot as plt\n\n# #############################################################################\n# Load the data and split it into separate pieces\ndata = pm.datasets.load_wineind()\ntrain, test = data[:150], data[150:]\n\n# Fit a simple auto_arima model\narima = pm.auto_arima(train, error_action='ignore', trace=1,\n seasonal=True, m=12)\n\n# #############################################################################\n# Plot actual test vs. forecasts:\nx = np.arange(test.shape[0])\nplt.scatter(x, test, marker='x')\nplt.plot(x, arima.predict(n_periods=test.shape[0]))\nplt.title('Actual test samples vs. forecasts')\nplt.show()"
]
}
],
"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_lexer": "ipython3",
"version": "3.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 0
}PK cN example_pipeline.ipynb{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Pipelines with auto_arima\n\n\n\nLike scikit-learn, ``pmdarima`` can fit \"pipeline\" models. That is, a pipeline\nconstitutes a list of arbitrary length comprised of any number of\n``BaseTransformer`` objects strung together ordinally, and finished with an\n``AutoARIMA`` object.\n\nThe benefit of a pipeline is the ability to condense a complex sequence of\nstateful transformations into a single object that can call ``fit``,\n``predict`` and ``update``. It can also be serialized into *one* pickle file,\nwhich greatly simplifies your life.\n\n.. raw:: html\n\n
\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print(__doc__)\n\n# Author: Taylor Smith \n\nimport numpy as np\nimport pmdarima as pm\nfrom pmdarima import pipeline, preprocessing as ppc, arima\nfrom matplotlib import pyplot as plt\n\n# Load the data and split it into separate pieces\ndata = pm.datasets.load_wineind()\ntrain, test = data[:150], data[150:]\n\n# Let's create a pipeline with multiple stages... the Wineind dataset is\n# seasonal, so we'll include a FourierFeaturizer so we can fit it without\n# seasonality\npipe = pipeline.Pipeline([\n (\"fourier\", ppc.FourierFeaturizer(m=12)),\n (\"arima\", arima.AutoARIMA(stepwise=True, trace=1, error_action=\"ignore\",\n seasonal=False, # because we use Fourier\n transparams=False,\n suppress_warnings=True))\n])\n\npipe.fit(train)\nprint(\"Model fit:\")\nprint(pipe)\n\n# We can compute predictions the same way we would on a normal ARIMA object:\npreds, conf_int = pipe.predict(n_periods=10, return_conf_int=True)\nprint(\"\\nForecasts:\")\nprint(preds)\n\n# Let's take a look at the actual vs. the predicted values:\nfig, axes = plt.subplots(2, 1, figsize=(12, 8))\n\nn_train = train.shape[0]\nx = np.arange(n_train + preds.shape[0])\naxes[0].plot(x[:n_train], train, alpha=0.75)\n# axes[0].scatter(x[n_train:], preds, alpha=0.4, marker='o')\naxes[0].scatter(x[n_train:], test[:preds.shape[0]], alpha=0.4, marker='x')\naxes[0].fill_between(x[n_train:], conf_int[:, 0], conf_int[:, 1],\n alpha=0.1, color='b')\naxes[0].set_title('Actual test samples vs. forecasts')\naxes[0].set_xlim((0, data.shape[0]))\n\n# We can also call `update` directly on the pipeline object, which will update\n# the intermittent transformers, where necessary:\nnewly_observed, still_test = test[:15], test[15:]\npipe.update(newly_observed, maxiter=10)\n\n# Calling predict will now predict from newly observed values\nnew_preds = pipe.predict(still_test.shape[0])\nprint(new_preds)\n\nx2 = np.arange(data.shape[0])\nn_trained_on = n_train + newly_observed.shape[0]\n\naxes[1].plot(x2[:n_train], train, alpha=0.75)\naxes[1].plot(x2[n_train: n_trained_on], newly_observed, alpha=0.75, c='orange')\n# axes[1].scatter(x2[n_trained_on:], new_preds, alpha=0.4, marker='o')\naxes[1].scatter(x2[n_trained_on:], still_test, alpha=0.4, marker='x')\naxes[1].set_title('Actual test samples vs. forecasts')\naxes[1].set_xlim((0, data.shape[0]))\n\nplt.show()"
]
}
],
"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_lexer": "ipython3",
"version": "3.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 0
}PK gN/Lu arima/example_auto_arima.ipynb{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Fitting an auto_arima model\n\n\n\nThis example demonstrates how we can use the ``auto_arima`` function to\nselect an optimal time series model. We'll be fitting our model on the lynx\ndataset available in the `datasets` submodule.\n\n.. raw:: html\n\n
\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print(__doc__)\n\n# Author: Taylor Smith \n\nimport pmdarima as pm\nfrom sklearn.metrics import mean_squared_error\nimport matplotlib.pyplot as plt\nimport numpy as np\n\n# #############################################################################\n# Load the data and split it into separate pieces\ndata = pm.datasets.load_lynx()\ntrain, test = data[:90], data[90:]\n\n# Fit a simple auto_arima model\nmodl = pm.auto_arima(train, start_p=1, start_q=1, start_P=1, start_Q=1,\n max_p=5, max_q=5, max_P=5, max_Q=5, seasonal=True,\n stepwise=True, suppress_warnings=True, D=10, max_D=10,\n error_action='ignore')\n\n# Create predictions for the future, evaluate on test\npreds, conf_int = modl.predict(n_periods=test.shape[0], return_conf_int=True)\n\n# Print the error:\nprint(\"Test RMSE: %.3f\" % np.sqrt(mean_squared_error(test, preds)))\n\n# #############################################################################\n# Plot the points and the forecasts\nx_axis = np.arange(train.shape[0] + preds.shape[0])\nx_years = x_axis + 1821 # Year starts at 1821\n\nplt.plot(x_years[x_axis[:train.shape[0]]], train, alpha=0.75)\nplt.plot(x_years[x_axis[train.shape[0]:]], preds, alpha=0.75) # Forecasts\nplt.scatter(x_years[x_axis[train.shape[0]:]], test,\n alpha=0.4, marker='x') # Test data\nplt.fill_between(x_years[x_axis[-preds.shape[0]:]],\n conf_int[:, 0], conf_int[:, 1],\n alpha=0.1, color='b')\nplt.title(\"Lynx forecasts\")\nplt.xlabel(\"Year\")"
]
}
],
"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_lexer": "ipython3",
"version": "3.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 0
}PK gN(
(
# arima/example_add_new_samples.ipynb{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Adding new observations to your model\n\n\n\nThis example demonstrates how to add new ground truth\nobservations to your model so that forecasting continues\nwith respect to true, observed values. This also slightly\nupdates the model parameters, taking several new steps from\nthe existing model parameters.\n\n.. raw:: html\n\n
\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print(__doc__)\n\n# Author: Taylor Smith \n\nimport pmdarima as pm\nimport matplotlib.pyplot as plt\nimport numpy as np\n\n# #############################################################################\n# Load the data and split it into separate pieces\ndata = pm.datasets.load_lynx()\ntrain, test = data[:100], data[100:]\n\n# #############################################################################\n# Fit with some validation (cv) samples\narima = pm.auto_arima(train, start_p=1, start_q=1, d=0, max_p=5, max_q=5,\n out_of_sample_size=10, suppress_warnings=True,\n stepwise=True, error_action='ignore')\n\n# Now plot the results and the forecast for the test set\npreds, conf_int = arima.predict(n_periods=test.shape[0],\n return_conf_int=True)\n\nfig, axes = plt.subplots(2, 1, figsize=(12, 8))\nx_axis = np.arange(train.shape[0] + preds.shape[0])\naxes[0].plot(x_axis[:train.shape[0]], train, alpha=0.75)\naxes[0].scatter(x_axis[train.shape[0]:], preds, alpha=0.4, marker='o')\naxes[0].scatter(x_axis[train.shape[0]:], test, alpha=0.4, marker='x')\naxes[0].fill_between(x_axis[-preds.shape[0]:], conf_int[:, 0], conf_int[:, 1],\n alpha=0.1, color='b')\n\n# fill the section where we \"held out\" samples in our model fit\n\naxes[0].set_title(\"Train samples & forecasted test samples\")\n\n# Now add the actual samples to the model and create NEW forecasts\narima.update(test)\nnew_preds, new_conf_int = arima.predict(n_periods=10, return_conf_int=True)\nnew_x_axis = np.arange(data.shape[0] + 10)\n\naxes[1].plot(new_x_axis[:data.shape[0]], data, alpha=0.75)\naxes[1].scatter(new_x_axis[data.shape[0]:], new_preds, alpha=0.4, marker='o')\naxes[1].fill_between(new_x_axis[-new_preds.shape[0]:],\n new_conf_int[:, 0],\n new_conf_int[:, 1],\n alpha=0.1, color='g')\naxes[1].set_title(\"Added new observed values with new forecasts\")\nplt.show()"
]
}
],
"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_lexer": "ipython3",
"version": "3.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 0
}PK fNܾ &