How do I smooth a noisy signal?¶
A daily reading jumps around, and a decision that follows it jumps too. SMOOTH and SMOOTH3 average it over a time constant. The script adds seeded noise to a reading of 100 and compares the spread before and after.
from nagasim import build as nb
# A daily reading of 100 with noise: NOISE_NORMAL(mean, sd, lo, hi) is a normal draw cut to [lo, hi],
# fixed for a given seed. SMOOTH and SMOOTH3 average it over about 5 days. The level only gives the
# model a step grid to run on.
m = nb.Model(time_limit=100, time_unit="days", sd={"dt": 0.5, "method": "euler"})
m.add_auxiliary(name="Reading", expr="NOISE_NORMAL(100, 20, 40, 160)")
m.add_auxiliary(name="Smooth", expr='SMOOTH(AUX("Reading"), 5)')
m.add_auxiliary(name="Smooth3", expr='SMOOTH3(AUX("Reading"), 5)')
m.add_level(name="Total", initial=0)
m.add_flow(name="intake", to="Total", rate='AUX("Smooth")')
series = m.run(seed=1)["timeseries"].pivot(index="Time", columns="Series", values="Value")
settled = series.loc[20:, ["aux/Reading", "aux/Smooth", "aux/Smooth3"]] # after the filters have warmed up
print("standard deviation after day 20:")
for name in ("Reading", "Smooth", "Smooth3"):
print(f" {name:<8} {settled['aux/' + name].std():5.1f}")
Save it as recipe-smooth-noise.py and run it with python. The script seeds itself, so the numbers below are the same on your machine.
standard deviation after day 20:
Reading 20.3
Smooth 4.6
Smooth3 5.2
How it works¶
NOISE_NORMAL(mean, sd, lo, hi)is a normal draw cut to[lo, hi], fixed for a given seed, so the page prints the same numbers every time.SMOOTH(x, tau)is a first-order average;SMOOTH3chains three stages oftau / 3each.- A 5-day average cuts the standard deviation from about 20 to about 5, at the cost of a lag of a few days.
Elements used¶
- Functions:
NOISE_NORMAL,SMOOTH,SMOOTH3. - Model-file sections:
auxiliaries,levels,flows. - Python:
Model.
Back to all recipes.