A for loop is used here to find the fraction of points upto a particular x.
The pnorm function is used to get the actual cumulative normal distribution.
Since the calculated, from the random sample set, is very close to the actual, the resulting plot is mostly magenta, rather than pure red or blue.
# ex3.R
num <- 10000
sig <- rnorm(10000)
x <- seq(from = min(sig), to = max(sig), length.out = 50)
y <- numeric(length(x))
for (i in 1:50) {
y[i] <- sum(sig<=x[i])/num
}
plot(x, y, type = 'l', col = 'red',
main = 'Cumulative Normal Distribution')
abline(x, pnorm(x), col = 'blue')
Output:
# ex3.R num <- 10000 sig <- rnorm(10000) x <- seq(from = min(sig), to = max(sig), length.out = 50) y <- numeric(length(x)) for (i in 1:50) { y[i] <- sum(sig<=x[i])/num } plot(x, y, type = 'l', col = 'red', main = 'Cumulative Normal Distribution') abline(x, pnorm(x), col = 'blue')
No comments:
Post a Comment