Another TI-BASIC challenge proposed to me by the same person who asked me last time was to calculate the probability mass function of a multinomial distribution. The formula is:
This formula can be broken down into three basic parts:
For each $i$, $x_i$ and $p_i$ are read, and the new values of $numerator$ and $denominator$ are calculated. Once this is done, $x_i$ and $p_i$ are no longer needed. Finally, once all of the numbers have been read in, the final answer is just
This program also does some sanity checking to make sure that $\sum_{i=1}^k p_i = 1$ and $\sum_{i=1}^k x_i = n$.
N | $n$ |
D | $denominator$ ($x_1 \cdots x_k$) |
M | $multiplier$ ($p_1^{x_1}\cdots p_k^{x_k}$) |
P | $p\_i$ |
O | $\sum_{i=1}^k p_i$ |
X | $x\_i$ |
W | $\sum_{i=1}^k x_i$ |
Download: MULTINOM.8Xp
Prompt N
1→D:1→M:0→O:0→W:1→X
While X≠0
Prompt X
If X≠0:Then
Prompt P
X+W→W:P+O→O
D*(X!)→D
M*(P^X)→M
End:End
If O≠1:Then
Disp "E: sum(P)≠1
Stop:End
If W≠N:Then
Disp "E: sum(X)≠N
Stop:End
((N!)/D)*M
In a recent three-way election for a large country, candidate A received 20% of the votes, candidate B received 30% of the votes, and candidate C received 50% of the votes. If six voters are selected randomly, what is the probability that there will be exactly one supporter for candidate A, two supporters for candidate B and three supporters for candidate C in the sample? (source)
To find the solution using prgmMULTINOM
:
N=?6 | Number of supporters (1+2+3) |
X=?1 | Supporters for candidate A |
P=?.2 | Votes for candidate A |
X=?2 | Supporters for candidate B |
P=?.3 | Votes for candidate B |
X=?3 | Supporters for candidate C |
P=?.5 | Votes for candidate C |
X=?0 | End of input |
.135 | Result |
Update December 2015: Added example of how to use the program, since I couldn't remember how myself.