Experiments with tossing a coin are a common way to illustrate some basic concepts in probability. A well-known example is computing the probability to have exactly $$k$$ heads after $$n$$ coin tosses.  The common assumption is independence of individual coin tosses, which allows to use simple multiplication to determine the joint probability. Another common assumption is that the coins used are not biased (i.e all coins have equal chances to give heads or tails).

Such a setup allows to use the binomial formula to find the answer. This elegant solution is made possible by the fact that the same coin is tossed multiple times, or equivalently if each toss uses a different coin, but all coins have the same probability for obtaining heads and tails.

Assignment

In this assignment we will explore the scenario where a series of coin tosses makes use of multiple coins, where each coin is biased in a different way.

Write a function countingHeads that takes an integer $$k$$ and an array (a list or a tuple) containing $$n$$ numbers $$p_1, p_2, \ldots, p_n$$, with $$p_i \in [0, 1]$$. The function must return the probability to obtain exactly $$k$$ heads when $$n$$ biased coins are tossed independently, with $$p_i$$ being the probability that the $$i$$-th coin comes up head.

Example

>>> countingHeads(1, (0.5, 0.5))
0.5
>>> countingHeads(2, (0.25, 0.5))
0.125
>>> countingHeads(0, (0.25, 0.75, 0.5))
0.09375
>>> countingHeads(1, (0.25, 0.75, 0.5))
0.40625
>>> countingHeads(2, (0.25, 0.75, 0.5))
0.40625
>>> countingHeads(3, (0.25, 0.75, 0.5))
0.09375