Skip to content

Commit

Permalink
Add grids to graphs
Browse files Browse the repository at this point in the history
  • Loading branch information
aaaakshat committed Oct 22, 2021
1 parent 9c54206 commit 3b598b8
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion ch04.r
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -92,13 +98,15 @@ 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)
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
Expand All @@ -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

Expand All @@ -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)

Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)

Expand All @@ -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()

#############

0 comments on commit 3b598b8

Please sign in to comment.