1 Find normal cumulative distribution value and quantile

mu <- 2; sigma <- 5
x <- seq (mu - 4*sigma, mu + 4*sigma, length=100)
x
##   [1] -18.0000000 -17.5959596 -17.1919192
##   [4] -16.7878788 -16.3838384 -15.9797980
##   [7] -15.5757576 -15.1717172 -14.7676768
##  [10] -14.3636364 -13.9595960 -13.5555556
##  [13] -13.1515152 -12.7474747 -12.3434343
##  [16] -11.9393939 -11.5353535 -11.1313131
##  [19] -10.7272727 -10.3232323  -9.9191919
##  [22]  -9.5151515  -9.1111111  -8.7070707
##  [25]  -8.3030303  -7.8989899  -7.4949495
##  [28]  -7.0909091  -6.6868687  -6.2828283
##  [31]  -5.8787879  -5.4747475  -5.0707071
##  [34]  -4.6666667  -4.2626263  -3.8585859
##  [37]  -3.4545455  -3.0505051  -2.6464646
##  [40]  -2.2424242  -1.8383838  -1.4343434
##  [43]  -1.0303030  -0.6262626  -0.2222222
##  [46]   0.1818182   0.5858586   0.9898990
##  [49]   1.3939394   1.7979798   2.2020202
##  [52]   2.6060606   3.0101010   3.4141414
##  [55]   3.8181818   4.2222222   4.6262626
##  [58]   5.0303030   5.4343434   5.8383838
##  [61]   6.2424242   6.6464646   7.0505051
##  [64]   7.4545455   7.8585859   8.2626263
##  [67]   8.6666667   9.0707071   9.4747475
##  [70]   9.8787879  10.2828283  10.6868687
##  [73]  11.0909091  11.4949495  11.8989899
##  [76]  12.3030303  12.7070707  13.1111111
##  [79]  13.5151515  13.9191919  14.3232323
##  [82]  14.7272727  15.1313131  15.5353535
##  [85]  15.9393939  16.3434343  16.7474747
##  [88]  17.1515152  17.5555556  17.9595960
##  [91]  18.3636364  18.7676768  19.1717172
##  [94]  19.5757576  19.9797980  20.3838384
##  [97]  20.7878788  21.1919192  21.5959596
## [100]  22.0000000
y <- dnorm (x, mean = mu, sd = sigma)
y
##   [1] 2.676605e-05 3.685906e-05 5.042761e-05
##   [4] 6.854197e-05 9.255692e-05 1.241725e-04
##   [7] 1.655030e-04 2.191544e-04 2.883095e-04
##  [10] 3.768180e-04 4.892923e-04 6.312033e-04
##  [13] 8.089733e-04 1.030062e-03 1.303036e-03
##  [16] 1.637621e-03 2.044724e-03 2.536414e-03
##  [19] 3.125860e-03 3.827216e-03 4.655439e-03
##  [22] 5.626033e-03 6.754730e-03 8.057083e-03
##  [25] 9.547985e-03 1.124112e-02 1.314837e-02
##  [28] 1.527911e-02 1.763958e-02 2.023217e-02
##  [31] 2.305477e-02 2.610017e-02 2.935553e-02
##  [34] 3.280201e-02 3.641457e-02 4.016188e-02
##  [37] 4.400651e-02 4.790533e-02 5.181015e-02
##  [40] 5.566856e-02 5.942500e-02 6.302204e-02
##  [43] 6.640180e-02 6.950744e-02 7.228477e-02
##  [46] 7.468379e-02 7.666022e-02 7.817679e-02
##  [49] 7.920446e-02 7.972336e-02 7.972336e-02
##  [52] 7.920446e-02 7.817679e-02 7.666022e-02
##  [55] 7.468379e-02 7.228477e-02 6.950744e-02
##  [58] 6.640180e-02 6.302204e-02 5.942500e-02
##  [61] 5.566856e-02 5.181015e-02 4.790533e-02
##  [64] 4.400651e-02 4.016188e-02 3.641457e-02
##  [67] 3.280201e-02 2.935553e-02 2.610017e-02
##  [70] 2.305477e-02 2.023217e-02 1.763958e-02
##  [73] 1.527911e-02 1.314837e-02 1.124112e-02
##  [76] 9.547985e-03 8.057083e-03 6.754730e-03
##  [79] 5.626033e-03 4.655439e-03 3.827216e-03
##  [82] 3.125860e-03 2.536414e-03 2.044724e-03
##  [85] 1.637621e-03 1.303036e-03 1.030062e-03
##  [88] 8.089733e-04 6.312033e-04 4.892923e-04
##  [91] 3.768180e-04 2.883095e-04 2.191544e-04
##  [94] 1.655030e-04 1.241725e-04 9.255692e-05
##  [97] 6.854197e-05 5.042761e-05 3.685906e-05
## [100] 2.676605e-05
plot (x, y, type = "l")

