Skip to content

Commit f469b64

Browse files
committed
Fix convergence chart not recording first set of manual trials
1 parent 311e7d8 commit f469b64

3 files changed

Lines changed: 27 additions & 7 deletions

File tree

client/src/probability-lab/engine/simulate-single.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@ export default function simulateSingleTrials(stateSlice, rng, count) {
2525
stateSlice.trials += 1;
2626
stateSlice.lastIndex = idx;
2727
if (trialHistory) trialHistory.push(idx);
28-
}
2928

30-
if (stateSlice.trials > 0) {
31-
const rel = stateSlice.counts.map((c) => c / stateSlice.trials);
32-
stateSlice.history.push({ trials: stateSlice.trials, rel });
33-
if (stateSlice.history.length > 2000) {
34-
stateSlice.history = stateSlice.history.filter((_, i) => i % 2 === 0);
29+
// Record convergence snapshot after every trial
30+
if (stateSlice.trials > 0) {
31+
const rel = stateSlice.counts.map((c) => c / stateSlice.trials);
32+
stateSlice.history.push({ trials: stateSlice.trials, rel });
3533
}
3634
}
35+
36+
// Decimate history if it exceeds 2000 entries (applied once after the loop)
37+
while (stateSlice.history.length > 2000) {
38+
stateSlice.history = stateSlice.history.filter((_, i) => i % 2 === 0);
39+
}
3740
}

client/src/probability-lab/ui/charts/line-chart.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,20 @@ export default function drawLineChart(canvas, xValues, yValues, theoryValue) {
7373
ctx.setLineDash([]);
7474
ctx.lineWidth = 1;
7575

76-
if (xValues.length < 2 || yValues.length < 2) return;
76+
// Handle single data point case: draw a dot
77+
if (xValues.length === 1 && yValues.length === 1) {
78+
const x = maxX === 0 ? 0 : (xValues[0] / maxX) * plotW;
79+
const y = (1 - clamp(yValues[0], 0, 1)) * plotH;
80+
const px = padding.left + x;
81+
const py = padding.top + y;
82+
ctx.fillStyle = primary;
83+
ctx.beginPath();
84+
ctx.arc(px, py, 3, 0, Math.PI * 2);
85+
ctx.fill();
86+
return;
87+
}
7788

89+
// Multiple data points: draw line with decimation
7890
const step = Math.max(1, Math.ceil(xValues.length / 500));
7991
ctx.strokeStyle = primary;
8092
ctx.lineWidth = 2;

tests/probability-lab/engine/simulate-single.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ describe('simulateSingleTrials', () => {
125125
expect(stateSlice.history.length).toBeLessThanOrEqual(2000);
126126
});
127127

128+
it('history is pruned below 2000 even for large batch counts', () => {
129+
simulateSingleTrials(stateSlice, mockRng, 50000);
130+
expect(stateSlice.history.length).toBeLessThanOrEqual(2000);
131+
});
132+
128133
it('maintains correct state across multiple calls', () => {
129134
simulateSingleTrials(stateSlice, mockRng, 3);
130135
const trialsAfterFirst = stateSlice.trials;

0 commit comments

Comments
 (0)