HT121
HT121

Reputation: 451

find the integer number of items from a list given a probability

Assume, I have a list with a length of 20 and every day, I want to find out the integer number of items from this list given a certain probability. After extracting the integer number of items, I want to do further operations.

m = [10,20,30,40, 50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200]
prob = 0.2

In the above example, I want to find out that how many items will be selected on average everyday given a 0.2 probability. So on average, from a list of length 20 and probability 0.2, I should get number of items = 4 every day. How can I get number of events given a probability in Python? Should I use Poisson to get number of events in a time period but I am not sure, how to include the size of the list in a Poisson function in Python. And how to get integer number of items when probability is lets say 0.113 or 0.31 etc. How can I obtain this in Python?

Upvotes: 0

Views: 823

Answers (2)

John Coleman
John Coleman

Reputation: 51998

Assuming that the selection or not of each list element is an independent event with probability prob, the number of items selected is a binomial random variable with parameters len(m) and prob. The expected value of such a random variable is prob * len(m).

If you want to simulate this random variable (rather than compute its expected value) you can use:

import random

def binomial_variate(n,p):
    return sum(random.random() < p for _ in range(n))

For example,

>>> trials = [binomial_variate(20,0.12) for _ in range(100)]
>>> trials
[5, 6, 2, 1, 2, 4, 3, 3, 2, 3, 4, 1, 3, 2, 2, 3, 1, 0, 2, 4, 6, 1, 1, 2, 2, 3, 4, 2, 2, 1, 2, 1, 3, 4, 2, 3, 2, 2, 4, 4, 5, 1, 1, 1, 1, 2, 2, 3, 2, 2, 5, 4, 4, 1, 4, 4, 3, 5, 2, 3, 2, 4, 3, 4, 4, 2, 2, 1, 2, 1, 2, 1, 3, 0, 4, 3, 2, 4, 1, 2, 1, 2, 3, 2, 2, 3, 2, 1, 4, 4, 2, 2, 2, 2, 2, 3, 3, 3, 4, 2]
>>> sum(trials)/100
2.55
>>> 20*0.12
2.4

Upvotes: 2

cocool97
cocool97

Reputation: 1251

You could just use something like int(len(m) * prob) in your python code and it will do the trick. Be sure to surround with a try/catch to avoid errors.
You should also verify that the prob value is composed between 0 and 1 included

Upvotes: 0

Related Questions