################################################
################################################
############ BINOMIAL EXAMPLE       ############
################################################
################################################
######## Set seed for reproducibility
set.seed(123)

# Number of trials
n <- 10

# Probability of success
p <- 0.5

# Simulate binomial distribution
simulated_data <- rbinom(n, size = n, prob = p)
?hist #The generic function hist computes a histogram of the given data values. 
# Plot the distribution
hist(simulated_data, breaks = seq(-0.5, n + 0.5, 1), col = "lightblue", main = "Binomial Distribution Simulation", xlab = "Number of Successes", ylab = "Frequency")
?abline             #This function adds one or more straight lines through the current plot.
# Add vertical lines for mean and standard deviation
abline(v = mean(simulated_data), col = "red", lty = 2, lwd = 2)
abline(v = sd(simulated_data), col = "blue", lty = 2, lwd = 2)
?legend     #This function can be used to add legends to plots. Note that a call to the function locator(1) can be used in place of the x and y arguments.
# Add legend
legend("topright", legend = c("Mean", "Standard Deviation"), col = c("red", "blue"), lty = 2, lwd = 2)

########### Binomial and Poisson distributions ########### 

# Parameters for Binomial and Poisson distributions
n <- 1000  # Large number of trials
pi <- 0.01  # Small probability of success
lambda <- n * pi  # Mean for the Poisson distribution

# Generate random numbers from a Binomial distribution
binomial_numbers <- rbinom(10000, size = n, prob = pi)

# Generate random numbers from a Poisson distribution
poisson_numbers <- rpois(10000, lambda = lambda)
?par #par can be used to set or query graphical parameters. Parameters can be set by specifying them as arguments to par in tag = value form, or by passing them as a list of tagged values.
# Plot histograms of the two distributions
par(mfrow = c(1, 2))
hist(binomial_numbers, breaks = 30, main = "Binomial Distribution", xlab = "X", ylab = "Frequency")
hist(poisson_numbers, breaks = 30, main = "Poisson Distribution", xlab = "X", ylab = "Frequency")

par(mfrow = c(1, 1))