pnorm (-6, mean = mu, sd = sigma)
## [1] 0.05479929
pnorm (-4, mean = mu, sd = sigma)
## [1] 0.1150697
pnorm (2, mean = mu, sd = sigma)
## [1] 0.5
pnorm (7, mean = mu, sd = sigma)
## [1] 0.8413447
qnorm (0.01, mean = mu, sd = sigma)
## [1] -9.631739
qnorm (c(0.05, 0.1, 0.4, 0.5, 0.6, 0.9, 0.95), mean = mu, sd = sigma)
## [1] -6.2242681 -4.4077578  0.7332645
## [4]  2.0000000  3.2667355  8.4077578
## [7] 10.2242681

2 Convert non-standard normal to normal

x <- seq (-11,4, length = 100)
d1 <- dnorm (x)
d2 <- dnorm (x, mean = -6, sd = 2)


plot (x, d1, type = "l", main = "Two Normal Densities",xaxp = c(-11,4,15))
lines (x, d2, col = "red")

for (z in seq (-1, 1.5, length = 20))
{
    arrows (x0 = z, y0 = 0, y1 = dnorm (z), code = 0)
    arrows (x0= -6 + z * 2, y0 = 0, 
            y1 = dnorm (-6 + z * 2, mean = -6, sd = 2), 
            code = 0, col = "red")
}

abline (h = 0)      

look at the equivalence of these numbers

mu <- 10
sigma <- 2

x <- 13

pnorm (x, mean = mu, sd = sigma)
## [1] 0.9331928
z <- (x-mu)/sigma
pnorm (z, mean = 0, sd = 1)
## [1] 0.9331928
y <- 8
pnorm (y, mean = mu, sd = sigma)
## [1] 0.1586553
z <- (y-mu)/sigma
pnorm (z, mean = 0, sd = 1)
## [1] 0.1586553

3 Approximation of binomial with normal

n <- 20; p <- 0.4
x <- 0:n


binom.x <- dbinom (x, size = n, p = p)
breaks <-  c(x - 0.5, n + 0.5)
hist.binom <- list (breaks = breaks, counts = binom.x) 
class (hist.binom) <- "histogram"
plot (hist.binom, main = "Binomial and Normal", xlab = "x")

x.norm <- seq (-0.5, n + 0.5, length = 200)
x.dnorm <- dnorm (x.norm, n*p, sqrt (n * p * (1-p)))
lines (x.norm, x.dnorm, col = "red" ) 

n <- 30; p <- 0.3
x <- 0:n


binom.x <- dbinom (x, size = n, p = p)
breaks <-  c(x - 0.5, n + 0.5)
hist.binom <- list (breaks = breaks, counts = binom.x) 
class (hist.binom) <- "histogram"
plot (hist.binom, main = "Binomial and Normal", xlab = "x")

x.norm <- seq (-0.5, n + 0.5, length = 200)
x.dnorm <- dnorm (x.norm, n*p, sqrt (n * p * (1-p)))
lines (x.norm, x.dnorm, col = "red" ) 

n <- 30; p <- 0.8
x <- 0:n


