Prompt specification
Build or customize the Three.js 3D scene "Observed sample and bootstrap distribution" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Observed sample and bootstrap distribution
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/observed-sample-and-bootstrap-distribution-x1774
Scene Description Prompt:
Generate many seeded resamples of one synthetic dataset and stack their means; distinguish the empirical resampling distribution from the unavailable real population distribution. Build this with Three.js from cdnjs as a stylized scene in one HTML file.
Working Three.js Starter (observed-sample-and-bootstrap-distribution-x1774):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="color-scheme" content="light" />
<title>Bootstrap: Observable Resampling, Unavailable Population</title>
<style>
:root {
--ink: #173235;
--muted: #667779;
--paper: #f4f0e8;
--panel: rgba(255, 253, 248, 0.88);
--teal: #087d78;
--coral: #f36f59;
--amber: #e6a72e;
--line: rgba(23, 50, 53, 0.14);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--paper);
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#viewport {
position: fixed;
inset: 0;
}
#viewport canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#viewport canvas:active {
cursor: grabbing;
}
.topbar {
position: fixed;
z-index: 10;
top: 26px;
left: 30px;
right: 30px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 28px;
pointer-events: none;
}
.brand {
max-width: 620px;
}
.eyebrow {
margin-bottom: 8px;
color: var(--teal);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(25px, 2.7vw, 43px);
font-weight: 760;
letter-spacing: -0.045em;
line-height: 1.05;
}
h1 span {
color: var(--muted);
font-weight: 520;
}
.stats {
display: grid;
grid-template-columns: repeat(2, minmax(145px, 1fr));
gap: 8px;
width: min(420px, 42vw);
}
.stat {
min-height: 67px;
padding: 11px 13px;
border: 1px solid rgba(255, 255, 255, 0.72);
border-radius: 13px;
background: rgba(255, 253, 248, 0.72);
box-shadow: 0 10px 35px rgba(31, 48, 48, 0.06);
backdrop-filter: blur(12px);
}
.stat-label {
display: block;
margin-bottom: 5px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.stat-value {
display: block;
font-variant-numeric: tabular-nums;
font-size: 16px;
font-weight: 760;
letter-spacing: -0.025em;
}
.controls {
position: fixed;
z-index: 20;
left: 30px;
bottom: 26px;
width: min(390px, calc(100vw - 60px));
padding: 16px;
border: 1px solid rgba(255, 255, 255, 0.78);
border-radius: 17px;
background: var(--panel);
box-shadow: 0 18px 60px rgba(31, 48, 48, 0.12);
backdrop-filter: blur(15px);
}
.control-row {
display: grid;
grid-template-columns: 1fr auto;
align-items: center;
gap: 12px;
margin-bottom: 12px;
}
.control-row label {
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.control-row output {
color: var(--ink);
font-size: 12px;
font-weight: 800;
font-variant-numeric: tabular-nums;
}
.seed-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-bottom: 13px;
}
input[type="number"] {
width: 100%;
height: 38px;
padding: 0 11px;
border: 1px solid var(--line);
border-radius: 9px;
outline: none;
background: rgba(255, 255, 255, 0.72);
color: var(--ink);
font-size: 13px;
font-weight: 700;
}
input[type="number"]:focus {
border-color: var(--teal);
box-shadow: 0 0 0 3px rgba(8, 125, 120, 0.1);
}
input[type="range"] {
width: 100%;
accent-color: var(--coral);
cursor: pointer;
}
.buttons {
display: flex;
gap: 8px;
}
button {
flex: 1;
height: 38px;
border: 1px solid var(--line);
border-radius: 9px;
background: transparent;
color: var(--ink);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.045em;
cursor: pointer;
transition:
transform 120ms ease,
background 120ms ease,
border-color 120ms ease;
}
button:hover {
transform: translateY(-1px);
border-color: rgba(23, 50, 53, 0.3);
background: rgba(255, 255, 255, 0.75);
}
button.primary {
border-color: var(--ink);
background: var(--ink);
color: #fffdf8;
}
button.primary:hover {
background: #24474a;
}
.method {
margin-top: 13px;
padding-top: 12px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.method strong {
color: var(--ink);
}
code {
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 0.94em;
}
.legend {
position: fixed;
z-index: 15;
right: 30px;
bottom: 28px;
width: 260px;
padding: 13px 15px;
border: 1px solid rgba(255, 255, 255, 0.72);
border-radius: 14px;
background: rgba(255, 253, 248, 0.76);
box-shadow: 0 12px 45px rgba(31, 48, 48, 0.08);
backdrop-filter: blur(12px);
pointer-events: none;
}
.legend-title {
margin-bottom: 9px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
margin-top: 7px;
color: var(--muted);
font-size: 11px;
}
.swatch {
display: inline-block;
flex: 0 0 auto;
width: 18px;
height: 8px;
border-radius: 2px;
}
.swatch.teal {
background: var(--teal);
}
.swatch.coral {
background: var(--coral);
}
.swatch.amber {
width: 3px;
height: 18px;
border-radius: 0;
background: var(--amber);
}
.swatch.dashed {
height: 13px;
border: 1.5px dashed #8b9797;
background: transparent;
}
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}
@media (max-width: 850px) {
.topbar {
top: 18px;
left: 18px;
right: 18px;
}
.stats {
display: none;
}
.controls {
left: 18px;
bottom: 18px;
width: min(360px, calc(100vw - 36px));
}
.legend {
display: none;
}
h1 {
max-width: 85vw;
}
}
@media (max-height: 650px) {
.method {
display: none;
}
.controls {
padding: 12px;
}
}
</style>
</head>
<body>
<div id="viewport"></div>
<header class="topbar">
<div class="brand">
<div class="eyebrow">Synthetic study · seeded bootstrap</div>
<h1>
Resampling is observable.<br />
<span>The population is not.</span>
</h1>
</div>
<div class="stats" aria-label="Current results">
<div class="stat">
<span class="stat-label">Synthetic sample</span>
<span class="stat-value" id="sampleStat">n = 48</span>
</div>
<div class="stat">
<span class="stat-label">Seeded resamples</span>
<span class="stat-value" id="resampleStat">B = 320</span>
</div>
<div class="stat">
<span class="stat-label">Mean of bootstrap means</span>
<span class="stat-value" id="meanStat">—</span>
</div>
<div class="stat">
<span class="stat-label">95% resample span</span>
<span class="stat-value" id="intervalStat">—</span>
</div>
</div>
</header>
<section class="controls" aria-label="Scene controls">
<div class="control-row">
<label for="seedInput">Bootstrap seed</label>
<output id="seedOutput">104729</output>
</div>
<div class="seed-row">
<input
id="seedInput"
type="number"
min="0"
max="4294967295"
step="1"
value="104729"
aria-label="Bootstrap seed"
/>
<button id="newSeedButton" type="button">New seed</button>
</div>
<div class="control-row">
<label for="countInput">Number of resamples</label>
<output id="countOutput">320</output>
</div>
<input
id="countInput"
type="range"
min="120"
max="640"
step="20"
value="320"
aria-label="Number of bootstrap resamples"
/>
<div class="buttons">
<button id="rebuildButton" class="primary" type="button">
Rebuild resamples
</button>
<button id="resetCameraButton" type="button">
Reset camera
</button>
</div>
<div class="method">
Every resample draws <strong>48 values with replacement</strong> from the
same fixed synthetic dataset, seeded <code>7319</code>. Each coral card
is one resample mean. The shaded curve is an empirical KDE of those
means—not the unavailable population distribution or a population mean.
</div>
</section>
<aside class="legend" aria-label="Legend">
<div class="legend-title">Reading the scene</div>
<div class="legend-row">
<span class="swatch teal"></span>
Original synthetic observations
</div>
<div class="legend-row">
<span class="swatch coral"></span>
Individual bootstrap means
</div>
<div class="legend-row">
<span class="swatch amber"></span>
Observed or resampled mean
</div>
<div class="legend-row">
<span class="swatch dashed"></span>
Empty frame: population unavailable
</div>
</aside>
<div id="summary" class="sr-only" aria-live="polite"></div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// UI and renderer
// -------------------------------------------------------------------------
const viewport = document.getElementById("viewport");
const ui = {
seed: document.getElementById("seedInput"),
seedOutput: document.getElementById("seedOutput"),
count: document.getElementById("countInput"),
countOutput: document.getElementById("countOutput"),
newSeed: document.getElementById("newSeedButton"),
rebuild: document.getElementById("rebuildButton"),
resetCamera: document.getElementById("resetCameraButton"),
sampleStat: document.getElementById("sampleStat"),
resampleStat: document.getElementById("resampleStat"),
meanStat: document.getElementById("meanStat"),
intervalStat: document.getElementById("intervalStat"),
summary: document.getElementById("summary")
};
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"A stylized Three.js scene showing a synthetic sample, stacked bootstrap means, and an empty population panel."
);
viewport.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xf4f0e8);
scene.fog = new THREE.Fog(0xf4f0e8, 24, 43);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 1.35, 0);
let cameraTheta = 0;
let cameraPhi = 0.47;
let cameraDistance = 19.4;
function updateCamera() {
const horizontalDistance = Math.cos(cameraPhi) * cameraDistance;
camera.position.set(
cameraTarget.x + Math.sin(cameraTheta) * horizontalDistance,
cameraTarget.y + Math.sin(cameraPhi) * cameraDistance,
cameraTarget.z + Math.cos(cameraTheta) * horizontalDistance
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
cameraTheta = 0;
cameraPhi = 0.47;
cameraDistance = window.innerWidth < 850 ? 24 : 19.4;
updateCamera();
}
// Direct, dependency-free orbit controls.
let dragging = false;
let dragX = 0;
let dragY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
dragX = event.clientX;
dragY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - dragX;
const dy = event.clientY - dragY;
dragX = event.clientX;
dragY = event.clientY;
cameraTheta -= dx * 0.0055;
cameraPhi = THREE.MathUtils.clamp(cameraPhi + dy * 0.004, 0.2, 1.15);
updateCamera();
});
function stopDragging(event) {
dragging = false;
if (
event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraDistance = THREE.MathUtils.clamp(
cameraDistance + event.deltaY * 0.012,
12,
31
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
// -------------------------------------------------------------------------
// Lighting and permanent environment
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xffffff, 0xa8a196, 2.25));
const keyLight = new THREE.DirectionalLight(0xfff7e8, 3.2);
keyLight.position.set(4, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -14;
keyLight.shadow.camera.right = 14;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0002;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0xb8dcff, 1.15);
rimLight.position.set(-8, 5, -5);
scene.add(rimLight);
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0xe8e2d8,
roughness: 0.94,
metalness: 0
});
const floor = new THREE.Mesh(
new THREE.BoxGeometry(26, 0.14, 9.5),
floorMaterial
);
floor.position.set(0, -0.1, 0);
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(24, 24, 0x9faaa8, 0xb8b9b1);
grid.position.y = -0.025;
grid.material.transparent = true;
grid.material.opacity = 0.22;
grid.material.depthWrite = false;
scene.add(grid);
// -------------------------------------------------------------------------
// Seeded data generation
// -------------------------------------------------------------------------
const DATA_SEED = 7319;
const DATA_SIZE = 48;
function mulberry32(seed) {
let state = seed >>> 0;
return function random() {
state = (state + 0x6d2b79f5) >>> 0;
let value = state;
value = Math.imul(value ^ (value >>> 15), value | 1);
value ^= value + Math.imul(value ^ (value >>> 7), value | 61);
return ((value ^ (value >>> 14)) >>> 0) / 4294967296;
};
}
function standardNormal(random) {
const u = Math.max(random(), 1e-12);
const v = random();
return Math.sqrt(-2 * Math.log(u)) * Math.cos(2 * Math.PI * v);
}
function generateSyntheticDataset(seed, size) {
const random = mulberry32(seed);
const values = [];
for (let index = 0; index < size; index += 1) {
const kind = random();
let value;
if (kind < 0.68) {
value = 0.15 + standardNormal(random) * 0.68;
} else if (kind < 0.93) {
value = 1.08 + standardNormal(random) * 0.48;
} else {
value = -1.08 + standardNormal(random) * 0.58;
}
values.push(value);
}
return values;
}
function derivedResampleSeed(baseSeed, index) {
let hash = (baseSeed ^ Math.imul(index + 1, 0x9e3779b1)) >>> 0;
hash = Math.imul(hash ^ (hash >>> 16), 0x7feb352d) >>> 0;
hash = Math.imul(hash ^ (hash >>> 15), 0x846ca68b) >>> 0;
return (hash ^ (hash >>> 16)) >>> 0;
}
function bootstrapMeans(values, baseSeed, count) {
const means = [];
for (let index = 0; index < count; index += 1) {
const random = mulberry32(derivedResampleSeed(baseSeed, index));
let sum = 0;
for (let draw = 0; draw < values.length; draw += 1) {
const sourceIndex = Math.floor(random() * values.length);
sum += values[sourceIndex];
}
means.push(sum / values.length);
}
return means;
}
function average(values) {
return values.reduce((sum, value) => sum + value, 0) / values.length;
}
function quantile(sortedValues, probability) {
const position = (sortedValues.length - 1) * probability;
const lower = Math.floor(position);
const upper = Math.ceil(position);
const fraction = position - lower;
if (lower === upper) return sortedValues[lower];
return (
sortedValues[lower] * (1 - fraction) +
sortedValues[upper] * fraction
);
}
function formatNumber(value) {
return Number(value).toFixed(3);
}
// -------------------------------------------------------------------------
// Three.js drawing helpers
// -------------------------------------------------------------------------
function roundedRectShape(width, height, radius) {
const shape = new THREE.Shape();
const x = -width / 2;
const y = -height / 2;
shape.moveTo(x + radius, y);
shape.lineTo(x + width - radius, y);
shape.quadraticCurveTo(x + width, y, x + width, y + radius);
shape.lineTo(x + width, y + height - radius);
shape.quadraticCurveTo(
x + width,
y + height,
x + width - radius,
y + height
);
shape.lineTo(x + radius, y + height);
shape.quadraticCurveTo(x, y + height, x, y + height - radius);
shape.lineTo(x, y + radius);
shape.quadraticCurveTo(x, y, x + radius, y);
return shape;
}
function canvasRoundRect(context, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
context.beginPath();
context.moveTo(x + r, y);
context.lineTo(x + width - r, y);
context.quadraticCurveTo(x + width, y, x + width, y + r);
context.lineTo(x + width, y + height - r);
context.quadraticCurveTo(
x + width,
y + height,
x + width - r,
y + height
);
context.lineTo(x + r, y + height);
context.quadraticCurveTo(x, y + height, x, y + height - r);
context.lineTo(x, y + r);
context.quadraticCurveTo(x, y, x + r, y);
context.closePath();
}
function makeTextSprite(text, options = {}) {
const fontSize = options.fontSize ?? 48;
const fontWeight = options.weight ?? 700;
const fontFamily =
'Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif';
const measuringCanvas = document.createElement("canvas");
const measuringContext = measuringCanvas.getContext("2d");
measuringContext.font = `${fontWeight} ${fontSize}px ${fontFamily}`;
const textWidth = measuringContext.measureText(text).width;
const paddingX = options.background ? 28 : 3;
const paddingY = options.background ? 15 : 3;
const canvas = document.createElement("canvas");
canvas.width = Math.ceil(textWidth + paddingX * 2);
canvas.height = Math.ceil(fontSize * 1.45 + paddingY * 2);
const context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
if (options.background) {
context.fillStyle = options.background;
canvasRoundRect(
context,
1,
1,
canvas.width - 2,
canvas.height - 2,
Math.min(18, canvas.height / 2)
);
context.fill();
}
context.font = `${fontWeight} ${fontSize}px ${fontFamily}`;
context.textAlign = "center";
context.textBaseline = "middle";
context.fillStyle = options.color ?? "#173235";
context.fillText(
text,
canvas.width / 2,
canvas.height / 2 + fontSize * 0.025
);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
texture.anisotropy = Math.min(
8,
renderer.capabilities.getMaxAnisotropy()
);
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
const worldHeight = options.height ?? 0.24;
let worldWidth = worldHeight * (canvas.width / canvas.height);
if (options.maxWidth && worldWidth > options.maxWidth) {
worldWidth = options.maxWidth;
}
sprite.scale.set(worldWidth, worldHeight, 1);
sprite.renderOrder = 30;
return sprite;
}
function addLabel(group, text, x, y, z, options = {}) {
const label = makeTextSprite(text, options);
label.position.set(x, y, z);
group.add(label);
return label;
}
function addLine(group, points, color, opacity = 1, dashed = false) {
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const material = dashed
? new THREE.LineDashedMaterial({
color,
transparent: true,
opacity,
dashSize: 0.12,
gapSize: 0.085,
depthWrite: false
})
: new THREE.LineBasicMaterial({
color,
transparent: opacity < 1,
opacity,
depthWrite: false
});
const line = new THREE.Line(geometry, material);
if (dashed) line.computeLineDistances();
group.add(line);
return line;
}
function addPlinth(group, x, width, color = 0xfbf8f1) {
const material = new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0
});
const plinth = new THREE.Mesh(
new THREE.BoxGeometry(width, 0.16, 2.55),
material
);
plinth.position.set(x, 0.055, -0.12);
plinth.castShadow = true;
plinth.receiveShadow = true;
group.add(plinth);
return plinth;
}
function addPanel(group, x, width, height, color = 0xfbf8f1) {
const geometry = new THREE.ExtrudeGeometry(
roundedRectShape(width, height, 0.18),
{
depth: 0.08,
bevelEnabled: true,
bevelSegments: 3,
steps: 1,
bevelSize: 0.025,
bevelThickness: 0.025
}
);
const material = new THREE.MeshStandardMaterial({
color,
roughness: 0.9,
metalness: 0
});
const panel = new THREE.Mesh(geometry, material);
panel.position.set(x, 0.13 + height / 2, -0.58);
panel.castShadow = true;
panel.receiveShadow = true;
group.add(panel);
return panel;
}
function gaussianKde(values, lower, upper, pointCount = 72) {
const mean = average(values);
const variance =
values.reduce((sum, value) => sum + (value - mean) ** 2, 0) /
values.length;
const standardDeviation = Math.sqrt(variance);
const bandwidth = Math.max(
standardDeviation * 0.3,
(upper - lower) / 45,
0.012
);
const points = [];
for (let index = 0; index < pointCount; index += 1) {
const x = lower + ((upper - lower) * index) / (pointCount - 1);
let density = 0;
for (const value of values) {
const z = (x - value) / bandwidth;
density += Math.exp(-0.5 * z * z);
}
density /=
values.length * bandwidth * Math.sqrt(2 * Math.PI);
points.push({ x, density });
}
const maximum = Math.max(...points.map((point) => point.density));
return points.map((point) => ({
x: point.x,
y: point.density / Math.max(maximum, 1e-12)
}));
}
// -------------------------------------------------------------------------
// Dynamic scene
// -------------------------------------------------------------------------
let dynamicGroup = null;
let currentResult = null;
function disposeDynamicGroup(group) {
group.traverse((object) => {
if (object.geometry && !object.isSprite) {
object.geometry.dispose();
}
if (object.material) {
const materials = Array.isArray(object.material)
? object.material
: [object.material];
for (const material of materials) {
if (material.map) material.map.dispose();
material.dispose();
}
}
});
}
function buildSamplePanel(group, values) {
const centerX = -5.55;
const plotWidth = 4.05;
const panelWidth = 4.7;
const panelHeight = 3.45;
addPlinth(group, centerX, panelWidth + 0.15);
addPanel(group, centerX, panelWidth, panelHeight, 0xfbf8f1);
addLabel(
group,
"01 SYNTHETIC SAMPLE",
centerX,
3.96,
0.12,
{ height: 0.25, maxWidth: 4.1 }
);
addLabel(
group,
"48 observed values · fixed seed 7319",
centerX,
3.58,
0.12,
{
height: 0.17,
color: "#748183",
weight: 600,
maxWidth: 3.9
}
);
addLabel(
group,
"OBSERVED",
centerX - 1.57,
3.16,
0.14,
{
height: 0.2,
maxWidth: 1.35,
color: "#ffffff",
background: "#087d78"
}
);
const minimum = Math.min(...values);
const maximum = Math.max(...values);
const padding = Math.max((maximum - minimum) * 0.08, 0.15);
const lower = minimum - padding;
const upper = maximum + padding;
const mapValue = (value) =>
centerX + ((value - lower) / (upper - lower) - 0.5) * plotWidth;
addLine(
group,
[
new THREE.Vector3(mapValue(lower), 0.29, 0.08),
new THREE.Vector3(mapValue(upper), 0.29, 0.08)
],
0x879695,
0.55
);
const pointGeometry = new THREE.SphereGeometry(0.068, 14, 10);
const pointMaterial = new THREE.MeshStandardMaterial({
color: 0x087d78,
roughness: 0.46,
metalness: 0.02,
emissive: 0x003b39,
emissiveIntensity: 0.12
});
values.forEach((value, index) => {
const point = new THREE.Mesh(pointGeometry, pointMaterial);
point.position.set(
mapValue(value),
0.39,
0.08 + (index % 3) * 0.035
);
point.castShadow = true;
group.add(point);
});
for (let index = 0; index <= 4; index += 1) {
const fraction = index / 4;
const value = lower + fraction * (upper - lower);
const x = mapValue(value);
addLine(
group,
[
new THREE.Vector3(x, 0.24, 0.12),
new THREE.Vector3(x, 0.34, 0.12)
],
0x82908f,
0.55
);
addLabel(group, value.toFixed(1), x, 0.075, 0.24, {
height: 0.13,
color: "#738180",
weight: 600
});
}
const sampleMean = average(values);
const sampleMeanX = mapValue(sampleMean);
const meanLine = new THREE.Mesh(
new THREE.BoxGeometry(0.035, 1.05, 0.035),
new THREE.MeshStandardMaterial({
color: 0xe6a72e,
roughness: 0.4,
emissive: 0x4c3100,
emissiveIntensity: 0.08
})
);
meanLine.position.set(sampleMeanX, 0.83, 0.15);
meanLine.castShadow = true;
group.add(meanLine);
const meanDot = new THREE.Mesh(
new THREE.SphereGeometry(0.095, 16, 12),
new THREE.MeshStandardMaterial({
color: 0xe6a72e,
roughness: 0.35
})
);
meanDot.position.set(sampleMeanX, 1.38, 0.15);
meanDot.castShadow = true;
group.add(meanDot);
addLabel(
group,
`sample mean ${formatNumber(sampleMean)}`,
sampleMeanX,
1.72,
0.18,
{
height: 0.19,
maxWidth: 2.25,
color: "#805812",
background: "#fff1c9"
}
);
addLabel(
group,
"mean value",
centerX,
0.075,
0.39,
{
height: 0.14,
color: "#738180",
weight: 600
}
);
}
function buildBootstrapPanel(group, means) {
const centerX = 0;
const plotWidth = 5.35;
const panelWidth = 6.2;
const panelHeight = 3.45;
const binCount = 18;
addPlinth(group, centerX, panelWidth + 0.15);
addPanel(group, centerX, panelWidth, panelHeight, 0xfbf8f1);
addLabel(
group,
"02 BOOTSTRAP MEANS",
centerX,
3.96,
0.12,
{ height: 0.25, maxWidth: 4.8 }
);
addLabel(
group,
`${means.length} seeded resamples · one card per mean`,
centerX,
3.58,
0.12,
{
height: 0.17,
color: "#748183",
weight: 600,
maxWidth: 4.7
}
);
addLabel(
group,
"EMPIRICAL",
centerX - 2.18,
3.16,
0.14,
{
height: 0.2,
maxWidth: 1.45,
color: "#ffffff",
background: "#f36f59"
}
);
const sorted = [...means].sort((a, b) => a - b);
const lower = sorted[0];
const upper = sorted[sorted.length - 1];
const span = Math.max(upper - lower, 0.04);
const domainLower = lower - span * 0.1;
const domainUpper = upper + span * 0.1;
const mapValue = (value) =>
centerX +
((value - domainLower) / (domainUpper - domainLower) - 0.5) *
plotWidth;
// Empirical KDE area behind the individual cards.
const density = gaussianKde(means, domainLower, domainUpper);
const densityShape = new THREE.Shape();
densityShape.moveTo(mapValue(density[0].x), 0.3);
for (const point of density) {
densityShape.lineTo(mapValue(point.x), 0.3 + point.y * 1.25);
}
densityShape.lineTo(mapValue(density[density.length - 1].x), 0.3);
densityShape.closePath();
const densityMesh = new THREE.Mesh(
new THREE.ShapeGeometry(densityShape),
new THREE.MeshBasicMaterial({
color: 0xf36f59,
transparent: true,
opacity: 0.12,
side: THREE.DoubleSide,
depthWrite: false
})
);
densityMesh.position.z = -0.3;
group.add(densityMesh);
const densityCurvePoints = density.map(
(point) =>
new THREE.Vector3(
mapValue(point.x),
0.3 + point.y * 1.25,
-0.285
)
);
addLine(group, densityCurvePoints, 0xe65e49, 0.62);
addLabel(
group,
"empirical KDE",
1.82,
2.7,
-0.2,
{
height: 0.16,
color: "#c65342",
weight: 700,
maxWidth: 1.5
}
);
// Determine bins and stack heights.
const binCounts = new Array(binCount).fill(0);
const assignments = means.map((value) => {
const fraction =
(value - domainLower) / (domainUpper - domainLower);
const bin = Math.min(
binCount - 1,
Math.max(0, Math.floor(fraction * binCount))
);
const level = binCounts[bin];
binCounts[bin] += 1;
return { value, bin, level };
});
const maximumStack = Math.max(...binCounts, 1);
const stackHeight = 2.55;
const verticalStep = stackHeight / maximumStack;
const tileHeight = Math.min(0.042, verticalStep * 0.7);
const tileGeometry = new THREE.BoxGeometry(0.105, tileHeight, 0.3);
const tileMaterials = [
new THREE.MeshStandardMaterial({
color: 0xf36f59,
roughness: 0.5,
metalness: 0.01,
emissive: 0x45110b,
emissiveIntensity: 0.08
}),
new THREE.MeshStandardMaterial({
color: 0xff8b75,
roughness: 0.55,
metalness: 0,
emissive: 0x351008,
emissiveIntensity: 0.06
})
];
assignments.forEach((assignment, index) => {
const tile = new THREE.Mesh(
tileGeometry,
tileMaterials[index % tileMaterials.length]
);
tile.position.set(
mapValue(assignment.value),
0.38 + assignment.level * verticalStep,
-0.03 + (assignment.level % 3) * 0.055
);
tile.receiveShadow = true;
group.add(tile);
});
// Bootstrap axis and ticks.
addLine(
group,
[
new THREE.Vector3(mapValue(domainLower), 0.28, 0.2),
new THREE.Vector3(mapValue(domainUpper), 0.28, 0.2)
],
0x879695,
0.55
);
for (let index = 0; index <= 5; index += 1) {
const fraction = index / 5;
const value =
domainLower + fraction * (domainUpper - domainLower);
const x = mapValue(value);
addLine(
group,
[
new THREE.Vector3(x, 0.23, 0.2),
new THREE.Vector3(x, 0.34, 0.2)
],
0x82908f,
0.55
);
addLabel(group, value.toFixed(2), x, 0.075, 0.24, {
height: 0.13,
color: "#738180",
weight: 600
});
}
addLabel(
group,
"bootstrap mean",
centerX,
0.075,
0.39,
{
height: 0.14,
color: "#738180",
weight: 600
}
);
const bootstrapMean = average(means);
const bootstrapMeanX = mapValue(bootstrapMean);
const meanLine = new THREE.Mesh(
new THREE.BoxGeometry(0.032, 2.93, 0.032),
new THREE.MeshStandardMaterial({
color: 0xe6a72e,
roughness: 0.4,
emissive: 0x4c3100,
emissiveIntensity: 0.08
})
);
meanLine.position.set(bootstrapMeanX, 1.75, -0.22);
group.add(meanLine);
const meanCap = new THREE.Mesh(
new THREE.OctahedronGeometry(0.09),
new THREE.MeshStandardMaterial({
color: 0xe6a72e,
roughness: 0.35
})
);
meanCap.position.set(bootstrapMeanX, 3.2, -0.22);
group.add(meanCap);
addLabel(
group,
`mean of means ${formatNumber(bootstrapMean)}`,
bootstrapMeanX,
3.37,
0.13,
{
height: 0.18,
maxWidth: 2.35,
color: "#805812",
background: "#fff1c9"
}
);
}
function buildPopulationPanel(group) {
const centerX = 5.55;
const width = 4;
const height = 3.45;
addPlinth(group, centerX, width + 0.15, 0xf0ece4);
// Deliberately empty: no density, points, curve, or population mean.
const borderPoints = [
new THREE.Vector3(centerX - width / 2, 0.25, -0.22),
new THREE.Vector3(centerX + width / 2, 0.25, -0.22),
new THREE.Vector3(centerX + width / 2, 0.25 + height, -0.22),
new THREE.Vector3(centerX - width / 2, 0.25 + height, -0.22)
];
const borderGeometry = new THREE.BufferGeometry().setFromPoints(
borderPoints
);
const borderMaterial = new THREE.LineDashedMaterial({
color: 0x83908f,
transparent: true,
opacity: 0.85,
dashSize: 0.13,
gapSize: 0.09,
depthWrite: false
});
const border = new THREE.LineLoop(borderGeometry, borderMaterial);
border.computeLineDistances();
group.add(border);
addLabel(
group,
"03 REAL POPULATION",
centerX,
3.96,
0.12,
{ height: 0.25, maxWidth: 3.7 }
);
addLabel(
group,
"no access · no population draws",
centerX,
3.58,
0.12,
{
height: 0.17,
color: "#748183",
weight: 600,
maxWidth: 3.5
}
);
addLabel(
group,
"UNAVAILABLE",
centerX,
3.16,
0.14,
{
height: 0.2,
maxWidth: 2.05,
color: "#8d4c1b",
background: "#ffe8cf"
}
);
const question = addLabel(group, "?", centerX, 2.18, 0.05, {
height: 1.15,
fontSize: 120,
color: "#738180",
weight: 500
});
question.material.opacity = 0.34;
addLabel(
group,
"population distribution",
centerX,
1.28,
0.12,
{
height: 0.2,
color: "#657475",
maxWidth: 3.1
}
);
addLabel(
group,
"not observed · not estimated",
centerX,
0.92,
0.12,
{
height: 0.16,
color: "#899492",
weight: 600,
maxWidth: 3.1
}
);
addLabel(
group,
"no population mean shown",
centerX,
0.54,
0.12,
{
height: 0.17,
color: "#9a5b27",
weight: 700,
maxWidth: 2.7
}
);
}
function rebuildScene() {
const seed = Number(ui.seed.value) >>> 0;
const count = Number(ui.count.value);
ui.seed.value = String(seed);
ui.seedOutput.value = String(seed);
ui.countOutput.value = String(count);
const values = generateSyntheticDataset(DATA_SEED, DATA_SIZE);
const means = bootstrapMeans(values, seed, count);
const sortedMeans = [...means].sort((a, b) => a - b);
const sampleMean = average(values);
const bootstrapMean = average(means);
const intervalLower = quantile(sortedMeans, 0.025);
const intervalUpper = quantile(sortedMeans, 0.975);
if (dynamicGroup) {
scene.remove(dynamicGroup);
disposeDynamicGroup(dynamicGroup);
}
dynamicGroup = new THREE.Group();
scene.add(dynamicGroup);
buildSamplePanel(dynamicGroup, values);
buildBootstrapPanel(dynamicGroup, means);
buildPopulationPanel(dynamicGroup);
ui.sampleStat.textContent = `n = ${DATA_SIZE}`;
ui.resampleStat.textContent = `B = ${count}`;
ui.meanStat.textContent = formatNumber(bootstrapMean);
ui.intervalStat.textContent =
`${formatNumber(intervalLower)} – ${formatNumber(intervalUpper)}`;
ui.summary.textContent =
`The synthetic sample has ${DATA_SIZE} observations with mean ` +
`${formatNumber(sampleMean)}. ${count} bootstrap resamples were ` +
`generated with seed ${seed}. The mean of the bootstrap means is ` +
`${formatNumber(bootstrapMean)}, with a 95 percent percentile span ` +
`from ${formatNumber(intervalLower)} to ${formatNumber(intervalUpper)}. ` +
`The real population distribution is unavailable and is not shown.`;
currentResult = {
seed,
count,
sampleMean,
bootstrapMean,
intervalLower,
intervalUpper
};
}
// -------------------------------------------------------------------------
// UI events
// -------------------------------------------------------------------------
ui.count.addEventListener("input", () => {
ui.countOutput.value = ui.count.value;
});
ui.count.addEventListener("change", rebuildScene);
ui.seed.addEventListener("change", rebuildScene);
ui.rebuild.addEventListener("click", rebuildScene);
ui.newSeed.addEventListener("click", () => {
let nextSeed;
if (window.crypto && window.crypto.getRandomValues) {
const randomValues = new Uint32Array(1);
window.crypto.getRandomValues(randomValues);
nextSeed = randomValues[0];
} else {
nextSeed = Math.floor(Math.random() * 4294967296);
}
ui.seed.value = String(nextSeed);
rebuildScene();
});
ui.resetCamera.addEventListener("click", resetCamera);
// -------------------------------------------------------------------------
// Start
// -------------------------------------------------------------------------
resetCamera();
rebuildScene();
renderer.setAnimationLoop(() => {
renderer.render(scene, camera);
});
</script>
</body>
</html>
```