Skip to content

Commit

Permalink
Add section 4.8
Browse files Browse the repository at this point in the history
  • Loading branch information
aaaakshat committed Oct 22, 2021
1 parent ced2ac5 commit 800e401
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions ch04.r
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 800e401

Please sign in to comment.