Experiments¶
An experiment binds a program to its data, the values it holds fixed, its inference configuration, its seed, and its sweeps. It is a checked declaration of the module, not a shell command, so every run it describes has a content hash, and every number it produces traces to the exact program, data, and configuration behind it. The hashing, caching, and provenance rules are stated in Experiments.
Form¶
object Obs : FinSet 4
export program regression(scale : Real) : Obs -> Obs
let a <- sample Normal(loc=0.0, scale=scale)
observe y : Obs <- Normal(loc=a, scale=0.5)
return a
experiment fit_regression
program regression
data y <- "data/y.json" @blake3:0d9a5e8a1b7e4f6c0f2b9d3c8a7e6f5d4c3b2a1908f7e6d5c4b3a29180706f5e
fix scale = 1.0
infer nuts(warmup = 500, draws = 1000, chains = 4)
seed 7
An experiment is an indented block of clauses after experiment NAME. program, infer, and
seed are required and occur once; reference occurs at most once; the other clauses may repeat.
| Clause | Meaning |
|---|---|
program E |
the entry the experiment runs: a program or a define of the module, or M.e of an imported module M |
data x <- "PATH" @blake3:HEX |
the parameter (or sample site) x takes the value of the file PATH, whose content hash is pinned |
fix x = V |
x takes the literal value V |
infer M(k = v, ...) |
the inference method M and its options |
seed N |
the seed every draw of the run starts from |
sweep x in [V1, ..., Vn] |
x ranges over the listed values; the experiment is one run per point |
reference "PATH" @blake3:HEX |
the stored reference results qvr experiment check compares against |
tolerance k = v, ... |
the pinned tolerances of that comparison |
link P = M |
the hole P of the program's package is filled with the module M for this experiment |
handle I with H |
the handler H is installed for the instance I around the entry |
A value written in an experiment (V, an option, a tolerance) is a literal: a number, a string,
true, false, or a list of them. A path is relative to the module's directory.
The clause words program, data, fix, infer, seed, sweep, reference, and tolerance
are keywords only at the start of a clause, so data, seed, and the others remain ordinary names
everywhere else:
An experiment runs the code and is not part of it. Like a test, it is kept beside the module's statements: compiling, transpiling, and hashing the module never read it.
Data and pins¶
A data clause pins the content hash of its file with @blake3: and the file's 64-digit BLAKE3
digest. The pin is part of the experiment's hash, so the hash is known without reading the data,
and reading checks the file against it: a file whose content no longer matches its pin is refused,
naming the clause, the digest the file has, and the pin that would accept it. A data clause
without a pin is refused too, with the pin to write.
A file is read by the reader its suffix selects. A .json file is its JSON value. A .csv file
with a header row is one list per column, keyed by the column's header, with numeric cells read as
numbers; a file with one column is that column's list. A program parameter of a type with
named axes is given its data by label, as qvr run gives it.
register_reader adds a reader for another suffix.
Inference methods¶
| Method | Runs | Options (defaults) |
|---|---|---|
sample |
a program or a computation, once on the reference machine | fuel (unbounded) |
exact |
a computation on the reference machine, its reals computed as rationals | fuel (unbounded) |
nuts |
a program's posterior, by the No-U-Turn sampler | warmup (500), draws (1000), chains (4) |
svi |
a program's posterior, by a mean-field normal guide | steps (1000), learning_rate (0.01), draws (1000) |
pathfinder |
a program's posterior, by multi-path Pathfinder | paths (4), draws (1000) |
An option the method does not have, a value of the wrong type, and a value below the option's
minimum are refused at the option. An omitted option takes its default, and writing the default out
does not change the experiment's hash.
register_method adds a method.
sample and exact run the entry as qvr run does: a computation
needs a value for every parameter; a program's parameters are its data and a sample site given a
value is conditioned on. A fitting method fits a program from any module graph: the model is
compiled from the program's ProgramSource, the root
module with every module it imports and the holes the experiment fills, so a program spread over
modules fits exactly as the same program written in one file does.
Sweeps¶
object Obs : FinSet 4
export program regression(scale : Real) : Obs -> Obs
let a <- sample Normal(loc=0.0, scale=scale)
observe y : Obs <- Normal(loc=a, scale=0.5)
return a
experiment prior_sensitivity
program regression
data y <- "data/y.json" @blake3:0d9a5e8a1b7e4f6c0f2b9d3c8a7e6f5d4c3b2a1908f7e6d5c4b3a29180706f5e
infer sample
seed 3
sweep scale in [0.5, 1.0, 2.0]
A sweep is a finite index type whose elements are its values. An experiment with sweeps is the family of runs indexed by the product of its sweeps, in clause order, and each point has its own hash and its own stored record. Changing one value of a sweep runs that point alone again.
Linking holes and installing handlers¶
An experiment is where a generic model meets its world model and its data. A link clause fills a
hole of the program's package for this experiment only; a different
filling is a different program, so it has a different hash. A handle clause installs a
handler around a computation entry, the first clause outermost, exactly
as handle I with H in written around a call of the entry does:
effect World
holds : Int -> Bool
instance world : World
handler parity for World : Real -> Real [coverage=total, forwards=none, implementation=authored]
return x =>
return x
holds(n : Int) resumes 1 =>
let half = n / 2
resume(2 * half == n)
define score(n : Int) : Real !{world} =
let b <- perform world.holds(n)
if b then
return 1.0
else
return 0.25
experiment parity_run
program score
fix n = 4
infer sample
seed 0
handle world with parity
The experiment runs the computation parity_run.entry, whose body installs the handlers around the
entry; a program's run fixes its own handlers, so a handle clause on a program is refused.
Running¶
qvr experiment run model.qvr:prior_sensitivity
qvr experiment hash model.qvr:prior_sensitivity
qvr experiment check model.qvr:prior_sensitivity
qvr experiment export model.qvr:prior_sensitivity --to crate/
run runs every point and stores each record under its hash in the project's cache
(.qvr/cache, see qvr cache). Running again reads the stored
records; a point whose hash changed runs again. --rerun computes every point regardless. hash
prints the experiment's hash and its points' hashes without running anything. export (and run
--export DIR) writes a Workflow Run RO-Crate of the runs with a W3C PROV account of each
(Provenance).
Checking against reference results¶
object Obs : FinSet 4
export program regression(scale : Real) : Obs -> Obs
let a <- sample Normal(loc=0.0, scale=scale)
observe y : Obs <- Normal(loc=a, scale=0.5)
return a
experiment posterior
program regression
data y <- "data/y.json" @blake3:0d9a5e8a1b7e4f6c0f2b9d3c8a7e6f5d4c3b2a1908f7e6d5c4b3a29180706f5e
fix scale = 1.0
infer nuts(warmup = 500, draws = 1000, chains = 4)
seed 7
reference "reference/posterior.json" @blake3:5b1c4e8f2a9d7c6b3e0f1a2d4c5b6a7980e1f2d3c4b5a6978e0f1d2c3b4a5968
tolerance mean = 0.1, sd = 0.2
qvr experiment check compares each point's run with the reference's point of the same swept
values. For a fit it compares every site's posterior mean, which must lie within mean reference
standard deviations of the reference mean, and its posterior standard deviation, which must lie
within sd times the reference standard deviation of it. For a run it compares the value, which
must lie within the absolute tolerance value. Each compared statistic needs a pinned tolerance.
qvr experiment check --write-reference PATH stores the current results as a reference file and
prints the reference clause that pins it.
Literate documents¶
An experiment of a literate module can be woven into its prose with an
{experiment} directive, which records the experiment's hash beside its result, so a woven
number is refused once the experiment it came from changes.