Download Notebook

MultinomialDistribution

[1]:
import numpy as np
np.set_printoptions(linewidth=200)
MultinomialDistribution is a joint probability distribution of a discrete random variables \((x_0, \dots, x_{n-1})\), with events of \(x_i\) are \(A_i\) and \(\sum_{a_0 \in A_0} \cdots \sum_{a_{n-1} \in A_{n-1}} p(x_0=a_0, \dots, x_{n-1}=a_{n-1}) = 1\).
The property ps of MultinomialDistribution is a flattened numpy array.
If \(s_i\) be sizes of \(A_i\), then the property shape of MultinomialDistribution is \((s_0, \dots, s_{n-1})\).
Exmaple.
Consider the following joint probability distribution:

x_0 \ x_1

0

1

2

0

0.0

0.10

0.15

1

0.20

0.25

0.30

Then ps = \([0.0, 0.10, 0.15, 0.20, 0.25, 0.30]\), and shape = \([2, 3]\).

Generate MultinomialDistribution object directly.

[2]:
from quara.objects.multinomial_distribution import MultinomialDistribution

ps = np.array([0.0, 0.10, 0.15, 0.20, 0.25, 0.30], dtype=np.float64)
shape = (2, 3)
dist = MultinomialDistribution(ps, shape=shape)
print(dist)
shape = (2, 3)
ps = [0.   0.1  0.15 0.2  0.25 0.3 ]

If shape is omitted, the number of random variables are assumed to be one.

[3]:
dist = MultinomialDistribution(ps)
print(dist)
shape = (6,)
ps = [0.   0.1  0.15 0.2  0.25 0.3 ]

specific properties

The property ps of MultinomialDistribution is a one-dimensional numpy array specified by the constructor argument ps.

[4]:
dist = MultinomialDistribution(ps, shape=shape)
print(f"ps: {dist.ps}")
ps: [0.   0.1  0.15 0.2  0.25 0.3 ]

The property shape of MultinomialDistribution is a tuple of int by the constructor argument shape.

[5]:
dist = MultinomialDistribution(ps, shape=shape)
print(f"shape: {dist.shape}")
shape: (2, 3)

marginalize

Returns MultinomialDistribution corresponding to marginal probability.
The marginal probability of variable outcome_indices_remain.
Exmaple.
Consider the following joint probability distribution:

x_0 \ x_1

0

1

2

0

0.0

0.10

0.15

1

0.20

0.25

0.30

Let outcome_indices_remain = \([0] (= [x_0])\).
Then marginal probability is \(p(x_0 = 0) = 0.25, p(x_0 = 1) = 0.75\).
[6]:
dist = MultinomialDistribution(ps, shape=shape)
marginalized_dist = dist.marginalize([0])
print(f"marginalize([0]): \n{marginalized_dist}")
marginalize([0]):
shape = (2,)
ps = [0.25 0.75]

conditionalize

Returns MultinomialDistribution corresponding to marginal probability.
The marginal probability of variable outcome_indices_remain.
Exmaple.
Consider the following joint probability distribution:

x_0 \ x_1

0

1

2

sum

0

0.0

0.10

0.15

0.25

1

0.20

0.25

0.30

0.75

Let conditional_variable_indices = \([0] (= [x_0])\), and conditional_variable_values = \([1]\) (i.e. \(x_0 = 1\)).
Then marginal probabilities are
  • \(p(x_1 = 0|x_0 = 1) = p(x_0 = 1, x_1 = 0) / p(x_0 = 1) = 0.20 / 0.75 \fallingdotseq 0.27\),

  • \(p(x_1 = 1|x_0 = 1) = p(x_0 = 1, x_1 = 1) / p(x_0 = 1) = 0.25 / 0.75 \fallingdotseq 0.33\),

  • \(p(x_1 = 2|x_0 = 1) = p(x_0 = 1, x_1 = 2) / p(x_0 = 1) = 0.30 / 0.75 \fallingdotseq 0.40\).

[7]:
dist = MultinomialDistribution(ps, shape=shape)
conditionalized_dist = dist.conditionalize([0], [1])
print(f"conditionalize([0], [1]): \n{conditionalized_dist}")
conditionalize([0], [1]):
shape = (3,)
ps = [0.26666667 0.33333333 0.4       ]

execute_random_sampling

Returns results of random sampling.
The first argument num is the number of trials per execution unit. The second argument size is the number of execution.
Optional argument random_generator is seed(int) or np.random.Generator used for sampling.
Examle.
num = 100, size = 3
[8]:
dist = MultinomialDistribution(ps, shape=shape)
samples = dist.execute_random_sampling(100, 3)
print(f"execute_random_sampling(): \n{samples}")
execute_random_sampling():
[array([ 0, 14, 15, 21, 25, 25]), array([ 0,  9, 15, 15, 22, 39]), array([ 0,  9, 15, 27, 26, 23])]

Download Notebook