1.1 Binomicke rodelenie (spojité rozdelenie)

# parametre
binom_ns <- list(10, 20, 50, 100)
binom_ps <- list(0.5, 0.7, 0.3, 0.9)
binom_means <- list()
binom_vars <- list()
  
for (i in 1:length(binom_ns)) {
  binom_means <- append(binom_means, binom_ns[[i]] * binom_ps[[i]])
  binom_vars <- append(binom_vars, binom_ns[[i]] * binom_ps[[i]] * (1 - binom_ps[[i]]))
}
  
cat("Binomial Distribution:\n")
cat("   Means: \t")
for (i in 1:length(binom_means)) {
  cat(binom_means[[i]], "\t | \t")
}
cat("\nVariance: \t")
for (i in 1:length(binom_means)) {
  cat(binom_vars[[i]], "\t | \t")
}
## Binomial Distribution:
##    Means:    
## 5     |  14   |  15   |  90   |  
## 
## Variance:    
## 2.5   |  4.2      |  10.5     |  9    |  
# --- grafy - hustota a distribucna funkcia ---
pmf_plots <- list()
cdf_plots <- list()
  
for (i in 1:length(binom_ns)) {
  binom_n <- binom_ns[[i]]
  binom_p <- binom_ps[[i]]
  
  # binomial PMF and CDF
  x_binom <- 0:binom_n
  pmf_binom <- dbinom(x_binom, size=binom_n, prob=binom_p)
  cdf_binom <- pbinom(x_binom, size=binom_n, prob=binom_p)
  
  binom_df <- data.frame(x_binom, pmf_binom, cdf_binom)
  
  # PMF plot
  plot_binom_pmf <- ggplot(binom_df, aes(x=x_binom)) +
  geom_bar(aes(y=pmf_binom), stat="identity", fill="blue") +
  labs(title=paste("Binom Distrib (n =", binom_n, ", p =", binom_p, ")"), y="Probability", x="x")
  
  # CDF plot
  plot_binom_cdf <- ggplot(binom_df, aes(x=x_binom)) +
  geom_line(aes(y=cdf_binom), color="red") +
  labs(title=paste("Binom Distrib (n =", binom_n, ", p =", binom_p, ")"), y="Cumulative Probability", x="x")
    
  # Saving plots for drawing
  pmf_plots[[i]] <- plot_binom_pmf
  cdf_plots[[i]] <- plot_binom_cdf
}

grid.arrange(grobs=pmf_plots, ncol=2)

grid.arrange(grobs=cdf_plots, ncol=2)

1.2 Normalne rozdelenie (diskrétne rozdelenie)

# parametre
normal_means <- list(0, 1, -1, 2)
normal_sds <- list(1, 0.5, 2, 1.5)
pmf_plots <- list()
cdf_plots <- list()

for (i in 1:length(normal_means)) {
  normal_mean <- normal_means[[i]]
  normal_sd <- normal_sds[[i]]
  
  x_normal <- seq(-4, 4, length=1000)
  pmf_normal <- dnorm(x_normal, mean=normal_mean, sd=normal_sd)
  cdf_normal <- pnorm(x_normal, mean=normal_mean, sd=normal_sd)
  
  normal_df <- data.frame(x_normal, pmf_normal, cdf_normal)
  
  # PMF plot
  plot_normal_pmf <- ggplot(normal_df, aes(x=x_normal)) +
  geom_line(aes(y=pmf_normal), color="blue") +
  labs(title=paste("Norm Distrib (mean =", normal_mean, ", sd =", normal_sd, ")"), y="Density", x="x")
  
  # CDF plot
  plot_normal_cdf <- ggplot(normal_df, aes(x=x_normal)) +
  geom_line(aes(y=cdf_normal), color="red") +
  labs(title=paste("Norm Distrib (mean =", normal_mean, ", sd =", normal_sd, ")"), y="Cumulative Probability", x="x")
  
  # Saving plots for drawing
  pmf_plots[[i]] <- plot_normal_pmf
  cdf_plots[[i]] <- plot_normal_cdf
}

grid.arrange(grobs=pmf_plots, ncol=2)

grid.arrange(grobs=cdf_plots, ncol=2)

2.1 Poissove rozdelenie (diskrétne rozdelenie)

pois_lambdas <- list(1, 4, 10, 15)

pmf_plots <- list()
cdf_plots <- list()

for (i in 1:length(pois_lambdas)) {
  pois_lambda <- pois_lambdas[[i]]
  
  # Poisson distribution, PMF and CDF 
  x_pois <- 0:15
  pmf_pois <- dpois(x_pois, lambda=pois_lambda)
  cdf_pois <- ppois(x_pois, lambda=pois_lambda)
  
  pois_df <- data.frame(x_pois, pmf_pois, cdf_pois)
  
  # PMF plot
  plot_pois_pmf <- ggplot(pois_df, aes(x=x_pois)) +
  geom_bar(aes(y=pmf_pois), stat="identity", fill="blue") +
  labs(title=paste("Poisson Distrib (lambda =", pois_lambda, ")"), y="Probability", x="x")
  
  # CDF plot
  plot_pois_cdf <- ggplot(pois_df, aes(x=x_pois)) +
  geom_line(aes(y=cdf_pois), color="red") +
  labs(title=paste("Poisson Distrib (lambda =", pois_lambda, ")"), y="Cumulative Probability", x="x")
  
  # Saving plots for drawing
  pmf_plots[[i]] <- plot_pois_pmf
  cdf_plots[[i]] <- plot_pois_cdf
}
  
grid.arrange(grobs=pmf_plots, ncol=2)

grid.arrange(grobs=cdf_plots, ncol=2)

2.2 Gamma rozdelenie (spojité rozdelenie)

gamma_alphas <- list(1, 2, 5, 7)
gamma_betas <- list(1, 0.5, 2, 3)

pmf_plots <- list()
cdf_plots <- list()

for (i in 1:length(gamma_alphas)) {
  alpha <- gamma_alphas[[i]]
  beta <- gamma_betas[[i]]
  
  # Gamma distribution, PMF and CDF 
  x_gamma <- seq(0, 20, length=1000)
  pmf_gamma <- dgamma(x_gamma, shape=alpha, rate=beta)
  cdf_gamma <- pgamma(x_gamma, shape=alpha, rate=beta)
  
  gamma_df <- data.frame(x_gamma, pmf_gamma, cdf_gamma)
  
  # PMF plot
  plot_gamma_pmf <- ggplot(gamma_df, aes(x=x_gamma)) +
  geom_line(aes(y=pmf_gamma), color="blue") +
  labs(title=paste("Gamma Distrib (a =", alpha, ", b =", beta, ")"), y="Density", x="x")
  
  # CDF plot
  plot_gamma_cdf <- ggplot(gamma_df, aes(x=x_gamma)) +
  geom_line(aes(y=cdf_gamma), color="red") +
  labs(title=paste("Gamma Distrib (a =", alpha, ", b =", beta, ")"), y="Cumulative Probability", x="x")
  
  # Store the plots in lists
  pmf_plots[[i]] <- plot_gamma_pmf
  cdf_plots[[i]] <- plot_gamma_cdf
}

grid.arrange(grobs=pmf_plots, ncol=2)

grid.arrange(grobs=cdf_plots, ncol=2)