日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

蒙特卡洛法—非均匀随机数的产生

發(fā)布時(shí)間:2025/3/14 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 蒙特卡洛法—非均匀随机数的产生 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.反變換法

設(shè)需產(chǎn)生分布函數(shù)為F(x)的連續(xù)隨機(jī)數(shù)X。若已有[0,1]區(qū)間均勻分布隨機(jī)數(shù)R,則產(chǎn)生X的反變換公式為:

F(x)=r, 即x=F-1(r)

反函數(shù)存在條件:如果函數(shù)y=f(x)是定義域D上的單調(diào)函數(shù),那么f(x)一定有反函數(shù)存在,且反函數(shù)一定是單調(diào)的。分布函數(shù)F(x)為是一個(gè)單調(diào)遞增函數(shù),所以其反函數(shù)存在。從直觀意義上理解,因?yàn)閞一一對(duì)應(yīng)著x,而在[0,1]均勻分布隨機(jī)數(shù)R≤r的概率P(R≤r)=r?因此,連續(xù)隨機(jī)數(shù)X≤x的概率P(X≤x)=P(R≤r)=r=F(x)

即X的分布函數(shù)為F(x)。

例子:下面的代碼使用反變換法在區(qū)間[0, 6]上生成隨機(jī)數(shù),其概率密度近似為P(x)=e-x??

1 import numpy as np 2 import matplotlib.pyplot as plt 3 4 # probability distribution we're trying to calculate 5 p = lambda x: np.exp(-x) 6 7 # CDF of p 8 CDF = lambda x: 1-np.exp(-x) 9 10 # invert the CDF 11 invCDF = lambda x: -np.log(1-x) 12 13 # domain limits 14 xmin = 0 # the lower limit of our domain 15 xmax = 6 # the upper limit of our domain 16 17 # range limits 18 rmin = CDF(xmin) 19 rmax = CDF(xmax) 20 21 N = 10000 # the total of samples we wish to generate 22 23 # generate uniform samples in our range then invert the CDF 24 # to get samples of our target distribution 25 R = np.random.uniform(rmin, rmax, N) 26 X = invCDF(R) 27 28 # get the histogram info 29 hinfo = np.histogram(X,100) 30 31 # plot the histogram 32 plt.hist(X,bins=100, label=u'Samples'); 33 34 # plot our (normalized) function 35 xvals=np.linspace(xmin, xmax, 1000) 36 plt.plot(xvals, hinfo[0][0]*p(xvals), 'r', label=u'p(x)') 37 38 # turn on the legend 39 plt.legend() 40 plt.show()

?一般來(lái)說(shuō),直方圖的外廓曲線接近于總體X的概率密度曲線。

?2.舍選抽樣法(Rejection Methold)

用反變換法生成隨機(jī)數(shù)時(shí),如果求不出F-1(x)的解析形式或者F(x)就沒(méi)有解析形式,則可以用F-1(x)的近似公式代替。但是由于反函數(shù)計(jì)算量較大,有時(shí)也是很不適宜的。另一種方法是由Von Neumann提出的舍選抽樣法。下圖中曲線w(x)為概率密度函數(shù),按該密度函數(shù)產(chǎn)生隨機(jī)數(shù)的方法如下:

基本的rejection methold步驟如下:

1. Draw x uniformly from [xmin ?xmax]

2.?Draw x uniformly from?[0, ymax]

3. if y < w(x),accept the sample, otherwise reject it

4. repeat

即落在曲線w(x)和X軸所圍成區(qū)域內(nèi)的點(diǎn)接受,落在該區(qū)域外的點(diǎn)舍棄。

例子:下面的代碼使用basic rejection sampling methold在區(qū)間[0, 10]上生成隨機(jī)數(shù),其概率密度近似為P(x)=e-x??

