Hybrid engine (Armor's core)¶
This is the part inside Armor that decides, reading by reading, whether a value is genuine or broken, and what to put in place of a broken one: the middle value of the previous readings.
Armor is the front door for a 1D series: it takes an array, returns a cleaned
array. Behind it sits hybrid_engine, the module that actually decides, sample by
sample, whether a value is a genuine reading or a bad one — and if bad, what to put
in its place.
Each sample is in one of two states: dynamic (a genuine movement of the signal, kept as measured) or static (replaced by the median of the previous window). A binary trigger decides, from how the sample relates to the recent past.
1. Call the engine¶
import numpy as np
from dense_armor.utility.hybrid_engine import hybrid_shield
rng = np.random.default_rng(42)
x = 0.5 + 0.1 * rng.standard_normal(300)
x[100], x[200] = 9999.0, float("nan")
clean, trigger, info = hybrid_shield(x)
print(round(float(clean[100]), 3), trigger[100], round(float(trigger.mean()), 2), info)
The engine returns the cleaned series, the trigger per sample (1 = dynamic, kept; 0 = static,
replaced) and a small dict: whether any sample fell back, and the window radius used,
min(20, max(3, n // 3)) (20 here). The spike at 100 has trigger 0 and becomes 0.463.
2. The trigger, symbol by symbol¶
For sample \(x_i\), with \(b_i\) the mean of the previous R samples (or riferimento[i] if a
reference series is given), \(s_i\) the local scale (standard deviation of the differences in the
window) and \(g_i\) the sign of the previous step \(x_{i-1} - x_{i-2}\):
- \(\Phi_i\) mixes alignment (does the change continue the direction of the last step?) and coherence (is the change small compared with the local scale?).
- \(v_i\), the dynamic vector, is the logarithmic change of magnitude weighted by \(\Phi_i\).
- A huge spike has \(|x_i - b_i| \gg s_i\), so the coherence term drives \(\Phi_i\) to 0, \(v_i = 0\), trigger 0: the sample is replaced by the window median.
The first two samples are never tested (the step direction needs two previous points).
3. What the trigger also replaces¶
On the example the trigger is 1 on 88 % of the samples. Besides the spike and the NaN, it also classifies as static ordinary samples that hardly differ from the baseline (\(v_i\) below 0.01), and replaces them with the window median:
import numpy as np
from dense_armor.utility.hybrid_engine import hybrid_shield
rng = np.random.default_rng(42)
x = 0.5 + 0.1 * rng.standard_normal(300)
x[100], x[200] = 9999.0, float("nan")
clean, trigger, info = hybrid_shield(x)
d = np.abs(clean - x)[np.isfinite(x) & (np.arange(300) != 100)]
print(int((d > 1e-12).sum()), round(float(d.max()), 4))
35 ordinary samples out of 298 change, by up to 0.29 (about three times the noise level), and they
are not counted as anomalies by Armor. Keep this in mind on smooth signals.
4. Non-finite values¶
Inf is turned into NaN and every NaN is filled from its local neighbourhood before the
trigger runs, so the windows never contain a non-finite value.
API reference¶
hybrid_engine ¶
Two-state filter for a 1D series: keep the raw value or fall back to a baseline.
hybrid_shield decides for every point whether the raw value is a genuine
change (trigger 1.0, the raw value is kept) or noise (trigger 0.0, the value
is replaced by a baseline). The baseline is the local median of the previous
points, or a reference series when one is given.
The decision for a point uses two things: whether the change from the
baseline points in the direction of the recent raw changes, and how large the
change is in units of the local volatility (the standard deviation of the
successive differences in the recent window). They are combined into a score
phi_ab in [0, 1] with the weights 0.6 and 0.4. The log of the ratio
|raw| / |baseline|, clipped to [-5, 5], times 5 and times phi_ab, is
v_dinamic; the trigger is 1.0 when |v_dinamic| is above 0.01.
The constants of the three steps are in GLOBAL_CONSTANTS.
hybrid_shield ¶
hybrid_shield(
serie: ndarray,
riferimento: ndarray | None = None,
radius_baseline: int | None = None,
) -> tuple[np.ndarray, np.ndarray, dict]
Filter a 1D series: the raw value where the trigger is 1.0, the baseline where it is 0.0.
Notes
The windows and the direction of the recent changes come from the raw sanitized series, not from the output. Taking them from the output feeds the baseline back into the windows, and a sustained step can then stay rejected.
The first two points always pass with trigger 1.0, because the direction needs two previous points. A NaN or a spike there must be caught before this function.
The local volatility is the standard deviation of the successive
differences of the window. A median-based scale would collapse to
zero in a window that crosses a transition. An isolated spike raises
the volatility for radius steps and makes the filter more
permissive in that window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
serie
|
ndarray
|
a 1D array of floats. NaN and Inf are accepted and sanitized. |
required |
riferimento
|
ndarray | None
|
when given, the baseline for point |
None
|
radius_baseline
|
int | None
|
optional fixed radius of the window of the blind mode. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
A tuple |
ndarray
|
output series (raw value where |
dict
|
|
tuple[ndarray, ndarray, dict]
|
|
tuple[ndarray, ndarray, dict]
|
|
Source code in dense_armor/utility/hybrid_engine.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | |
Details¶
The engine generalises the logic already verified in Dense-Evolution's own
ia_utils.vector_healing.enhanced_dense_healing_hybrid. The trigger and
the NaN handling are the same as there; this version computes all samples in one vectorised
call (jax.vmap).
See also: Armor — the wearable class built on this engine.