Lab

Seven experiments show when each method works and when it can mislead.

Lab in half a minute

Moving through the seven experiments and reading a threshold.

Read the transcript

Lab is a playground for the methods behind industrial AI. Pit five anomaly detectors against the same data, and see which one catches the spike, the drift, or the shift. Slide the threshold, and watch the trade-off between false alarms and missed events. Explore OEE, process capability, PID tuning, aliasing and forecasts, each with a plain verdict. It runs in C and WebAssembly, in your browser. The data is synthetic: methods, not field accuracy.

Narration: synthetic voice (Piper, voice “Joe”, CC0 public domain). Captions are burned into the video.

In three steps.

Pick an experiment
Move a control
Read the result
1

Which detector should I trust?

The maths

z = |y − median| / (1.4826 · MAD)

F1 = 2PR / (P + R)

Scenario

All are synthetic; the test is the last half of the series.

1
1

1 is balanced; raising it adds alarms.

Computing…

Value series and true anomalies

Shaded areas are the true events.

Method lanes

Red cell: alarm. Top lane: true events.

F1 by method

A high F1 means precision and recall are both good.

Method
Look for
  • Red cells: alarms
  • Top lane: true events
  • High F1: good balance
Technical detail
yi=50+0.01i+3sin2πi40+σεiy_i = 50 + 0.01 i + 3 \sin\frac{2 \pi i}{40} + \sigma \varepsilon_ii=0…399i = 0 \ldots 399

The first 200 rows train, the last 200 test; every count is from the test part.

zi=|yi−y˜|1.4826·MAD>3.5sz_i = \frac{|y_i - \tilde{y}|}{1.4826 \cdot \mathrm{MAD}} > \frac{3.5}{s}

Robust z: ỹ and MAD from training; s is the sensitivity.

|yi−yˆi|σˆ>3s\frac{|y_i - \hat{y}_i|}{\hat{\sigma}} > \frac{3}{s}σˆ=RSST−2\hat{\sigma} = \sqrt{\frac{\mathrm{RSS}}{T - 2}}

Regression residual: a least-squares line fitted on training.

ei=0.2ri+0.8ei−1e_i = 0.2 \, r_i + 0.8 \, e_{i-1}|ei|>3sσˆ0.21.8(1−0.82(i+1))|e_i| > \frac{3}{s} \, \hat{\sigma} \sqrt{\frac{0.2}{1.8} \left( 1 - 0.8^{2(i+1)} \right)}

EWMA, λ = 0.2. r: the residual from a baseline of a line plus one sine (period 8–120, least RSS) fitted on training.

Si+=max(0,Si−1++di−k)S^{+}_i = \max(0,\; S^{+}_{i-1} + d_i - k)Si−=max(0,Si−1−−di−k)S^{-}_i = \max(0,\; S^{-}_{i-1} - d_i - k)max(S+,S−)>5s\max(S^{+}, S^{-}) > \frac{5}{s}

CUSUM: d = r / σ̂, k = 0.5.

score=2−E[h(x)]/c(128)>Qtrain(1−0.01s)\mathrm{score} = 2^{-E[h(x)] / c(128)} > Q_{\mathrm{train}}(1 - 0.01 s)

Isolation Forest: features are the value, its difference, and the mean and spread of the last 5 values; 100 trees, ψ = 128; the threshold is clipped to [0.5, 0.999].

P=TPTP+FPP = \frac{TP}{TP + FP}R=TPTP+FNR = \frac{TP}{TP + FN}F1=2TP2TP+FP+FNF_1 = \frac{2 \, TP}{2 \, TP + FP + FN}

Counting is per row: a 30-row burst is 30 positives, a single spike is 1. A drift counts as an event once it exceeds the noise (σ/0.04 rows after it starts).

SettingValueNote
VerdictF1 ≥ 0.7 · ≥ 0.4good · watch · otherwise attention

Source lab/lab.js · wasm/kutupml.js · dev/native/kutupml.c

2

More alerts, better results?

The maths

P = TP / (TP + FP) · R = TP / (TP + FN)

The series comes from experiment 1.

—

The threshold is in the method’s own units.

Computing…

–

Up and right is better. Marked point: current threshold.

F1 against threshold

The peak is the threshold with the best F1.

The threshold is picked on the test part, so the result is optimistic.

Look for
  • Higher threshold, fewer alarms
  • Check the F1 peak
  • Precision and recall together
