从(任意)连续概率分布中模拟 [英] Simulate from an (arbitrary) continuous probability distribution

查看:73
本文介绍了从(任意)连续概率分布中模拟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,对于在实线上定义的归一化概率密度函数,

For a normalized probability density function defined on the real line, for example

p(x) = (2/pi) * (1/(exp(x)+exp(-x))

(这只是一个示例;该解决方案应适用于我们可以定义的任何连续PDF)R中是否有一个软件包可以从分发中进行模拟?我知道R的许多发行版的内置模拟器.

(this is just an example; the solution should apply for any continuous PDF we can define) is there a package in R to simulate from the distribution? I am aware of R's built-in simulators for many distributions.

我可以在一组分位数上以数值方式计算逆累积分布函数,将其存储在表格中,然后使用表格从均匀变量映射到所需分布的变量.已经有可以执行此操作的软件包了吗?

I could numerically compute the inverse cumulative distribution function at a set of quantiles, store them in a table, and use the table to map from uniform variates to variates from the desired distribution. Is there already a package that does this?

推荐答案

这是使用distr软件包的一种方法,为此目的而设计.

Here is a way using the distr package, which is designed for this.

library(distr)
p    <- function(x) (2/pi) * (1/(exp(x)+exp(-x)))  # probability density function
dist <-AbscontDistribution(d=p)  # signature for a dist with pdf ~ p
rdist <- r(dist)                 # function to create random variates from p

set.seed(1)                      # for reproduceable example
X <- rdist(1000)                 # sample from X ~ p
x <- seq(-10,10, .01)
hist(X, freq=F, breaks=50, xlim=c(-5,5))
lines(x,p(x),lty=2, col="red")

您当然也可以使用注释中任一链接中所述的方法,以R为基数.

You can of course also do this is base R using the methodology described in any one of the links in the comments.

这篇关于从(任意)连续概率分布中模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