Monday, January 16, 2017

The Bacteria Division Puzzle

Q. A jar has a single cell of a bacteria. After every fixed interval of time the bacteria either splits into two with probability 2/5, does nothing with probability 2/5 or dies out with probability 1/5. What is the probability that the bacteria would continue to make a large family tree over an extended period of time?

A. The situation can be described by the following visual.
Assume that the required probability is 'p'. The term 1 - p would represent the probability that the ecosystem eventually dies out. Each of the above scenarios contributes a quantum of probability towards the ecosystem eventually dying out. Lets start off by represent 1 - p as 'x'. The probability that the bacteria die out is

The total of each of the above must add up to the probability that the bacteria eventually die out, which is 'x'. So you can phrase the problem recursively as

This simplifies to

which is a quadratic equation, yielding a solution as

This further yields x = 1/2 or x = 1. It's easy to see why x = 1 isn't a solution because scenario 1 would rule that out immediately. This yields a probability of 50%.

In order to test this out, it's also relatively easy to code it up in Python. The following code shows how to simulate this scenario.

import sys
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

dist_estimates = []

for index in range(400):
    counter = 0
    Repeating 100 times for estimating the probability
    for epoch in range(100):
        num_bacteria = 1
        Looking ahead 10 steps
        for iter in range(20):
            bacteria = np.zeros(int(num_bacteria))
            Step through each bacteria
            for nb_iter in range(int(num_bacteria)):
                Decide if each of the bacteria
                will die/stay/split in this time slice
                prob = np.random.uniform(0,1)
                if prob < 0.2:
                    ''' This one dies '''
                    bacteria[nb_iter] = 0
                elif prob >= 0.2 and prob <= 0.6:
                    ''' This one stays as is '''
                    bacteria[nb_iter] = 1
                    ''' This one splits into two '''
                    bacteria[nb_iter] = 2
            num_bacteria = np.sum(bacteria)
        if np.sum(bacteria) > 0:
            counter += 1
Visualization is easy
Use matplotlib and seaborn

p = sns.distplot(dist_estimates,kde=False,rug=True).get_figure()

The above code generates a plot of estimates for the said probability. This came out the following way which validates our method.

If you are looking to learn probability, programming or data science related books, a good set of books are listed here

No comments:

Post a Comment