Technical detail
t=slider1000·maxi∈testscoreit = \frac{\text{slider}}{1000} \cdot \max_{i \in \mathrm{test}} \mathrm{score}_ialarmi⇔scorei>t\text{alarm}_i \iff \mathrm{score}_i > t
t∗=argmaxt∈{t0…t200}F1(t)t^{\ast} = \arg\max_{t \in \{t_0 \ldots t_{200}\}} F_1(t)tj=j200maxscoret_j = \frac{j}{200} \max \mathrm{score}

A 201-step grid; the first threshold with the best F1.

SettingValueNote
Verdict≥ 0.9 · ≥ 0.6F1(t) / F1(t*): good · watch · otherwise attention

t* is searched on the test part, so the best F1 shown is optimistic; in real use the threshold should be chosen on separate validation data.

Source lab/lab.js · wasm/kutupml.js · dev/native/kutupml.c

3

Where did the OEE go?

The maths

OEE = A × P × Q

Computing…

Loss waterfall

From planned time, each loss is subtracted in turn; OEE remains.

OEE

The needle shows OEE; colours mark the 60% and 85% bands.

The 85% line is a benchmark, not your target.

Look for
  • The verdict names the biggest loss
  • Bars fall in order
  • 85%: a benchmark
Technical detail
A=truntplanA = \frac{t_{\mathrm{run}}}{t_{\mathrm{plan}}}P=cideal·ntotal60trunP = \frac{c_{\mathrm{ideal}} \cdot n_{\mathrm{total}}}{60 \, t_{\mathrm{run}}}Q=ngoodntotalQ = \frac{n_{\mathrm{good}}}{n_{\mathrm{total}}}OEE=A·P·Q\mathrm{OEE} = A \cdot P \cdot Q

Times in minutes, the ideal cycle in seconds per unit; the 60 makes them match.

stops=1−A\text{stops} = 1 - Aspeed=A(1−P)\text{speed} = A (1 - P)quality=AP(1−Q)\text{quality} = A P (1 - Q)

Waterfall losses in percentage points of planned time; the order is fixed (stops, speed, quality).

SettingValueNote
Verdict85%OEE ≥ 85% good, else watch; P > 100% raises an input warning and is not clipped

Source lab/lab.js · wasm/analytics.js

4

Does the process fit the tolerance?

The maths

Cpk = min(USL − μ, μ − LSL) / 3σw

My own measurements

One measurement per line. At least 20.

Computing…

Distribution and spec limits

Red bars are outside specification. The dashed curve is the normal distribution.

Look for
  • Compare Cp with Cpk
  • 1.33: a common target
  • Is the spread normal?
Technical detail
σw=MR‾d2=11.128·1n−1∑i=2n|xi−xi−1|\sigma_w = \frac{\overline{MR}}{d_2} = \frac{1}{1.128} \cdot \frac{1}{n - 1} \sum_{i=2}^{n} |x_i - x_{i-1}|

Short-term spread from consecutive differences (d₂ = 1.128 for n = 2). The data order matters.

Cp=USL−LSL6σwC_p = \frac{USL - LSL}{6 \sigma_w}Cpk=min(USL−μ,μ−LSL)3σwC_{pk} = \frac{\min(USL - \mu,\; \mu - LSL)}{3 \sigma_w}
Pp=USL−LSL6sP_p = \frac{USL - LSL}{6 s}Ppk=min(USL−μ,μ−LSL)3sP_{pk} = \frac{\min(USL - \mu,\; \mu - LSL)}{3 s}

s: the standard deviation of the whole sample (n − 1).

ppm=106[Φ(LSL−μs)+1−Φ(USL−μs)]\mathrm{ppm} = 10^6 \left[ \Phi\left( \frac{LSL - \mu}{s} \right) + 1 - \Phi\left( \frac{USL - \mu}{s} \right) \right]

Assumes a normal distribution; uses the overall s, not σ_w.

JB=n6(g12+(g2−3)24)JB = \frac{n}{6} \left( g_1^2 + \frac{(g_2 - 3)^2}{4} \right)p=e−JB/2p = e^{-JB / 2}

Jarque–Bera normality test (g₁ skewness, g₂ kurtosis); a warning if p < 0.05.

SettingValueNote
VerdictCpk ≥ 1.33 · ≥ 1.0good · borderline · otherwise attention; off-centre note if Cp − Cpk > 0.2
Histogram24bins over a range that also covers the spec limits

Source lab/lab.js · wasm/kutupml.js · dev/native/kutupml.c

5

How does a control loop react?

The maths

u = Kp·e + (Kp / Ti)·∫e dt − Kp·Td·dyf/dt

