/ concept-collection / stan-web-ide
Sign in
concept-collection / stan-web-ide
stan-web-ide / src / sampleWorkspace.ts
104 lines · 2.9 KBBlameHistoryRaw
1// Files seeded into new projects.
3const readme = `# stan sample project
5Bayesian linear regression, run entirely in your browser.
7- \`linear.stan\` — the model (with syntax checking, hover docs, completion,
8 and auto-format from the Stan language server).
9- \`data.json\` — the data: 20 noisy points around y = 2 + 1.5 x.
10- \`fit.sample\` — a sampling run: which program, which data, sampling
11 parameters, and the output directory. Opens as a form; press
12 **Run sampling** there (or ▶ in the tab bar). Reopen as raw YAML via the
13 tab context menu.
14- \`quick.sample\` — the same fit with fewer iterations and a random seed.
16Compiling the Stan program needs a **compilation server** (sampling itself
17runs locally, in a web worker). The status bar shows the configured server;
18click it to change. To run one on your machine:
20 docker run -p 8083:8080 -it ghcr.io/flatironinstitute/stan-wasm-server:latest
22When a run finishes, its output directory (e.g. \`out/fit/\`) appears in the
23Explorer: \`chain_*.csv\` (one row per draw), \`summary.csv\` (mean, sd,
24percentiles, ESS, Rhat per parameter), \`sampling_opts.json\`, and
25\`console.txt\`.
27Edits save with **Ctrl+S** and persist in your browser. Runs use current
28editor contents, saved or not.
29`;
31const linearStan = `// Bayesian linear regression: y ~ normal(alpha + beta * x, sigma)
32data {
33 int<lower=0> N;
34 vector[N] x;
35 vector[N] y;
37parameters {
38 real alpha;
39 real beta;
40 real<lower=0> sigma;
42model {
43 alpha ~ normal(0, 5);
44 beta ~ normal(0, 5);
45 sigma ~ normal(0, 2);
46 y ~ normal(alpha + beta * x, sigma);
48generated quantities {
49 // posterior predictive draw at x = 6
50 real y_at_6 = normal_rng(alpha + beta * 6, sigma);
52`;
54const dataJson = `{
55 "N": 20,
56 "x": [0.17, 0.41, 0.77, 0.96, 1.23, 1.46, 1.66, 1.8, 2.01, 2.32,
57 2.59, 3.05, 3.21, 3.45, 3.61, 3.83, 4.03, 4.52, 4.75, 5.0],
58 "y": [2.13, 2.9, 3.51, 3.69, 3.41, 4.05, 4.56, 3.9, 3.5, 5.17,
59 6.14, 6.62, 6.55, 7.31, 7.34, 6.33, 8.81, 8.79, 8.56, 9.16]
61`;
63const fitSample = `# A sampling run. This file opens as a form; use the tab context menu to
64# edit the raw YAML. Paths are relative to this file.
65stan: linear.stan
66data: data.json
67output_dir: out/fit
68num_chains: 4
69num_warmup: 1000
70num_samples: 1000
71seed: 42
72`;
74const quickSample = `# A quicker look: fewer iterations, random seed each run.
75stan: linear.stan
76data: data.json
77output_dir: out/quick
78num_chains: 2
79num_warmup: 200
80num_samples: 200
81`;
83export const sampleWorkspace: Record<string, string> = {
84 '/README.md': readme,
85 '/linear.stan': linearStan,
86 '/data.json': dataJson,
87 '/fit.sample': fitSample,
88 '/quick.sample': quickSample,
89};
91const emptyStan = `// Write your Stan program here.
92parameters {
93 real mu;
95model {
96 mu ~ normal(0, 1);
98`;
100export const emptyWorkspace: Record<string, string> = {
101 '/main.stan': emptyStan,
102 '/data.json': '{}\n',
103 '/fit.sample': `stan: main.stan\ndata: data.json\noutput_dir: out/fit\n`,
104};
moveopenescclose