3
$\begingroup$

I'm trying to generate multivariate monomials in variables $p_1, \ldots, p_n$. Each monomial has either $p_i$ as a factor, otherwise $(1-p_i)$. I need to generate all monomials which have exactly $k$ variables in the $p_i$ form.

(Essentially there are $n$ coin tosses, the coins having probabilities $p_i$, and I want all monomials corresponding to the probabilities of different ways of getting exactly $k$ heads.)

I tried generating subsets of $\{1,\ldots, n\}$ of size $k$ and manipulating the resulting lists into monomials, but I'm too inexperienced with Mathematica.

eg. if $n=3$ and $k=1$, the list of monomials are $$\{p_1(1-p_2)(1-p_3), (1-p_1)p_2(1-p_3), (1-p_1)(1-p_2)p_3\}$$

$\endgroup$

1 Answer 1

4
$\begingroup$
ClearAll[f1, f2]
f1[n_, i_] := Permutations /@ IntegerPartitions[i, {n}, {0, 1}]
f2[n_, i_] := Module[{pr = Array[Subscript[p, #]&, n]}, 
  Times @@ (pr^# (1 - pr)^(1 - #)) & /@ f1[n, i][[1]]]

Examples:

f2[3, 1] // TeXForm

$\left\{p_1 \left(1-p_2\right) \left(1-p_3\right),\left(1-p_1\right) p_2 \left(1-p_3\right),\left(1-p_1\right) \left(1-p_2\right) p_3\right\}$

f2[4, 2] // Column // TeXForm

$\begin{array}{l} p_1 p_2 \left(1-p_3\right) \left(1-p_4\right) \\ p_1 \left(1-p_2\right) p_3 \left(1-p_4\right) \\ p_1 \left(1-p_2\right) \left(1-p_3\right) p_4 \\ \left(1-p_1\right) p_2 p_3 \left(1-p_4\right) \\ \left(1-p_1\right) p_2 \left(1-p_3\right) p_4 \\ \left(1-p_1\right) \left(1-p_2\right) p_3 p_4 \\ \end{array}$

Alternatively, you can use the PDF of the joint distribution of n independent Bernoulli random variables:

ClearAll[f3]
f3[n_, i_] := Module[{dist = ProductDistribution @@ 
     Array[BernoulliDistribution @ Subscript[p, #] &, n]},
  PDF[dist, #] & /@ First[Permutations /@ IntegerPartitions[i, {n}, {0, 1}]]]

f3[3, 1] == f2[3, 1]

True

f3[5, 3] == f2[5, 3]

True

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.