library(tidyverse)   # Only needed for the plot below. The probability functions are Core R.

# Example - Number of Naturals in 10 Craps Rolls: bin(10,.222)
########################################################################################

# A Natural (sum of 7 or 11 on two dice) has probability 8/36 = .222 on each roll.
rolls <- tibble(
  outcomes = 0:10,
  prob = dbinom(0:10, size=10, prob=.222)
)
rolls

ggplot(data = rolls, mapping = aes(x = outcomes, y = prob)) +
  geom_col() +
  scale_x_continuous(breaks = 0:10) +
  labs(y = "Probability", x = "X = Number of Naturals in 10 Craps rolls")


# Binomial formula gives the exact height of each bar
choose(10,4)*(.222)^4*(1-.222)^6   # P(X=4) for bin(10,.222)

# pbinom() gives a left tail probability
pbinom(4, size=10, prob=.222)   # P(X≤4) for a bin(10,.222) distribution

# lower.tail=FALSE gives a right tail probability (strictly greater)
pbinom(4, size=10, prob=.222, lower.tail=FALSE)   # P(X>4)


# Example - P(X=4) using either left or right tail probabilities
########################################################################################

# P(X≤4) - P(X≤3)
pbinom(4, size=10, prob=.222) - pbinom(3, size=10, prob=.222)
# P(X>3) - P(X>4)
pbinom(3, size=10, prob=.222, lower.tail=FALSE) - pbinom(4, size=10, prob=.222, lower.tail=FALSE)

# dbinom() gives the height of a single bar directly
dbinom(4, size=10, prob=.222)   # P(X=4) for bin(10,.222)
