user1792899
user1792899

Reputation:

Monte Carlo Simulations Python

I am using Monte Carlo simulations to simulate a preservation system. I have devices in my simulation with certain fail rate. Lets say the fail rate is 1/100000 meaning 1 out of 100000 devices fail each year. If the device fail I pay for the replacement. But I don't get how to catch the device failure randomly in the simulation. As I understand I need to use probability distribution to simulate this behavior. But I do not get how to do it.

Functions in random package seem to provide random number in a particular range which I do not have. Also I do not know what sort of distribution this is.

I hope the question makes sense to people. Any help will be appreciated.

Update on this:

I did this

    #/usr/bin/python
    import random 

    def main():
        count =0
        #fail rate is 1% of 100000, I need to do this probabilistically
        for _ in range(1,100000):
            x=random.random()
            if x <= 1.0/100000:
                count += 1

                print "x = "+str(x)+" fail device"
        print "device failed "+str(count)+"  times"

    if __name__ == '__main__':
        main()

and I get output like

        x = 0.000743343826396 fail device
        device failed 1  times
        [Finished in 0.0s]

Thanks to timgeb for the help! However, I wonder if this behavior is the same as considering a fail rate of 1/100000?

Upvotes: 1

Views: 7207

Answers (1)

DrV
DrV

Reputation: 23550

The number of failing devices per yer (or whatever unit of time) follows the Poisson distribution. If you have n units for m years, and the failure rate per year is p, then you can get a random number for the number of failing units by using numpy.random.poisson:

import numpy as np

n = 100000
m = 5
p = 1.0 / 100000

failed = np.random.poisson(n * m * p)

The function np.random.poisson will randomly determine the number of failing units. Run the last line several times to see what happens.

AFAIK, pure python does not have a Poisson distribution, even though it can be calculated with some maths from, say, Gaussian random numbers.

Upvotes: 2

Related Questions