Simulated

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 b0,b1,…b_0, b_1, \dots of the polynomial b0+b1x+⋯b_0 + b_1 x + \cdots, or any function of xx. 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

PartWhat it doesWhat it tests
noisea distribution added to each observation: y=f(x)+εy = f(x) + \varepsilonThe baseline. Try a skewed or heavy-tailed distribution in place of the normal.
spreada function of xx that multiplies the noise: y=f(x)+s(x) εy = f(x) + s(x)\,\varepsilonProcedures that assume equal variance.
contamination{ rate, noise }: with probability rate the noise comes from a second distribution; or { rate, value }: the observation is replaced outrightOutliers. Means against medians, least squares against robust fits.
xNoisea distribution added to the recorded xx; yy is made from the true xxErrors in variables: the slope estimate shrinks toward zero.
roundobservations 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 limitDetection limits, a scale that tops out.
missingthe 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 xx 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 y=β0+β1x+εy = \beta_0 + \beta_1 x + \varepsilon fitted by least squares:

  • Noise in yy with mean zero leaves the slope unbiased, whatever its distribution; contamination with mean zero only makes it noisier.
  • Noise in the recorded xx with variance σu2\sigma_u^2 biases the slope toward zero by the factor σx2/(σx2+σu2)\sigma_x^2 / (\sigma_x^2 + \sigma_u^2). With xx uniform on (0,10)(0, 10), σx2=100/12\sigma_x^2 = 100/12, and with σu=2\sigma_u = 2 the factor is 0.6760.676: a true slope of 0.50.5 is estimated as about 0.3380.338.
  • Censoring yy 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.

Try itEdit the code and run it with @ratmath/stats and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs