.. code:: python
%matplotlib inline
from d2l import mxnet as d2l
from mxnet import np, npx
import random
npx.set_np()
.. raw:: html

.. raw:: html
.. code:: python
%matplotlib inline
from d2l import torch as d2l
import torch
from torch.distributions import multinomial
.. raw:: html

.. raw:: html
.. code:: python
%matplotlib inline
from d2l import tensorflow as d2l
import tensorflow as tf
import tensorflow_probability as tfp
import numpy as np
.. raw:: html

.. raw:: html
.. code:: python
fair_probs = [1.0 / 6] * 6
np.random.multinomial(1, fair_probs)
.. parsed-literal::
:class: output
array([0, 0, 0, 1, 0, 0], dtype=int64)
.. raw:: html

.. raw:: html
.. code:: python
fair_probs = torch.ones([6]) / 6
multinomial.Multinomial(1, fair_probs).sample()
.. parsed-literal::
:class: output
tensor([1., 0., 0., 0., 0., 0.])
.. raw:: html

.. raw:: html
.. code:: python
fair_probs = tf.ones(6) / 6
tfp.distributions.Multinomial(1, fair_probs).sample()
.. parsed-literal::
:class: output
.. raw:: html

.. raw:: html
.. code:: python
np.random.multinomial(10, fair_probs)
.. parsed-literal::
:class: output
array([1, 1, 5, 1, 1, 1], dtype=int64)
.. raw:: html

.. raw:: html
.. code:: python
multinomial.Multinomial(10, fair_probs).sample()
.. parsed-literal::
:class: output
tensor([2., 0., 3., 1., 2., 2.])
.. raw:: html

.. raw:: html
.. code:: python
tfp.distributions.Multinomial(10, fair_probs).sample()
.. parsed-literal::
:class: output
.. raw:: html

.. raw:: html
.. code:: python
counts = np.random.multinomial(1000, fair_probs).astype(np.float32)
counts / 1000
.. parsed-literal::
:class: output
array([0.162, 0.149, 0.178, 0.17 , 0.166, 0.175])
.. raw:: html

.. raw:: html
.. code:: python
# Store the results as 32-bit floats for division
counts = multinomial.Multinomial(1000, fair_probs).sample()
counts / 1000 # Relative frequency as the estimate
.. parsed-literal::
:class: output
tensor([0.1810, 0.1780, 0.1800, 0.1490, 0.1530, 0.1590])
.. raw:: html

.. raw:: html
.. code:: python
counts = tfp.distributions.Multinomial(1000, fair_probs).sample()
counts / 1000
.. parsed-literal::
:class: output
.. raw:: html

.. raw:: html
.. code:: python
counts = np.random.multinomial(10, fair_probs, size=500)
cum_counts = counts.astype(np.float32).cumsum(axis=0)
estimates = cum_counts / cum_counts.sum(axis=1, keepdims=True)
d2l.set_figsize((6, 4.5))
for i in range(6):
d2l.plt.plot(estimates[:, i].asnumpy(),
label=("P(die=" + str(i + 1) + ")"))
d2l.plt.axhline(y=0.167, color='black', linestyle='dashed')
d2l.plt.gca().set_xlabel('Groups of experiments')
d2l.plt.gca().set_ylabel('Estimated probability')
d2l.plt.legend();
.. figure:: output_probability_245b7d_51_0.svg
.. raw:: html

.. raw:: html
.. code:: python
counts = multinomial.Multinomial(10, fair_probs).sample((500,))
cum_counts = counts.cumsum(dim=0)
estimates = cum_counts / cum_counts.sum(dim=1, keepdims=True)
d2l.set_figsize((6, 4.5))
for i in range(6):
d2l.plt.plot(estimates[:, i].numpy(),
label=("P(die=" + str(i + 1) + ")"))
d2l.plt.axhline(y=0.167, color='black', linestyle='dashed')
d2l.plt.gca().set_xlabel('Groups of experiments')
d2l.plt.gca().set_ylabel('Estimated probability')
d2l.plt.legend();
.. figure:: output_probability_245b7d_54_0.svg
.. raw:: html

.. raw:: html
.. code:: python
counts = tfp.distributions.Multinomial(10, fair_probs).sample(500)
cum_counts = tf.cumsum(counts, axis=0)
estimates = cum_counts / tf.reduce_sum(cum_counts, axis=1, keepdims=True)
d2l.set_figsize((6, 4.5))
for i in range(6):
d2l.plt.plot(estimates[:, i].numpy(),
label=("P(die=" + str(i + 1) + ")"))
d2l.plt.axhline(y=0.167, color='black', linestyle='dashed')
d2l.plt.gca().set_xlabel('Groups of experiments')
d2l.plt.gca().set_ylabel('Estimated probability')
d2l.plt.legend();
.. figure:: output_probability_245b7d_57_0.svg
.. raw:: html

.. raw:: html
`Discussions `__
.. raw:: html

.. raw:: html
`Discussions `__
.. raw:: html

.. raw:: html
`Discussions `__
.. raw:: html

.. raw:: html