[英]How to generate random data from pareto density function in R?
I am trying to generate data from given pareto density in R. 我正在尝试根据R中的给定pareto密度生成数据。
Pareto density: F(x) = |X|^(-3) * 1 |x|>1 帕累托密度:F(x)= | X | ^(-3)* 1 | x |> 1
I know that I need to use rpareto function from actuar library, but I am not sure how should I transform given pareto density into parameters. 我知道我需要使用执行器库中的rpareto函数,但是我不确定如何将给定的pareto密度转换为参数。
The Pareto distribution is simple enough to allow for the transformation method (aka Inverse Transform Sampling ). 帕累托分布足够简单,可以使用转换方法(也称为逆变换采样 )。 The CDF of the Pareto distribution is
帕累托分布的CDF为
$$F(x) = \\int_{x_{min}}^xf(x')\\,dx' = 1- \\left(\\frac{x_{min}}{x}\\right^k$$ $$ F(x)= \\ int_ {x_ {min}} ^ xf(x')\\,dx'= 1- \\ left(\\ frac {x_ {min}} {x} \\ right ^ k $$
which can be analytically inverted to 可以解析为
$$F^{-1}(y) = x_{min} \\cdot (1-y)^{-1/k}$$ $$ F ^ {-1-1(y)= x_ {min} \\ cdot(1-y)^ {-1 / k} $$
Thus, if you transform random numbers uniformly distributed on $(0,1)$ with $F^{-1}$, you obtain Pareto distributed random numbers. 因此,如果用$ F ^ {-1} $变换均匀分布在$(0,1)$上的随机数,则可以获得Pareto分布的随机数。
Edit: Oh sorry, apparently Latex-Code is not supported here. 编辑:抱歉,显然这里不支持Latex-Code。 Here is, for your convenince, the R code:
为了方便起见,这里是R代码:
k <- 5 # parameter k of the Pareto distribution
x.min <- 2 # cutoff point of Pareto distribution
N <- 500 # number of random points
x.random <- x.min*(1-runif(N))^(-1/k)
And here is the practical demonstration that it works: 这是可行的实际演示:
h <- hist(x.random, freq=FALSE, plot=TRUE)
x <- seq(x.min, h$breaks[length(h$breaks)], by=0.01)
lines(x, k*x.min^k/x^(k+1), col="red")
Using inverse sampling method, formulas from Pareto Distribution 使用逆采样方法,来自帕累托分布的公式
Here is updated version with mean and sd computed 这是带有平均值和标准差的更新版本
rpar <- function(n, xm, a) {
v <- runif(n)
xm / v^(1.0/a)
}
rpar_mean <- function(xm, a) {
result <- 1/0 # Inf
if (a > 1.0)
result <- a*xm/(a - 1.0)
result
}
rpar_var <- function(xm, a) {
result <- 1/0 # Inf
if (a > 2.0)
result <- xm*xm*a/((a - 1.0)^2*(a - 2.0))
result
}
set.seed(54122345)
xm = 1.0
a = 3.0
q <- rpar(10000, xm, a)
print(mean(q))
print(rpar_mean(xm, a))
print(sd(q))
print(sqrt(rpar_var(xm, a)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.