# `sample <n>` — uniform-random reservoir time model

Prefix `LSMP`. The `sample <n>` time model and its
backing atom (`SeriesBackendReservoir`). Paired with [LSER.14] (time
models), [LDET] (the determinism guarantee
this satisfies), and
[RDSC.5a] (the `SeriesBackendReservoir` atom).

---

(LSMP-1)=
## What `sample` is [LSMP.1]

(LSMP-1-1)=
[LSMP.1.1] `sample <n>` declares a **uniform random sample of size `n`** from
the unbounded stream of absorbed samples — a reservoir, maintained over the
slot's lifetime (per `by:` identity). Aggregations apply to the sampled bag,
not to a time window.

*ratified*

(LSMP-1-2)=
[LSMP.1.2] `sample` is a reserved time-model keyword, not an aggregation name.
Authors write the statistic explicitly (`mean sample 200`, `p95 sample 200`).

(LSMP-1-3)=
[LSMP.1.3] A reservoir has **no time axis and no insertion order**: it is a
count-bounded (`n`-sample) summary of all-time history whose cost is
independent of arrival rate. That is its distinguishing property — `ring_buffer`
state scales with `window × rate`; a reservoir is fixed at `n`.

(LSMP-2)=
## Determinism [LSMP.2]

(LSMP-2-1)=
[LSMP.2.1] **The reservoir is fully replay-deterministic and cross-platform
bit-exact.** Each `by:` identity's reservoir under `sample n` holds up to $n$
slots, numbered from 0, and a 64-bit generator state $s$ that starts at
`0x9e3779b97f4a7c15`. A runtime must absorb an identity's $i$-th sample
($i$ counted from 1, in event-trace order) by this rule and no other:

- $i \le n$: the sample fills slot $i - 1$, and $s$ does not change.
- $i > n$: $s \leftarrow \mathrm{xs}(s)$, then $j = s \bmod i$. If $j < n$, the
  sample replaces slot $j$; otherwise it is discarded.

$\mathrm{xs}$ is xorshift64 over unsigned 64-bit integers:
$x \leftarrow x \oplus (x \ll 13)$, then $x \leftarrow x \oplus (x \gg 7)$, then
$x \leftarrow x \oplus (x \ll 17)$, each shift discarding the bits it moves out.

The same recorded event trace therefore produces the identical bag on every
run and on every conforming platform; the rule is pure 64-bit integer
arithmetic, so there is no floating-point or libm divergence.

*ratified*

(LSMP-2-3)=
[LSMP.2.3] Aggregations over the bag use only correctly-rounded IEEE-754
operations (`+ − × ÷ √`), taken over the bag's slots in order 0 to $n-1$
([LSMP.2.1]) and in the order [LSER.16.3.3] gives for `sample`, so the computed
values are cross-platform bit-exact ([LDET.3.4]). Queries are read-only.

*ratified*  —  *see also:* [LDET.3.4](#LDET-3-4), [LSER.16.3.3](#LSER-16-3-3), [LSMP.2.1](#LSMP-2-1)

(LSMP-3)=
## Aggregation applicability [LSMP.3]

(LSMP-3-1)=
[LSMP.3.1] Admitted over the bag: `mean`, `median`, `pXX`, `variance`, `pvariance`,
`stdev`, `pstdev`, `min`, `max`, `sum`, `count`. These are **statistical**
summaries, and a uniform sample is an unbiased (or order-statistic) estimator
of the population value.

*ratified*

(LSMP-3-2)=
[LSMP.3.2] Not admitted, with the reason:

| Aggregation | Why undefined over a reservoir bag |
|---|---|
| `first`, `last` | reservoir sampling does not preserve insertion order |
| `rate`, `direction` | a reservoir bag has no time axis |
| `bool_and`, `bool_or` | an **exact predicate**, not a statistic: a uniform sample can omit the one value that determines the AND/OR, so the reduction over the sample is not a faithful answer over all observations (LSER.16.8.3) |
| `count_distinct` | a uniform sample under-counts distinct values; cardinality is not preserved |
| `count_of` | reservoir sampling does not preserve observation counts |
| `contains` | a random sample answers "is this value in the bag", not "ever observed" |

*see also:* [LSER.16.8.3](#LSER-16-8-3)

(LSMP-3-2a)=
[LSMP.3.2a] The compiler rejects every aggregation [LSMP.3.2] lists —
`first`, `last`, `rate`, `direction`, `bool_and`, `bool_or`, `count_distinct`,
`count_of` and `contains` — under `sample` with
`DS_VALIDATE_SERIES_AGG_TIME_MODEL_MISMATCH`: each is a ✗ with no footnote in
[LSER.18.1], a combination with no defined semantics. The `SeriesBackendReservoir` *runtime*
backing can compute `bool_and` / `bool_or` over the bag; that capability is deliberately unreachable —
the language contract (the compiler) governs, and it admits only the
statistical aggregations above. `min` / `max` are admitted as order
statistics **of the retained sample**, not claimed as the population
extremes.

The compiler rejects these with the closest-form hint shape of [LSER.15.2],
pointing at `all_time` or `sliding`.

*ratified*  —  *see also:* [LSMP.3.2](#LSMP-3-2), [LSER.18.1](#LSER-18-1), [LSER.15.2](#LSER-15-2)

(LSMP-5)=
## Backing — `SeriesBackendReservoir` [LSMP.5]

(LSMP-5-1)=
[LSMP.5.1] `sample <n>` compiles to the **`SeriesBackendReservoir`** atom (reservoir sampling,
O(`n`) state independent of stream length;
[RDSC.5a], the series-backend atoms). The `max_samples` field carries the reservoir capacity `n`.

*see also:* [RDSC.5a](#RDSC-5a)

(LSMP-5-2)=
[LSMP.5.2] `persist:` is sample-retaining ([LSER.8.3.2]): retained samples
forward to the samples plane and replay reconstructs the bag — deterministic
because the retention PRNG is fixed-seed (§[LSMP.2]).

*Deferred: not binding in this edition.*

*see also:* [LSER.8.3.2](#LSER-8-3-2), [LSMP.2](#LSMP-2)

(LSMP-6)=
## DSP note [LSMP.6]

(LSMP-6-1)=
[LSMP.6.1] A uniform random sample destroys temporal structure and, over
all-time, mixes regimes of a non-stationary signal — so it is rarely the right
question for signal processing. It answers distributional / ad-hoc queries
("p37 over a year of readings at fixed memory"), not windowed or spectral ones.
DSP rules want `sliding`/`tumbling` windows or the signal aggregations
([LSIG]); `sample` is an operational-analytics
convenience.