Presets

2.2
5 s
0 s

Process

1
8 s
1 s

Computing…

Process value

The dashed line is the expected response if the suggestion is applied.

Controller output

The output is limited between 0% and 100%.

Look for
  • Watch overshoot and ringing
  • Recovery after the load
  • Compare with the suggestion
Technical detail
τdydt=−y+Ku(t−θ)\tau \frac{dy}{dt} = -y + K \, u(t - \theta)

First-order process with dead time; explicit Euler, Δt = 0.1 s, 240 s. The dead time adds one extra sample: the effective delay is θ + 0.1 s.

ek=r−yke_k = r - y_kDk=TfTf+ΔtDk−1−KpTdTf+Δt(yk−yk−1)D_k = \frac{T_f}{T_f + \Delta t} D_{k-1} - \frac{K_p T_d}{T_f + \Delta t} (y_k - y_{k-1})Tf=Td10T_f = \frac{T_d}{10}

The derivative acts on the filtered measurement, not the error, so a setpoint change gives no kick.

uk=Kpek+Ik+Dku_k = K_p e_k + I_k + D_kukc=min(100,max(0,uk))u^{c}_k = \min(100, \max(0, u_k))
Ik+1=Ik+Δt(KpTiek+ukc−ukTt)I_{k+1} = I_k + \Delta t \left( \frac{K_p}{T_i} e_k + \frac{u^{c}_k - u_k}{T_t} \right)Tt=max(Ti2,Δt)T_t = \max\left( \frac{T_i}{2}, \Delta t \right)

Back-calculation anti-windup.

IAE=∑k|r−yk|Δt\mathrm{IAE} = \sum_k |r - y_k| \, \Delta tovershoot=maxkyk−rr·100\text{overshoot} = \frac{\max_k y_k - r}{r} \cdot 100
SettingValueNote
Setpoint · disturbance50 · −15added to the output from t ≥ 120 s
Rise · settling10–90% · ±2%settling is searched only in the first 120 s; overshoot and IAE cover all 240 s
Suggestion grid11 × 5 × 2 = 110Kp ∈ {0.2 … 12}, Ti ∈ {2, 4, 8, 16, 32}, Td ∈ {0, Ti/8}
Suggestion ruleleast IAEamong runs with overshoot ≤ 20% and final error ≤ 1; for this process model only
Verdict25% · 10% · 60 snot settled or overshoot > 25% attention; overshoot > 10%, settling > 60 s or not reaching 90% watch

Source lab/lab.js · wasm/kutupml.js · dev/native/kutupml.c

6

Why does fast vibration look slow?

The maths

fapparent = |f − k · fs|, fs > 2f

A one-second record. The sampling rate fs is the number of samples per second.

60 Hz
50 Hz

Computing…

Signal and samples

Dots are samples; the line passes through them.

Apparent frequency

A marker at the apparent frequency (not a computed spectrum), the true frequency and the Nyquist limit.

Look for
  • Nyquist limit: fs/2
  • Apparent frequency differs
  • Raise the sampling rate
Technical detail
fN=fs2f_{\mathrm{N}} = \frac{f_s}{2}fapp=‖f−round⁡(ffs)fs‖f_{\mathrm{app}} = \left| f - \operatorname{round}\left( \frac{f}{f_s} \right) f_s \right|

Example: f = 60 Hz, fs = 50 Hz → fN = 25 Hz, apparent 10 Hz.

xk=sin(2πftk)x_k = \sin(2 \pi f \, t_k)tk=kfs,k=0…⌊fs⌋−1t_k = \frac{k}{f_s}, \quad k = 0 \ldots \lfloor f_s \rfloor - 1

f < fs/2 counts as faithful, f = fs/2 as the edge, f > fs/2 as aliased. The “apparent frequency” chart is a marker at fapp, not a computed spectrum. No anti-aliasing filter is modelled.

Source lab/lab.js

7

Does the clever model beat the simple one?

The maths

MASE = MAE / MAEnaive
2
Forecasts to show

240 hours: 192 training, 48 test.

Computing…

Series and forecasts

Vertical line: training | testing.

MAE

A shorter bar is better.

Error table

MethodMAE
Look for
  • Beat seasonal naive
  • MASE below 1
  • Check other periods
Technical detail
yt=100+0.05t+8sin2πt24+σεty_t = 100 + 0.05 t + 8 \sin\frac{2 \pi t}{24} + \sigma \varepsilon_tt=0…239t = 0 \ldots 239

192 hours train, 48 test.

