@mcmcjs/stan 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +30 -0
- package/dist/index.cjs +1000 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +263 -0
- package/dist/index.d.ts +263 -0
- package/dist/index.js +976 -0
- package/dist/index.js.map +1 -0
- package/package.json +56 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import { CommandRunner, DrawBatch, ToolInfo, Engine, FitProgress, FitResult, RuntimeVersion } from '@mcmcjs/engine';
|
|
2
|
+
import { ResolvedSpec, Samples } from '@mcmcjs/core';
|
|
3
|
+
|
|
4
|
+
/** The spec channel meaning "use whatever CmdStan is installed locally". */
|
|
5
|
+
declare const INSTALLED_CMDSTAN_CHANNEL = "installed";
|
|
6
|
+
/** The CmdStan version `mcmc setup --engine stan` provisions. */
|
|
7
|
+
declare const PINNED_CMDSTAN_VERSION = "2.39.0";
|
|
8
|
+
/** A CmdStan installation found on this machine. */
|
|
9
|
+
interface CmdStanInstall {
|
|
10
|
+
version: string;
|
|
11
|
+
/** The cmdstan home directory (contains makefile, bin/stanc, examples/). */
|
|
12
|
+
home: string;
|
|
13
|
+
}
|
|
14
|
+
/** The managed root for CmdStan installs: `<data>/mcmcjs/stan`. */
|
|
15
|
+
declare function managedStanRoot(): string;
|
|
16
|
+
/** A directory is a usable CmdStan home when the makefile and stanc are present. */
|
|
17
|
+
declare function isCmdStanHome(dir: string): boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Every CmdStan install visible to mcmcjs, newest first: an explicit
|
|
20
|
+
* MCMCJS_CMDSTAN/CMDSTAN home wins, then the managed root, then the
|
|
21
|
+
* conventional `~/.cmdstan` used by other Stan interfaces.
|
|
22
|
+
*/
|
|
23
|
+
declare function listCmdStanInstalls(): CmdStanInstall[];
|
|
24
|
+
/**
|
|
25
|
+
* Resolves the CmdStan install a fit runs on. `installed` (the default channel)
|
|
26
|
+
* takes the newest local install; an explicit version must match exactly.
|
|
27
|
+
*/
|
|
28
|
+
declare function resolveCmdStan(requested?: string): CmdStanInstall;
|
|
29
|
+
|
|
30
|
+
interface CompiledModel {
|
|
31
|
+
/** Path to the compiled model executable. */
|
|
32
|
+
binaryPath: string;
|
|
33
|
+
/** True when a previously compiled binary was reused. */
|
|
34
|
+
cached: boolean;
|
|
35
|
+
}
|
|
36
|
+
interface CompileOptions {
|
|
37
|
+
/** Runs make; injectable for tests. Compiles can take minutes. */
|
|
38
|
+
runner?: CommandRunner;
|
|
39
|
+
/** Cache root; defaults to `<managed>/models`. */
|
|
40
|
+
cacheRoot?: string;
|
|
41
|
+
}
|
|
42
|
+
/** The compile cache directory for a model source on a CmdStan version. */
|
|
43
|
+
declare function modelCacheDir(source: string, version: string, cacheRoot?: string): string;
|
|
44
|
+
/**
|
|
45
|
+
* Compiles a Stan model to an executable through CmdStan's make, cached by
|
|
46
|
+
* source content and CmdStan version so edits recompile and reruns are instant.
|
|
47
|
+
*/
|
|
48
|
+
declare function compileModel(install: CmdStanInstall, modelPath: string, options?: CompileOptions): Promise<CompiledModel>;
|
|
49
|
+
|
|
50
|
+
/** Stan CSV column -> streamed leaf name; null for dropped diagnostic columns. */
|
|
51
|
+
declare function columnToLeaf(column: string): string | null;
|
|
52
|
+
interface StanCsvTail {
|
|
53
|
+
/** Reads newly appended rows and emits any full batches. */
|
|
54
|
+
poll(): void;
|
|
55
|
+
/** Final poll plus a flush of the remaining partial batch. */
|
|
56
|
+
finish(): void;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Incrementally tails one growing CmdStan CSV, batching completed draws into
|
|
60
|
+
* DrawBatch messages. Comment lines (the config echo, adaptation block, and
|
|
61
|
+
* timing footer) are skipped; the first non-comment line is the header. A
|
|
62
|
+
* trailing line without its newline is left buffered until it completes, and a
|
|
63
|
+
* row is only accepted when its field count matches the header.
|
|
64
|
+
*/
|
|
65
|
+
declare function createStanCsvTail(filePath: string, opts: {
|
|
66
|
+
/** 0-based chain index reported in batches. */
|
|
67
|
+
chain: number;
|
|
68
|
+
/** Draws per batch. */
|
|
69
|
+
batchSize: number;
|
|
70
|
+
onBatch: (batch: DrawBatch) => void;
|
|
71
|
+
}): StanCsvTail;
|
|
72
|
+
|
|
73
|
+
/** A summary of the Stan toolchain available for inference. */
|
|
74
|
+
interface StanDoctorReport {
|
|
75
|
+
cmdstan: ToolInfo;
|
|
76
|
+
stanc: ToolInfo;
|
|
77
|
+
make: ToolInfo;
|
|
78
|
+
cxx: ToolInfo;
|
|
79
|
+
/** The install a fit would use, when one exists. */
|
|
80
|
+
install?: CmdStanInstall;
|
|
81
|
+
/** True when a fit can compile and sample: CmdStan plus the build toolchain. */
|
|
82
|
+
ready: boolean;
|
|
83
|
+
}
|
|
84
|
+
/** Detects the installed Stan toolchain and reports whether inference can run. */
|
|
85
|
+
declare function runDoctor(runner?: CommandRunner): Promise<StanDoctorReport>;
|
|
86
|
+
|
|
87
|
+
declare const stanEngine: Engine;
|
|
88
|
+
|
|
89
|
+
interface StanSpawnResult {
|
|
90
|
+
code: number;
|
|
91
|
+
stderr: string;
|
|
92
|
+
cancelled?: boolean;
|
|
93
|
+
}
|
|
94
|
+
interface StanSpawnOptions {
|
|
95
|
+
/** Called once per complete stdout line (CmdStan prints progress there). */
|
|
96
|
+
onStdoutLine?: (line: string) => void;
|
|
97
|
+
signal?: AbortSignal;
|
|
98
|
+
timeoutMs?: number;
|
|
99
|
+
}
|
|
100
|
+
/** Spawns one CmdStan process, streaming stdout lines to the caller. */
|
|
101
|
+
type StanSpawn = (command: string, args: string[], opts?: StanSpawnOptions) => Promise<StanSpawnResult>;
|
|
102
|
+
/**
|
|
103
|
+
* The production StanSpawn: detached on POSIX so cancellation kills the whole
|
|
104
|
+
* process group, stdout parsed line-by-line for progress, stderr captured for
|
|
105
|
+
* error reporting.
|
|
106
|
+
*/
|
|
107
|
+
declare function createStanSpawn(defaultTimeoutMs?: number): StanSpawn;
|
|
108
|
+
/** Parses one CmdStan progress line; null when the line is not progress. */
|
|
109
|
+
declare function parseIterationLine(line: string): {
|
|
110
|
+
iteration: number;
|
|
111
|
+
total: number;
|
|
112
|
+
warmup: boolean;
|
|
113
|
+
} | null;
|
|
114
|
+
|
|
115
|
+
interface StanFitIo {
|
|
116
|
+
/** Spawns CmdStan processes; injectable so runFit is testable without CmdStan. */
|
|
117
|
+
spawn?: StanSpawn;
|
|
118
|
+
/** Where the samples file is written. */
|
|
119
|
+
outPath: string;
|
|
120
|
+
/** Where the run record is written; defaults to `<outPath>.run.json`. */
|
|
121
|
+
recordPath?: string;
|
|
122
|
+
/** Streamed per-chain sampling progress. */
|
|
123
|
+
onProgress?: (progress: FitProgress) => void;
|
|
124
|
+
/** Streamed draw batches as sampling proceeds. */
|
|
125
|
+
onDraws?: (batch: DrawBatch) => void;
|
|
126
|
+
/** Draws per streamed batch (default 25). */
|
|
127
|
+
drawBatchSize?: number;
|
|
128
|
+
/** Aborts an in-progress fit; the run then ends with a "cancelled" status. */
|
|
129
|
+
signal?: AbortSignal;
|
|
130
|
+
/** Source path when the data came from a referenced file; recorded, not copied. */
|
|
131
|
+
dataFile?: string;
|
|
132
|
+
/** Overrides the recorded data hash (e.g. the data file's bytes hash). */
|
|
133
|
+
dataSha256?: string;
|
|
134
|
+
/** Model compile pass-through, injectable for tests. */
|
|
135
|
+
compile?: CompileOptions;
|
|
136
|
+
tmpDir?: string;
|
|
137
|
+
}
|
|
138
|
+
/** The per-chain CmdStan invocation. Chains share one seed; `id` varies the stream. */
|
|
139
|
+
declare function chainArgs(spec: ResolvedSpec, chain1: number, dataPath: string, csvPath: string): string[];
|
|
140
|
+
/** Runs one Stan inference on a resolved CmdStan install. */
|
|
141
|
+
declare function runFit(spec: ResolvedSpec, install: CmdStanInstall, io: StanFitIo): Promise<FitResult>;
|
|
142
|
+
|
|
143
|
+
interface StanMatrixEntry {
|
|
144
|
+
version: string;
|
|
145
|
+
status: "ok" | "error" | "cancelled";
|
|
146
|
+
samplesFile?: string;
|
|
147
|
+
runtimeActual?: string;
|
|
148
|
+
elapsedMs: number;
|
|
149
|
+
stage?: FitResult["stage"];
|
|
150
|
+
error?: string;
|
|
151
|
+
}
|
|
152
|
+
interface StanMatrixResult {
|
|
153
|
+
entries: StanMatrixEntry[];
|
|
154
|
+
/** True only when every requested version ran and succeeded. */
|
|
155
|
+
ok: boolean;
|
|
156
|
+
}
|
|
157
|
+
interface StanMatrixIo {
|
|
158
|
+
/** Directory the per-version samples files are written into. */
|
|
159
|
+
outDir: string;
|
|
160
|
+
dataFile?: string;
|
|
161
|
+
dataSha256?: string;
|
|
162
|
+
/** Continue after a version fails instead of stopping. */
|
|
163
|
+
keepGoing?: boolean;
|
|
164
|
+
signal?: AbortSignal;
|
|
165
|
+
/** Fit pass-through, injectable for tests. */
|
|
166
|
+
fit?: Pick<StanFitIo, "spawn" | "compile">;
|
|
167
|
+
}
|
|
168
|
+
/** Runs one spec across several installed CmdStan versions. */
|
|
169
|
+
declare function runMatrix(spec: ResolvedSpec, versions: string[], io: StanMatrixIo): Promise<StanMatrixResult>;
|
|
170
|
+
|
|
171
|
+
interface StanPredictIo {
|
|
172
|
+
/** Where the posterior-predictive samples file is written. */
|
|
173
|
+
outPath: string;
|
|
174
|
+
/** The posterior samples file fed to prediction. */
|
|
175
|
+
samplesPath: string;
|
|
176
|
+
/** Spawns CmdStan processes; injectable so runPredict is testable without CmdStan. */
|
|
177
|
+
spawn?: StanSpawn;
|
|
178
|
+
/** Model compile pass-through, injectable for tests. */
|
|
179
|
+
compile?: CompileOptions;
|
|
180
|
+
signal?: AbortSignal;
|
|
181
|
+
tmpDir?: string;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* The prediction data: [data] with [predict].data overrides. Unlike the Julia
|
|
185
|
+
* engines, targets are not blanked; Stan predictions come from the model's own
|
|
186
|
+
* generated quantities block, which reads the data as-is.
|
|
187
|
+
*/
|
|
188
|
+
declare function predictData(spec: ResolvedSpec): Record<string, unknown>;
|
|
189
|
+
/**
|
|
190
|
+
* One Stan-CSV of fitted parameters per chain, rebuilt from the samples file.
|
|
191
|
+
* CmdStan's generate_quantities reads parameters by header name and ignores
|
|
192
|
+
* extra columns, so a header plus the parameter draws is sufficient.
|
|
193
|
+
*/
|
|
194
|
+
declare function fittedParamsCsv(samples: Samples, chain: number): string;
|
|
195
|
+
/** Whether a scalarized variable name belongs to a predict target base name. */
|
|
196
|
+
declare function matchesTarget(name: string, targets: readonly string[]): boolean;
|
|
197
|
+
/**
|
|
198
|
+
* Draws posterior-predictive samples through CmdStan's generate_quantities:
|
|
199
|
+
* the model's generated quantities block re-runs once per posterior draw, and
|
|
200
|
+
* the columns matching [predict].targets become the predictive samples file.
|
|
201
|
+
*/
|
|
202
|
+
declare function runPredict(spec: ResolvedSpec, install: CmdStanInstall, io: StanPredictIo): Promise<FitResult>;
|
|
203
|
+
|
|
204
|
+
/** An executable and its arguments, run directly (no shell word-splitting). */
|
|
205
|
+
interface InstallCommand {
|
|
206
|
+
command: string;
|
|
207
|
+
args: string[];
|
|
208
|
+
}
|
|
209
|
+
/** A single provisioning step needed to make the toolchain ready. */
|
|
210
|
+
interface SetupStep {
|
|
211
|
+
tool: "toolchain" | "cmdstan" | "build";
|
|
212
|
+
label: string;
|
|
213
|
+
/** The command to run, or null when the step cannot be performed automatically. */
|
|
214
|
+
command: InstallCommand | null;
|
|
215
|
+
}
|
|
216
|
+
/** The managed home a given CmdStan version installs into. */
|
|
217
|
+
declare function managedCmdStanHome(version: string): string;
|
|
218
|
+
/** The ordered steps needed to reach a ready toolchain, given the current state. */
|
|
219
|
+
declare function planSetup(report: StanDoctorReport, platform: NodeJS.Platform, version?: string): SetupStep[];
|
|
220
|
+
type StepStatus = "ran" | "skipped" | "failed" | "unsupported";
|
|
221
|
+
interface SetupStepResult extends SetupStep {
|
|
222
|
+
status: StepStatus;
|
|
223
|
+
/** Error detail when the step failed. */
|
|
224
|
+
detail?: string;
|
|
225
|
+
}
|
|
226
|
+
interface StanSetupResult {
|
|
227
|
+
cmdstan: ToolInfo;
|
|
228
|
+
stanc: ToolInfo;
|
|
229
|
+
make: ToolInfo;
|
|
230
|
+
cxx: ToolInfo;
|
|
231
|
+
/** True when a Stan fit can run after setup. */
|
|
232
|
+
ready: boolean;
|
|
233
|
+
steps: SetupStepResult[];
|
|
234
|
+
}
|
|
235
|
+
interface StanSetupOptions {
|
|
236
|
+
/** Runs detection commands. Injectable for tests. */
|
|
237
|
+
runner?: CommandRunner;
|
|
238
|
+
/** Runs install steps, which may take several minutes. Injectable for tests. */
|
|
239
|
+
installer?: CommandRunner;
|
|
240
|
+
/** Target platform; defaults to the current process platform. */
|
|
241
|
+
platform?: NodeJS.Platform;
|
|
242
|
+
/** Plan the steps but do not run them. */
|
|
243
|
+
dryRun?: boolean;
|
|
244
|
+
/** CmdStan version to provision; defaults to the pinned version. */
|
|
245
|
+
version?: string;
|
|
246
|
+
}
|
|
247
|
+
/** Installs the CmdStan toolchain needed for Stan inference. */
|
|
248
|
+
declare function runSetup(options?: StanSetupOptions): Promise<StanSetupResult>;
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Lists the installed CmdStan versions, newest first. The newest is the
|
|
252
|
+
* default: the `installed` channel resolves to it.
|
|
253
|
+
*/
|
|
254
|
+
declare function listVersions(): RuntimeVersion[];
|
|
255
|
+
/** Installs a CmdStan version into the managed root. */
|
|
256
|
+
declare function addVersion(version: string, options?: Omit<StanSetupOptions, "version">): Promise<StanSetupResult>;
|
|
257
|
+
/**
|
|
258
|
+
* Removes a managed CmdStan install. Only versions under the managed root are
|
|
259
|
+
* removable; `~/.cmdstan` installs and explicit env-var homes are never touched.
|
|
260
|
+
*/
|
|
261
|
+
declare function removeVersion(version: string): void;
|
|
262
|
+
|
|
263
|
+
export { type CmdStanInstall, type CompileOptions, type CompiledModel, INSTALLED_CMDSTAN_CHANNEL, PINNED_CMDSTAN_VERSION, type SetupStep, type SetupStepResult, type StanCsvTail, type StanDoctorReport, type StanFitIo, type StanMatrixEntry, type StanMatrixIo, type StanMatrixResult, type StanPredictIo, type StanSetupOptions, type StanSetupResult, type StanSpawn, type StanSpawnResult, type StepStatus, addVersion, chainArgs, columnToLeaf, compileModel, createStanCsvTail, createStanSpawn, fittedParamsCsv, isCmdStanHome, listCmdStanInstalls, listVersions, managedCmdStanHome, managedStanRoot, matchesTarget, modelCacheDir, parseIterationLine, planSetup, predictData, removeVersion, resolveCmdStan, runDoctor, runFit, runMatrix, runPredict, runSetup, stanEngine };
|