## ARNet: Nonlinear autoregression with feed-forward neural networks (or any estimator really)
A Python port of [nnetar.R](https://www.rdocumentation.org/packages/forecast/versions/8.23.0/topics/nnetar): nonlinear
autoregression with feed-forward neural networks. Here, we made it a bit more generic to use any kind of estimator
respecting the sklearn's interface, though the default is to use a single layer MLPRegressor from sklearn. Following
nnetar.R, the ordinal (`p`) and seasonal (`P`) lags to look back are automatically chosen if left
unspecified. Furthermore, number of hidden neurons in the default estimator is also chosen automatically following the
heuristic in nnetar.R.
Installation:
```sh
pip install arnet
```
Here are some snippets to illustrate the usage:
### Fit-predict flow
```py
from arnet import ARNet
y_train, y_test = ...
model = ARNet()
model.fit(y_train)
predictions = model.predict(n_steps=y_test.size)
```
Instantaniate the model, fit to data and predict; that's all.
If you have side information, i.e., exogenous regressors to help in prediction, you can supply them like so:
```py
X_train, X_test, y_train, y_test = ...
model = ARNet()
model.fit(X_train, y_train)
predictions = model.predict(X_test)
```
If you have seasonality in the series:
```py
# An automatic `P` (seasonal lags) will be chosen as left unspecified
model = ARNet(seasonality=12)
model.fit(...)
predictons = model.predict(...)
```
Default base model is an [`MLPRegressor`](https://scikit-learn.org/stable/modules/generated/sklearn.neural_network.MLPRegressor.html); if you want to use another one, that's okay too:
```py
from sklearn.ensemble import RandomForestRegressor
model = ARNet(RandomForestRegressor(max_features=0.9))
model.fit(...).predict(...)
```
In fact, if you use [`LinearRegression`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html), you
effectively have the linear AR(p) model as a subset! [Here](https://mustafaaydn.github.io/arnet/index.html#linear-ar-p-as-a-subset) is an example for that.
### Prediction intervals
Following the procedure [here](https://otexts.com/fpp3/nnetar.html#prediction-intervals-5), the model is able to produce prediction intervals for the future given confidence levels:
```py
model = ARNet()
model.fit(y_train)
predictions, intervals = model.predict(n_steps=y_test.size, return_intervals=True, alphas=[80, 95])
```
You can also obtain the simulated paths by issuing `return_paths=True`.
### Validation
There is a `.validate` method to perform time series validation (expanding window) on a parameter grid with either a full search or a randomized one:
```py
X, y = ...
param_grid = {"p": [2, 5, 8, None], "estimator__solver": ["sgd", "adam"]}
n_iter = -1 # -1 (or None) means full grid search; any positive integer would mean a randomized search
search = ARNet.validate(y, param_grid, X=X, n_iter=n_iter)
best_model = search["best_estimator_"]
# Do something with the best model
```
### Plotting
There is also a helper static function for plotting lines -- it might be helpful in visualizing the true values along with the predictions and intervals.
```py
preds_in_sample = model.fitted_values_ # fitted values are available as a post-fit attribute
preds_out_sample, intervals = model.predict(n_steps=y_test.size, return_intervals=True, alphas=[80, 95])
ARNet.plot(lines=[y_train, preds_in_sample, y_test, preds_out_sample],
labels=["y-train", "in-sample preds", "y-test", "out-sample preds"],
true_indexes=[0, 2],
intervals=intervals)
```
Here is an example plot output:
![example plot](tests/figures/example_plot.png)
For examples with a dataset in action, please see [here](https://mustafaaydn.github.io/arnet/index.html); for the API reference, see [here](https://mustafaaydn.github.io/arnet/arnet.html).
Raw data
{
"_id": null,
"home_page": null,
"name": "arnet",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": "time series, autoregression, neural networks, prediction",
"author": "Mustafa Ayd\u0131n",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/e3/02/cbe476d7e2295ad0dedd4ad92539e781b3df5b7567ae25198661b1488bce/arnet-0.0.1.tar.gz",
"platform": null,
"description": "## ARNet: Nonlinear autoregression with feed-forward neural networks (or any estimator really)\r\nA Python port of [nnetar.R](https://www.rdocumentation.org/packages/forecast/versions/8.23.0/topics/nnetar): nonlinear\r\nautoregression with feed-forward neural networks. Here, we made it a bit more generic to use any kind of estimator\r\nrespecting the sklearn's interface, though the default is to use a single layer MLPRegressor from sklearn. Following\r\nnnetar.R, the ordinal (`p`) and seasonal (`P`) lags to look back are automatically chosen if left\r\nunspecified. Furthermore, number of hidden neurons in the default estimator is also chosen automatically following the\r\nheuristic in nnetar.R.\r\n\r\nInstallation:\r\n```sh\r\npip install arnet\r\n```\r\n\r\nHere are some snippets to illustrate the usage:\r\n### Fit-predict flow\r\n```py\r\nfrom arnet import ARNet\r\n\r\ny_train, y_test = ...\r\n\r\nmodel = ARNet()\r\nmodel.fit(y_train)\r\npredictions = model.predict(n_steps=y_test.size)\r\n```\r\nInstantaniate the model, fit to data and predict; that's all.\r\n\r\nIf you have side information, i.e., exogenous regressors to help in prediction, you can supply them like so:\r\n```py\r\nX_train, X_test, y_train, y_test = ...\r\n\r\nmodel = ARNet()\r\nmodel.fit(X_train, y_train)\r\npredictions = model.predict(X_test)\r\n```\r\n\r\nIf you have seasonality in the series:\r\n```py\r\n# An automatic `P` (seasonal lags) will be chosen as left unspecified\r\nmodel = ARNet(seasonality=12)\r\nmodel.fit(...)\r\npredictons = model.predict(...)\r\n```\r\n\r\nDefault base model is an [`MLPRegressor`](https://scikit-learn.org/stable/modules/generated/sklearn.neural_network.MLPRegressor.html); if you want to use another one, that's okay too:\r\n```py\r\nfrom sklearn.ensemble import RandomForestRegressor\r\n\r\nmodel = ARNet(RandomForestRegressor(max_features=0.9))\r\nmodel.fit(...).predict(...)\r\n```\r\nIn fact, if you use [`LinearRegression`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html), you\r\neffectively have the linear AR(p) model as a subset! [Here](https://mustafaaydn.github.io/arnet/index.html#linear-ar-p-as-a-subset) is an example for that.\r\n\r\n### Prediction intervals\r\nFollowing the procedure [here](https://otexts.com/fpp3/nnetar.html#prediction-intervals-5), the model is able to produce prediction intervals for the future given confidence levels:\r\n```py\r\nmodel = ARNet()\r\nmodel.fit(y_train)\r\npredictions, intervals = model.predict(n_steps=y_test.size, return_intervals=True, alphas=[80, 95])\r\n```\r\nYou can also obtain the simulated paths by issuing `return_paths=True`.\r\n\r\n### Validation\r\nThere is a `.validate` method to perform time series validation (expanding window) on a parameter grid with either a full search or a randomized one:\r\n```py\r\nX, y = ...\r\nparam_grid = {\"p\": [2, 5, 8, None], \"estimator__solver\": [\"sgd\", \"adam\"]}\r\nn_iter = -1 # -1 (or None) means full grid search; any positive integer would mean a randomized search\r\n\r\nsearch = ARNet.validate(y, param_grid, X=X, n_iter=n_iter)\r\nbest_model = search[\"best_estimator_\"]\r\n# Do something with the best model\r\n```\r\n\r\n### Plotting\r\nThere is also a helper static function for plotting lines -- it might be helpful in visualizing the true values along with the predictions and intervals.\r\n```py\r\npreds_in_sample = model.fitted_values_ # fitted values are available as a post-fit attribute\r\npreds_out_sample, intervals = model.predict(n_steps=y_test.size, return_intervals=True, alphas=[80, 95])\r\n\r\nARNet.plot(lines=[y_train, preds_in_sample, y_test, preds_out_sample],\r\n labels=[\"y-train\", \"in-sample preds\", \"y-test\", \"out-sample preds\"],\r\n true_indexes=[0, 2],\r\n intervals=intervals)\r\n```\r\nHere is an example plot output:\r\n![example plot](tests/figures/example_plot.png)\r\n\r\nFor examples with a dataset in action, please see [here](https://mustafaaydn.github.io/arnet/index.html); for the API reference, see [here](https://mustafaaydn.github.io/arnet/arnet.html).\r\n",
"bugtrack_url": null,
"license": null,
"summary": "Nonlinear autoregression with feed-forward neural networks (or any estimator really)",
"version": "0.0.1",
"project_urls": {
"Source code": "https://github.com/mustafaaydn/arnet"
},
"split_keywords": [
"time series",
" autoregression",
" neural networks",
" prediction"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "f86d4b4de476a840edda1a3087638ae3af5b231d49fc3cc4a65fae1da92cfcac",
"md5": "bf4c6d1843aae1b88a8ea3b0b3c318dc",
"sha256": "44a8af6ec6b381557c5ad9ba99524bcae949a9cab50ade9a9937d1375abef703"
},
"downloads": -1,
"filename": "arnet-0.0.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "bf4c6d1843aae1b88a8ea3b0b3c318dc",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 19302,
"upload_time": "2024-08-03T11:06:43",
"upload_time_iso_8601": "2024-08-03T11:06:43.341580Z",
"url": "https://files.pythonhosted.org/packages/f8/6d/4b4de476a840edda1a3087638ae3af5b231d49fc3cc4a65fae1da92cfcac/arnet-0.0.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "e302cbe476d7e2295ad0dedd4ad92539e781b3df5b7567ae25198661b1488bce",
"md5": "3d423a73347998b9176d22db7c53c8f6",
"sha256": "31ddc4b64c83aec11cacf908ed3dfab324eadae0f629cf03f4bff564e9140a15"
},
"downloads": -1,
"filename": "arnet-0.0.1.tar.gz",
"has_sig": false,
"md5_digest": "3d423a73347998b9176d22db7c53c8f6",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 24062,
"upload_time": "2024-08-03T11:06:45",
"upload_time_iso_8601": "2024-08-03T11:06:45.371039Z",
"url": "https://files.pythonhosted.org/packages/e3/02/cbe476d7e2295ad0dedd4ad92539e781b3df5b7567ae25198661b1488bce/arnet-0.0.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-03 11:06:45",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "mustafaaydn",
"github_project": "arnet",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "arnet"
}