Metadata-Version: 2.4
Name: sysidentpy
Version: 0.9.0
Summary: A Python Package For System Identification Using NARMAX Models
Author: Wilson Rocha Lacerda Junior
Author-email: wilsonrljr@outlook.com
Maintainer-email: Wilson Rocha Lacerda Junior <wilsonrljr@outlook.com>
License: BSD 3-Clause License
        
        Copyright (c) 2019, Wilson Rocha; Luan Pascoal; Samuel Oliveira; Samir Martins
        All rights reserved.
        
        Redistribution and use in source and binary forms, with or without
        modification, are permitted provided that the following conditions are met:
        
        * Redistributions of source code must retain the above copyright notice, this
          list of conditions and the following disclaimer.
        
        * Redistributions in binary form must reproduce the above copyright notice,
          this list of conditions and the following disclaimer in the documentation
          and/or other materials provided with the distribution.
        
        * Neither the name of the copyright holder nor the names of its
          contributors may be used to endorse or promote products derived from
          this software without specific prior written permission.
        
        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
        AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
        DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
        FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
        SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
        CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
        OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
        OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
        
Project-URL: homepage, http://sysidentpy.org
Project-URL: documentation, http://sysidentpy.org/
Project-URL: repository, https://github.com/wilsonrljr/sysidentpy
Project-URL: changelog, https://github.com/wilsonrljr/sysidentpy/blob/main/CHANGELOG.md
Keywords: data-science,forecasting,NARMAX,NARX,system-identification,machine-learning,time-series,time-series-analysis,time-series-classification,time-series-regression
Classifier: Intended Audience :: Science/Research
Classifier: Intended Audience :: Information Technology
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Education
Classifier: License :: OSI Approved :: BSD License
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Science/Research
Classifier: Topic :: Scientific/Engineering
Classifier: Topic :: Software Development
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: Software Development :: Libraries
Classifier: Operating System :: OS Independent
Requires-Python: <3.16,>=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
License-File: AUTHORS
Requires-Dist: numpy<=2.3.5,>=1.19.2
Requires-Dist: scipy>=1.8.0
Requires-Dist: matplotlib>=3.3.2
Provides-Extra: dev
Requires-Dist: pytest<10.0.0,>=7.0.0; extra == "dev"
Requires-Dist: pytest-cov<7.0.0,>=2.12.0; extra == "dev"
Requires-Dist: flake8<7.0.0,>=3.8.3; extra == "dev"
Requires-Dist: black>=23.3.0; extra == "dev"
Requires-Dist: pre-commit; extra == "dev"
Requires-Dist: ruff<1.0.0,>=0.15.0; extra == "dev"
Requires-Dist: array-api-strict>=2.0; extra == "dev"
Provides-Extra: examples
Requires-Dist: scikit-learn; extra == "examples"
Requires-Dist: catboost; extra == "examples"
Provides-Extra: docs
Requires-Dist: mkdocs<2.0.0,>=1.1.2; extra == "docs"
Requires-Dist: mkdocs-material<=9.6.6,>=8.1.4; extra == "docs"
Requires-Dist: mdx-include<2.0.0,>=1.4.1; extra == "docs"
Requires-Dist: mkdocs-markdownextradata-plugin<=0.2.6,>=0.1.7; extra == "docs"
Requires-Dist: typer<0.5.0,>=0.4.1; extra == "docs"
Requires-Dist: pyyaml<7.0.0,>=5.3.1; extra == "docs"
Requires-Dist: mkdocstrings[python]<0.29,>=0.25.2; extra == "docs"
Requires-Dist: mkdocs-redirects>=1.2.0; extra == "docs"
Requires-Dist: mkdocs-minify-plugin<=0.8.0; extra == "docs"
Requires-Dist: mkdocs-glightbox<=0.4.0; extra == "docs"
Requires-Dist: mkdocs-jupyter<=0.25.1; extra == "docs"
Requires-Dist: mkdocs-autorefs>=1.3.1; extra == "docs"
Requires-Dist: jinja2>=3.0.3; extra == "docs"
Requires-Dist: mkdocs-static-i18n[material]==1.3.0; extra == "docs"
Requires-Dist: requests<3.0.0,>=2.31.0; extra == "docs"
Provides-Extra: all
Requires-Dist: torch<3.0.0,>=1.7.1; extra == "all"
Dynamic: license-file

