R中的伯努利分布
伯努利分布是二项分布的一种特殊情况,其中只执行一次试验。它是伯努利试验(只有两种结果,即成功或失败的试验)的离散概率分布。例如,它可以表示为掷硬币,其中得到正面的概率为 0.5,得到反面的概率为 0.5。它是一个随机变量的概率分布,取值为 1 的概率为p ,取值为 0 的概率为q=1-p 。伯努利分布是 n=1 的二项式分布的特例。
该分布的概率质量函数f 在可能的结果 k 上由下式给出:
上述关系还可以表示为:
在 R 编程语言中,伯努利分布有 4 个内置函数,下面将讨论所有这些函数。
德伯恩()
R 编程中的dbern( )函数测量伯努利分布的密度函数。
Syntax: dbern(x, prob, log = FALSE)
Parameter:
- x: vector of quantiles
- prob: probability of success on each trial
- log: logical; if TRUE, probabilities p are given as log(p)
在统计学中,它由以下公式给出:
例子:
R
# import Rlab library
library(Rlab)
# x values for the
# dbern( ) function
x <- seq(0, 10, by = 1)
# using dbern( ) function
# to x to obtain corresponding
# Bernoulli PDF
y <- dbern(x, prob = 0.7)
# plot dbern values
plot(y, type = "o")
R
# import Rlab library
library(Rlab)
# x values for the
# pbern( ) function
x <- seq(0, 10, by = 1)
# using pbern( ) function
# to x to obtain corresponding
# Bernoulli CDF
y <- pbern(x, prob = 0.7)
# plot pbern values
plot(y, type = "o")
R
# import Rlab library
library(Rlab)
# x values for the
# qbern( ) function
x <- seq(0, 1, by = 0.2)
# using qbern( ) function
# to x to obtain corresponding
# Bernoulli QF
y <- qbern(x, prob = 0.5,lower.tail = TRUE, log.p = FALSE)
# plot qbern values
plot(y, type = "o")
R
# import Rlab library
library(Rlab)
set.seed(98999)
# sample size
N <- 1000
# generate random variables using
# rbern( ) function
random_values <- rbern(N, prob = 0.5)
# print the values
print(random_values)
# plot of randomly
# drawn density
hist(random_values,breaks = 10,main = "")
输出:
pbern()
R 编程中的pbern()函数给出了伯努利分布的分布函数。分布函数或累积分布函数(CDF) 或累积频率函数描述变量 X 取小于或等于数字 x 的值的概率。
Syntax: pbern(q, prob, lower.tail = TRUE, log.p = FALSE)
Parameter:
- q: vector of quantiles
- prob: probability of success on each trial
- lowe.tail: logical
- log.p: logical; if TRUE, probabilities p are given as log(p).
例子:
电阻
# import Rlab library
library(Rlab)
# x values for the
# pbern( ) function
x <- seq(0, 10, by = 1)
# using pbern( ) function
# to x to obtain corresponding
# Bernoulli CDF
y <- pbern(x, prob = 0.7)
# plot pbern values
plot(y, type = "o")
输出:
上图表示 R 中伯努利分布的累积分布函数。
qbern()
qbern( )给出伯努利分布的分位数函数。统计术语中的分位数函数指定随机变量的值,使得变量小于或等于该值的概率等于给定的概率。
Syntax: qbern(p, prob, lower.tail = TRUE, log.p = FALSE)
Parameter:
- p: vector of probabilities.
- prob: probability of success on each trial.
- lower.tail: logical
- log.p: logical; if TRUE, probabilities p are given as log(p).
例子:
电阻
# import Rlab library
library(Rlab)
# x values for the
# qbern( ) function
x <- seq(0, 1, by = 0.2)
# using qbern( ) function
# to x to obtain corresponding
# Bernoulli QF
y <- qbern(x, prob = 0.5,lower.tail = TRUE, log.p = FALSE)
# plot qbern values
plot(y, type = "o")
输出:
伯恩()
R 编程中的rbern()函数用于生成伯努利分布的随机数向量。
Syntax: rbern(n, prob)
Parameter:
- n: number of observations.
- prob: number of observations.
例子:
电阻
# import Rlab library
library(Rlab)
set.seed(98999)
# sample size
N <- 1000
# generate random variables using
# rbern( ) function
random_values <- rbern(N, prob = 0.5)
# print the values
print(random_values)
# plot of randomly
# drawn density
hist(random_values,breaks = 10,main = "")
输出:
[1] 0 0 1 1 0 0 0 0 0 1 0 0 0 1 0 1 1 0 1 0 1 1 1 0 1 0 1 1 0 1 0 1 1 0 1 1 1 1 0 1 0 0 0 0 1 1 1 0 1 1 1 1 1 1 0 0 0 0 1 0 0 0 0 0 0 0 1
[68] 1 1 1 0 0 1 0 1 0 0 0 0 0 1 0 0 1 0 0 0 1 1 0 1 1 0 0 0 1 1 0 1 1 1 1 0 1 0 0 0 0 1 0 0 1 1 0 0 0 0 0 1 0 1 0 0 1 0 1 0 1 0 1 1 1 0 1
[135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