1 # -*- coding: utf-8 -*- 2 ''' 3 The following code produces samples that follow the distribution P(x)=e^?x 4 for x=[0, 10] and generates a histogram of the sampled distribution. 5 ''' 6 import numpy as np 7 import matplotlib.pyplot as plt 8 9 10 P = lambda x: np.exp(-x) 11 12 # domain limits 13 xmin = 0 # the lower limit of our domain 14 xmax = 10 # the upper limit of our domain 15 16 # range limit (supremum) for y 17 ymax = 1 18 19 N = 10000 # the total of samples we wish to generate 20 accepted = 0 # the number of accepted samples 21 samples = np.zeros(N) 22 count = 0 # the total count of proposals 23 24 # generation loop 25 while (accepted < N): 26 27 # pick a uniform number on [xmin, xmax) (e.g. 0...10) 28 x = np.random.uniform(xmin, xmax) 29 30 # pick a uniform number on [0, ymax) 31 y = np.random.uniform(0,ymax) 32 33 # Do the accept/reject comparison 34 if y < P(x): 35 samples[accepted] = x 36 accepted += 1 37 38 count +=1 39 40 print count, accepted 41 42 # get the histogram info 43 # If bins is an int, it defines the number of equal-width bins in the given range 44 (n, bins)= np.histogram(samples, bins=30) # Returns: n-The values of the histogram,n是直方圖中柱子的高度 45 46 # plot the histogram 47 plt.hist(samples,bins=30,label=u'Samples') # bins=30即直方圖中有30根柱子 48 49 # plot our (normalized) function 50 xvals=np.linspace(xmin, xmax, 1000) 51 plt.plot(xvals, n[0]*P(xvals), 'r', label=u'P(x)') 52 53 # turn on the legend 54 plt.legend() 55 plt.show()

>>>?

99552 10000

3.推廣的舍取抽樣法

從上圖中可以看出,基本的rejection methold法抽樣效率很低,因?yàn)殡S機(jī)數(shù)x和y是在區(qū)間[xmin ?xmax]和區(qū)間[0 ?ymax]上均勻分布的,產(chǎn)生的大部分點(diǎn)不會(huì)落在w(x)曲線之下(曲線e-x的形狀一邊高一邊低,其曲線下的面積占矩形面積的比例很小,則舍選抽樣效率很低)。為了改進(jìn)簡(jiǎn)單舍選抽樣法的效率,可以構(gòu)造一個(gè)新的密度函數(shù)q(x)(called a proposal distribution from which we can readily draw samples),使它的形狀接近p(x),并選擇一個(gè)常數(shù)k使得kq(x)≥w(x)對(duì)于x定義域內(nèi)的值都成立。對(duì)應(yīng)下圖,首先從分布q(z)中生成隨機(jī)數(shù)z0,然后按均勻分布從區(qū)間[0 ? kq(z0)]生成一個(gè)隨機(jī)數(shù)u0。?if?u0?> p(z0) then the sample is rejected,otherwise?u0?is retained. ?即下圖中灰色區(qū)域內(nèi)的點(diǎn)都要舍棄。可見(jiàn),由于隨機(jī)點(diǎn)u0只出現(xiàn)在曲線kq(x)之下,且在q(x)較大處出現(xiàn)次數(shù)較多,從而大大提高了采樣效率。顯然q(x)形狀越接近p(x),則采樣效率越高。?

?

根據(jù)上述思想,也可以表達(dá)采樣規(guī)則如下:

1. Draw x from your proposal distribution q(x)

2. Draw y uniformly from [0 ?1]

3. if y < p(x)/kq(x) , accept the sample, otherwise reject it

4. repeat

下面例子中選擇函數(shù)p(x)=1/(x+1)作為proposal distribution,k=1。曲線1/(x+1)的形狀與e-x相近。

1 import numpy as np 2 import matplotlib.pyplot as plt 3 4 p = lambda x: np.exp(-x) # our distribution 5 g = lambda x: 1/(x+1) # our proposal pdf (we're choosing k to be 1) 6 CDFg = lambda x: np.log(x +1) # generates our proposal using inverse sampling 7 8 # domain limits 9 xmin = 0 # the lower limit of our domain 10 xmax = 10 # the upper limit of our domain 11 12 # range limits for inverse sampling 13 umin = CDFg(xmin) 14 umax = CDFg(xmax) 15 16 N = 10000 # the total of samples we wish to generate 17 accepted = 0 # the number of accepted samples 18 samples = np.zeros(N) 19 count = 0 # the total count of proposals 20 21 # generation loop 22 while (accepted < N): 23 24 # Sample from g using inverse sampling 25 u = np.random.uniform(umin, umax) 26 xproposal = np.exp(u) - 1 27 28 # pick a uniform number on [0, 1) 29 y = np.random.uniform(0, 1) 30 31 # Do the accept/reject comparison 32 if y < p(xproposal)/g(xproposal): 33 samples[accepted] = xproposal 34 accepted += 1 35 36 count +=1 37 38 print count, accepted 39 40 # get the histogram info 41 hinfo = np.histogram(samples,50) 42 43 # plot the histogram 44 plt.hist(samples,bins=50, label=u'Samples'); 45 46 # plot our (normalized) function 47 xvals=np.linspace(xmin, xmax, 1000) 48 plt.plot(xvals, hinfo[0][0]*p(xvals), 'r', label=u'p(x)') 49 50 # turn on the legend 51 plt.legend() 52 plt.show()

