Binomial distribution
The binomial distribution is our model for the total number of successes in \(n\) independent binary trials, each with identical probability of success \(p\in[0,\,1]\).
Basic properties
| Notation | \(X\sim\text{Binom}(n,\,p)\) |
| Range | \(\{0,\,1,\,2,\,...,\,n-1,\,n\}\) |
| Parameter space | \(p\in[0,\,1]\) |
| PMF | \(P(X=k)=\binom{n}{k}p^k(1-p)^{n-k}\) |
| Expectation | \(np\) |
| Variance | \(np(1-p)\) |
R commands
Here is the documentation for the suite of commands that let you work with the binomial distribution in R:
Play around!
Notice that, for all values of \(p\in(0,\,1)\), the shape of the PMF becomes more like the bell curve as \(n\) grows:
#| '!! shinylive warning !!': |
#| shinylive does not work in self-contained HTML documents.
#| Please set `embed-resources: false` in your metadata.
#| standalone: true
#| viewerHeight: 500
library(shiny)
discrete_pmf <- function(x, p, xlim = c(min(x) - 1, max(x) + 1), label = "", add_mean = FALSE){
plot(x, p,
pch = 19,
cex = 1,
xlab = "",
ylab = "",
main = "",
ylim = c(0, 1.05 * max(p)),
yaxs = "i",
yaxt = "n",
xlim = xlim,
xaxt = "n",
bty = "n"
)
segments(x,
rep(0, length(x) + 1),
x1 = x,
y1 = p,
lwd = 3
)
axis(1, at = floor(xlim[1]):ceiling(xlim[2]), cex.axis = 1)
axis(2, at = seq(0, 1, length.out = 11), las = 1, cex.axis = 1.5)
legend("topright", label, bty = "n", cex = 3)
if(add_mean == TRUE){
mtext("E(X)", side = 1, at = sum(x * p), col = "red", line = 2)
}
}
discrete_cdf <- function(x, p, xlim = c(min(x) - 1, max(x) + 1), label = ""){
closeddot = cumsum(p)
opencircle = c(0, closeddot[1:length(x)-1])
plot(x, closeddot, pch = 19, cex = 1,
ylim = c(0, 1),
ylab = "", main = "", xlab = "",
yaxt = "n",
xlim = xlim,
xaxt = "n",
#yaxs = "i",
bty = "n")
points(x, opencircle, cex = 1)
segments(c(xlim[1], x), c(0, closeddot), c(x, xlim[2]), c(0, closeddot), lwd = 1)
axis(1, at = floor(xlim[1]):ceiling(xlim[2]), cex.axis = 1)
axis(2, at = seq(0, 1, length.out = 11), las = 1, cex.axis = 1.5)
legend("bottomright", label, bty = "n", cex = 3)
}
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Binomial distribution CDF and PMF"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("p",
"Probability of success (p):",
min = 0,
max = 1,
value = 0.5,
step = 0.01),
sliderInput("n",
"Number of trials (n):",
min = 1,
max = 100,
value = 5,
step = 1)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$distPlot <- renderPlot({
p <- input$p
n <- input$n
par(mfrow = c(2, 1), mar = c(4, 4, 2, 2))
discrete_cdf(0:n, dbinom(0:n, n, p))
discrete_pmf(0:n, dbinom(0:n, n, p), add_mean = TRUE)
})
}
# Run the application
shinyApp(ui = ui, server = server)
