2016-02-14 65 views

回答

0

如果這是一個cdf,你只需倒置該函數。那將是:

F(x) = (sin(x) + 1)/2 
2*F(x) -1 = sin(x) 
sin^-1(2*F(x) -1) = x 

# in R code 
Finv = function(p) { 
    x = 180/pi * asin(2*p - 1) 
    ind = abs(x) < (2/pi) 
    x[ind] = 0 
    return(x) 
} 

現在我們有一個逆cdf表達式。接下來,如果我們簡單地爲p輸入0和1之間的隨機值,我們可以僞隨機地從f(x)中抽樣。這種方法被稱爲Inverse transform sampling。要展現它的作品:

Finv = function(p) { 
    x = 180/pi * asin(2*p - 1) # invere F 
    ind = abs(x) < (2/pi) # which fall in range pi/2 
    x = x[ind] # save only those that fall in range of pi/2 
    return(x) 
} 
p = runif(1e6) 
x = Finv(p) 
hist(x, breaks = 50, freq = FALSE) 
+0

謝謝 - 我忘記了這種方法。 – noneofyourbusiness617