From 57175dbf3472fc3a12b23b39010f779c40cb40b2 Mon Sep 17 00:00:00 2001 From: aaaakshat <33050725+aaaakshat@users.noreply.github.com> Date: Thu, 21 Oct 2021 12:06:24 -0400 Subject: [PATCH 1/7] Add pdf/cdf of a urv --- ch04.r | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 ch04.r diff --git a/ch04.r b/ch04.r new file mode 100644 index 0000000..c0ee293 --- /dev/null +++ b/ch04.r @@ -0,0 +1,12 @@ +# Chapter 4.3 (Cumulative Distribution Function) + +## CDF of a Gaussian mixture + +# R code to generate the PDF and CDF +x = seq(-5, 10, (15)/1500) +f = dunif(x, -3, 4) +F = punif(x, -3, 4) +plot(x, f, type="n") +lines(x, f, lwd=5) +plot(x, F, type="n") +lines(x, F, lwd=5) From 73aff20a439289f3b2472a5c80fcb053eb6758b8 Mon Sep 17 00:00:00 2001 From: aaaakshat <33050725+aaaakshat@users.noreply.github.com> Date: Thu, 21 Oct 2021 12:28:09 -0400 Subject: [PATCH 2/7] Add pdf/cdf of ERV --- ch04.r | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/ch04.r b/ch04.r index c0ee293..138d3bd 100644 --- a/ch04.r +++ b/ch04.r @@ -1,12 +1,23 @@ # Chapter 4.3 (Cumulative Distribution Function) -## CDF of a Gaussian mixture +## 4.10 CDF of a Uniform Random Variable # R code to generate the PDF and CDF -x = seq(-5, 10, (15)/1500) +x = seq(-5, 10, (5+10)/1500) f = dunif(x, -3, 4) F = punif(x, -3, 4) plot(x, f, type="n") lines(x, f, lwd=5) plot(x, F, type="n") lines(x, F, lwd=5) + +## 4.11 CDF of an exponential random variable + +# R code to generate the PDF and CDF +x = seq(-5, 10, (5+10)/1500) +f = dexp(x, 1/2) +F = pexp(x, 1/2) +plot(x, f, type="n") +lines(x, f, lwd=5) +plot(x, F, type="n") +lines(x, F, lwd=5) From 8afad0932064f19b2ecbc7f97b507c800c67fe98 Mon Sep 17 00:00:00 2001 From: aaaakshat <33050725+aaaakshat@users.noreply.github.com> Date: Thu, 21 Oct 2021 15:42:06 -0400 Subject: [PATCH 3/7] Add URM stat functions --- ch04.r | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/ch04.r b/ch04.r index 138d3bd..fd93c98 100644 --- a/ch04.r +++ b/ch04.r @@ -21,3 +21,39 @@ plot(x, f, type="n") lines(x, f, lwd=5) plot(x, F, type="n") lines(x, F, lwd=5) + +# Chapter 4.5 + +# Generate a uniform random variable + +# R code to generate 1000 uniform random numbers +a = 0; b = 1; +X = runif(1000, a, b) +hist(X) + +# Mean, variance, median, mode of a uniform random variable + +# R code to computer empirical mean, var, median, mode +library(pracma) +a = 0; b = 1; +X = runif(1000, a, b) +M = mean(X) +V = var(X) +Med = median(X) +Mod = Mode(X) + +# R code to compute mean and variance +unifstat = function(a, b) { + M = (a+b)/2 + V = ((a-b)^2)/12 + return(list(mean = M, var = V)) +} + +a = 0; b = 1; +M = unifstat(a, b)$mean +V = unifstat(a, b)$var + +# R code to compute the probability P(0.2 < X < 0.3) +a = 0; b = 1; +F = punif(0.3, a, b) - punif(0.2, a, b) + From d85ba280ba0515ee030973100686741e79ccb2f5 Mon Sep 17 00:00:00 2001 From: aaaakshat <33050725+aaaakshat@users.noreply.github.com> Date: Thu, 21 Oct 2021 16:43:59 -0400 Subject: [PATCH 4/7] Add PDF and CDF of exponential RV --- ch04.r | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/ch04.r b/ch04.r index fd93c98..98c4c9d 100644 --- a/ch04.r +++ b/ch04.r @@ -57,3 +57,29 @@ V = unifstat(a, b)$var a = 0; b = 1; F = punif(0.3, a, b) - punif(0.2, a, b) + +## PDF and CDF of an exponential random variable + +# R code to plot the exponential PDF +lambda1 = 1/2 +lambda2 = 1/5 +x = seq(0, 1, (0+1)/1000) +f1 = dexp(x, 1/lambda1) +f2 = dexp(x, 1/lambda2) +plot(x, f2, type="n") +lines(x, f1, lwd=4, col="blue") +lines(x, f2, lwd=4, col="red") +legend(0, 1, legend=c(expression(paste(lambda, "=5")), expression(paste(lambda, "=2"))), col=c("red", "blue"), lty=1:1) + +# R code to plot the exponential CDF +lambda1 = 1/2 +lambda2 = 1/5 +x = seq(0, 1, (0+1)/1000) +F1 = pexp(x, 1/lambda1) +F2 = pexp(x, 1/lambda2) +plot(x, F2, type="n") +lines(x, F1, lwd=4, col="blue") +lines(x, F2, lwd=4, col="red") +legend(0, 1, legend=c(expression(paste(lambda, "=5")), expression(paste(lambda, "=2"))), col=c("red", "blue"), lty=1:1) + + From bd54518db54e2c091dbe1026e5cd4db2ccbd02a9 Mon Sep 17 00:00:00 2001 From: aaaakshat <33050725+aaaakshat@users.noreply.github.com> Date: Thu, 21 Oct 2021 17:21:13 -0400 Subject: [PATCH 5/7] Add chapter 4.6 --- ch04.r | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/ch04.r b/ch04.r index 98c4c9d..de58f57 100644 --- a/ch04.r +++ b/ch04.r @@ -82,4 +82,41 @@ lines(x, F1, lwd=4, col="blue") lines(x, F2, lwd=4, col="red") legend(0, 1, legend=c(expression(paste(lambda, "=5")), expression(paste(lambda, "=2"))), col=c("red", "blue"), lty=1:1) +# Chapter 4.6 + +## PDF and CDF of a Gaussian random variable + +# R code to generate a Gaussian PDF +x = seq(-10, 10, (10+10)/1000) +mu = 0; sigma = 1; +f = dnorm(x, mu, sigma) +plot(x, f, type="n") +lines(x, f, lwd=4, col="blue") + +# R code to generate standard Gaussian PDF and CDF +x = seq(-5, 5, (5+5)/1000) +f = dnorm(x) +F = pnorm(x) +plot(x, f) +plot(x, F) + +# R code to verify standardised Gaussian +x = seq(-5, 5, (5+5)/1000) +mu = 3; sigma = 2; +f1 = dnorm((x-mu)/sigma, 0, 1) # Standardised +f2 = dnorm(x, mu, sigma) # Raw + +# R code to compute skewness and kurtosis +library(e1071) +X = rgamma(10000, 3, 5) +s = skewness(X) +k = kurtosis(X) + +# R code to show the histogram of Z = X1 + X2 + X3 +N = 10000 +X1 = runif(N, 1, 6) +X2 = runif(N, 1, 6) +X3 = runif(N, 1, 6) +Z = X1 + X2 + X3 +hist(Z, breaks=seq(2.5,17.5)) From ced2ac59676a642e43dd6647e93f951b73b3cbac Mon Sep 17 00:00:00 2001 From: aaaakshat <33050725+aaaakshat@users.noreply.github.com> Date: Thu, 21 Oct 2021 21:39:59 -0400 Subject: [PATCH 6/7] Fix hist(Z) for gaussian RV --- ch04.r | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ch04.r b/ch04.r index de58f57..6340d7a 100644 --- a/ch04.r +++ b/ch04.r @@ -118,5 +118,5 @@ X1 = runif(N, 1, 6) X2 = runif(N, 1, 6) X3 = runif(N, 1, 6) Z = X1 + X2 + X3 -hist(Z, breaks=seq(2.5,17.5)) +hist(Z, breaks=seq(2.5,18.5)) From 800e4016e40259240575e48b442da01491cced10 Mon Sep 17 00:00:00 2001 From: aaaakshat <33050725+aaaakshat@users.noreply.github.com> Date: Thu, 21 Oct 2021 22:13:10 -0400 Subject: [PATCH 7/7] Add section 4.8 --- ch04.r | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/ch04.r b/ch04.r index 6340d7a..22a4fc6 100644 --- a/ch04.r +++ b/ch04.r @@ -120,3 +120,29 @@ X3 = runif(N, 1, 6) Z = X1 + X2 + X3 hist(Z, breaks=seq(2.5,18.5)) +# Chapter 4.8 + +## Generating Gaussians from uniform + +# R code to generate Gaussian from uniform +library(HDInterval) +mu = 3 +sigma = 2 +U = runif(10000, 0, 1) +gU = sigma * inverseCDF(U, pnorm) + mu; +hist(U) +hist(gU) + +# R code to generate exponential random variables +lambda = 1 +U = runif(10000, 0, 1) +gU = -(1/lambda)*log(1-U) + +# R code to generate the desired random variables +U = runif(10000, 0, 1) +gU = rep(0, 10000) +gU[U >= 0.0 & U <= 0.1] = 1 +gU[U > 0.1 & U <= 0.6] = 2 +gU[U > 0.6 & U <= 0.9] = 3 +gU[U > 0.9 & U <= 1] = 4 +hist(gU)