2011-12-20 85 views
6

我想通過一個模擬解決方案來解決「象徵性」解決的問題。現在,我想知道如何直接使用Mathematica進行集成。Mathematica中的集成

請考慮一個以r = 1爲圓心的目標,以(0,0)爲中心。我想模擬一下我的擲箭投擲目標的概率。

現在,我沒有偏見扔,那就是平均我就要揍中心畝= 0,但我的方差爲1

考慮我的鏢的座標,因爲它擊中目標(或牆:-))我有以下分佈,2個高斯:

XDistribution : 1/Sqrt[2 \[Pi]\[Sigma]^2] E^(-x^2/(2 \[Sigma]^2)) 

YDistribution : 1/Sqrt[2 \[Pi]\[Sigma]^2] E^(-y^2/(2 \[Sigma]^2)) 

有了這些2分佈在0與等於方差= 1爲中心,我的聯合分佈成爲高斯二元如:

1/(2 \[Pi]\[Sigma]^2) E^(-((x^2 + y^2)/(2 \[Sigma]^2))) 

所以我需要知道我碰到目標的概率或x^2 + y^2的概率小於1.

在極座標系中進行轉換後的積分首先給出了我的解決方案: .39。仿真證實,它使用:

[email protected][ 
    If[ 
     EuclideanDistance[{ 
         RandomVariate[NormalDistribution[0, Sqrt[1]]], 
         RandomVariate[NormalDistribution[0, Sqrt[1]]] 
         }, {0, 0}] < 1, 1,0], {1000000}]/1000000 

我覺得要解決使用Mathematica的整合能力,這個問題更優雅的方式有,但從來沒有得到映射醚工作。

回答

6

實際上有幾種方法可以做到這一點。

最簡單的辦法是使用NIntegrate爲:

JointDistrbution = 1/(2 \[Pi] \[Sigma]^2) E^(-((x^2 + y^2)/(2 \[Sigma]^2))); 
NIntegrate[JointDistrbution /. \[Sigma] -> 1, {y, -1, 1}, 
    {x, -Sqrt[1 - y^2], Sqrt[1 - y^2]}] // Timing 

Out[1]= {0.009625, 0.393469} 

這是另一種方式來做到這一點經驗(類似於上面的例子),但比使用NIntegrate慢了許多:

(EuclideanDistance[#, {0, 0}] <= 1 & /@ # // Boole // Total)/ 
    [email protected]# &@RandomVariate[NormalDistribution[0, 1], {10^6, 2}] // 
    N // Timing 

Out[2]= {5.03216, 0.39281} 
+0

我發現有趣的是,Mathematica也能夠'集成[]'JointDistribution。 – 2011-12-21 12:36:43

4

內置功能NProbability也快:

NProbability[ x^2 + y^2 <= 1, {x, y} \[Distributed] 
BinormalDistribution[{0, 0}, {1, 1}, 0]] // Timing 

NProbability[x^2 + y^2 <= 1, x \[Distributed] 
NormalDistribution[0, 1] && y \[Distributed] 
NormalDistribution[0, 1] ] // Timing 

既給{0.031, 0.393469}

由於n標準法線平方和分佈ChiSquare[n],你會得到一個更精簡的表達NProbability[z < 1, z \[Distributed] ChiSquareDistribution[2]]其中z=x^2+y^2xy分佈NormalDistribution[0,1]。時間與上述相同:{0.031, 0.393469}

編輯:計時器爲Vista 64位Core2雙核T9600 2.80GHz機器與8G內存(MMA 8.0.4)。 Yoda在這臺機器上的解決方案的計時時間爲{0.031, 0.393469}

編輯2:使用ChiSquareDistribution[2]仿真可以做如下:

(data = RandomVariate[ChiSquareDistribution[2], 10^5]; 
    Probability[w <= 1, w \[Distributed] data] // N) // Timing 

產生{0.031, 0.3946}

編輯3:計時更多細節:

對於

[email protected]@Table[[email protected] 
    NProbability[x^2 + y^2 <= 1, {x, y} \[Distributed] 
    BinormalDistribution[{0, 0}, {1, 1}, 0]], {10}] 

我得到{0.047, 0.031, 0.031, 0.031, 0.031, 0.016, 0.016, 0.031, 0.015, 0.016}

對於

[email protected]@Table[[email protected] 
NProbability[x^2 + y^2 <= 1, 
x \[Distributed] NormalDistribution[0, 1] && 
    y \[Distributed] NormalDistribution[0, 1] ], {10}] 

我得到{0.047, 0.031, 0.032, 0.031, 0.031, 0.016, 0.031, 0.015, 0.016, 0.031}

對於

[email protected]@Table[[email protected] 
NProbability[z < 1, 
z \[Distributed] ChiSquareDistribution[2]], {10}] 

我得到{0.047, 0.015, 0.016, 0.016, 0.031, 0.015, 0.016, 0.016, 0.015, 0.}

對於Yoda的

[email protected]@Table[[email protected](JointDistrbution = 
    1/(2 \[Pi] \[Sigma]^2) E^(-((x^2 + y^2)/(2 \[Sigma]^2))); 
NIntegrate[ 
    JointDistrbution /. \[Sigma] -> 1, {y, -1, 
    1}, {x, -Sqrt[1 - y^2], Sqrt[1 - y^2]}]), {10}] 

我得到{0.031, 0.032, 0.015, 0., 0.016, 0., 0.015, 0.016, 0.016, 0.}

對於經驗估計

[email protected]@Table[[email protected](Probability[w <= 1, 
w \[Distributed] data] // N), {10}] 

{0.031, 0.016, 0.016, 0., 0.015, 0.016, 0.015, 0., 0.016, 0.016}

+0

我發現它非常可疑,你的時機對你所有的三個解決方案來說都是完全相同的_and_ mine ...我當然會得到非常不同的時機 – abcd 2011-12-20 05:20:02

+0

@yoda,好奇不是嗎?我正要問你是否可以在你的機器上運行上面的代碼。 – kglr 2011-12-20 05:41:08

+0

這些是我爲你的三種方法(按你列出的順序)和我的(最後一個)獲得的時間點:'{0.035673,0.022273,0.097494,0.009067}' – abcd 2011-12-20 05:46:11