Multinomial

Reference

Application: sample ratio mismatch.

Consider a new experimental unit \(n\) is assigned to one of \(i \in \{1, 2, 3\}\) groups with probabilities \(\mathbf{\theta} = [0.1, 0.3, 0.6]\). Therefore, groups are \(\mathrm{Multinomial}(1, \mathbf{\theta})\) distributed.

import numpy as np

theta = np.array([0.1, 0.3, 0.6])
size = 1000
np.random.seed(1)
xs = np.random.multinomial(1, theta, size=size)
print(xs)
[[0 1 0]
 [0 0 1]
 [0 0 1]
 ...
 [0 1 0]
 [1 0 0]
 [0 0 1]]

We can test the hypothesis

\[ \begin{align} H_0: \mathbf{\theta} = \mathbf{\theta_0} \\ H_1: \mathbf{\theta} \neq \mathbf{\theta_0} \end{align} \]

with \(\mathbf{\theta_0} = [0.1, 0.4, 0.5]\) and estimate \((1 - \alpha)\) confidence sequences for \(\mathbf{\theta}\) using the Multinomial test:

from savvi.multinomial import Multinomial
alpha = 0.05
theta_0 = np.array([0.1, 0.4, 0.5])
multinomial = Multinomial(alpha, theta_0)
AttributeError: _ARRAY_API not found
(CVXPY) Oct 24 05:36:17 PM: Encountered unexpected exception importing solver ECOS:
ImportError('numpy.core.multiarray failed to import')
(CVXPY) Oct 24 05:36:17 PM: Encountered unexpected exception importing solver SCS:
ImportError('numpy.core.multiarray failed to import')
(CVXPY) Oct 24 05:36:17 PM: Encountered unexpected exception importing solver ECOS_BB:
ImportError('numpy.core.multiarray failed to import')
(CVXPY) Oct 24 05:36:17 PM: Encountered unexpected exception importing solver OSQP:
ImportError('numpy.core.multiarray failed to import')
AttributeError: _ARRAY_API not found
AttributeError: _ARRAY_API not found
AttributeError: _ARRAY_API not found

For each new unit sample \(n\), we run the test. If \(p_n < \alpha\), we have the option to stop running:

sequence = multinomial.batch(xs)
optional_stop = next(s for s in sequence if s.p_value <= alpha)
optional_stop
Parameter Estimate CI Lower CI Upper
\(\theta_0\) 0.0995 0.0567 0.1494
\(\theta_1\) 0.3209 0.2609 0.4028
\(\theta_2\) 0.5796 0.4971 0.6518

Sample size: 402, P-value: 0.0485

Code
%config InlineBackend.figure_formats = ["svg"]
from savvi.utils import plot

plot(sequence, theta);