博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Monte Carlo method
阅读量:2442 次
发布时间:2019-05-10

本文共 3487 字,大约阅读时间需要 11 分钟。

最近在用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,当然只是最简单的情况。

转载地址:http://jxiqb.baihongyu.com/

你可能感兴趣的文章
wps宏的功能_宏与功能之间的区别
查看>>
while和do while循环之间的区别
查看>>
适用于Windows / Mac / Linux的5种最佳Python IDE
查看>>
字符串转换整数python_Python将字符串转换为整数
查看>>
程序员连续剧_每个程序员都应该看的5部最佳电视连续剧
查看>>
人工智能优缺点_人工智能的优缺点
查看>>
运算符重载 python_Python运算符重载
查看>>
tensorflow简介_TensorFlow简介
查看>>
矩阵 python 加法_Python矩阵加法
查看>>
python快速排序_Python快速排序
查看>>
人工神经网络导论_神经网络导论
查看>>
C ++ STL无序多集– std :: unordered_multiset
查看>>
深度学习导论
查看>>
go-back-n_iMyFone D-Back iPhone数据恢复
查看>>
MailboxValidator –批量电子邮件列表清理服务
查看>>
机器学习中常见的最优化算法_最常见的机器学习算法
查看>>
css图片和边框之间有间隔_CSS和CSS3之间的区别
查看>>
iphone浏览器劫持修复_修复iPhone卡在Apple徽标问题上的问题
查看>>
5个最佳Python机器学习IDE
查看>>
c语言中定义和声明的区别_C中声明和定义之间的区别
查看>>