From c296bcaf7262343fa9f1bfa209f5262a79113ae0 Mon Sep 17 00:00:00 2001 From: aaaakshat <33050725+aaaakshat@users.noreply.github.com> Date: Fri, 22 Oct 2021 17:03:36 -0400 Subject: [PATCH 1/3] Add Gaussian CDF/PDF --- ch04.r | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/ch04.r b/ch04.r index 22a4fc6..0c922d8 100644 --- a/ch04.r +++ b/ch04.r @@ -1,6 +1,23 @@ +############# # Chapter 4.3 (Cumulative Distribution Function) -## 4.10 CDF of a Uniform Random Variable +## CDF of a Gaussian Mixture +# R code to generate the PDF and CDF +library(EnvStats) +x = seq(-5, 10, (5+10)/1000) +x = sort(x) +f = dnormMix(x, 0, 1, 5, 1, p.mix=c(0.7)) +plot(x,f, xlim=c(-5,10), ylim=c(0,0.4), type="n") +grid() +lines(x, f, lwd=4, col="blue") +polygon(c(min(x), x[x<=0.8]), c(f[x<=0.8], 0), col="lightblue") + +F = pnormMix(x, 0, 1, 5, 1, p.mix=c(0.7)) +plot(x, F, xlim=c(-5,10), ylim=c(0,0.4), type="n") + +# R code to generate the PDF and CDF + +## CDF of a Uniform randow variable # R code to generate the PDF and CDF x = seq(-5, 10, (5+10)/1500) @@ -11,7 +28,7 @@ lines(x, f, lwd=5) plot(x, F, type="n") lines(x, F, lwd=5) -## 4.11 CDF of an exponential random variable +## CDF of an exponential random variable # R code to generate the PDF and CDF x = seq(-5, 10, (5+10)/1500) @@ -22,6 +39,7 @@ lines(x, f, lwd=5) plot(x, F, type="n") lines(x, F, lwd=5) +############# # Chapter 4.5 # Generate a uniform random variable From 9c54206cbff9a3270cd9683ad20c044bc6964100 Mon Sep 17 00:00:00 2001 From: aaaakshat <33050725+aaaakshat@users.noreply.github.com> Date: Fri, 22 Oct 2021 17:43:09 -0400 Subject: [PATCH 2/3] Reorder code blocks as per the website --- ch04.r | 57 +++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 14 deletions(-) diff --git a/ch04.r b/ch04.r index 0c922d8..b76ec48 100644 --- a/ch04.r +++ b/ch04.r @@ -2,8 +2,10 @@ # Chapter 4.3 (Cumulative Distribution Function) ## CDF of a Gaussian Mixture + # R code to generate the PDF and CDF library(EnvStats) + x = seq(-5, 10, (5+10)/1000) x = sort(x) f = dnormMix(x, 0, 1, 5, 1, p.mix=c(0.7)) @@ -15,9 +17,7 @@ polygon(c(min(x), x[x<=0.8]), c(f[x<=0.8], 0), col="lightblue") F = pnormMix(x, 0, 1, 5, 1, p.mix=c(0.7)) plot(x, F, xlim=c(-5,10), ylim=c(0,0.4), type="n") -# R code to generate the PDF and CDF - -## CDF of a Uniform randow variable +## CDF of a Uniform random variable # R code to generate the PDF and CDF x = seq(-5, 10, (5+10)/1500) @@ -42,14 +42,14 @@ lines(x, F, lwd=5) ############# # Chapter 4.5 -# Generate a uniform random variable +## 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 +## Mean, variance, median, mode of a uniform random variable # R code to computer empirical mean, var, median, mode library(pracma) @@ -60,6 +60,8 @@ V = var(X) Med = median(X) Mod = Mode(X) +## Mean and Variance computation (not on "Code and Data" section of website, but written in textbook p.34) + # R code to compute mean and variance unifstat = function(a, b) { M = (a+b)/2 @@ -71,14 +73,16 @@ a = 0; b = 1; M = unifstat(a, b)$mean V = unifstat(a, b)$var +## Probability of a uniform random variable + # 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) -## PDF and CDF of an exponential random variable +## PDF of an exponential random variable -# R code to plot the exponential PDF +# R code to generate the PDF and CDF of an exponential random variable lambda1 = 1/2 lambda2 = 1/5 x = seq(0, 1, (0+1)/1000) @@ -89,10 +93,6 @@ 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") @@ -100,9 +100,10 @@ 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 +## Generate Gaussian PDF (not on "Code and Data" section on website, but written in textbook p.42) # R code to generate a Gaussian PDF x = seq(-10, 10, (10+10)/1000) @@ -111,12 +112,18 @@ f = dnorm(x, mu, sigma) plot(x, f, type="n") lines(x, f, lwd=4, col="blue") +## PDF and CDF of a Gaussian random variable + # 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) +plot(x, f, type = "n") +lines(x, f, lwd=6) +plot(x, F, type = "n") +lines(x, F, lwd=6) + +## Verify standardised gaussian (not on "Code and Data" section on website, but written in textbook p.45) # R code to verify standardised Gaussian x = seq(-5, 5, (5+5)/1000) @@ -124,12 +131,27 @@ mu = 3; sigma = 2; f1 = dnorm((x-mu)/sigma, 0, 1) # Standardised f2 = dnorm(x, mu, sigma) # Raw +# Skewness and kurtosis of a random variable + +# R code to plot a Gamma distribution +x = seq(0, 30, (0+30)/1000) +theta = 1 +plot(x, dgamma(x, 2, theta), type = "n") +lines(x, dgamma(x, 2, theta), lwd = 4) +lines(x, dgamma(x, 5, theta), lwd = 4, col = "#333333") +lines(x, dgamma(x, 10, theta), lwd = 4, col = "#666666") +lines(x, dgamma(x, 15, theta), lwd = 4, col = "#999999") +lines(x, dgamma(x, 20, theta), lwd = 4, col = "#CCCCCC") +legend(23, 0.36, legend=c("k = 2", "k = 5", "k = 10", "k = 15", "k = 20"), col=c("1", "#333333", "#666666", "#999999", "#CCCCCC"), lwd = 4,lty=1:1) + # R code to compute skewness and kurtosis library(e1071) X = rgamma(10000, 3, 5) s = skewness(X) k = kurtosis(X) +# Histogram of Z = X1 + X2 + X3 (not on "Code and "Data" section, but written in textbook p.50) + # R code to show the histogram of Z = X1 + X2 + X3 N = 10000 X1 = runif(N, 1, 6) @@ -138,6 +160,7 @@ X3 = runif(N, 1, 6) Z = X1 + X2 + X3 hist(Z, breaks=seq(2.5,18.5)) +############# # Chapter 4.8 ## Generating Gaussians from uniform @@ -151,11 +174,15 @@ gU = sigma * inverseCDF(U, pnorm) + mu; hist(U) hist(gU) +## Generating exponential random variables from uniform random nums (not in "code" section, but in textbook p.63) + # R code to generate exponential random variables lambda = 1 U = runif(10000, 0, 1) gU = -(1/lambda)*log(1-U) +## Plotting PDFs based on transformed vars (not in "code" section, but in textbook p.65) + # R code to generate the desired random variables U = runif(10000, 0, 1) gU = rep(0, 10000) @@ -164,3 +191,5 @@ 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) + +############# From 3b598b83eb8b8c4b6193f5bdfba6a4e0a00b63ce Mon Sep 17 00:00:00 2001 From: aaaakshat <33050725+aaaakshat@users.noreply.github.com> Date: Fri, 22 Oct 2021 17:45:35 -0400 Subject: [PATCH 3/3] Add grids to graphs --- ch04.r | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/ch04.r b/ch04.r index b76ec48..37857b2 100644 --- a/ch04.r +++ b/ch04.r @@ -10,12 +10,13 @@ x = seq(-5, 10, (5+10)/1000) x = sort(x) f = dnormMix(x, 0, 1, 5, 1, p.mix=c(0.7)) plot(x,f, xlim=c(-5,10), ylim=c(0,0.4), type="n") -grid() lines(x, f, lwd=4, col="blue") polygon(c(min(x), x[x<=0.8]), c(f[x<=0.8], 0), col="lightblue") +grid() F = pnormMix(x, 0, 1, 5, 1, p.mix=c(0.7)) plot(x, F, xlim=c(-5,10), ylim=c(0,0.4), type="n") +grid() ## CDF of a Uniform random variable @@ -25,8 +26,10 @@ f = dunif(x, -3, 4) F = punif(x, -3, 4) plot(x, f, type="n") lines(x, f, lwd=5) +grid() plot(x, F, type="n") lines(x, F, lwd=5) +grid() ## CDF of an exponential random variable @@ -36,8 +39,10 @@ f = dexp(x, 1/2) F = pexp(x, 1/2) plot(x, f, type="n") lines(x, f, lwd=5) +grid() plot(x, F, type="n") lines(x, F, lwd=5) +grid() ############# # Chapter 4.5 @@ -48,6 +53,7 @@ lines(x, F, lwd=5) a = 0; b = 1; X = runif(1000, a, b) hist(X) +grid() ## Mean, variance, median, mode of a uniform random variable @@ -92,6 +98,7 @@ 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) +grid() F1 = pexp(x, 1/lambda1) F2 = pexp(x, 1/lambda2) @@ -99,6 +106,7 @@ 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) +grid() ############# # Chapter 4.6 @@ -111,6 +119,7 @@ mu = 0; sigma = 1; f = dnorm(x, mu, sigma) plot(x, f, type="n") lines(x, f, lwd=4, col="blue") +grid() ## PDF and CDF of a Gaussian random variable @@ -120,8 +129,10 @@ f = dnorm(x) F = pnorm(x) plot(x, f, type = "n") lines(x, f, lwd=6) +grid() plot(x, F, type = "n") lines(x, F, lwd=6) +grid() ## Verify standardised gaussian (not on "Code and Data" section on website, but written in textbook p.45) @@ -143,6 +154,7 @@ lines(x, dgamma(x, 10, theta), lwd = 4, col = "#666666") lines(x, dgamma(x, 15, theta), lwd = 4, col = "#999999") lines(x, dgamma(x, 20, theta), lwd = 4, col = "#CCCCCC") legend(23, 0.36, legend=c("k = 2", "k = 5", "k = 10", "k = 15", "k = 20"), col=c("1", "#333333", "#666666", "#999999", "#CCCCCC"), lwd = 4,lty=1:1) +grid() # R code to compute skewness and kurtosis library(e1071) @@ -159,6 +171,7 @@ X2 = runif(N, 1, 6) X3 = runif(N, 1, 6) Z = X1 + X2 + X3 hist(Z, breaks=seq(2.5,18.5)) +grid() ############# # Chapter 4.8 @@ -172,7 +185,9 @@ sigma = 2 U = runif(10000, 0, 1) gU = sigma * inverseCDF(U, pnorm) + mu; hist(U) +grid() hist(gU) +grid() ## Generating exponential random variables from uniform random nums (not in "code" section, but in textbook p.63) @@ -191,5 +206,6 @@ 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) +grid() #############