Monte Carlo method

最近在用simset仿真r ray的particle detection,涉及到monte carlo方法,在以前做NMF接触过一些.

 

其实就是跑大量数据然后看其统计特性

比方说画一个方形,然后里面紧贴画个圆
不断往这个图案上随机丢针
丢个几十万
然后可以数插在圆内和圆外的针数
这样就可以计算出pi来了,呵呵
这就是所谓Monte Carlo仿真

举个简单的例子:Birthday Problem


Birthday Problem - Classical Approach
Simple examples of Monte-Carlo simulation are almost embarrassingly simple. Suppose we want to find out the
probability that, out of a group of thirty people, two people share a birthday. It’s a classic problem in probability,
with a surprisingly large answer.
Classically, you approach it like this: Pick people (and their birthdays) randomly, one at a time. We will keep
track of the probability that there are no shared birthdays.
• The first person can have any birthday, and there is still a 100% chance of no shared birthdays.
• The second person has one chance of overlapping with the first person, so there is a 364/365 chance of
placing him/her without an overlap. The probability of no shared birthdays is 364/365.

• The third person has two chances of overlapping with the first two people, so there is a 363/365 chance
of placing him/her without overlaps (two days are taken). The probability of no shared birthdays is now
(364/365) · (363/365).
• The fourth person has three chances of overlapping with the first three people, so there is a
362/365 chance of placing him/her without overlaps. The probability of no shared birthdays is now
(364/365) · (363/365) · (362/365).
• ...
• The thirtieth person has 29 chances of overlapping with the first three people, so there is a 336/365
chance of placing him/her without overlaps. The probability of having no shared birthdays is now
(364/365) · (363/365) · (362/365) · . . . · (336/365).
The overall probability of no overlapping birthdays is then 0.294, giving a 71% chance that at least one pair of
people have overlapping birthdays. It’s not too complex if you see the trick of keeping track of the probability
of zero overlaps, rather than trying to add up the probability of one or more overlaps. It also takes some
thought to realize that the probabilities are conditioned properly, so that multiplying together all the various
P (N th person doesn’t overlap|first N − 1 people don’t overlap) factors.


Birthday Problem – Monte-Carlo Approach
The solution here is conceptually very simple:
1. Pick 30 random numbers in the range [1,365]. Each number represents one day of the year.
2. Check to see if any of the thirty are equal.
3. Go back to step 1 and repeat 10,000 times.
4. Report the fraction of trials that have matching birthdays.

 


A computer program in Python to do this calculation is quite simple:
#!/usr/bin/env python
import random # Get a random number generator.
NTRIALS = 10000 # Enough trials to get an reasonably accurate answer.
NPEOPLE = 30 # How many people in the group?
matches = 0 # Keep track of how many trials have matching birthdays.
for trial in range(NTRIALS): # Do a bunch of trials...
taken = {} # A place to keep track of which birthdays
          #
         are already taken on this trial.
for person in range(NPEOPLE):
day = random.randint(0, 365)
if day in taken:
matches += 1
break
taken[day] = 1
# Put the people’s birthdays down, one at a time...
# On a randomly chosen day.
# A match!
# No need to look for more than one.
# Mark the day as taken.
print ’The fraction of trials that have matching birthdays is’, float(matches)/NTRIALS
And the answer is:
The fraction of trials that have matching birthdays is 0.7129

 

其实在以前上概率课是接触过类似的东西,没想到就是monte carlo,当然只是最简单的情况。

你可能感兴趣的:(python,report,Random,float,pair,Numbers)