binom.x <- dbinom (x, size = n, p = p)
breaks <-  c(x - 0.5, n + 0.5)
hist.binom <- list (breaks = breaks, counts = binom.x) 
class (hist.binom) <- "histogram"
plot (hist.binom, main = "Binomial and Normal", xlab = "x")

x.norm <- seq (-0.5, n + 0.5, length = 200)
x.dnorm <- dnorm (x.norm, n*p, sqrt (n * p * (1-p)))
lines (x.norm, x.dnorm, col = "red" ) 

n <- 300; p <- 0.8
x <- 0:n


binom.x <- dbinom (x, size = n, p = p)
breaks <-  c(x - 0.5, n + 0.5)
hist.binom <- list (breaks = breaks, counts = binom.x) 
class (hist.binom) <- "histogram"
plot (hist.binom, main = "Binomial and Normal", xlab = "x")
x.norm <- seq (-0.5, n + 0.5, length = 200)
x.dnorm <- dnorm (x.norm, n*p, sqrt (n * p * (1-p)))
lines (x.norm, x.dnorm, col = "red" ) 

plot (hist.binom, main = "Binomial and Normal", xlab = "x", xlim = c(200,280))
x.norm <- seq (-0.5, n + 0.5, length = 200)
x.dnorm <- dnorm (x.norm, n*p, sqrt (n * p * (1-p)))
lines (x.norm, x.dnorm, col = "red" ) 

4 Demonstration of the normal approximation for binomial

4.1 When n is small

n<- 30
p <- 0.3
mu <- n * p 
sigma <- sqrt(n * p * (1-p))

x <- c(3,3)
sum(dbinom (x[1]:x[2], size = n, p = p))
## [1] 0.007203389
pnorm(x[2]+0.5, mu, sigma) - pnorm (x[1]-0.5, mu,sigma)
## [1] 0.009413254
x <- c(3,10)
sum(dbinom (x[1]:x[2], size = n, p = p))
## [1] 0.7282572
pnorm(x[2]+0.5, mu, sigma) - pnorm (x[1]-0.5, mu,sigma)
## [1] 0.7201478
x <- c(12,15)
sum(dbinom (x[1]:x[2], size = n, p = p))
## [1] 0.1529514
pnorm(x[2]+0.5, mu, sigma) - pnorm (x[1]-0.5, mu,sigma)
## [1] 0.1548158
n<- 30
p <- 0.1
mu <- n * p 
sigma <- sqrt(n * p * (1-p))

x <- c(3,3)
sum(dbinom (x[1]:x[2], size = n, p = p))
## [1] 0.2360879
pnorm(x[2]+0.5, mu, sigma) - pnorm (x[1]-0.5, mu,sigma)
## [1] 0.2390933
x <- c(3,10)
sum(dbinom (x[1]:x[2], size = n, p = p))
## [1] 0.5885597
pnorm(x[2]+0.5, mu, sigma) - pnorm (x[1]-0.5, mu,sigma)
## [1] 0.6195441
x <- c(12,15)
sum(dbinom (x[1]:x[2], size = n, p = p))
## [1] 1.528083e-05
pnorm(x[2]+0.5, mu, sigma) - pnorm (x[1]-0.5, mu,sigma)
## [1] 1.152224e-07

4.2 When n is large

n<- 200
p <- 0.1
mu <- n * p 
sigma <- sqrt(n * p * (1-p))

x <- c(15,15)
sum(dbinom (x[1]:x[2], size = n, p = p))
## [1] 0.05012925
pnorm(x[2]+0.5, mu, sigma) - pnorm (x[1]-0.5, mu,sigma)
## [1] 0.04699686
x <- c(15,25)
sum(dbinom (x[1]:x[2], size = n, p = p))
## [1] 0.8065965
pnorm(x[2]+0.5, mu, sigma) - pnorm (x[1]-0.5, mu,sigma)
## [1] 0.8051494
x <- c(25,26)
sum(dbinom (x[1]:x[2], size = n, p = p))
## [1] 0.07766934
pnorm(x[2]+0.5, mu, sigma) - pnorm (x[1]-0.5, mu,sigma)
## [1] 0.08166895