gpt4 book ai didi

r - 从受限制的帕累托分布中抽取随机数

转载 作者:行者123 更新时间:2023-12-04 09:47:24 24 4
gpt4 key购买 nike

我是R的新手,并且需要有关如何从参数s和beta的帕累托分布的有限区域中抽取随机数的建议。 (系统:Windows 7,R 2.15.2。)

(1)我在向量data $ t中有数据;每个数据点我都会称之为data&tx

对于这些数据,根据https://stats.stackexchange.com/questions/27426/how-do-i-fit-a-set-of-data-to-a-pareto-distribution-in-r估计帕累托分布的参数s&beta

pareto.MLE <- function(X)
{
n <- length(X)
m <- min(X)
a <- n/sum(log(X)-log(m))
return( c(m,a) )
}

(2)现在,我需要根据该观察点(=数据点:data $ tx)绘制此帕累托分布(s,beta,参见(1))的尽可能多的随机数(RndNew)。对于平局,必须从中抽取随机数的区域限制为RndNewx> = data $ tx;的区域。换句话说:RndNewx一定不能小于相应的data $ tx。

问题:如何告诉R限制从中绘制随机数的Pareto分布的面积为RndNewx> = data $ tx?

多谢您的协助!

最佳答案

从截断的分布中采样的标准方法包括三个步骤。这是一个具有正态分布的示例,因此您可以理解。

n <- 1000
lower_bound <- -1
upper_bound <- 1

将CDF应用于上下边界,以找到分布边缘的分位数。
(quantiles <- pnorm(c(lower_bound, upper_bound)))
# [1] 0.1586553 0.8413447

从这些分位数之间的均匀分布中采样。
uniform_random_numbers <- runif(n, quantiles[1], quantiles[2])

应用逆CDF。
truncated_normal_random_numbers <- qnorm(uniform_random_numbers)



用于pareto分发的CDF为
ppareto <- function(x, scale, shape)
{
ifelse(x > scale, 1 - (scale / x) ^ shape, 0)
}

逆是
qpareto <- function(y, scale, shape)
{
ifelse(
y >= 0 & y <= 1,
scale * ((1 - y) ^ (-1 / shape)),
NaN
)
}

我们可以重做上面的示例以使用这些Pareto函数。
n <- 1000
scale <- 1
shape <- 1
lower_bound <- 2
upper_bound <- 10

(quantiles <- ppareto(c(lower_bound, upper_bound), scale, shape))
uniform_random_numbers <- runif(n, quantiles[1], quantiles[2])
truncated_pareto_random_numbers <- qpareto(uniform_random_numbers, scale, shape)



为了更容易重用此代码,我们可以将其包装到一个函数中。下限和上限具有与分布范围相匹配的默认值,因此,如果不传递值,则将获得非截断的帕累托分布。
rpareto <- function(n, scale, shape, lower_bound = scale, upper_bound = Inf)
{
quantiles <- ppareto(c(lower_bound, upper_bound), scale, shape)
uniform_random_numbers <- runif(n, quantiles[1], quantiles[2])
qpareto(uniform_random_numbers, scale, shape)
}

关于r - 从受限制的帕累托分布中抽取随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14497349/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com