>>>?

24051 10000

可以對(duì)比基本的舍取法和改進(jìn)的舍取法的結(jié)果,前者產(chǎn)生符合要求分布的10000個(gè)隨機(jī)數(shù)運(yùn)算了99552步,后者運(yùn)算了24051步,可以看到效率明顯提高。

?

參考:

http://iacs-courses.seas.harvard.edu/courses/am207/blog/lecture-3.html

http://blog.csdn.net/xianlingmao/article/details/7768833

http://blog.sina.com.cn/s/blog_60b44d6a0101l45z.html

http://www.ruanyifeng.com/blog/2015/07/monte-carlo-method.html

轉(zhuǎn)載于:https://www.cnblogs.com/21207-iHome/p/5266399.html

總結(jié)

以上是生活随笔為你收集整理的蒙特卡洛法—非均匀随机数的产生的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 精品三级 | 国产色爽 | 久久99久久99精品免观看软件 | 最新av网站在线观看 | 亚洲视频在线观看一区二区三区 | 国产成人自拍在线 | 成人国产精品一区二区 | 国产jizz | 99久久国产热无码精品免费 | 免费在线a| 国产主播在线播放 | 成人私密视频 | 不卡影院av | 国产第3页 | 国产欧美日韩视频 | 久久久久亚洲视频 | 国产精品久久久久一区二区 | 天堂在线观看免费视频 | 99免费国产 | 欧美一区二区福利 | 懂色av成人一区二区三区 | 国产日韩精品一区二区三区 | 日韩一区高清 | 中文字幕一区二区三区乱码不卡 | 亚洲一级片在线播放 | 欧美午夜精品理论片a级按摩 | 日本女优中文字幕 | 日韩免费高清视频网站 | www,xxx日本 | 日韩欧美国产亚洲 | 人人澡澡人人 | 国产一级片 | 特级毛片a | 午夜影视在线观看 | 啦啦啦免费高清视频在线观看 | 高清视频一区二区三区 | 97在线观看 | 亚洲性网站 | www.天天干| 亚洲国产综合在线 | 欧美干| 精品国产一区二区三区在线 | 人禽高h交 | 一区二区三区在线视频免费观看 | 亚洲精品一区二区三区蜜桃 | 久久精品免费看 | 欧美久久久精品 | 影院色原网站 | 国产精品视屏 | 精品一区二区不卡 | 羞羞答答一区 | 国产精品一卡 | 91精品国产高清91久久久久久 | 国产91看片 | 人人爽人人爽人人片 | 精品久久在线观看 | 97超碰国产精品无码蜜芽 | 91久久精品一区二区三 | 日韩精品在线观看一区二区三区 | 少妇精品导航 | 亚洲性激情 | 欧美日韩激情一区 | 大地资源在线观看免费高清版粤语 | 91激情| 五月天社区 | 青草视频在线观看免费 | 大尺度做爰啪啪床戏 | 又黄又爽的网站 | 精品一区二区成人免费视频 | 国产精品99久久久 | 日屁视频 | 欧美日韩网站 | 97免费在线观看视频 | 日韩第八页 | 久久国产情侣 | xx视频在线| 天天操天天干视频 | 男人日女人的网站 | 亚洲精品国产精品国自产网站按摩 | 日本簧片在线观看 | 夜夜骚av一区二区三区 | 午夜免费福利视频 | 久久成人乱码欧美精品一区二区 | 九色一区 | 黄色影音| 久草福利资源在线 | 精品久久网站 | 国产黄在线免费观看 | 中文字幕第12页 | 精品一区二区在线视频 | 亚洲视频在线播放 | 狂野欧美性猛交xxxx巴西 | 成人高潮视频 | 韩国美女福利视频 | 碧蓝之海动漫在线观看免费高清 | 久久高清av| 嫩草精品| 一久久 | 久久精品国产亚洲av成人 |