在R中定义指数分布以估计概率 [英] Defining exponential distribution in R to estimate probabilities
问题描述
我有一堆随机变量(X1,....,Xn)
,即i.i.d. Exp(1/2)
并表示特定事件的持续时间.因此,该分布显然有2的期望值,但是我在R中定义它时遇到了问题.我进行了一些研究,发现了所谓的蒙特卡洛刺激法,但是我似乎没有找到我想要的东西在里面.
我要估算的一个示例是:假设我们有10个随机变量(X1,..,X10)
如上分布,并且我们想确定例如概率P([X1+...+X10<=25])
.
谢谢.
您知道R中的rexp()
函数吗?通过在R控制台中输入?rexp
来查看文档页面.
您对所需概率的蒙特卡洛估计的快速解答:
mean(rowSums(matrix(rexp(1000 * 10, rate = 0.5), 1000, 10)) <= 25)
我已经生成1000组10个指数样本,并将它们放入1000 * 10矩阵中.我们取行总和并获得1000个条目的向量.介于0到25之间的值的比例是期望概率的经验估计.
谢谢,这很有帮助!我可以在代码中使用
replicate
来使它看起来像这样:F <- function(n, B=1000) mean(replicate(B,(rexp(10, rate = 0.5))))
,但是我无法输出正确的结果.
replicate
在这里也会生成一个矩阵,但是它是一个10 * 1000的矩阵(而不是我的答案中的1000 * 10的矩阵),所以现在您需要使用colSums
.另外,您将n
放在哪里?
正确的功能应该是
F <- function(n, B=1000) mean(colSums(replicate(B, rexp(10, rate = 0.5))) <= n)
对于给定示例的非蒙特卡洛方法,请参见其他答案.指数分布是伽马分布的特例,后者具有可加性.
我给您的是蒙特卡洛方法,因为您在问题中将其命名,并且该方法适用于您的示例.
I have a bunch of random variables (X1,....,Xn)
which are i.i.d. Exp(1/2)
and represent the duration of time of a certain event. So this distribution has obviously an expected value of 2, but I am having problems defining it in R. I did some research and found something about a so-called Monte-Carlo Stimulation, but I don't seem to find what I am looking for in it.
An example of what i want to estimate is: let's say we have 10 random variables (X1,..,X10)
distributed as above, and we want to determine for example the probability P([X1+...+X10<=25])
.
Thanks.
Are you aware of rexp()
function in R? Have a look at documentation page by typing ?rexp
in R console.
A quick answer to your Monte Carlo estimation of desired probability:
mean(rowSums(matrix(rexp(1000 * 10, rate = 0.5), 1000, 10)) <= 25)
I have generated 1000 set of 10 exponential samples, putting them into a 1000 * 10 matrix. We take row sum and get a vector of 1000 entries. The proportion of values between 0 and 25 is an empirical estimate of the desired probability.
Thanks, this was helpful! Can I use
replicate
with this code, to make it look like this:F <- function(n, B=1000) mean(replicate(B,(rexp(10, rate = 0.5))))
but I am unable to output the right result.
replicate
here generates a matrix, too, but it is an 10 * 1000 matrix (as opposed to a 1000* 10 one in my answer), so you now need to take colSums
. Also, where did you put n
?
The correct function would be
F <- function(n, B=1000) mean(colSums(replicate(B, rexp(10, rate = 0.5))) <= n)
For non-Monte Carlo method to your given example, see the other answer. Exponential distribution is a special case of gamma distribution and the latter has additivity property.
I am giving you Monte Carlo method because you name it in your question, and it is applicable beyond your example.
这篇关于在R中定义指数分布以估计概率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!