Skip to content

Convolution of dice ​

examples/convolution.py computes the joint probability table of a fair die and a die biased toward six, the distribution of their sum, and a matplotlib chart of that distribution. Its Animation view turns the table into that distribution.

Notebookscene.pyExported page

Open the notebook and its view in Studio from a clone of the repository:

sh
pixi run edit convolution

Values and a figure ​

scene.py reads the table and the sums as values, and the chart as a figure:

python
joint = np.asarray(value("joint", default=np.outer(FAIR, FAIR).tolist()), dtype=float)
probabilities = np.asarray(
    value("probabilities", default=np.convolve(FAIR, FAIR).tolist()), dtype=float
)
...
chart = output("distribution_figure", height=2.6)
if chart is not None:
    self.play(FadeOut(VGroup(bars, labels)), FadeIn(chart), ...)

The scene follows each diagonal of the table into one bar of the sum's distribution, based on SumAlongDiagonal. It then replaces its own bars with the notebook's distribution_figure, so the reader sees that both describe the same distribution.

Static export ​

The view's states.yaml prepares every value of the bias slider. A zero-python export renders one movie per value, and the exported page swaps movies as the reader moves the slider. The Exported page link under the movie opens this export:

sh
pixi run marimo-studio view export animation --target examples/convolution.py \
  --output dist/convolution --runtime zero-python