Drift Detector — Machine Learning/Anomaly Detection
Machine_Learning/Anomaly_Detection/Drift_Detector · 1 input / 2 output port(s) at insert · exports to Python, MATLAB, Java, Rust, C, C++, VHDL, Verilog, SystemVerilog, PLC Structured Text
Description#
The block's own DESCRIPTION_HTML, rendered verbatim — the same text the config dialog's info panel and the library navigator show. Fix a wrong sentence in the block's .cpp (R-D9), never here.
Drift Detector
Machine Learning / Anomaly Detection
Watches a scalar score for a sustained shift away from the value the model was fitted around, and raises a flag when the evidence for one accumulates. With d = u − Reference Mean, the Detector parameter selects one of the three standard sequential monitors:
- EWMA – s ← (1−a)·s + a·d, and stat = |s|. A smoothed estimate of the current mean offset; it forgets, so a transient fades on its own.
- CUSUM – gp ← max(0, gp + d − K) and gm ← max(0, gm − d − K), with stat = max(gp, gm). Two-sided, and it accumulates, so it catches a shift far smaller than the noise.
- Page-Hinkley – m ← m + d − K and mm ← min(mm, m), with stat = m − mm. One-sided: it reports how far the running sum has climbed above its own lowest point.
drift is 1 on a sample where stat exceeds Threshold and 0 elsewhere. On that sample the accumulators reset to zero, so the monitor re-arms itself – and stat is reported before the reset, so the firing sample carries the value that actually crossed.
Ports
- u – the monitored score, a scalar [1,1]. Anything whose typical level is known from the training run: a reconstruction error, a Mahalanobis distance, a confidence, a residual.
- stat – a scalar [1,1]: the detector's statistic this step, in the units of u.
- drift – a scalar [1,1]: 1 or 0, ready to drive a switch, an enable or a Signal Recorder.
Parameters
- Detector – which monitor runs. Each is a different code path, so
each is verified as a mode of its own:
- EWMA – forgets at the rate Smoothing Factor sets. Use it when a drift that stops mattering should stop being reported.
- CUSUM – accumulates in both directions. The most sensitive of the three to a small persistent shift, and the usual first choice.
- Page-Hinkley – accumulates upward only. Use it when only an increase is a fault, which is the common case for an error score.
- Reference Mean – a scalar: the level the score sat at while the model was healthy. Everything is measured relative to it.
- Drift Allowance – a scalar ≥ 0: how much departure per sample is treated as noise rather than evidence. Used by CUSUM and Page-Hinkley; EWMA ignores it. Conventionally about half the smallest shift worth catching.
- Threshold – a scalar > 0: the level of stat that raises drift. Larger means slower to fire and less prone to a false alarm.
- Smoothing Factor – a scalar in (0, 1]: EWMA's weight on the newest sample. Small forgets slowly, 1 forgets everything but the current sample. Used by EWMA only; the other two ignore it.
- Sampling Time (s) – zero or less inherits the solver's rate; a positive value runs the block at that period.
Code export
All ten targets: Python, MATLAB, Java, Rust, C, C++, VHDL, Verilog, SystemVerilog and PLC Structured Text. Every parameter is baked into the body at export time; there is no tunable parameter object, and the selected detector is the only code emitted.
The three HDL targets are genuine synthesizable Q16.16, not the
simulation-only real arithmetic most of this family needs: the whole
monitor is adds, constant multiplies and comparisons, with no division and no
transcendental. The accumulators are registered state, and the reset on a firing
sample is what keeps them inside the fixed-point range.
Simulink bridge
None. Sequential change detection of this kind lives in the Statistics
and Machine Learning Toolbox, which is not installed on this machine, so there is
no block whose parameters this one could be mapped onto or verified against; and
nothing in the base Simulink libraries accumulates evidence this way –
Detect Change reports that a signal differs from its previous value,
which is a different question entirely. The bridge reports the block rather than
dropping it silently, and it has no parity testbench, which is the
documented consequence of Support::None rather than a gap. Code
export verification still covers it across all ten languages, in all three
detector modes.
Notes
- Stateful and inherently discrete
(
setDiscreteOnlyBlock(true)): the accumulators advance once per sample and have no derivative to integrate. - The state starts at zero and cannot be seeded, deliberately – a monitor begins with no accumulated evidence. To restart one mid-run, gate its input rather than looking for a reset port.
- Nonlinear, and deliberately carries no state space. The max/min clamps and the threshold are not describable by A/B/C/D, so model reduction correctly refuses the block.
- Tuning is a trade, not a setting. A smaller Drift Allowance or Threshold fires sooner and more often; the pair is what sets the false-alarm rate, and it is worth reading stat on a healthy run before choosing either.
Code facts#
| Fact | Value |
|---|---|
| registered type | Machine_Learning/Anomaly_Detection/Drift_Detector |
| family | Machine_Learning/Anomaly_Detection |
| solver environment class | ICoreBlock_0_Machine_Learning_1_Anomaly_Detection_2_Drift_Detector |
| source | src/ICoreSDK/ICoreBlockLibrary/Blocks/Machine_Learning/Anomaly_Detection/Drift_Detector/ICoreBlock_0_Machine_Learning_1_Anomaly_Detection_2_Drift_Detector.cpp |
| header | src/ICoreSDK/ICoreBlockLibrary/Blocks/Machine_Learning/Anomaly_Detection/Drift_Detector/ICoreBlock_0_Machine_Learning_1_Anomaly_Detection_2_Drift_Detector.h |
| default size on canvas | 130 × 80 px |
| ports at insert | 1 in, 2 out |
| code generators implemented | Python, MATLAB, Java, Rust, C, C++, VHDL, Verilog, SystemVerilog, PLC Structured Text |
Ports#
| # | Direction | Signal type | Description label |
|---|---|---|---|
| 1 | in | ICoreDouble | u |
| 2 | out | ICoreDouble | stat |
| 3 | out | ICoreDouble | drift |
Ports the constructor creates. A block whose port list changes with its configuration adds or removes ports at load time; the count above is the one a freshly inserted block has.
Configuration variables#
| Config variable | Default | Simulink parameter |
|---|---|---|
Detector | EWMA%~%CUSUM%~%Page-Hinkley~~EWMA | — |
Reference Mean | 0 | — |
Drift Allowance | 0.5 | — |
Threshold | 1 | — |
Smoothing Factor | 0.2 | — |
Every block also carries Sampling Time (s) from ICoreBlockSolverEnvironment: zero or less inherits the solver's rate, a positive value runs the block at that period.
Simulink bridge#
| support | Support::None |
| Simulink path | — |
| port-count rule | PortsParam::None |
SampleTime parameter | yes |
Caveat (shown to the user): no Simulink equivalent available here: sequential change detection (EWMA/CUSUM/Page-Hinkley) belongs to the Statistics and Machine Learning Toolbox, which is not installed on this machine, and no base-library block accumulates evidence this way -- Detect Change reports that a signal differs from its previous value, which is a different question. There is therefore no parameter set to map onto and no reference to run a parity testbench against
Catalog contract: src/ICoreSDK/ICoreCoder/ICoreCommandSystem/SimulinkBridge/ICoreSimulinkBlockCatalog.h
Description vs code#
The lists agree. check_block_descriptions.py finds no disagreement between the description's Ports, Parameters, Code export and Simulink bridge lists and the code's.
The verdict above is
tools/docs/check_block_descriptions.py(P7.1), which compares LISTS. It cannot read a sentence: "stateless" on a block with a state, an initial-value semantic the recursion does not implement, a "not synthesizable" caveat the HDL banner contradicts. That is the agent audit (P7.3) on BLOCK_DESCRIPTION_AUDIT.md, and this tool's green is not a substitute for one.
File banner (developer view)#
The top comment of the block's .cpp — the maths, the realization and the export strategy, addressed to whoever changes it. It must not contradict the description above (P7.5).
Drift Detector — has the process moved away from what the model was trained on? Three sequential change monitors over a scalar score, one per mode, all sharing d = u - mu:
EWMA s := (1-a)*s + a*d stat = |s| CUSUM gp := max(0, gp + d - K) stat = max(gp, gm) gm := max(0, gm - d - K) Page-Hinkley m := m + d - K, mm := min(mm, m) stat = m - mm
and everywhere: drift = (stat > H), then the accumulators RESET on the sample that fired. stat is published BEFORE that reset, so the firing sample reports the value that crossed.
Three properties make this block cheap in all ten backends, and each is load-bearing:
- NO DIVISION AND NO TRANSCENDENTAL. Every constant (mu, K, H, a, 1-a) is folded at export
time and the sample loop is adds, constant multiplies and comparisons -- so the three HDL targets are genuine synthesizable Q16.16 rather than the simulation-only
realpath most of this family needs.
- THE STATE STARTS AT ZERO in every target, by definition rather than by convention: a
monitor begins having accumulated no evidence. Nothing is seeded, so no generator reads a live member and the §7 "ten backends agree with each other and not the simulation" trap cannot arise here.
- THE RESET KEEPS THE ACCUMULATORS BOUNDED. An un-reset CUSUM walks monotonically away on a
biased input and would eventually leave Q16.16's ±32768 range; signal-and-restart is the standard behaviour anyway, and it re-arms the monitor without a second port.
⚠ The mode is fixed at export time, so each generator emits ONE detector's straight-line code. The three sequences are kept visually parallel in every language on purpose -- they are the same four steps (accumulate, compare, publish, store) wherever you read them.
Sample results#
The same rig also ran:
| Stimulus | What it is | Output range |
|---|---|---|
impulse | Impulse: one sample of 1 at k = 5, 0 elsewhere (Repeating Sequence Stair) | 0 … 0.2 |
ramp | Ramp: slope 1 from t = 0 | 0 … 1.784 |
sine | Sine Wave: amplitude 1, 2 rad/s, no phase, no bias | 0 … 0.7839 |
table | Repeating Sequence Stair: [-2 -1 -0.5 0 0.5 1 2 3], one entry per sample | 0.01581 … 1.086 |
Plotted: step — Step: 0 -> 1 at t = 1 s
Category dynamic · sample time 0.1 · 60 steps · commit ccf005c8 · produced by docsSample --out <folder> --steps 60 · data docs/generated/samples/Machine_Learning__Anomaly_Detection__Drift_Detector.json · the SVG is generated from those numbers by tools/docs/plot_svg.py, so it is a run and not a drawing (R-D10).