Skip to content

Add Dice.from_dist to approximate distributions via quantile slicing - #14

Merged
koaning merged 3 commits into
mainfrom
koaning/dice-from-dist
Jun 17, 2026
Merged

Add Dice.from_dist to approximate distributions via quantile slicing#14
koaning merged 3 commits into
mainfrom
koaning/dice-from-dist

Conversation

@koaning

@koaning koaning commented Jun 17, 2026

Copy link
Copy Markdown
Owner

What

Adds Dice.from_dist(dist, n=6, quantiles=None) — a classmethod that approximates a continuous distribution with a Dice by slicing equiprobable quantiles out of it.

How

  • Duck-typed input: resolves an inverse-CDF from .ppf (scipy frozen distributions) or .inv_cdf (stdlib statistics.NormalDist), raising TypeError if neither is present. No new package dependency.
  • n (default 6): number of equiprobable faces, placed at the midpoint of each quantile bin — inv_cdf((i + 0.5) / n).
  • quantiles: optional explicit sequence of probabilities in (0, 1) to slice at instead of n midpoints; faces stay equiprobable. Validated to be non-empty with every q strictly in (0, 1) (avoids ±inf on unbounded distributions), else ValueError.
  • Outcomes are raw floats (most faithful); duplicate outcomes accumulate and the constructor renormalizes.

Notebook

Demo cells added in nbs/__init__.py (source of truth): a statistics.NormalDist example, the documented kwargs, and a numpy exponential example that doubles as a duck-typing showcase. dicekit/__init__.py regenerated via make build.

Tests

8 new tests in tests/test_basics.py: equiprobable faces, normal symmetry, monotonic midpoints, custom quantiles, duck-typed .ppf stub, mean tracking, TypeError on bad object, ValueError on bad quantiles. uv run pytest -q34 passed.

🤖 Generated with Claude Code

koaning and others added 3 commits June 17, 2026 12:39
`Dice.from_dist(dist, n=6, quantiles=None)` builds a dice that approximates
a continuous distribution by slicing equiprobable quantiles. It is duck-typed
on the inverse-CDF: works with scipy frozen dists (.ppf) and the stdlib
statistics.NormalDist (.inv_cdf), so no new dependency is added.

Includes notebook demos (NormalDist plus a numpy exponential showing the
duck-typing) and tests covering equiprobability, normal symmetry, custom
quantiles, mean tracking, and the error cases.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
# Conflicts:
#	dicekit/__init__.py
#	nbs/__init__.py
Configure shared Conductor scripts for the uv + marimo workflow:
- setup: uv sync
- run: marimo edit on the nbs/ folder, bound to $CONDUCTOR_PORT, headless
- archive: make clean
- run_mode: concurrent (per-workspace port, no shared local state)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@koaning
koaning merged commit e1a38ed into main Jun 17, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant