w1 <- seq(from=1,to=2,by=.001) F1 <- 0.5*(w1^2 - 2*w1 + 1) w2 <- seq(from=2,to=3,by=.001) F2 <- 0.5*(6*w2 - w2^2 - 7) plot(c(w1,w2),c(F1,F2),type="l",xlab="w",ylab="F(w)") ################# graph F for a discrete distribution ######## x <- 0:20 F <- pbinom(x,size=20,prob=.4) pts <- length(x) plot(c(x[1],max(x)),c(0,1),type="n",xlab="x",ylab="F(x)") lines(c(x[1]-1,x[1]),c(0,0),col=1) for (i in 1:(pts-1)) {lines(c(x[i],x[i+1]),c(F[i],F[i]),col=1)} lines(c(x[pts],x[pts]+1),c(1,1),col=1) points(x,F,pch="[",col=1,cex=.5) points(x[2:pts],F[1:(pts-1)],pch=")",col=1,cex=.5) ##################### Numerical integration n <- 10^6 w <- 20/n x <- seq(from=w/2,by=w,to=n*w) lambda <- .5 fx <- function(x,lambda) lambda*exp(-lambda*x) plot(x,fx(x,lambda),type="l") integral.approx <- (max(x)-min(x))*mean(fx(x,lambda)) EX.approx <- (max(x)-min(x))*mean(x*fx(x,lambda)) EX.approx EX2.approx <- (max(x)-min(x))*mean((x^2)*fx(x,lambda)) EX2.approx var.approx.1 <- EX2.approx - EX.approx^2 var.approx.1 var.approx.2 <- (max(x)-min(x))*mean(((x-EX.approx)^2)*fx(x,lambda)) var.approx.2 xfx.lambda <- function(x) x*lambda*exp(-lambda*x) integrate(xfx.lambda, lower=.00001, upper=20,subdivisions=1000000) f2.lambda <- function(x) ((x-1.999)^2)*lambda*exp(-lambda*x) integrate(f2.lambda, lower=.0001, upper=20,subdivisions=10000)