Models with noise
Choose the truth. Say how observations depart from it. Draw data, hand the data to a procedure, and see whether it finds the truth you chose. Real data never come with the answer attached; these do.
A truth and its noise
import { model, normal, uniform, fast } from "@ratmath/stats";
const line = model({
truth: [2, 0.5], // y = 2 + 0.5·x
x: uniform(0, 10), // where the x values come from
noise: normal(0, 1), // added to each observation
});
String(line); // "model(truth 2 + 0.5·x; x from uniform(0, 10); noise normal(0, 1))"
const data = line.sample(50, 42); // 50 observations, seed 42
String(data);
// "50 simulated observations from model(…) (seed 42; 0 contaminated, 0 censored, 0 missing)"
data.x; data.y; // what was observed
data.trueY; // the truth at each x, before the noise
fast.leastSquares(data.x, data.y).slope; // 0.4197866209598898: one estimate of 0.5
truth is a number (every observation has this value, before noise), a list of coefficients of the polynomial , or any function of . x is a list that is reused in order (a fixed design), a distribution, or a function of the generator; leave it out for a model with no predictor.
The data set remembers where it came from: kind is "simulated", and seed and model reproduce it.
Departures
| Part | What it does | What it tests |
|---|---|---|
| noise | a distribution added to each observation: | The baseline. Try a skewed or heavy-tailed distribution in place of the normal. |
| spread | a function of that multiplies the noise: | Procedures that assume equal variance. |
| contamination | { rate, noise }: with probability rate the noise comes from a second distribution; or { rate, value }: the observation is replaced outright | Outliers. Means against medians, least squares against robust fits. |
| xNoise | a distribution added to the recorded ; is made from the true | Errors in variables: the slope estimate shrinks toward zero. |
| round | observations are rounded to multiples of a step (or { x, y } steps) | Coarse instruments, ties. |
| censor | { below, above }: values past a limit are recorded as the limit | Detection limits, a scale that tops out. |
| missing | the probability an observation is lost (recorded as NaN) | Handling of gaps. data.complete() returns the pairs that are present. |
The data set records what happened to each observation: contaminated, censored and missing are lists of flags, and trueX and trueY are the values before any departure. A procedure never sees these; you use them to mark its homework.
One change at a time
const dirty = line.with({ contamination: { rate: 0.1, noise: normal(0, 10) } });
const d2 = dirty.sample(50, 42);
String(d2); // "… (seed 42; 7 contaminated, 0 censored, 0 missing)"
d2.x[0] === data.x[0]; // true: the same x values
fast.leastSquares(d2.x, d2.y).r2; // 0.115…, down from 0.701…
with returns the same model with some parts changed. Each part of a model draws from its own named stream, so with the same seed the two data sets share their values, and the observations that were not contaminated are identical. The difference between the two fits is caused by the contamination and by nothing else.
What the departures do to a slope
For a line fitted by least squares:
- Noise in with mean zero leaves the slope unbiased, whatever its distribution; contamination with mean zero only makes it noisier.
- Noise in the recorded with variance biases the slope toward zero by the factor . With uniform on , , and with the factor is : a true slope of is estimated as about .
- Censoring from above flattens the top of the line, and the slope with it.
The code box measures each of these, and the experiments page turns such measurements into coverage and power.
Exact data
sample(n, seed, { exact: true }) makes every draw a fraction (see exact draws), so that exact statistics of the data, such as a mean as a fraction or an exact permutation test, are reproducible to the last digit. It is much slower and meant for small samples.
@ratmath/stats and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.