Let $r$ be the number of success resulting from $n$ independent trials with unknown success probability $p$, such that $X$ follows $Binomial$ $Distribution$.
Let $X_i$ be a random sample from $Bin(r, \theta)$, population with $r$ known, such that $0 \leq \theta \leq 1.$
`X \sim Bin(r, \theta)`
then the $maximum$ $likelihood$ $estimator$ of $\theta$ is
`\hat{\theta}_{MLE} = \frac{\bar{X}}{r}`
Proof. with the probability mass function of Binomial Distribution
`P(X=x)=\binom{r}{x} (\theta)^{x} (1-\theta)^{r-x} I_{(x \in 0,1,2,....,n)} \tag{1}`
the Likelihood equation is given by
`L(\theta, x) = \prod_{i=1}^{n} P(X_i=x_i) \tag{2}`
`L(\theta, x)=\prod_{i=1}^{n} \binom{r}{x_i} \theta^{\sum x_i} (1-\theta)^{r-\sum x_i} \tag{3}`
now, taking log both sides
partially differentiate with respect to $\theta$ we get
`\frac{\partial}{\partial \theta}\log L = \frac{\sum_{i=1}^{n} x_i}{\theta} - \frac{\sum_{i=1}^{n} (r-x_i)}{\left(1 - \theta\right)} \tag{5}`
`\hat{\theta}_{MLE} = \frac{\sum x_i}{r n} =\frac{\bar{x}}{r} \tag{6}`
# R program to create a binomial distribution bar plot
# User inputs for number of trials and probability of success
n <- as.numeric(readline("Enter the number of trials (n): "))
p <- as.numeric(readline("Enter the probability of success (p): "))
# Ensure valid inputs
if (n <= 0 || p < 0 || p > 1) {
  stop("Invalid inputs! Ensure n > 0 and 0 <= p <= 1.")
}
# Binomial probabilities
x <- seq(0, n, by = 1)
probabilities <- dbinom(x, size = n, prob = p)
# Create the bar plot
barplot(
  probabilities, 
  names.arg = x, 
  col = 'skyblue', 
  xlab = 'Number of Successes', 
  ylab = 'Probability', 
  main = paste("Binomial Distribution (n =", n, ", p =", p, ")"),
  border = 'blue'
)
grid(col = 'gray')