@adriansteffan/reactive 0.1.2 → 0.1.4
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/.claude/settings.local.json +24 -1
- package/README.md +219 -5
- package/dist/{mod-Bb_FAy0j.js → mod-DHUjIRfF.js} +31267 -14835
- package/dist/mod.d.ts +104 -1
- package/dist/reactive.es.js +50 -41
- package/dist/reactive.umd.js +10202 -55
- package/dist/style.css +1 -1
- package/dist/{web-DmsP-pmx.js → web-BGLPXsYu.js} +1 -1
- package/dist/{web-BfWhpr9p.js → web-DGGPscOK.js} +1 -1
- package/package.json +8 -1
- package/src/components/canvasblock.tsx +4 -3
- package/src/components/plaininput.tsx +2 -1
- package/src/components/quest.tsx +8 -7
- package/src/components/randomdotkinetogram.tsx +36 -25
- package/src/components/text.tsx +2 -1
- package/src/components/upload.tsx +2 -1
- package/src/mod.tsx +4 -0
- package/src/utils/array.ts +4 -2
- package/src/utils/ddm.test.ts +233 -0
- package/src/utils/ddm.ts +230 -0
- package/src/utils/distributions.test.ts +209 -0
- package/src/utils/distributions.ts +191 -0
- package/src/utils/simulation.ts +9 -4
- package/template/simulate.ts +24 -4
- package/template/src/Experiment.tsx +7 -9
package/src/utils/ddm.ts
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { normal, uniform } from './distributions';
|
|
2
|
+
|
|
3
|
+
// --- Distribution & Parameter Types ---
|
|
4
|
+
|
|
5
|
+
export type NormalDist = { type: 'normal'; mean: number; sd: number };
|
|
6
|
+
export type UniformDist = { type: 'uniform'; min: number; max: number };
|
|
7
|
+
export type ParameterDef = number | NormalDist | UniformDist;
|
|
8
|
+
|
|
9
|
+
export type BoundaryDef =
|
|
10
|
+
| number // Symmetric: upper is +b, lower is -b
|
|
11
|
+
| [number, number]; // Asymmetric: [lowerBound, upperBound]
|
|
12
|
+
|
|
13
|
+
export type CollapseDef = {
|
|
14
|
+
rate: number; // Speed at which boundaries shrink per ms
|
|
15
|
+
delay?: number; // Pause in ms (from phase start) before collapse begins
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// --- Post-Stimulus Strategy Types ---
|
|
19
|
+
|
|
20
|
+
export type ContinueStrategy = {
|
|
21
|
+
type: 'continue';
|
|
22
|
+
residualDrift?: number; // Drift rate after stimOffset (Default: 0)
|
|
23
|
+
noiseMultiplier?: number; // Multiplier for noiseLevel (Default: 1)
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export type CollapseStrategy = {
|
|
27
|
+
type: 'collapse';
|
|
28
|
+
collapseDef: CollapseDef; // Takes over boundary dynamics after stimOffset
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export type SnapshotStrategy = {
|
|
32
|
+
type: 'snapshot'; // Immediate forced choice at stimOffset
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export type PostStimStrategyDef = ContinueStrategy | CollapseStrategy | SnapshotStrategy;
|
|
36
|
+
|
|
37
|
+
// --- Main Configuration ---
|
|
38
|
+
|
|
39
|
+
export interface DDMTrialParams {
|
|
40
|
+
// Core cognitive parameters (support distributions for inter-trial variability)
|
|
41
|
+
driftRate: ParameterDef;
|
|
42
|
+
boundaries: BoundaryDef;
|
|
43
|
+
startingPoint: ParameterDef;
|
|
44
|
+
noiseLevel: ParameterDef;
|
|
45
|
+
|
|
46
|
+
// Timing parameters
|
|
47
|
+
sensoryDelay?: ParameterDef; // Dead time before accumulation (ms)
|
|
48
|
+
motorDelay?: ParameterDef; // Dead time for physical execution (ms)
|
|
49
|
+
timeLimit: number; // Maximum allowed trial duration (ms)
|
|
50
|
+
stimOffset: number; // Timestamp when stimulus vanishes (ms)
|
|
51
|
+
dt?: number; // Resolution in ms (Default: 1)
|
|
52
|
+
|
|
53
|
+
// Global behaviors
|
|
54
|
+
contaminationRate?: number; // Probability (0-1) of a random guess
|
|
55
|
+
stimulusCollapse?: CollapseDef; // Boundary dynamics DURING stimulus visibility
|
|
56
|
+
|
|
57
|
+
// Post-stimulus behavior
|
|
58
|
+
postStimStrategy: PostStimStrategyDef;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// --- Result ---
|
|
62
|
+
|
|
63
|
+
export interface DDMTrialResult {
|
|
64
|
+
choice: 0 | 1 | null;
|
|
65
|
+
rt: number; // Total reaction time including delays (ms)
|
|
66
|
+
finalEvidence: number; // Accumulator value at trial end
|
|
67
|
+
isContaminated: boolean; // True if trial was a random lapse
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// --- Helpers ---
|
|
71
|
+
|
|
72
|
+
function resolveParam(def: ParameterDef): number {
|
|
73
|
+
if (typeof def === 'number') return def;
|
|
74
|
+
if (def.type === 'normal') return normal(def.mean, def.sd);
|
|
75
|
+
return uniform(def.min, def.max);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function resolveParamClamped(def: ParameterDef): number {
|
|
79
|
+
return Math.max(0, resolveParam(def));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// --- Simulation ---
|
|
83
|
+
|
|
84
|
+
export function simulateDDMTrial(params: DDMTrialParams): DDMTrialResult {
|
|
85
|
+
const dt = params.dt ?? 1;
|
|
86
|
+
const sqrtDt = Math.sqrt(dt);
|
|
87
|
+
|
|
88
|
+
// A1. Resolve starting point
|
|
89
|
+
const startingPoint = resolveParam(params.startingPoint);
|
|
90
|
+
|
|
91
|
+
// A2. Contamination check
|
|
92
|
+
if (params.contaminationRate && uniform(0, 1) < params.contaminationRate) {
|
|
93
|
+
return {
|
|
94
|
+
choice: uniform(0, 1) < 0.5 ? 0 : 1,
|
|
95
|
+
rt: uniform(0, params.timeLimit),
|
|
96
|
+
finalEvidence: startingPoint,
|
|
97
|
+
isContaminated: true,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// A3. Resolve remaining distributions for this trial
|
|
102
|
+
const baseDrift = resolveParam(params.driftRate);
|
|
103
|
+
const baseNoise = Math.max(0, resolveParam(params.noiseLevel));
|
|
104
|
+
const sensoryDelay = params.sensoryDelay != null ? resolveParamClamped(params.sensoryDelay) : 0;
|
|
105
|
+
const motorDelay = params.motorDelay != null ? resolveParamClamped(params.motorDelay) : 0;
|
|
106
|
+
|
|
107
|
+
// A4. Resolve boundaries
|
|
108
|
+
let currentUpper: number;
|
|
109
|
+
let currentLower: number;
|
|
110
|
+
if (Array.isArray(params.boundaries)) {
|
|
111
|
+
currentLower = params.boundaries[0];
|
|
112
|
+
currentUpper = params.boundaries[1];
|
|
113
|
+
} else {
|
|
114
|
+
currentUpper = params.boundaries;
|
|
115
|
+
currentLower = -params.boundaries;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// A5. Fast-forward clock past sensory delay
|
|
119
|
+
let time = sensoryDelay;
|
|
120
|
+
let evidence = startingPoint;
|
|
121
|
+
let boundaryHit = false;
|
|
122
|
+
let choice: 0 | 1 | null = null;
|
|
123
|
+
|
|
124
|
+
// B. Simulation loop
|
|
125
|
+
while (time < params.timeLimit) {
|
|
126
|
+
time += dt;
|
|
127
|
+
|
|
128
|
+
let currentDrift = baseDrift;
|
|
129
|
+
let currentNoise = baseNoise;
|
|
130
|
+
|
|
131
|
+
if (time <= params.stimOffset) {
|
|
132
|
+
// Phase 1: Stimulus visible
|
|
133
|
+
if (params.stimulusCollapse) {
|
|
134
|
+
const elapsed = time - sensoryDelay;
|
|
135
|
+
if (elapsed > (params.stimulusCollapse.delay ?? 0)) {
|
|
136
|
+
currentUpper -= params.stimulusCollapse.rate * dt;
|
|
137
|
+
currentLower += params.stimulusCollapse.rate * dt;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
} else {
|
|
141
|
+
// Phase 2: Post-stimulus
|
|
142
|
+
const strategy = params.postStimStrategy;
|
|
143
|
+
if (strategy.type === 'snapshot') {
|
|
144
|
+
const distToUpper = Math.abs(evidence - currentUpper);
|
|
145
|
+
const distToLower = Math.abs(evidence - currentLower);
|
|
146
|
+
return {
|
|
147
|
+
choice: distToUpper <= distToLower ? 1 : 0,
|
|
148
|
+
rt: time + motorDelay,
|
|
149
|
+
finalEvidence: evidence,
|
|
150
|
+
isContaminated: false,
|
|
151
|
+
};
|
|
152
|
+
} else if (strategy.type === 'continue') {
|
|
153
|
+
currentDrift = strategy.residualDrift ?? 0;
|
|
154
|
+
currentNoise = baseNoise * (strategy.noiseMultiplier ?? 1);
|
|
155
|
+
} else {
|
|
156
|
+
// collapse
|
|
157
|
+
currentDrift = 0;
|
|
158
|
+
const elapsed = time - params.stimOffset;
|
|
159
|
+
if (elapsed > (strategy.collapseDef.delay ?? 0)) {
|
|
160
|
+
currentUpper -= strategy.collapseDef.rate * dt;
|
|
161
|
+
currentLower += strategy.collapseDef.rate * dt;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// B3. Check if evidence crossed or was passed by a boundary
|
|
167
|
+
if (evidence >= currentUpper) {
|
|
168
|
+
choice = 1;
|
|
169
|
+
boundaryHit = true;
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
if (evidence <= currentLower) {
|
|
173
|
+
choice = 0;
|
|
174
|
+
boundaryHit = true;
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// B4. Step evidence
|
|
179
|
+
evidence += currentDrift * dt + currentNoise * sqrtDt * normal(0, 1);
|
|
180
|
+
|
|
181
|
+
// B5. Check thresholds after stepping
|
|
182
|
+
if (evidence >= currentUpper) {
|
|
183
|
+
choice = 1;
|
|
184
|
+
boundaryHit = true;
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
if (evidence <= currentLower) {
|
|
188
|
+
choice = 0;
|
|
189
|
+
boundaryHit = true;
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// C. Finalize output
|
|
195
|
+
let rt: number;
|
|
196
|
+
if (boundaryHit) {
|
|
197
|
+
rt = time + motorDelay;
|
|
198
|
+
} else {
|
|
199
|
+
// timeLimit reached; no decision made
|
|
200
|
+
rt = params.timeLimit;
|
|
201
|
+
choice = null;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return { choice, rt, finalEvidence: evidence, isContaminated: false };
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export function mapDDMChoice(
|
|
208
|
+
choice: 0 | 1 | null,
|
|
209
|
+
validKeys: string[],
|
|
210
|
+
correctResponse?: string | string[],
|
|
211
|
+
): string | null {
|
|
212
|
+
if (choice == null || validKeys.length === 0) return null;
|
|
213
|
+
const correctKeys = Array.isArray(correctResponse)
|
|
214
|
+
? correctResponse.map((k) => k.toLowerCase())
|
|
215
|
+
: correctResponse ? [correctResponse.toLowerCase()] : null;
|
|
216
|
+
|
|
217
|
+
if (!correctKeys) {
|
|
218
|
+
return validKeys[Math.floor(uniform(0, validKeys.length))];
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (choice === 1) {
|
|
222
|
+
const matches = validKeys.filter((k) => correctKeys.includes(k.toLowerCase()));
|
|
223
|
+
if (matches.length > 0) return matches[Math.floor(uniform(0, matches.length))];
|
|
224
|
+
} else {
|
|
225
|
+
const wrong = validKeys.filter((k) => !correctKeys.includes(k.toLowerCase()));
|
|
226
|
+
if (wrong.length > 0) return wrong[Math.floor(uniform(0, wrong.length))];
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
return validKeys[Math.floor(uniform(0, validKeys.length))];
|
|
230
|
+
}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { sobol, halton, seedDistributions, normal, uniform, poisson, sampleParticipants } from './distributions';
|
|
3
|
+
|
|
4
|
+
// Known Sobol sequence values (1D, Van der Corput base 2, skipping the zero point)
|
|
5
|
+
// Reference: https://en.wikipedia.org/wiki/Van_der_Corput_sequence
|
|
6
|
+
const SOBOL_1D_EXPECTED = [0.5, 0.75, 0.25, 0.375, 0.875, 0.625, 0.125, 0.1875];
|
|
7
|
+
|
|
8
|
+
// Known Halton sequence values (base 2 and 3, skipping the zero point)
|
|
9
|
+
// Reference: https://en.wikipedia.org/wiki/Halton_sequence
|
|
10
|
+
const HALTON_2D_EXPECTED = [
|
|
11
|
+
[0.5, 1 / 3],
|
|
12
|
+
[0.25, 2 / 3],
|
|
13
|
+
[0.75, 1 / 9],
|
|
14
|
+
[0.125, 4 / 9],
|
|
15
|
+
[0.625, 7 / 9],
|
|
16
|
+
];
|
|
17
|
+
|
|
18
|
+
describe('sobol', () => {
|
|
19
|
+
it('produces known 1D Van der Corput values', () => {
|
|
20
|
+
const result = sobol(8, [{ distribution: 'uniform', min: 0, max: 1 }]) as number[];
|
|
21
|
+
expect(result).toHaveLength(8);
|
|
22
|
+
for (let i = 0; i < SOBOL_1D_EXPECTED.length; i++) {
|
|
23
|
+
expect(result[i]).toBeCloseTo(SOBOL_1D_EXPECTED[i], 10);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('produces known 2D values', () => {
|
|
28
|
+
const result = sobol(4, [
|
|
29
|
+
{ distribution: 'uniform', min: 0, max: 1 },
|
|
30
|
+
{ distribution: 'uniform', min: 0, max: 1 },
|
|
31
|
+
]) as number[][];
|
|
32
|
+
// First 2D Sobol points (after skipping zero): [0.5, 0.5], [0.75, 0.25], [0.25, 0.75]
|
|
33
|
+
expect(result[0]).toEqual([expect.closeTo(0.5, 10), expect.closeTo(0.5, 10)]);
|
|
34
|
+
expect(result[1]).toEqual([expect.closeTo(0.75, 10), expect.closeTo(0.25, 10)]);
|
|
35
|
+
expect(result[2]).toEqual([expect.closeTo(0.25, 10), expect.closeTo(0.75, 10)]);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('scales uniform dimensions correctly', () => {
|
|
39
|
+
const result = sobol(3, [{ distribution: 'uniform', min: 200, max: 800 }]) as number[];
|
|
40
|
+
// 0.5 * 600 + 200 = 500, 0.75 * 600 + 200 = 650, 0.25 * 600 + 200 = 350
|
|
41
|
+
expect(result[0]).toBeCloseTo(500, 10);
|
|
42
|
+
expect(result[1]).toBeCloseTo(650, 10);
|
|
43
|
+
expect(result[2]).toBeCloseTo(350, 10);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('produces correct count of points', () => {
|
|
47
|
+
const result = sobol(100, [{ distribution: 'uniform', min: 0, max: 1 }]) as number[];
|
|
48
|
+
expect(result).toHaveLength(100);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('returns flat array for 1D, nested for multi-D', () => {
|
|
52
|
+
const r1 = sobol(3, [{ distribution: 'uniform', min: 0, max: 1 }]);
|
|
53
|
+
expect(typeof r1[0]).toBe('number');
|
|
54
|
+
|
|
55
|
+
const r2 = sobol(3, [
|
|
56
|
+
{ distribution: 'uniform', min: 0, max: 1 },
|
|
57
|
+
{ distribution: 'uniform', min: 0, max: 1 },
|
|
58
|
+
]);
|
|
59
|
+
expect(Array.isArray(r2[0])).toBe(true);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('all 1D values are in [0, 1] for unit range', () => {
|
|
63
|
+
const result = sobol(1000, [{ distribution: 'uniform', min: 0, max: 1 }]) as number[];
|
|
64
|
+
for (const v of result) {
|
|
65
|
+
expect(v).toBeGreaterThanOrEqual(0);
|
|
66
|
+
expect(v).toBeLessThan(1);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('produces no duplicate points in 2D', () => {
|
|
71
|
+
const result = sobol(100, [
|
|
72
|
+
{ distribution: 'uniform', min: 0, max: 1 },
|
|
73
|
+
{ distribution: 'uniform', min: 0, max: 1 },
|
|
74
|
+
]) as number[][];
|
|
75
|
+
const keys = new Set(result.map(([a, b]) => `${a},${b}`));
|
|
76
|
+
expect(keys.size).toBe(100);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('is deterministic (same call twice gives same result)', () => {
|
|
80
|
+
const a = sobol(50, [{ distribution: 'uniform', min: 0, max: 1 }]);
|
|
81
|
+
const b = sobol(50, [{ distribution: 'uniform', min: 0, max: 1 }]);
|
|
82
|
+
expect(a).toEqual(b);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('throws for dimensions > 21', () => {
|
|
86
|
+
const specs = Array.from({ length: 22 }, () => ({ distribution: 'uniform' as const, min: 0, max: 1 }));
|
|
87
|
+
expect(() => sobol(10, specs)).toThrow('1-21 dimensions');
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
describe('halton', () => {
|
|
92
|
+
it('produces known 2D values (bases 2, 3)', () => {
|
|
93
|
+
const result = halton(5, [
|
|
94
|
+
{ distribution: 'uniform', min: 0, max: 1 },
|
|
95
|
+
{ distribution: 'uniform', min: 0, max: 1 },
|
|
96
|
+
]) as number[][];
|
|
97
|
+
for (let i = 0; i < HALTON_2D_EXPECTED.length; i++) {
|
|
98
|
+
expect(result[i][0]).toBeCloseTo(HALTON_2D_EXPECTED[i][0], 10);
|
|
99
|
+
expect(result[i][1]).toBeCloseTo(HALTON_2D_EXPECTED[i][1], 10);
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('is deterministic', () => {
|
|
104
|
+
const a = halton(50, [{ distribution: 'uniform', min: 0, max: 1 }]);
|
|
105
|
+
const b = halton(50, [{ distribution: 'uniform', min: 0, max: 1 }]);
|
|
106
|
+
expect(a).toEqual(b);
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
describe('normal distribution spec', () => {
|
|
111
|
+
it('produces values centered around the mean', () => {
|
|
112
|
+
const result = sobol(1000, [{ distribution: 'normal', mean: 500, sd: 100 }]) as number[];
|
|
113
|
+
const avg = result.reduce((a, b) => a + b, 0) / result.length;
|
|
114
|
+
expect(avg).toBeCloseTo(500, 0);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('median is close to mean (symmetric distribution)', () => {
|
|
118
|
+
const result = sobol(1000, [{ distribution: 'normal', mean: 500, sd: 100 }]) as number[];
|
|
119
|
+
const sorted = [...result].sort((a, b) => a - b);
|
|
120
|
+
const median = sorted[500];
|
|
121
|
+
expect(median).toBeCloseTo(500, 0);
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
describe('poisson distribution spec', () => {
|
|
126
|
+
it('produces non-negative integers', () => {
|
|
127
|
+
const result = sobol(100, [{ distribution: 'poisson', mean: 5 }]) as number[];
|
|
128
|
+
for (const v of result) {
|
|
129
|
+
expect(v).toBeGreaterThanOrEqual(0);
|
|
130
|
+
expect(Number.isInteger(v)).toBe(true);
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it('mean is close to lambda', () => {
|
|
135
|
+
const result = sobol(1000, [{ distribution: 'poisson', mean: 5 }]) as number[];
|
|
136
|
+
const avg = result.reduce((a, b) => a + b, 0) / result.length;
|
|
137
|
+
expect(avg).toBeCloseTo(5, 0);
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
describe('sampleParticipants', () => {
|
|
142
|
+
it('returns objects with the correct keys', () => {
|
|
143
|
+
const result = sampleParticipants('sobol', 5, {
|
|
144
|
+
rtMean: { distribution: 'normal', mean: 500, sd: 100 },
|
|
145
|
+
threshold: { distribution: 'uniform', min: 0.3, max: 0.7 },
|
|
146
|
+
});
|
|
147
|
+
expect(result).toHaveLength(5);
|
|
148
|
+
for (const p of result) {
|
|
149
|
+
expect(p).toHaveProperty('rtMean');
|
|
150
|
+
expect(p).toHaveProperty('threshold');
|
|
151
|
+
expect(typeof p.rtMean).toBe('number');
|
|
152
|
+
expect(typeof p.threshold).toBe('number');
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it('sobol produces deterministic results', () => {
|
|
157
|
+
const a = sampleParticipants('sobol', 10, { x: { distribution: 'uniform', min: 0, max: 1 } });
|
|
158
|
+
const b = sampleParticipants('sobol', 10, { x: { distribution: 'uniform', min: 0, max: 1 } });
|
|
159
|
+
expect(a).toEqual(b);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it('halton produces deterministic results', () => {
|
|
163
|
+
const a = sampleParticipants('halton', 10, { x: { distribution: 'uniform', min: 0, max: 1 } });
|
|
164
|
+
const b = sampleParticipants('halton', 10, { x: { distribution: 'uniform', min: 0, max: 1 } });
|
|
165
|
+
expect(a).toEqual(b);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it('random method produces valid values', () => {
|
|
169
|
+
const result = sampleParticipants('random', 50, {
|
|
170
|
+
score: { distribution: 'poisson', mean: 5 },
|
|
171
|
+
});
|
|
172
|
+
expect(result).toHaveLength(50);
|
|
173
|
+
for (const p of result) {
|
|
174
|
+
expect(Number.isInteger(p.score)).toBe(true);
|
|
175
|
+
expect(p.score).toBeGreaterThanOrEqual(0);
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it('works with a single dimension', () => {
|
|
180
|
+
const result = sampleParticipants('sobol', 3, {
|
|
181
|
+
rt: { distribution: 'uniform', min: 200, max: 800 },
|
|
182
|
+
});
|
|
183
|
+
expect(result[0].rt).toBeCloseTo(500, 10);
|
|
184
|
+
expect(result[1].rt).toBeCloseTo(650, 10);
|
|
185
|
+
expect(result[2].rt).toBeCloseTo(350, 10);
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
describe('seedDistributions', () => {
|
|
190
|
+
it('makes pseudorandom samplers reproducible', () => {
|
|
191
|
+
seedDistributions(42);
|
|
192
|
+
const a = [normal(0, 1), uniform(0, 10), poisson(5)];
|
|
193
|
+
|
|
194
|
+
seedDistributions(42);
|
|
195
|
+
const b = [normal(0, 1), uniform(0, 10), poisson(5)];
|
|
196
|
+
|
|
197
|
+
expect(a).toEqual(b);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it('different seeds produce different sequences', () => {
|
|
201
|
+
seedDistributions(1);
|
|
202
|
+
const a = normal(0, 1);
|
|
203
|
+
|
|
204
|
+
seedDistributions(2);
|
|
205
|
+
const b = normal(0, 1);
|
|
206
|
+
|
|
207
|
+
expect(a).not.toBe(b);
|
|
208
|
+
});
|
|
209
|
+
});
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import mt19937 from '@stdlib/random-base-mt19937';
|
|
2
|
+
import normalFactory from '@stdlib/random-base-normal';
|
|
3
|
+
import poissonFactory from '@stdlib/random-base-poisson';
|
|
4
|
+
import uniformFactory from '@stdlib/random-base-uniform';
|
|
5
|
+
import normalQuantile from '@stdlib/stats-base-dists-normal-quantile';
|
|
6
|
+
import poissonQuantile from '@stdlib/stats-base-dists-poisson-quantile';
|
|
7
|
+
import generateHalton from 'halton';
|
|
8
|
+
|
|
9
|
+
// --- Pseudorandom samplers (seedable) ---
|
|
10
|
+
|
|
11
|
+
let normal: (mu: number, sigma: number) => number = normalFactory;
|
|
12
|
+
let poisson: (lambda: number) => number = poissonFactory;
|
|
13
|
+
let uniform: (min: number, max: number) => number = uniformFactory;
|
|
14
|
+
|
|
15
|
+
interface StdlibPRNG {
|
|
16
|
+
normalized: () => number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function seedDistributions(seed: number): void {
|
|
20
|
+
const prng = mt19937.factory({ seed }) as unknown as StdlibPRNG;
|
|
21
|
+
normal = normalFactory.factory({ prng: prng.normalized });
|
|
22
|
+
poisson = poissonFactory.factory({ prng: prng.normalized });
|
|
23
|
+
uniform = uniformFactory.factory({ prng: prng.normalized });
|
|
24
|
+
Math.random = () => uniform(0, 1);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// --- QMC dimension specs ---
|
|
28
|
+
// Inverse CDF transform: https://en.wikipedia.org/wiki/Inverse_transform_sampling
|
|
29
|
+
|
|
30
|
+
type UniformSpec = { distribution: 'uniform'; min: number; max: number };
|
|
31
|
+
type NormalSpec = { distribution: 'normal'; mean: number; sd: number };
|
|
32
|
+
type PoissonSpec = { distribution: 'poisson'; mean: number };
|
|
33
|
+
type DimensionSpec = UniformSpec | NormalSpec | PoissonSpec;
|
|
34
|
+
|
|
35
|
+
// Inverse CDFs return +-Infinity at exactly 0 or 1, so clamp to open interval (0, 1).
|
|
36
|
+
function clampUnit(value: number): number {
|
|
37
|
+
return Math.max(1e-10, Math.min(1 - 1e-10, value));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function resolveTransform(spec: DimensionSpec): (unitValue: number) => number {
|
|
41
|
+
switch (spec.distribution) {
|
|
42
|
+
case 'uniform':
|
|
43
|
+
return (u) => spec.min + u * (spec.max - spec.min);
|
|
44
|
+
case 'normal':
|
|
45
|
+
return (u) => normalQuantile(clampUnit(u), spec.mean, spec.sd);
|
|
46
|
+
case 'poisson':
|
|
47
|
+
// QMC variance reduction is less effective on discrete distributions
|
|
48
|
+
// due to integer clumping at quantile boundaries.
|
|
49
|
+
return (u) => poissonQuantile(clampUnit(u), spec.mean);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function transformPoints(unitPoints: number[][], specs: DimensionSpec[]): number[] | number[][] {
|
|
54
|
+
const transforms = specs.map(resolveTransform);
|
|
55
|
+
if (specs.length === 1) {
|
|
56
|
+
return unitPoints.map((point) => transforms[0](point[0]));
|
|
57
|
+
}
|
|
58
|
+
return unitPoints.map((point) =>
|
|
59
|
+
point.map((unitValue, dim) => transforms[dim](unitValue)),
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// --- Sobol sequence (browser-compatible implementation as existing packages want to use fs) ---
|
|
64
|
+
// Algorithm: Bratley & Fox (1988), https://doi.org/10.1145/42288.214372
|
|
65
|
+
// Joe-Kuo direction numbers for dimensions 2-21 (dimension 1 uses Van der Corput base 2).
|
|
66
|
+
// Each entry is [degree, polynomialCoefficients, initialDirectionNumbers].
|
|
67
|
+
// Direction numbers: Joe & Kuo (2008), https://doi.org/10.1137/070709359
|
|
68
|
+
// Data: https://web.maths.unsw.edu.au/~fkuo/sobol/joe-kuo-old.1111
|
|
69
|
+
const JOE_KUO: [number, number, number[]][] = [
|
|
70
|
+
[1, 0, [1]], // dim 2
|
|
71
|
+
[2, 1, [1, 1]], // dim 3
|
|
72
|
+
[3, 1, [1, 3, 7]], // dim 4
|
|
73
|
+
[3, 2, [1, 1, 5]], // dim 5
|
|
74
|
+
[4, 1, [1, 3, 1, 1]], // dim 6
|
|
75
|
+
[4, 4, [1, 1, 3, 7]], // dim 7
|
|
76
|
+
[5, 2, [1, 3, 3, 9, 9]], // dim 8
|
|
77
|
+
[5, 13, [1, 3, 7, 13, 3]], // dim 9
|
|
78
|
+
[5, 7, [1, 1, 5, 11, 27]], // dim 10
|
|
79
|
+
[5, 14, [1, 3, 5, 1, 15]], // dim 11
|
|
80
|
+
[5, 11, [1, 1, 7, 3, 29]], // dim 12
|
|
81
|
+
[5, 4, [1, 3, 7, 7, 21]], // dim 13
|
|
82
|
+
[6, 1, [1, 1, 1, 9, 23, 37]], // dim 14
|
|
83
|
+
[6, 16, [1, 3, 3, 5, 19, 33]], // dim 15
|
|
84
|
+
[6, 13, [1, 1, 3, 13, 11, 7]], // dim 16
|
|
85
|
+
[6, 22, [1, 1, 7, 13, 25, 5]], // dim 17
|
|
86
|
+
[6, 19, [1, 3, 5, 11, 7, 11]], // dim 18
|
|
87
|
+
[6, 25, [1, 1, 1, 3, 13, 39]], // dim 19
|
|
88
|
+
[7, 1, [1, 3, 1, 15, 17, 63, 13]], // dim 20
|
|
89
|
+
[7, 32, [1, 1, 5, 5, 1, 59, 33]], // dim 21
|
|
90
|
+
];
|
|
91
|
+
|
|
92
|
+
const BITS = 32;
|
|
93
|
+
const SCALE = 2 ** BITS;
|
|
94
|
+
|
|
95
|
+
function buildDirectionNumbers(dimension: number): Uint32Array {
|
|
96
|
+
const directions = new Uint32Array(BITS);
|
|
97
|
+
if (dimension === 0) {
|
|
98
|
+
for (let i = 0; i < BITS; i++) directions[i] = 1 << (BITS - 1 - i);
|
|
99
|
+
return directions;
|
|
100
|
+
}
|
|
101
|
+
const [degree, coefficients, initial] = JOE_KUO[dimension - 1];
|
|
102
|
+
for (let i = 0; i < degree; i++) directions[i] = initial[i] << (BITS - 1 - i);
|
|
103
|
+
for (let i = degree; i < BITS; i++) {
|
|
104
|
+
directions[i] = directions[i - degree] ^ (directions[i - degree] >>> degree);
|
|
105
|
+
for (let j = 1; j < degree; j++) {
|
|
106
|
+
if ((coefficients >>> (degree - 1 - j)) & 1) directions[i] ^= directions[i - j];
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return directions;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function generateSobol(count: number, dimensions: number): number[][] {
|
|
113
|
+
if (dimensions < 1 || dimensions > 21) {
|
|
114
|
+
throw new RangeError('sobol() supports 1-21 dimensions');
|
|
115
|
+
}
|
|
116
|
+
const allDirections = Array.from({ length: dimensions }, (_, dim) => buildDirectionNumbers(dim));
|
|
117
|
+
const state = new Uint32Array(dimensions);
|
|
118
|
+
const points: number[][] = [new Array<number>(dimensions).fill(0)];
|
|
119
|
+
for (let index = 1; index < count; index++) {
|
|
120
|
+
// Find the position of the rightmost zero bit
|
|
121
|
+
let rightmostZero = 0;
|
|
122
|
+
let bits = index - 1;
|
|
123
|
+
while ((bits & 1) === 1) { bits >>>= 1; rightmostZero++; }
|
|
124
|
+
for (let dim = 0; dim < dimensions; dim++) state[dim] ^= allDirections[dim][rightmostZero];
|
|
125
|
+
points.push(Array.from(state, (value) => value / SCALE));
|
|
126
|
+
}
|
|
127
|
+
return points;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// --- Halton sequence ---
|
|
131
|
+
// Halton (1960), https://doi.org/10.1007/BF01386213
|
|
132
|
+
const PRIMES = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73];
|
|
133
|
+
const HALTON_MAX_EFFECTIVE_DIMS = 6;
|
|
134
|
+
|
|
135
|
+
function sobol(count: number, specs: DimensionSpec[]): number[] | number[][] {
|
|
136
|
+
// Generate count+1 and skip the first point (always zero in every dimension)
|
|
137
|
+
const unitPoints = generateSobol(count + 1, specs.length).slice(1);
|
|
138
|
+
return transformPoints(unitPoints, specs);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function halton(count: number, specs: DimensionSpec[]): number[] | number[][] {
|
|
142
|
+
if (specs.length > HALTON_MAX_EFFECTIVE_DIMS) {
|
|
143
|
+
console.warn(`Halton sequence quality degrades above ${HALTON_MAX_EFFECTIVE_DIMS} dimensions. Consider using sobol() instead.`);
|
|
144
|
+
}
|
|
145
|
+
const bases = PRIMES.slice(0, specs.length);
|
|
146
|
+
// Generate count+1 and skip the first point (always zero in every dimension)
|
|
147
|
+
const unitPoints = (generateHalton(count + 1, bases) as number[][]).slice(1);
|
|
148
|
+
return transformPoints(unitPoints, specs);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
type SamplingMethod = 'sobol' | 'halton' | 'random';
|
|
152
|
+
|
|
153
|
+
function sampleParticipants<T extends Record<string, DimensionSpec>>(
|
|
154
|
+
method: SamplingMethod,
|
|
155
|
+
count: number,
|
|
156
|
+
specs: T,
|
|
157
|
+
): Array<{ [K in keyof T]: number }> {
|
|
158
|
+
const keys = Object.keys(specs) as (keyof T & string)[];
|
|
159
|
+
const dimSpecs = keys.map((k) => specs[k]);
|
|
160
|
+
|
|
161
|
+
let points: number[] | number[][];
|
|
162
|
+
switch (method) {
|
|
163
|
+
case 'sobol':
|
|
164
|
+
points = sobol(count, dimSpecs);
|
|
165
|
+
break;
|
|
166
|
+
case 'halton':
|
|
167
|
+
points = halton(count, dimSpecs);
|
|
168
|
+
break;
|
|
169
|
+
case 'random':
|
|
170
|
+
points = randomPoints(count, dimSpecs);
|
|
171
|
+
break;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return Array.from({ length: count }, (_, i) => {
|
|
175
|
+
const row = keys.length === 1 ? [points[i] as number] : (points[i] as number[]);
|
|
176
|
+
return Object.fromEntries(keys.map((k, d) => [k, row[d]])) as { [K in keyof T]: number };
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function randomPoints(count: number, specs: DimensionSpec[]): number[] | number[][] {
|
|
181
|
+
const transforms = specs.map(resolveTransform);
|
|
182
|
+
if (specs.length === 1) {
|
|
183
|
+
return Array.from({ length: count }, () => transforms[0](uniform(0, 1)));
|
|
184
|
+
}
|
|
185
|
+
return Array.from({ length: count }, () =>
|
|
186
|
+
transforms.map((t) => t(uniform(0, 1))),
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export { normal, poisson, uniform, seedDistributions, sobol, halton, sampleParticipants };
|
|
191
|
+
export type { DimensionSpec, UniformSpec, NormalSpec, PoissonSpec, SamplingMethod };
|
package/src/utils/simulation.ts
CHANGED
|
@@ -151,7 +151,9 @@ export async function simulateParticipant(
|
|
|
151
151
|
|
|
152
152
|
|
|
153
153
|
export interface RunSimulationConfig {
|
|
154
|
-
participants: ParticipantState[] | { generator: (index: number) => ParticipantState; count: number };
|
|
154
|
+
participants: ParticipantState[] | { generator: (index: number) => ParticipantState; count: number } | (() => ParticipantState[]);
|
|
155
|
+
/** Seed for reproducible simulations. Each participant gets seed + workerIndex. */
|
|
156
|
+
seed?: number;
|
|
155
157
|
backendPort?: number;
|
|
156
158
|
concurrency?: number;
|
|
157
159
|
}
|
|
@@ -162,9 +164,11 @@ export async function orchestrateSimulation(config: RunSimulationConfig, scriptP
|
|
|
162
164
|
const port = config.backendPort ?? 8001;
|
|
163
165
|
const backendUrl = `http://localhost:${port}/backend`;
|
|
164
166
|
|
|
165
|
-
const participantCount =
|
|
166
|
-
? config.participants.length
|
|
167
|
-
: config.participants
|
|
167
|
+
const participantCount = typeof config.participants === 'function'
|
|
168
|
+
? config.participants().length
|
|
169
|
+
: Array.isArray(config.participants)
|
|
170
|
+
? config.participants.length
|
|
171
|
+
: config.participants.count;
|
|
168
172
|
|
|
169
173
|
const backend = spawn('npx', ['tsx', 'src/backend.ts'], {
|
|
170
174
|
cwd: './backend',
|
|
@@ -212,6 +216,7 @@ export async function orchestrateSimulation(config: RunSimulationConfig, scriptP
|
|
|
212
216
|
...process.env,
|
|
213
217
|
_REACTIVE_WORKER_INDEX: String(i),
|
|
214
218
|
_REACTIVE_BACKEND_URL: backendUrl,
|
|
219
|
+
...(config.seed !== undefined ? { _REACTIVE_SIMULATION_SEED: String(config.seed) } : {}),
|
|
215
220
|
},
|
|
216
221
|
stdio: ['ignore', 'pipe', 'pipe'],
|
|
217
222
|
});
|