e-Mathematics > Probability and Statistics
for 3470-001 student.

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

$\displaystyle x_1,x_2,\ldots,x_n,
$

we can calculate the sample mean

$\displaystyle \bar{x} = \dfrac{x_1+x_2+\cdots +x_n}{n},
$

which corresponds to the mean of probability mass function. Given a vector sample of random sample, it can be obtained by mean().
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

$\displaystyle s^2 = \dfrac{(x_1-\bar{x})^2+(x_2-\bar{x})^2+\cdots +(x_n-\bar{x})^2}{n-1}
$

has a slightly different formula, and var() can be used to calculate it. Similarly, sd() calculates the standard deviation.

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