Multinomial
Constructor
Multinomial({ps: ..., n: ...})
ps: probability vector (a JS array of reals) whose elements sum to 1n: number of trials (integer,>= 1)Support: a vector/array of counts with the same length as
psand sumn
This differs from Discrete/Categorical, which allow unnormalized weights.
Executable example
1var ps = [0.1, 0.3, 0.6]; // MUST sum to 1 for Multinomial
2var n = 5;
3
4var d = Multinomial({ps: ps, n: n});
5
6display("A Multinomial sample is a vector of counts (same length as ps):");
7display(sample(d));
8
9var counts = [0, 2, 3]; // length 3, sums to 5
10display("score(counts) is the log-probability of exactly these counts:");
11display(d.score(counts));
12
13// Typical pattern: use Multinomial as a likelihood over observed count vectors.
14var model = function() {
15 // discrete prior over candidate probability vectors:
16 var theta = uniformDraw([0.2, 0.5, 0.8]);
17 var probs = [theta, 1-theta]; // sums to 1
18
19 // observed data: 7 times category 0, 3 times category 1
20 var obsCounts = [7, 3];
21 observe(Multinomial({ps: probs, n: sum(obsCounts)}), obsCounts);
22
23 return theta;
24};
25
26display("Posterior over theta given counts [7,3] (exact enumerate):");
27display(Infer({method: 'enumerate', model: model}));
A Multinomial sample is a vector of counts (same length as ps):
[ 2, 0, 3 ]
score(counts) is the log-probability of exactly these counts:
-1.6378373869557987
Posterior over theta given counts [7,3] (exact enumerate):
dist {
params: { dist: { '0.8': [Object], '0.5': [Object], '0.2': [Object] } },
supp: [ 0.8, 0.5, 0.2 ],
getDist: [Function (anonymous)]
}
undefined
Common failure modes
psdoes not sum to 1 (usenormalize(ps)if you have weights).The observed count vector: - has a different length than
ps, or - does not sum ton.