nnstab verifies the exponential stability of positive feedback systems with a neural network controller. It is implemented in Python and in MATLAB (compatible with GNU Octave), and it uses sector bounds of the network computed with nnsector.
nnstab considers the closed-loop system
with state
-
(A1)
$B \ge 0$ and$C \ge 0$ (entrywise); -
(A2)
$\pi(0) = 0$ , so that the origin is an equilibrium. This holds, for example, for networks without biases and with tanh, ReLU or leaky ReLU activation functions.
The matrix
The verification has two stages:
-
Global stage. nnstab tests whether the origin is globally
exponentially stable, that is, for every initial state
$x_0 \ge 0$ . - Local stage. If global stability cannot be established, nnstab computes an estimate of the region of attraction: a set of initial states from which every solution converges exponentially to the origin. It searches for the largest such set.
pip install git+https://github.com/hmon91/nnstab.gitThis command also installs nnsector. The package requires Python 3.10 or
later and NumPy. PyTorch is optional and is needed only for PyTorch models
(pip install torch). To install the package for development and run the
tests:
git clone https://github.com/hmon91/nnstab.git
cd nnstab
pip install -e ".[test]"
pytestimport nnstab as nst
A, B, C, net = nst.example_system("local") # or a user-defined system
res = nst.verify(A, B, C, net)
print(res)The first lines of the output are:
Locally exponentially stable (the global test failed).
region: {x >= 0 : v^T x <= 3.563}, v = [1.929, 2.089, 1]
it holds every x0 >= 0 with sum(x0) <= 1.706
in it: sum(x(t)) <= 2.09 exp(-0.000324 t) sum(x0)
output box 0 <= y <= 1.848
The full report continues with the details of the global and the local test. The lines shown above have the following meaning.
| Line | Meaning |
|---|---|
region |
The certified region of attraction |
sum(x0) <= 1.706 |
Every initial state |
sum(x(t)) <= ... |
The guaranteed exponential bound for solutions that start in |
output box |
The box of outputs on which the local sector bound was computed. |
The largest region has a small guaranteed decay rate, because its box lies
at the limit of what the test allows. A larger guaranteed decay rate is
obtained on a smaller region with the option min_decay:
res = nst.verify(A, B, C, net, min_decay=0.5)
# The region contains every x0 >= 0 with sum(x0) <= 1.32,
# and sum(x(t)) <= 2.1 exp(-0.501 t) sum(x0) holds on it.The result can also be used in code:
res.status # "global", "local" or "none"
res.stable # True if the global or the local test succeeded
res.contains(x0) # True if the result covers the initial state x0
res.global_check # details of the global test (GlobalCheck)
res.local_check # details of the local test (LocalCheck; None after a global result)User-defined systems.
import numpy as np
import nnsector as ns
import nnstab as nst
A = np.array([[-3.0, 3.5, 2.0],
[ 2.0, -4.0, 1.0],
[ 3.5, 3.5, -5.0]])
B = np.array([[1.0], [0.5], [1.0]])
C = np.array([[1.0, 1.0, 0.5]])
net = ns.Network([W1, W2, W3], activation="tanh") # no biases, hence pi(0) = 0
res = nst.verify(A, B, C, net)A trained PyTorch model can be passed in place of net, for example a
torch.nn.Sequential of nn.Linear(..., bias=False) layers and nn.Tanh()
activations. It is converted with nnsector.Network.from_torch.
Sector condition. Suppose that the network satisfies, entrywise,
where
-
(C1)
$A + B\Gamma_1 C$ is Metzler, that is, all its off-diagonal entries are nonnegative (this keeps the state nonnegative); -
(C2)
$A + B\Gamma_2 C$ is Hurwitz, that is, all its eigenvalues have negative real parts.
If
Global test. nnsector provides the global bound
Local test. On a box of outputs
where
where decay) and
overshoot). The local test combines local
sector bounds with a localized form of the positive Aizerman conditions, as
in [1].
Search over boxes. A larger box allows a larger region, but its slopes
are farther apart, and beyond some size the test usually fails. Unless a box
is specified, nnstab considers the boxes
Choice of v="perron"), v="lp", min_decay (or half of the largest rate if min_decay is 0). This is a
linear program. Because of its structure, nnstab solves it exactly as a
linear complementarity problem with an M-matrix, in at most
Interpretation. Both tests are sufficient conditions. If a test succeeds, stability is certified. If both tests fail, the closed loop may still be stable.
verify(A, B, C, net, ybar=None, direction=None, v="perron", min_decay=0.0, rtol=1e-3)
| Argument | Description |
|---|---|
ybar |
Tests only the box |
direction |
The direction |
v |
"perron" (default): largest guaranteed decay rate. "lp": largest region for a prescribed decay rate. |
min_decay |
Smallest acceptable decay rate (default 0). Larger values give smaller regions. |
rtol |
Relative accuracy of the bisection (default 1e-3). |
| Function | Description |
|---|---|
verify(A, B, C, net, ...) |
Global test, followed by the search for a local region if the global test fails. Returns a Verification. |
check_global(A, B, C, net) |
Global test only. Returns a GlobalCheck. |
check_local(A, B, C, net, ybar, v="perron", min_decay=0) |
Local test on one box. Returns a LocalCheck. |
simulate(A, B, C, net, x0, t_end, dt=0.01) |
Simulates the closed loop (fourth-order Runge-Kutta method) from one or several initial states. |
sample_region(check, k, seed=0) |
Draws k initial states uniformly from a certified region, for validation by simulation. |
example_system(kind) |
The example systems "local" and "global". Returns A, B, C, net. |
example_controller() |
The example network: one input, two hidden layers of eight tanh neurons, one output, and no biases. |
is_metzler, is_hurwitz, spectral_abscissa, hurwitz_certificate, perron_vector, copositive_vector, largest_region_vector |
Matrix routines used by the tests. |
The network may be an nnsector.Network or a torch.nn.Sequential. For one
input, B may be given as a vector; for one output, C may be given as a
vector.
The result objects contain the following fields:
-
GlobalCheck:Gamma1,Gamma2,M1(the matrix$A + B\Gamma_1 C$ ),M2(the matrix$A + B\Gamma_2 C$ ),metzler,hurwitz,certified,decay,v,overshoot, andreason(the cause of a failure). -
LocalCheck: the same fields withgamma1andgamma2, together withybar,rho,radius(every initial state$x_0 \ge 0$ whose entries sum to at mostradiuslies in the region),method, and the methodcontains(x). -
Verification:status,stable,global_check,local_check,boxes_tried,note, and the methodcontains(x).
Printing any of these objects gives a short report.
The example controller (example_controller()) was trained to approximate
-
example_system("local"): the matrix$A$ is unstable (it has the eigenvalue 0.95). The controller stabilizes the loop near the origin but not for large states; for example, the solution from$x_0 = (4, 4, 4)$ diverges. Global stability therefore does not hold, and nnstab returns a region of attraction that contains every$x_0 \ge 0$ with$\lVert x_0 \rVert_1 \le 1.706$ . -
example_system("global"): a stable plant with small input and output matrices. The global test succeeds.
| File | Content |
|---|---|
examples/example1_local.py |
The local example, with a validation of the region by simulation and an optional plot (requires matplotlib). |
examples/example2_global.py |
The global example, with a simulation from large initial states. |
examples/example3_pytorch.py |
Training of a PyTorch controller without biases, followed by its verification. |
The test suite (pytest) validates the results by simulation: for 25
randomly generated systems, every certified result is checked along
simulated solutions (nonnegativity of the state, invariance of the region,
and the guaranteed decay rate). When SciPy is installed, the vector computed
with v="lp" is also compared with the solution of SciPy's linear
programming solver.
A MATLAB implementation with the same functions is provided in
matlab/. It requires the MATLAB version of nnsector
and MATLAB R2016b or later, and it also runs in GNU Octave. Its results
agree with those of the Python implementation up to rounding errors.
If you use nnstab in your research, please cite [1] and [2].
[1] H. Montazeri Hedesh, M. K. Wafi, and M. Siami, "Local Stability and Region of Attraction Analysis for Neural Network Feedback Systems under Positivity Constraints," in Proc. 2025 IEEE 64th Conference on Decision and Control (CDC), Rio de Janeiro, Brazil, 2025. doi: 10.1109/CDC57313.2025.11312171. Preprint: arXiv:2505.22889.
[2] H. Montazeri Hedesh and M. Siami, "Ensuring Both Positivity and Stability Using Sector-Bounded Nonlinearity for Systems With Neural Network Controllers," IEEE Control Systems Letters, vol. 8, pp. 1685-1690, 2024. doi: 10.1109/LCSYS.2024.3416237. Preprint: arXiv:2406.12744.
@inproceedings{montazerihedesh2025local,
author = {Montazeri Hedesh, Hamidreza and Wafi, Moh Kamalul and Siami, Milad},
title = {Local Stability and Region of Attraction Analysis for Neural Network
Feedback Systems under Positivity Constraints},
booktitle = {2025 IEEE 64th Conference on Decision and Control (CDC)},
year = {2025},
doi = {10.1109/CDC57313.2025.11312171}
}
@article{montazerihedesh2024ensuring,
author = {Montazeri Hedesh, Hamidreza and Siami, Milad},
title = {Ensuring Both Positivity and Stability Using Sector-Bounded Nonlinearity
for Systems With Neural Network Controllers},
journal = {IEEE Control Systems Letters},
volume = {8},
pages = {1685--1690},
year = {2024},
doi = {10.1109/LCSYS.2024.3416237}
}The positive Aizerman result on which both tests rest is due to:
[3] R. Drummond, C. Guiver, and M. C. Turner, "Aizerman Conjectures for a Class of Multivariate Positive Systems," IEEE Transactions on Automatic Control, vol. 68, no. 8, pp. 5073-5080, 2023. doi: 10.1109/TAC.2022.3217740.
nnstab is released under the BSD 3-Clause License. See LICENSE.