Experiment
![]()
NAME
Experiment — run a model over a grid of lever settings and many seeds, and summarize the results
SYNOPSIS
java -cp spicegrinder.jar com.obsvra.spicegrinder.tools.Experiment [options] model.xml|model.json
On a jpackage install, the same tool ships as a native launcher in the command-line package, in both editions:
experiment [options] model.xml|model.json
DESCRIPTION
Experiment answers the question a single run cannot: how much does a result change when a lever changes, and how sure are we? It runs the model for every setting of the levers you name (a lever is a <declare> of the model, as in grind --declare), for many seeds each, reduces every run to a few numbers (the measures you define), and writes a table of means with 95 percent intervals, paired differences from a baseline, and, for yes-or-no measures, proportions with Wilson intervals.
It runs in process, on all the processors available, and does not start a new program for each run: thousands of runs of a small model take seconds.
The same seeds are used for every setting. A node’s random numbers depend only on the seed and the node’s name, so two settings that differ in one lever see the same random inputs everywhere else (common random numbers). Differences between settings are then much tighter than the difference of two independent estimates, and --baseline reports them with paired intervals.
A grid value written ~Distribution(name=value;name=value) is drawn once per seed instead of fixed (see RANDOM LEVERS). A model must be a file. A lever must be a declare of the model, which makes a model that uses levers a Pro model; without --grid and --set it runs any model, with the seeds as the only replication.
OPTIONS
| Option | Meaning |
|---|---|
--count N | Rows per run. Required unless the model ends itself (a ProcessFlow with until or closes, a finite source); a model that does not end within 50 million rows without a count is an error. |
--seeds N | Replications per setting (default 30). --first-seed S is the first seed (default 1); the seeds are S, S+1, … |
--grid NAME=V1,V2,... | A lever and its values. Also NAME=LO..HI (whole steps) and NAME=LO..HI..STEP. Repeatable; the settings are the product of the levers, the first one varying slowest. Text values (rule=nearest,least-loaded) are fine. |
--set NAME=V | A declare fixed for every setting (repeatable). |
--local-root DIR | Use DIR as the model’s local root for this run, ahead of project.root, a .spicegrinder-root marker and the Git work-tree top. A relative data file (a FlatFile file, an Email wordListFile) not beside the model is looked for there before the working directory, and a relative <Import> after the model’s folder; each must stay inside the folder it was found in. The desktop app passes its Local root setting this way. See FlatFile and the Model File Format Reference. |
--model-dir DIR | With - (stdin): resolve the model’s relative paths (<Import> files and data files) as if it were saved in DIR. The desktop app passes an edited model’s own folder this way. |
--measure "name=formula" | A measure (repeatable; see below). |
--skip N | Ignore the first N rows of each run (or path): a warm-up. |
--path-rows P | A run is blocks of P rows, each an independent path (the model’s own resetEvery makes them independent). Measures are computed per path, and the summary is over all the paths of all the runs. --count must be a multiple of P. |
--baseline "NAME=V,..." | A setting to compare the others with, on the same seeds. Name every grid lever. Adds diff, dlo and dhi columns. Repeat it for several baselines: the columns are then diff1, dlo1, dhi1, diff2, … in the order given. |
--one-at-a-time | With --baseline: run the baseline and then each lever alone at each of its other values, the rest at the baseline (a sensitivity table of 1 + the sum of (values - 1) settings, not the product). The grid lists the values to try. |
--keep EXPR | Measure only the rows for which EXPR (of c0, c1, …, row, the model’s column names and the levers) is not 0; applied after --skip. &&, ` |
--by EXPR | Split the rows that pass --keep by the value of EXPR (of the same names as --keep) and evaluate every measure separately for each group: one summary row per setting and group, with a group column. --by station gives each station of an event log its own queue length and utilization from one call; see GROUPS. Not with --search, --pareto, --welch, --effects or --format heat. |
--quantiles LIST | Add the quantiles of each measure across the runs (or paths) as columns: 0.05,0.5,0.95 gives q5, q50, q95. Unlike the quantile reducer, which works on the rows of one run. |
--pareto LIST | name:min or name:max pairs, comma-separated. Adds a pareto column: 1 for a setting that no other setting matches on every listed measure’s mean and beats on one. |
--confirm "S;S" | After the main run, re-run the named settings (each NAME=V,NAME=V naming every grid lever) and the baselines on fresh seeds: --confirm-seeds N (default as --seeds) and --confirm-first-seed S (default the seed after the main seeds). The summary gets a leading stage column with main and confirm. |
--format F | csv (the default), compact (an aligned table for reading: each measure as mean and interval, with its paired differences), or heat:MEASURE (the means of one measure as a grid, the first lever down and the second across; the other levers must have one value). |
--lever NAME=LO..HI[:int] | A lever’s range, for --design (two or more; repeatable). :int rounds to whole numbers. |
--design lhs|factorial | Settings other than the grid, over the --lever ranges; see DESIGNS below. Replaces --grid; not with --baseline, --one-at-a-time, --confirm or --format heat:.... With lhs, --points N (and --design-seed S, default 1); with factorial, --generators "c=a*b,..." for a fraction and --effects FILE for the main effects. |
--batches B | Cut each run into B consecutive blocks (--count a multiple of B; B at least 3) and summarize their means. Like --path-rows, without claiming the blocks are independent. Adds the ac1 column and a note when it is above 0.2 in size. |
--welch EXPR | With --count: the mean over the seeds of EXPR in each block of --welch-window W rows (default 1), written to --welch-out FILE as block,row,mean. Standard error says where the series seems to settle; try that as --skip. |
--adjust bonferroni|sidak | With --baseline: widen dlo and dhi so that all the comparisons with a baseline together are 95 percent. |
--search NAME=VALUES | Find the smallest value of one lever that meets a requirement, instead of running a grid; see SEARCH below. VALUES is written as for --grid and must be numbers in increasing order. Not with --grid, --baseline, --one-at-a-time, --confirm, --quantiles, --pareto, --format, --runs or --stats. |
--require "M <= X" | With --search: the requirement, M <= X or M >= X, where M is one of the --measure names. |
--find smallest|largest | With --search: the smallest value that meets the requirement (the default) or the largest (for a lever that makes things worse as it grows, such as an arrival rate). |
--out FILE | The summary table, one row per setting (default standard output). |
--runs FILE | One row per run (or path) and setting with the value of every measure. |
--stats LIST | Which statistics to write for each measure, from mean,sd,se,lo,hi,min,max,wlo,whi,nan (default all). |
--threads N | Threads (default the processors available). The results do not depend on it. |
--quiet | No progress on standard error. |
--live | As each setting’s last seed ends, write live: and one line of JSON to standard error: the setting’s place in the table (setting, from 0, and of), its levers, n, and each measure’s mean, lo and hi, the values its row of the summary will carry (null for a value that is not a number). The settings finish in any order when there are several threads. The summary itself is unchanged. Written even with --quiet; not under --by, whose rows are per group; a search and the --confirm rows write none. |
--setup FILE | Take the options from a setup file (see SETUP FILES below). --name NAME picks one of the file’s setups; it is not needed when the file has one. |
--audit | Enable the audit instrumentation stream for this run (see AUDIT below). |
--seat, --runtime | Tag this run’s instance.kind as a human-driven seat, or as an unattended instance, overriding the default, as for grind. |
MEASURES
A measure is name=formula. The formula combines reducers, each of which collapses the rows of one run (or one path) to a number, with the expression language of Calculate (+ - * /, sqrt, log, min, max of several values, if, …). Inside a reducer the names are the output columns c0, c1, … and, if the model names its columns (columns on the dataset), those names too (mean(stock)), row (the row’s number, from 0 in the run or path), and every numeric declare of the model by its name, at the value in force for the setting (the model’s default, or the setting’s own).
| Reducer | Value |
|---|---|
mean(e), sum(e), min(e), max(e), sd(e) | over the rows (min and max with one argument; with two or more they are the usual functions in the formula) |
first(e), last(e) | the first and the last row’s value |
count(e), frac(e) | the rows where e is not 0, and that as a fraction of the rows |
quantile(e, p) | the p quantile, by interpolation between the sorted values of the rows of one run (or path): a quantile of a per-row value such as the wait at the end of each second, not of a per-entity time unless each row is one entity (output="entities") |
twmean(v, t) | the time-weighted mean of v, where t is the clock column: each value holds until the next row (for an event log, whose rows are not evenly spaced) |
twmean(v, t, start, end) | the same, with the clock time the run starts (the value before the first row is 0) and the time it ends (the last value holds until then) |
rows() | the number of rows |
runs(c), longest(c) | the number of runs of consecutive rows where c is not 0, and the length of the longest, in rows (load-shed events, stock-out spells, attacks); 0 for no rows |
minmean(e, k), maxmean(e, k) | the lowest and the highest mean of e over k consecutive rows (the coldest 48 hours, the busiest hour of a minute-by-minute log); NaN when there are fewer than k rows or k is not a whole number of at least 1 |
atmax(e, v), atmin(e, v) | the value of v on the first row where e is largest (smallest): atmax(shortfall, row) is the hour of the worst shortfall |
The expression language includes &&, || and ! (each gives 1 or 0, and the right side of && and || is evaluated only when needed), so a combined condition is written count(c0 > 5 && c1 < 3). Multiplying the conditions, count((c0 > 5) * (c1 < 3)), still works and means the same. Inside a --measure or --keep given in an XML file or attribute, write && and <; on a shell command line quote the whole argument.
The same reducers summarize groups of rows inside a model with the Summarize node (D60): one row per year, per day or per episode, written by grind or any other driver, with no file of every row.
Examples: wait=mean(c3), fill=1 - sum(c4)/sum(c2), empty=count(c5 > 0.5), extinct=max(c1) > 0 (a latched flag column: 1 for a path that went extinct; max(extinct) > 0 if the model names its columns), queue=twmean(c4, c0, 0, 24).
GROUPS
--by EXPR answers “for each station”, “for each class”, “for each hour of the day” in one call. The rows that pass --skip and --keep are split by the value of EXPR, an expression of the columns with the same names as --keep and the measures (c0, c1, …, row, the model’s column names and the levers), and every measure is evaluated on the rows of each group alone, exactly as if that group had been selected with its own --keep. The event log of a ProcessFlow carries each station’s own queue and busy on that station’s rows, so one call gives the time-weighted queue length and utilization of every station:
experiment --count 3000 --seeds 3 --by station --stats mean \
--measure "queue=twmean(queue, time)" --measure "busy=twmean(busy, time)" model.xml
group,n,runs,ended,queue.mean,busy.mean
0,3,3,0,4.296538421893022,0.8558575075814513
1,3,3,0,0.02808399529191181,0.515401005919854
2,3,3,0,0.3413102179888911,0.47326142347321
The result for a group is bit-for-bit the one that --keep "station == 1" gives for that station: the group has its own reducers, so twmean(queue, time) runs from the group’s own first row to its last (or between the start and end you give), and first, last, sd, quantile and the rest see the group’s rows in order. The longer form, one --keep "station == K" call for each K, still works and gives the same numbers.
- The group value and the order. The group is the numeric value of
EXPR, printed as a whole number when it is one (2, not2.0). The groups are in ascending numeric order, after the lever columns; in--format compactthe column readsstation=2(the expression, then the value).--runsgets the samegroupcolumn after the levers. - The same groups everywhere. The groups are those that any run of any setting saw, so every setting has a row for every group, and a baseline comparison (
--baseline,--one-at-a-time,--adjust) pairs each group with the same group at the baseline setting, on the same seeds. - A group with no rows in a run (or in a path, with
--path-rows) is handled as an empty--keepis: its measures are those of no rows (mean,last,twmeanand the other averages are NaN,sum,countandrows()are 0). A NaN is left out of the summary and counted innan. - A group value that is not a finite number (NaN or infinite) for a row that is measured is an error naming the row, the setting and the seed: a row cannot belong to no group quietly. Guard it in
--keep(--keep "station >= 0") if some rows should be left out. - At most 1,000 groups (in one run, and across the runs). More is an error: the expression is probably a continuous value, not a label.
- What it does not combine with:
--search,--pareto(it would compare groups with each other),--welch,--effectsand--format heat:...are errors that list what--bydoes work with: the plain summary,--grid,--design,--baseline,--one-at-a-time,--confirm,--keep,--skip,--path-rows,--batches,--quantiles,--stats,--format csv|compactand--runs.
OUTPUT
The summary has the lever values first (then group, with --by), then n (units: runs, or paths), runs, ended (runs that ended themselves), and for each measure: mean, sd, se, a 95 percent Student’s t interval lo and hi, min, max, the Wilson 95 percent interval wlo and whi (empty unless the measure is a proportion, see below), and nan (values left out because they were not numbers). With --baseline, each measure also has diff, dlo and dhi: the mean paired difference from the baseline setting, and its interval.
The progress line on standard error gives the runs done, the time so far and an estimate of the time to go.
Whether a measure is a proportion is decided from its formula, not from its values. It is a proportion, and gets the Wilson interval, when the whole formula is one frac(e), or one mean(e) where e is a comparison or a logical expression (mean(c0 > 5), an indicator that is 0 or 1 for each row), or is itself a comparison or logical expression of reducers (max(c1) > 0, which is 0 or 1 for each run or path). Even then the Wilson interval is given only while every value of the measure is 0 or 1, since it describes yes-or-no outcomes; the fraction of rows in each run, say, is not one. Any other measure (count(...), sum(...), max(c1), a mean) gets the Student t interval alone, even when every value happens to be 0 or 1, and its wlo and whi are empty. The column layout does not change. A measure is also never a proportion under --batches, whose values are block means. --require uses the Wilson interval for a proportion and the t interval otherwise.
The t interval of a value that is never negative is cut at 0, and of a share (every value between 0 and 1) at 1.
A comparison of many settings will show some differences by chance, and the best setting of a grid is selection-biased. Re-run the setting you choose, and its nearest rivals, on fresh seeds before relying on it: --confirm does that in the same command and reports both.
RANDOM LEVERS
A lever whose value is uncertain can be drawn for each seed: --grid "demand=~Normal(mean=100;sigma=10)". The part after ~ is any scalar distribution of the library with its parameters by name, separated by ; (a , would separate grid values). For each seed the runner draws one number and sets the declare to it for that run. The draw follows the seed and the lever’s name, so it is the same in every setting of the other levers (common random numbers hold), and the same seeds give the same draws. The measures see the drawn value as the declare’s value, and --runs adds a NAME.drawn column. The spread of a measure then includes the uncertainty of the parameter.
DESIGNS
For more than about four levers the full grid is too big. --design takes the ranges of the levers (--lever NAME=LO..HI, :int for whole numbers) and builds the settings:
--design lhs --points N: a Latin hypercube of N settings: each range is cut into N equal strata and each stratum is used exactly once per lever, at a random place in it.--design-seed S(default 1) makes the design reproducible.--design factorial: every lever at its low and high end, the full 2^k settings.--generators "c=a*b"makes a fraction: the level ofcis the product of the levels ofaandb, so 3 levers take 4 settings; the effect ofcis then mixed up with the interaction ofaandb(aliased).--effects FILEwrites each lever’s main effect on each measure (lever,measure,low,high,effect).
The settings run like a grid does, on the same seeds. Interpret a hypercube with a regression or by looking, a factorial with its effects.
STEADY STATE
For a model that runs for ever (a queue, a store), the question is the long-run average, and two things go wrong: the start is not typical (the warm-up), and the rows of one run are not independent.
- Warm-up:
--welch c3 --welch-window 100 --welch-out welch.csv --count 100000writes the mean over the seeds ofc3in each block of 100 rows and says on standard error where it seems to settle: the first row from which every later block mean is within the larger of 5 percent of the last half’s mean and 2 standard deviations of the last half’s block means. Look at the series before relying on that; then use it as--skip. A run too short to settle in its first half gets no suggestion. - Batch means:
--batches 20 --count 200000 --skip 5000cuts each run into 20 blocks, takes the measure of each, and summarizes those. Theac1column is the lag-1 autocorrelation of the block means: near 0 is good; above 0.2 in size standard error says to use fewer, longer batches, because the interval would be too narrow. - Many differences at once: with
--baseline, 20 settings give 19 intervals, and one in 20 of them will miss by chance.--adjust bonferroni(orsidak) widens each so that all together are 95 percent. To trust the setting you pick,--confirmit on fresh seeds.
SEARCH
--search answers “how much of this lever do I need”: the smallest number of servers for which the 95th-percentile wait stays under 10 minutes. It does not run every value; it bisects.
experiment --search capacity=1..40 --require "p95 <= 10" --measure "p95=quantile(c3, 0.95)" \
--count 20000 --seeds 30 model.xml
It assumes that once a value meets the requirement, every larger value does too (for --find largest, every smaller one). It cannot check this; the table lists every value it tried, so a model that breaks the assumption shows a pass below a fail.
A value passes only when the whole 95 percent interval of the measure is on the right side of the limit (the t interval, or the Wilson interval when the measure is a proportion, as described under OUTPUT), fails when the whole interval is on the wrong side, and is unclear when the interval includes the limit. Unclear counts as not passing, so the answer errs towards more of the lever. All values run on the same seeds, as in a grid.
The answer, and the value next to it on the side that did not pass, are then re-run on fresh seeds (--confirm-seeds, default as --seeds; --confirm-first-seed, default the seed after the main seeds). Trust the confirmed row: a value chosen because it passed is selection-biased. The answer is confirmed when it passes again.
The output has one row per value tried, in the order of the values, then the confirmation rows:
capacity,stage,verdict,n,runs,p95.mean,p95.lo,p95.hi,answer
stage is search or confirm, verdict is pass, fail or unclear, lo and hi are the interval the verdict used, and answer is 1 on the chosen value’s rows. A line on standard error says the answer and whether it was confirmed. Only the required measure is reported; run an ordinary experiment at the answer to see the others. Searching several levers at once is not supported; a grid with --pareto shows trade-offs.
EXAMPLES
The probability that a fluctuating population dies out within 50 years, for three carrying capacities and two levels of variability, 1,000 paths for each of 5 seeds:
experiment --count 50000 --path-rows 50 --seeds 5 --grid K=20,50,100 --grid sigma=0.2,0.6 \
--stats mean,wlo,whi --measure "extinct=max(c1) > 0" --measure "final=last(c0)" \
samples/pro/extinction-paths.xml
K,sigma,n,runs,ended,extinct.mean,extinct.wlo,extinct.whi,final.mean,final.wlo,final.whi
20,0.2,5000,5,0,0.0,0.0,7.677e-4,16.97,,
20,0.6,5000,5,0,0.1804,0.1700,0.1913,10.31,,
50,0.2,5000,5,0,0.0,0.0,7.677e-4,42.42,,
50,0.6,5000,5,0,0.0532,0.0473,0.0598,29.03,,
100,0.2,5000,5,0,0.0,0.0,7.677e-4,84.84,,
100,0.6,5000,5,0,0.0264,0.0223,0.0312,59.25,,
The effect of a lever, compared with a baseline on the same seeds:
experiment --count 8760 --seeds 30 --grid C=0,10,20,40 --baseline C=0 \
--measure "grid=sum(c1)" --measure "empty=count(c5 > 0.5)" battery.xml
SETUP FILES
A setup file holds named setups, each of which is the options of one experiment command without its model. The desktop app’s Experiment window saves one beside the model as MODEL.experiments.json; it is plain text, so it can also be written by hand, reviewed and kept with the model.
experiment --setup store.experiments.json --name lanes store.xml
{
"version": 1,
"experiments": [
{
"name": "lanes",
"design": "grid",
"levers": [{ "name": "lanes", "levels": "3..8" }, { "name": "express", "levels": "0,1" }],
"set": [{ "name": "arrivals", "value": "120" }],
"measures": [{ "name": "wait", "formula": "mean(c3)" }],
"count": 20000,
"seeds": 30,
"baseline": [{ "name": "lanes", "value": "4" }, { "name": "express", "value": "0" }],
"extra": ["--quantiles", "0.05,0.5,0.95"]
}
]
}
| Entry | Stands for |
|---|---|
name | The setup’s name, unique in the file. |
design | grid (the default); one-at-a-time, which adds --one-at-a-time and needs baseline; search, which takes its one lever as --search NAME=VALUES and needs search; or lhs or factorial, whose levers are ranges, taken as --lever NAME=LO..HI (levels is the range, with :int for whole numbers). |
levers | One --grid NAME=LEVELS each, in order; levels is written as for --grid. |
set | One --set NAME=VALUE each. |
measures | One --measure "NAME=FORMULA" each, in order. |
count, seeds, firstSeed, skip | --count, --seeds (30 if absent), --first-seed, --skip. Leave count out for a model that ends itself. |
baseline | --baseline, one value for every lever. |
search | With the design search: { "measure": "p95", "op": "<=", "limit": "10", "find": "smallest" } stands for --require "p95 <= 10", and "find": "largest" adds --find largest. |
points, designSeed | With the design lhs: --points (required) and --design-seed. |
generators | With the design factorial: --generators, such as "c=a*b". |
extra | Any other options, as separate arguments, exactly as they would be typed. |
An entry that is not in this table is an error. The setup’s options take the place of --setup on the command line, so options after it are added to the setup’s own: a later --seeds 100 replaces the file’s, and a later --measure adds one.
AUDIT
With --audit, or with audit already enabled in ~/.spicegrinder/instrumentation.properties, an experiment writes three records to the audit stream: the instance’s start, one experiment.summary, and the instance’s stop. There is one summary for the experiment, not one per run. It carries spicegrinder.model.path, spicegrinder.mode (experiment), spicegrinder.experiment.kind (grid or search), .settings, .seeds, .first_seed, .runs, .rows_per_run (when --count is given), .measures, .exit_code and event.duration_ms, with the instance.kind every record has: seat when a person is at a terminal, runtime otherwise, or what --seat or --runtime says. experiment runs the model in its own process and never starts grind, so without this an experiment leaves nothing in the audit stream.
EXIT STATUS
0 on success; 1 if a run fails (the message names the setting and the seed); 2 for a bad option; 3 for --search when no value in the range meets the requirement or the answer did not hold on fresh seeds (the table is still written).