simpson_n <- function(ftn, a, b, n = 100) { # numerical integral of ftn from a t
ID: 2926393 • Letter: S
Question
simpson_n <- function(ftn, a, b, n = 100) {
# numerical integral of ftn from a to b
# using Simpson's rule with n subdivisions
#
# ftn is a function of a single variable
# we assume a < b and n is a positive even integer
n <- max(c(2*(n %/% 2), 4))
h <- (b-a)/n
x.vec1 <- seq(a+h, b-h, by = 2*h)
x.vec2 <- seq(a+2*h, b-2*h, by = 2*h)
f.vec1 <- sapply(x.vec1, ftn)
f.vec2 <- sapply(x.vec2, ftn)
S <- h/3*(ftn(a) + ftn(b) + 4*sum(f.vec1) + 2*sum(f.vec2))
return(S)
}
source("../scripts/simpson_n.r")
phi <- function(x) return(exp(-x^2/2)/sqrt(2*pi))
Phi <- function(z) {
if (z < 0) {
return(0.5 - simpson_n(phi, z, 0))
} else {
return(0.5 + simpson_n(phi, 0, z))
}
}
z <- seq(-5, 5, by = 0.1)
phi.z <- sapply(z, phi)
Phi.z <- sapply(z, Phi)
plot(z, Phi.z, type = "l", ylab = "", main = "phi(z) and Phi(z)")
lines(z, phi.z)
Explanation / Answer
Using a z-score calculator, the z-values for the following cumulative probability values are:
(i)
When p = 0.5, z = 3.57*10-7
(ii)
When p = 0.95, z = 1.64
(iii)
When p = 0.975, z = 1.96
(iv)
When p = 0.99, z = 2.33
Hope this helps !
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.