#probability and cummulative functions
p = function(x) 6 * x * (1 - x)
cdf = function(x, width) cumsum(p(x)*(width/length(x)))
#take the midpoint of 100 even-spaced intervals
x = ((0:99) + 0.5)/100
#plot the pdf
plot.new()
plot(x, p(x), type="l", col="blue", ylab = " ")
#plot the cdf with a reference line to show it goes to 1
lines(x, cdf(x, 1), type = "l", col="red")
lines(c(0,1), c(1,1), lty=2)
#pretty it up a bit
title("p(x) = 6x(1-x)")
text(0,1.3, "p(x)", pos=4, col="blue")
text(0,1.1, "cdf(x)", pos=4, col="red")
Oooh and aaaah (though I have no idea why blue and red saved to very dark blue and maroon - they were quite bright when displayed in RStudio).
No comments:
Post a Comment