yˆT+jnaive=yT−1\hat{y}^{\mathrm{naive}}_{T+j} = y_{T-1}yˆT+jseasonal=yT−24+(jmod24)\hat{y}^{\mathrm{seasonal}}_{T+j} = y_{T - 24 + (j \bmod 24)}j=0…47j = 0 \ldots 47

24-hour average: the mean of the last 24 training hours.

ℓt=αyt+(1−α)(ℓt−1+ϕbt−1)\ell_t = \alpha y_t + (1 - \alpha)(\ell_{t-1} + \phi b_{t-1})bt=β(ℓt−ℓt−1)+(1−β)ϕbt−1b_t = \beta (\ell_t - \ell_{t-1}) + (1 - \beta) \phi b_{t-1}
yˆT−1+h=ℓ+bϕ(1−ϕh)1−ϕ\hat{y}_{T-1+h} = \ell + b \, \frac{\phi (1 - \phi^h)}{1 - \phi}h=1…48h = 1 \ldots 48

Damped Holt: α, β, φ from a 7 × 4 × 4 = 112 grid that minimises one-step-ahead MAE on training.

ℓt+1=ℓt+st\ell_{t+1} = \ell_t + s_tst+1=ρsts_{t+1} = \rho \, s_tρ=e−κ\rho = e^{-\kappa}

Kalman local linear trend with a damped slope: noises and κ by maximum likelihood (Nelder–Mead); the forecast applies this transition h times.

MASE=148∑j=047|yT+j−yˆT+j|1T−1∑t=0T−2|yt+1−yt|\mathrm{MASE} = \frac{\frac{1}{48} \sum_{j=0}^{47} |y_{T+j} - \hat{y}_{T+j}|}{\frac{1}{T - 1} \sum_{t=0}^{T-2} |y_{t+1} - y_t|}

Denominator: the mean absolute error of the one-step naive forecast on training.

SettingValueNote
Verdictseasonal naivewatch if naive, 24-hour, Holt or Kalman beats it on MAE; otherwise good

Source lab/lab.js · wasm/kutupml.js · dev/native/kutupml.c

How do I read the result?

  1. Red cell: alarm
  2. Shade: true event
  3. Dashed: threshold or expected response
  4. Shorter bar: better
The data are synthetic and not a diagnosis; results show how a method behaves. Test on your own labelled data before use.

Terms used on this page

Each experiment’s “Technical detail” panel gives the formulas and the values in the code; short definitions of the terms are here.

Precision
Of the rows that raised an alarm, how many were inside a real event? High precision means few false alarms. Lab counts row by row.
Recall
Of the rows inside real events, how many raised an alarm? High recall means few missed events. Lab counts row by row.
F1 score
The harmonic mean of precision and recall. It is high only when both are high, so sacrificing one cannot inflate it.
EWMA
An exponentially weighted average that gives recent values more weight. It shows small persistent drifts before single readings do.
CUSUM
A counter that accumulates deviations from normal. Small deviations add up step by step; an alarm is raised when the total crosses a limit.
Isolation Forest
The easier it is to isolate a point with random splits, the more unusual it is. A point separated in few splits scores high.
OEE
Overall equipment effectiveness = availability × performance × quality. It condenses how much of the planned time became good output.
Cp
The specification width divided by the process spread, assuming the process is centred. It measures spread only, not shift.
Cpk
Cp adjusted for the mean. It uses the side closer to a limit, so an off-centre process scores lower.
ppm
Estimated parts out of specification per million. It assumes a normal distribution; read it roughly when the sample is small.
Overshoot
How far the process value goes above the target, in percent. It often happens while approaching the target.
Nyquist limit
Half the sampling rate (fs/2). Frequencies below it can be followed correctly; those above appear at a wrong frequency.
MASE
The forecast error divided by the error of a one-step naive forecast on the training data. Below 1 means better than that naive forecast.
MAD
The median of absolute deviations from the median. A spread measure that resists outliers.
Damped Holt trend
A simple method that tracks level and slope separately. The slope fades over time, so forecasts do not run away.
Kalman trend
A state-space model that estimates level and slope from noisy measurements; the slope fades over time. Its parameters are chosen from the data by maximum likelihood.

Frequently asked

What is Lab for?

It shows when common engineering and data methods work and when they can mislead, through small experiments.

Does Lab upload anything?

No. Calculations run in your browser; numbers you paste are not sent to any server.

Go deeper.

Full library
ASP Dijital

Start with a production problem.

Let’s review your data source, your existing system and the decision you want to improve.

Discuss your project ↗