-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from bishta/chap-04
Add Chapter 4
- Loading branch information
Showing
1 changed file
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
# Chapter 4.3 (Cumulative Distribution Function) | ||
|
||
## 4.10 CDF of a Uniform Random Variable | ||
|
||
# R code to generate the PDF and CDF | ||
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) | ||
|
||
# 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) | ||
|
||
|
||
## 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) | ||
|
||
# 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,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) |