[英]How to generate random values from a distribution with known density function in R?
讓我們從Pareto(1.7,70)
。
library(EnvStats)
custom_density <- function(z, location, shape) {
stopifnot(shape > 1)
ExpectS <- location * shape
ExpectS <- ExpectS / (shape - 1)
#' this should be equal to 100
# print(ExpectS)
(1 - EnvStats::ppareto(z, location = location, shape = shape)) /
ExpectS
# 100
}
custom_density(10, shape = 1.7, location = 70)
這就是我解釋您的自定義密度的方式。
plot.new()
curve(
custom_density(x, shape = 1.7, location = 70),
xlim = c(-2500,1000))
curve(dgamma(x, shape = 0.6, scale = 300), xlim = c(0, 1000), add = TRUE)
這就是自定義密度的樣子,我們也可以將它與更常見的東西聯系起來,比如伽瑪。 我選擇參數直到兩條曲線足夠相似。
然后我決定拒絕采樣作為從中采樣的方式。
有一個包可以促進這個{AR}
:
library(AR)
AR::AR.Sim(
100,
f_X = function(x) custom_density(x, shape = 1.7, location = 70),
Y.dist = "gamma",
Y.dist.par = c(shape = 0.6, rate = 1/300)
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.