Expectation and Sample Mean
Vectors are used in arithmetic expressions, and operations (e.g., "+" or "^2") are performed element by element. And sum() is used to obtain the sum of values in a vector.
prob = c(0.2, 0.3, 0.4, 0.1) x = c(1,2,3,4) barplot(prob, names.arg=x, col=2) x * prob mu = sum(x * prob) cat("\n E[X] =", mu) sigma.square = sum((x - mu)^2 * prob) cat("\n Var(X) =", sigma.square) cat("\n SD =", sqrt(sigma.square))
Sample mean and variance. Given a random sample


size = 200 sample = sample(x, size=size, replace=T, prob=prob) cat("\n sample =", sample) hist(sample, freq=F, breaks=seq(0.5,length(prob)+0.5,by=1.0), col='green') prob.mass(x,prob,lty=2) cat("\n sample mean =", mean(sample)) cat("\n sample var =", var(sample)) cat("\n sample SD =", sd(sample))The sample variance

Sample R code. You can download expdemo.R, and run it.
If the distribution is binomial, a vector of probabilities is automatically generated as in the following example.
n = 20 p = 0.2 x = 0:n prob = dbinom(x,n,p) barplot(prob,names.arg=x,col=2) mu = sum(x * prob) cat("\n E[X] =", mu) sigma.square = sum((x - mu)^2 * prob) cat("\n Var(X) =", sigma.square)Also, it becomes much simpler to generate a random sample for the binomial distribution.
size = 1000 sample = rbinom(size,n,p) hist(sample, freq=F, breaks=seq(-0.5,n+0.5,by=1.0), ylim=c(0,max(prob)+0.1), col='green') prob.mass(x,prob,lty=2) cat("\n sample mean =", mean(sample)) cat("\n sample var =", var(sample))
Sample R code. You can download bexpdemo.R, and run it.
© TTU Mathematics