<div align="center">
<img src="images/sysidentpy-logo.svg" width="640" height="320" />

[![DOI](https://img.shields.io/badge/DOI-10.21105%2Fjoss.02384-%23FF7800)](https://joss.theoj.org/papers/10.21105/joss.02384)
[![PyPI version](https://img.shields.io/pypi/v/sysidentpy?color=%23ff7800)](https://pypi.org/project/sysidentpy/)
[![License](https://img.shields.io/pypi/l/sysidentpy?color=%23FF7800)](https://opensource.org/licenses/BSD-3-Clause)
[![openissues](https://img.shields.io/github/issues/wilsonrljr/sysidentpy?color=%23FF7800)](https://github.com/wilsonrljr/sysidentpy/issues)
[![issuesclosed](https://img.shields.io/github/issues-closed-raw/wilsonrljr/sysidentpy?color=%23FF7800)](https://github.com/wilsonrljr/sysidentpy/issues)
[![downloads](https://img.shields.io/pypi/dm/sysidentpy?color=%23FF7800)](https://pypi.org/project/sysidentpy/)
[![python](https://img.shields.io/pypi/pyversions/sysidentpy?color=%23FF7800)](https://pypi.org/project/sysidentpy/)
[![status](https://img.shields.io/pypi/status/sysidentpy?color=%23FF7800)](https://pypi.org/project/sysidentpy/)
[![discord](https://img.shields.io/discord/711610087700955176?color=%23FF7800&label=discord)](https://discord.gg/7afBSzU4)
[![contributors](https://img.shields.io/github/contributors/wilsonrljr/sysidentpy?color=%23FF7800)](https://github.com/wilsonrljr/sysidentpy/graphs/contributors)
[![stars](https://img.shields.io/github/stars/wilsonrljr/sysidentpy?style=social)](https://github.com/wilsonrljr/sysidentpy/stargazers)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/bd1a6d1572984ced98a549b3f4919748)](https://app.codacy.com/gh/wilsonrljr/sysidentpy/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
[![Codacy Badge](https://app.codacy.com/project/badge/Coverage/bd1a6d1572984ced98a549b3f4919748)](https://app.codacy.com/gh/wilsonrljr/sysidentpy/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_coverage)

<h1 align="center"> NARMAX Methods For System Identification and TimeSeries Forecasting </h1>
<h3 align="center">From Classical Approaches to Neural Networks</h3>

**SysIdentPy** offers State-of-the-Art techniques to build your NARMAX models, including its variants `NARX`, `NARMA`, `NAR`, `NFIR`, `ARMAX`, `ARX`, `ARMA` and others. It also includes tons of interesting examples to help you build nonlinear forecasting models using SysIdentPy.

</div>

## Table of Contents

- [What is SysIdentPy?](#introduction)
- [How do I install SysIdentPy?](#how-do-i-install-sysidentpy)
- [Features](#what-are-the-main-features-of-sysidentpy)
- [Why does SysIdentPy exist?](#why-does-sysidentpy-exist)
- [How do I use SysIdentPy?](#how-do-i-use-SysIdentPy)
- [Examples](#examples)
- [Communication](#communication)
- [Citation](#citation)
- [Inspiration](#inspiration)
- [Contributors](#contributors)
- [Sponsors](#sponsors)


## Introduction

SysIdentPy is an open-source Python module for System Identification using **NARMAX** models built on top of **numpy** and is distributed under the 3-Clause BSD license. SysIdentPy provides an easy-to-use and  flexible framework for building Dynamical Nonlinear Models for time series and dynamic systems.

With **SysIdentPy**, you can:

- Build and customize nonlinear forecasting models.
- Utilize state-of-the-art techniques for model structure selection and parameter estimation.
- Experiment with neural NARX models and other advanced algorithms.

Check our [documentation](https://sysidentpy.org)!

For an in depth documentation, check our companion book:

<a href="https://sysidentpy.org/book/0-Preface/">
  <img src="https://github.com/wilsonrljr/sysidentpy-data/blob/4085901293ba5ed5674bb2911ef4d1fa20f3438d/book/assets/Nonlinear_System_identification.png?raw=true" alt="Nonlinear System Identification" style="width: 200px; height: auto;" />
</a>


## How do I install SysIdentPy?

The easiest way to get SysIdentPy running is to install it using ``pip``
``` console
pip install sysidentpy
```

### Requirements

`SysIdentPy` requires:

- Python (>= 3.10)
- NumPy (>= 1.19.2) for numerical algorithms
- Matplotlib >= 3.3.2 for static plotting and visualizations
- Pytorch (>=1.7.1) for building NARX neural networks
- scipy (>= 1.8.0) for numerical and optimization algorithms

The library is compatible with Linux, Windows, and macOS. Some examples may also require additional packages like pandas.

For more details, check our [installation guide](https://sysidentpy.org/getting-started/getting-started/)

## Experimental Array API Support

SysIdentPy includes experimental, opt-in Array API support following the same general approach used by SciPy and scikit-learn.

- Enable dispatch with `set_config(array_api_dispatch=True)` or `config_context(array_api_dispatch=True)`.
- Current backend-native support includes the supported model structure selection algorithms, simulation, metrics, utilities, and the `Polynomial`, `Fourier`, and `Bilinear` basis functions.
- Current automated coverage is strongest for NumPy, PyTorch, and `array_api_strict`. CuPy and JAX remain experimental compatibility targets.
- On non-NumPy backends, 1-step prediction stays backend-native. Sequential prediction (`steps_ahead=None` and `steps_ahead > 1`) currently runs through a NumPy/CPU fallback and converts predictions back to the original namespace/device.

See the [Array API dispatch guide](https://sysidentpy.org/user-guide/how-to/array-api-dispatch/) for the exact support matrix and current limitations.

## What are the main features of SysIdentPy?

| Feature | What is this? |
|----------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| NARMAX philosophy | You can build variations of NARMAX models like NARX, NAR, NARMA, NFIR, ARMA, ARX, AR, and others. |
| Model Structure Selection | Easy-to-use methods to select the best terms to build your models, including FROLS, MetaMSS, AOLS, UOFR, Entropic Regression, RMSS, and Orthogonal Floating Search (OSF, OIF, OOS/O2S), with several combinations with parameter estimation techniques to select the model terms. |
| Basis Function | You can use up to 8 different basis functions to build your models. You can set linear and nonlinear basis functions and ensemble them to get custom NARMAX models. |
| Parameter Estimation | More than 15 methods to estimate the model parameters and test different structure selection scenarios. |
| Multiobjective Parameter Estimation | You can use affine information to estimate the model parameters minimizing different objective functions. |
| Model Simulation | You can reproduce results from papers easily with SimulateNARMAX class. Moreover, you can test published models with different parameter estimation methods and compare the performance. |
| Neural NARX | You can use SysIdentPy with Pytorch to create custom neural NARX models architectures which support all the optimizers and loss functions from Pytorch. |
| General Estimators | You can use estimators from packages like scikit-learn, Catboost, and many other compatible interfaces and composition tools to create NARMAX models. |

## Why does SysIdentPy exist?

SysIdentPy aims to be a free and open-source package to help the community to design NARMAX models for System Identification and TimeSeries Forecasting. More than that, be a free and robust alternative to one of the most used tools to build NARMAX models, which is the Matlab's System Identification Toolbox.

The project is actively maintained by Wilson R. L. Junior and looking for contributors.

## How do I use SysIdentPy?

The [SysIdentPy documentation](https://sysidentpy.org) includes more than 20 examples to help get you started:
- Quickstart guide, for an [entry-level description of the main SysIdentPy concepts](https://sysidentpy.org/getting-started/quickstart-guide/)
- A dedicated section focusing on SysIdentPy features, like model structure selection algorithms, basis functions, parameter estimation, and more.
- A dedicated section focusing on use cases using SysIdentPy with real world datasets. Besides, there is some brief comparisons and benchmarks against other time series tools, like Prophet, Neural Prophet, ARIMA, and more.


### Examples
```python
from torch import nn
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sysidentpy.metrics import root_relative_squared_error
from sysidentpy.utils.generate_data import get_siso_data


# Generate a dataset of a simulated dynamical system
x_train, x_valid, y_train, y_valid = get_siso_data(
  n=1000,
  colored_noise=False,
  sigma=0.001,
  train_percentage=80
)
```


#### Building Polynomial NARX models with FROLS algorithm

```python
from sysidentpy.model_structure_selection import FROLS
from sysidentpy.basis_function import Polynomial
from sysidentpy.parameter_estimation import LeastSquares
from sysidentpy.metrics import root_relative_squared_error
from sysidentpy.utils.generate_data import get_siso_data
from sysidentpy.utils.display_results import results
from sysidentpy.utils.plotting import plot_residues_correlation, plot_results
from sysidentpy.residues.residues_correlation import (
    compute_residues_autocorrelation,
    compute_cross_correlation,
)

basis_function = Polynomial(degree=2)
estimator = LeastSquares()
model = FROLS(
    order_selection=True,
    n_info_values=3,
    ylag=2,
    xlag=2,
    info_criteria="aic",
    estimator=estimator,
    err_tol=None,
    basis_function=basis_function,
)
model.fit(X=x_train, y=y_train)
yhat = model.predict(X=x_valid, y=y_valid)
rrse = root_relative_squared_error(y_valid, yhat)
print(rrse)
r = pd.DataFrame(
	results(
		model.final_model, model.theta, model.err,
		model.n_terms, err_precision=8, dtype='sci'
		),
	columns=['Regressors', 'Parameters', 'ERR'])
print(r)

```
```console
Regressors     Parameters        ERR
0        x1(k-2)     0.9000  0.95556574
1         y(k-1)     0.1999  0.04107943
2  x1(k-1)y(k-1)     0.1000  0.00335113
````
```python
plot_results(y=y_valid, yhat=yhat, n=100, figsize=(14, 3), linewidth=1.5)
```
![polynomial](https://github.com/wilsonrljr/sysidentpy-data/blob/main/docs/images/readme_polynomial_narmax.png?raw=true)

#### NARX Neural Network
```python
from sysidentpy.neural_network import NARXNN
from sysidentpy.basis_function import Polynomial
from sysidentpy.utils.plotting import plot_residues_correlation, plot_results
from sysidentpy.residues.residues_correlation import compute_residues_autocorrelation
from sysidentpy.residues.residues_correlation import compute_cross_correlation

class NARX(nn.Module):
    def __init__(self):
        super().__init__()
        self.lin = nn.Linear(4, 10)
        self.lin2 = nn.Linear(10, 10)
        self.lin3 = nn.Linear(10, 1)
        self.tanh = nn.Tanh()

    def forward(self, xb):
        z = self.lin(xb)
        z = self.tanh(z)
        z = self.lin2(z)
        z = self.tanh(z)
        z = self.lin3(z)
        return z

basis_function=Polynomial(degree=1)

narx_net = NARXNN(
  net=NARX(),
  ylag=2,
  xlag=2,
  basis_function=basis_function,
  model_type="NARMAX",
  loss_func='mse_loss',
  optimizer='Adam',
  epochs=200,
  verbose=False,
  optim_params={'betas': (0.9, 0.999), 'eps': 1e-05} # optional parameters of the optimizer
)

narx_net.fit(X=x_train, y=y_train)
yhat = narx_net.predict(X=x_valid, y=y_valid)
plot_results(y=y_valid, yhat=yhat, n=100, figsize=(14, 3), linewidth=1.5)
```
![neural](https://github.com/wilsonrljr/sysidentpy-data/blob/main/docs/images/readme_narx_network.png?raw=true)

#### Catboost-narx
```python
from catboost import CatBoostRegressor
from sysidentpy.general_estimators import NARX
from sysidentpy.basis_function import Polynomial
from sysidentpy.utils.plotting import plot_residues_correlation, plot_results
from sysidentpy.residues.residues_correlation import compute_residues_autocorrelation
from sysidentpy.residues.residues_correlation import compute_cross_correlation


basis_function=Polynomial(degree=1)

catboost_narx = NARX(
  base_estimator=CatBoostRegressor(
    iterations=300,
    learning_rate=0.1,
    depth=6),
  xlag=2,
  ylag=2,
  basis_function=basis_function,
  fit_params={'verbose': False}
)

catboost_narx.fit(X=x_train, y=y_train)
yhat = catboost_narx.predict(X=x_valid, y=y_valid)
plot_results(y=y_valid, yhat=yhat, n=100, figsize=(14, 3), linewidth=1.5)
```
![catboost](https://github.com/wilsonrljr/sysidentpy-data/blob/main/docs/images/readme_catboost_narx.png?raw=true)

#### Catboost without NARX configuration

The following is the Catboost performance without the NARX configuration.


```python
catboost = CatBoostRegressor(
  iterations=300,
  learning_rate=0.1,
  depth=6
)
catboost.fit(x_train, y_train, verbose=False)
plot_results(y=y_valid, yhat=catboost.predict(x_valid), n=100, figsize=(14, 3), linewidth=1.5)
```
![catboost](https://github.com/wilsonrljr/sysidentpy-data/blob/main/docs/images/readme_catboost_without_narx.png?raw=true)

The examples directory has several Jupyter notebooks with tutorials of how to use the package and some specific applications of sysidentpy. Try it out!

## Communication

- Discord server: https://discord.gg/8eGE3PQ

  [![discord](https://img.shields.io/discord/711610087700955176?color=%23FF7800&label=discord)](https://discord.gg/8eGE3PQ)


- Website: http://sysidentpy.org

## Citation
[![DOI](https://img.shields.io/badge/DOI-10.21105%2Fjoss.02384-%23FF7800)](https://joss.theoj.org/papers/10.21105/joss.02384)

If you use SysIdentPy on your project, please [drop me a line](mailto:wilsonrljr@outlook.com).

If you use SysIdentPy on your scientific publication, we would appreciate citations to the following paper:

- Lacerda et al., (2020). SysIdentPy: A Python package for System Identification using NARMAX models. Journal of Open Source Software, 5(54), 2384, https://doi.org/10.21105/joss.02384

```
@article{Lacerda2020,
  doi = {10.21105/joss.02384},
  url = {https://doi.org/10.21105/joss.02384},
  year = {2020},
  publisher = {The Open Journal},
  volume = {5},
  number = {54},
  pages = {2384},
  author = {Wilson Rocha Lacerda Junior and Luan Pascoal Costa da Andrade and Samuel Carlos Pessoa Oliveira and Samir Angelo Milani Martins},
  title = {SysIdentPy: A Python package for System Identification using NARMAX models},
  journal = {Journal of Open Source Software}
}
```

## Inspiration

The documentation and structure (even this section) is openly inspired by Scikit-learn, EinsteinPy, and many others as we used (and keep using) them to learn.

## Contributors

<a href="https://github.com/wilsonrljr/sysidentpy/graphs/contributors">
  <img src="https://contributors-img.web.app/image?repo=wilsonrljr/sysidentpy" width = 500/>
</a>

## Sponsors

**Special thanks** to our **sponsors**

### Monthly Sponsors

<a href="https://github.com/statisticallyinsifnificant">
    <img alt="statisticallyinsifnificant" src="https://avatars.githubusercontent.com/u/158107107?v=4" width="90" height="90">
</a>

<hr />

### Individual Sponsors

<a href="https://github.com/nataliakeles">
    <img alt="Nath Keles" src="https://avatars.githubusercontent.com/u/61664158?v=4" width="90" height="90">
</a>

<hr />

  [wilsonrljr's sponsor profile]: https://github.com/sponsors/wilsonrljr


### Powered by
[![PyCharm logo](https://resources.jetbrains.com/storage/products/company/brand/logos/PyCharm.svg)](https://jb.gg/OpenSourceSupport)

<hr />

