连续均匀分布continuous-uniform-distribution
引言
当从一个连续的区间内,比如[a, b],随机选择随机数的时候,给出的随机数的数值就服从连续均匀分布。其概率函数,也可以比较容易理解,就是分段函数,区间外的部分概率是0,区间内的部分概率是均匀的值,即区间长度的倒数。
连续均匀分布是指从a到b的连续区间内随机选择随机数的概率分布,其密度函数定义如下:
比如,当 a = 1, b = 3时,连续均匀分布的概率函数f(x)的图像是:
R实践
- R中提供了
dunif
函数,dunif
gives the density,也即dunif函数计算,取出某个值的概率,如下:
dunif(x, min = 0, max = 1, log = FALSE)
# 从下可以看出,概率值在指定的区间内,是均匀的。在指定区间之外,概率值就是0.
> dunif(x = 0.5, min = 0, max = 1)
[1] 1
> dunif(x = 0.2, min = 0, max = 1)
[1] 1
> dunif(x = 0, min = 0, max = 3)
[1] 0.333
> dunif(x = -1, min = 0, max = 3)
[1] 0
> dunif(x = 4, min = 0, max = 3)
[1] 0
# 从下可以看出,均匀概率值的大小即为 1/(b-a)
> dunif(x = 0.2, min = 0, max = 2)
[1] 0.5
> dunif(x = 0.2, min = 0, max = 3)
[1] 0.333
所以可以利用dunif
函数绘制上图的概率函数曲线,如下:
> a <- 1
> b <- 3
> x <- seq(0, 5, 0.01)
> y <- dunif(x = x, min = a, max = b)
> plot(x, y, main = "Density of uniform distribution",xlab = "x",ylab = "Density",col = "red",pch = 19, type = 'l')
- R 中提供了
punif
函数,punif gives the distribution function ,如下:
punif(q, min = 0, max = 1, lower.tail = TRUE, log.p = FALSE)
> punif(q = 0.5, min = 0, max = 1, lower.tail = TRUE, log.p = FALSE)
[1] 0.5
> punif(q = 0.5, min = 0, max = 2, lower.tail = TRUE, log.p = FALSE)
[1] 0.25
> punif(q = 0.5, min = 0, max = 3, lower.tail = TRUE, log.p = FALSE)
[1] 0.167
> punif(q = 0.2, min = 0, max = 1, lower.tail = TRUE, log.p = FALSE)
[1] 0.2
> punif(q = 0.1, min = 0, max = 1, lower.tail = TRUE, log.p = FALSE)
[1] 0.1
- R 中提供了
runif
函数,是生成随机数的函数。如下:
runif(n, min = 0, max = 1)
> runif(n = 3, min = 0, max = 1)
[1] 0.240 0.812 0.876
> runif(n = 3, min = 0, max = 10)
[1] 9.30 6.60 6.89
参考
- [Continuous Uniform Distribution(http://www.r-tutor.com/elementary-statistics/probability-distributions/continuous-uniform-distribution)