Options: the strange orders, the number of rules, the way a rule predicts¶
The model can ignore strange orders, keep a few rules and choose how a rule predicts.

1. The strange orders¶
With anomaly_threshold set, a rule ignores the orders that look strange to it. In this example we try three orders, 20 times each: one that is strange in both features, one that is strange in a single feature, and one that is normal.
import numpy as np
from dense_armor.core.online.rules import AMRulesRegressor
rows = ({"size": 40.0, "spread": 50.0}, {"size": 0.5, "spread": 50.0}, {"size": 0.5, "spread": 0.2})
for x in rows:
r = np.random.default_rng(0)
m = AMRulesRegressor(n_min=100, anomaly_threshold=-0.75, seed=0)
for _ in range(1500):
s = float(r.uniform(-1, 1))
m.learn_one({"size": s, "spread": float(r.uniform(-1, 1))}, 5.0 * (s > 0))
for _ in range(20):
m.learn_one(x, 5.0)
print(m.n_anomalies_)
All 20 orders are ignored when both features are strange, none when only one is, and none when the order is normal. The score of an order is the mean, over the features, of log(P / (1 - P)), where P is the probability of the feature's value for the rule (from the Cantelli inequality). An order is ignored when its score is under the threshold, and only by a rule that has seen more than m_min orders (the paper, Section 3.5). An ignored order is not learned by any rule.
2. Few rules¶
max_rules is the largest number of rules, max_literals the largest number of conditions in a rule.
import numpy as np
from dense_armor.core.online.rules import AMRulesRegressor
for kw in ({"max_rules": 1}, {"max_rules": 3}, {"max_literals": 1}):
r = np.random.default_rng(0)
m = AMRulesRegressor(n_min=100, seed=0, **kw)
for _ in range(2000):
s = float(r.uniform(-1, 1))
m.learn_one({"size": s, "spread": float(r.uniform(-1, 1))}, 5.0 * (s > 0))
print(kw, len(m.rules_))
max_rules = 1 keeps 1 rule. With max_rules = 3 the list has 2 rules: the cap is a limit, not a target. With max_literals = 1 the list has 2 rules, each with at most one condition. When the list is full the default rule cannot make a new rule.
3. How a rule predicts¶
A rule answers with the mean of its targets (mean), with a small linear model (perceptron) or with the one of the two that has the smaller recent error (adaptive, the default). The mean absolute error over the last 1000 of 3000 orders:
import numpy as np
from dense_armor.core.online.rules import AMRulesRegressor
for p in ("adaptive", "mean", "perceptron"):
r = np.random.default_rng(0)
m = AMRulesRegressor(n_min=100, prediction=p, seed=0)
e = []
for _ in range(3000):
s = float(r.uniform(-1, 1))
x = {"size": s, "spread": float(r.uniform(-1, 1))}
y = 5.0 * (s > 0) + 0.1 * float(r.normal())
e.append(abs(m.predict_one(x) - y))
m.learn_one(x, y)
print(p, round(float(np.mean(e[-1000:])), 3))
adaptive and mean give 0.252 and 0.251, perceptron 0.337. When several rules cover an order the answers are averaged; with ordered_set=True only the first rule that covers the order answers and learns (the paper, Section 3.3).

The table shows the mean absolute error of the slippage, in basis points, over the block of 100 orders that ends at the order in the header (median of 3 streams):
| 100 | 500 | 1000 | 3000 | |
|---|---|---|---|---|
adaptive |
2.217 | 0.355 | 0.136 | 0.355 |
mean |
2.552 | 0.355 | 0.136 | 0.355 |
perceptron |
2.218 | 3.824 | 0.344 | 0.502 |
max_rules = 1 |
2.217 | 0.537 | 0.684 | 1.02 |
max_rules = 5 |
2.217 | 0.355 | 0.136 | 0.834 |
max_rules = 20 |
2.217 | 0.355 | 0.136 | 0.355 |
4. The parameters¶
| Parameter | Meaning | Default |
|---|---|---|
ordered_set |
True learns and predicts with the first rule that covers a sample; False with all of them |
False |
prediction |
"adaptive", "mean" or "perceptron" |
"adaptive" |
learning_rate |
step of the delta rule of the linear models | 0.01 |
anomaly_threshold |
an order with a score under it is ignored; None turns the test off |
None |
m_min |
orders a rule needs before it checks anomalies | 30 |
max_split_points |
split values kept per feature and rule | 50 |
max_rules |
largest number of rules | 100 |
max_literals |
largest number of conditions of a rule | 20 |
The constructor raises ValueError on a non-positive n_min, learning_rate, ph_lambda, max_rules, max_literals or max_split_points, on delta or alpha outside (0, 1), a negative tau, ph_phi or m_min, or an unknown prediction. budget_s is 0.01 s and memory_class is O(window).
Source: Duarte, Gama, Bifet (2016), Algorithms 1 and 2, Sections 3.1 to 3.5.
5. The same on a robot¶
A small model for the torque of a joint: at most 3 rules of at most 2 conditions.
import numpy as np
from dense_armor.core.online.rules import AMRulesRegressor
r = np.random.default_rng(1)
m = AMRulesRegressor(n_min=100, max_rules=3, max_literals=2, seed=0)
for _ in range(1500):
v = float(r.uniform(-1, 1))
m.learn_one({"speed": v, "load": float(r.uniform(-1, 1))}, 5.0 * (v > 0))
print(len(m.rules_), round(m.predict_one({"speed": 0.5, "load": 0.0}), 1), round(m.predict_one({"speed": -0.5, "load": 0.0}), 1))
There are 2 rules, and the answers are 5.0 for a positive speed and 0.0 for a negative one.
API reference¶
AMRulesRegressor ¶
AMRulesRegressor(
ordered_set: bool = False,
n_min: int = 200,
delta: float = 1e-07,
tau: float = 0.05,
ph_lambda: float = 35.0,
ph_phi: float = 0.005,
alpha: float = 0.99,
learning_rate: float = 0.01,
prediction: str = "adaptive",
anomaly_threshold: float | None = None,
m_min: int = 30,
max_split_points: int = 50,
max_rules: int = 100,
max_literals: int = 20,
seed: int | None = None,
feature_keys: list | None = None,
)
Bases: Regressor
Adaptive Model Rules regressor (Duarte, Gama and Bifet).
Cost and memory. learn_one updates the rules that cover the sample:
for each, one insertion in a sorted list per feature (at most
max_split_points values) and one small linear model; every n_min
samples a rule scans its candidate splits. A model has at most
max_rules rules of at most max_literals conditions and
d * max_split_points stored split values each, so the state is
constant in the length of the stream and linear in the number of
features. There is no pure init_state / step form: the state is
a set of rules of changing shape.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ordered_set
|
bool
|
|
False
|
n_min
|
int
|
samples a rule sees before it tries to expand. Default 200. |
200
|
delta
|
float
|
one minus the confidence of the Hoeffding bound. Default 1e-7. |
1e-07
|
tau
|
float
|
tie threshold on the bound. Default 0.05. |
0.05
|
ph_lambda
|
float
|
threshold of the Page-Hinkley test. Default 35.0. It is in the unit of the absolute error of the target. |
35.0
|
ph_phi
|
float
|
magnitude of the changes the Page-Hinkley test allows. Default 0.005. |
0.005
|
alpha
|
float
|
fading factor of the error that chooses the prediction strategy. Default 0.99. |
0.99
|
learning_rate
|
float
|
step of the delta rule of the linear models. Default 0.01. |
0.01
|
prediction
|
str
|
|
'adaptive'
|
anomaly_threshold
|
float | None
|
threshold |
None
|
m_min
|
int
|
samples a rule needs before it checks anomalies. Default 30. |
30
|
max_split_points
|
int
|
split values kept per feature and rule. Default 50. |
50
|
max_rules
|
int
|
largest number of rules. Default 100. |
100
|
max_literals
|
int
|
largest number of conditions of a rule. Default 20. |
20
|
seed
|
int | None
|
seed of the generator of the initial weights. |
None
|
feature_keys
|
list | None
|
names of the features. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
on a non-positive |
Examples:
>>> from dense_armor.core.online.rules.amrules import AMRulesRegressor
>>> model = AMRulesRegressor(seed=0)
>>> model.predict_one({"a": 1.0})
0.0
>>> _ = model.learn_one({"a": 1.0}, 2.0)
>>> model.predict_one({"a": 1.0})
2.0
References
Duarte, Gama, Bifet (2016), Algorithms 1 and 2, Sections 3.1 to 3.5.
Source code in dense_armor/core/online/rules/amrules.py
learn_one ¶
Update the rule set with one sample.
A sample with a missing or non-finite value or target is skipped
and counted in n_missing.
Source code in dense_armor/core/online/rules/amrules.py
predict_one ¶
Prediction of the covering rules, 0.0 before the first sample.
With return_std the result is a pair: the prediction and the
mean of the standard deviations of the targets of the covering
rules.
Source code in dense_armor/core/online/rules/amrules.py
explain_one ¶
Conditions of the rules that cover x ([] for the default rule).
Source code in dense_armor/core/online/rules/amrules.py
Details¶
Choices¶
ordered_set=False,anomaly_threshold=Noneand the value 0.01 oflearning_rateare choices of the library; the paper fixes none of them.- The figures are drawn by
docs/assets/rules/make_rules_style_figures.py: 3000 orders,n_min= 100, blocks of 100 orders, median of 3 seeds.