简体   繁体   English

R 中的经验 CDF 与理论 CDF

[英]Empirical CDF vs Theoretical CDF in R

I want to check the "probability integral transform" theorem using R.我想使用 R 检查“概率积分变换”定理。 Let's suppose X is an exponential random variable with lambda = 5 .假设X是一个指数随机变量,其中lambda = 5 I want to check that the random variable U = F_X = 1 - exp(-5*X) has a uniform (0,1) distribution.我想检查随机变量U = F_X = 1 - exp(-5*X)是否具有均匀 (0,1) 分布。 How would you do it?你会怎么做?

I would start in this way:我会以这种方式开始:

nsample <- 1000
lambda <- 5
x <- rexp(nsample, lambda) #1000 exponential observation
u <- 1- exp(-lambda*x) #CDF of x 

Then I need to find the CDF of u and compare it with the CDF of a Uniform (0,1).然后我需要找到 u 的 CDF 并将其与 Uniform (0,1) 的 CDF 进行比较。

For the empirical CDF of u I could use the ECDF function:对于你的经验 CDF,我可以使用 ECDF function:

ECDF_u <- ecdf(u) #empirical CDF of U

Now I should create the theoretical CDF of Uniform (0,1) and plot it on the same graph of the ECDF in order to compare the two graphs.现在我应该在 ECDF 的同一图上创建 Uniform (0,1) 和 plot 的理论 CDF,以便比较这两个图。

Can you help with the code?你能帮忙看一下代码吗?

You are almost there.你快到了。 You don't need to compute the ECDF yourself – qqplot will take care of this.你不需要自己计算 ECDF—— qqplot会处理这个。 All you need is your sample ( u ) and data from the distribution you want to check against.您所需要的只是您的样本 ( u ) 和您要检查的分布中的数据。 The lazy (and not quite correct) approach would be to check against a random sample drawn from a uniform distribution:懒惰(而且不太正确)的方法是检查从均匀分布中抽取的随机样本:

qqplot(runif(nsample), u)

But of course, it is better to plot against the theoretical quantiles:但当然,最好是 plot 针对理论分位数:

# the actual plot
qqplot( qunif(ppoints(length(u))), u )
# add a line
qqline(u, distribution=qunif, col='red', lwd=2)

在此处输入图像描述

Looks pretty good to me.在我看来还不错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM