TU Wien:Statistik und Wahrscheinlichkeitstheorie UE (Bura)/Übungen 2019W/4.7

Aus VoWi
Zur Navigation springen Zur Suche springen
Histograms of averages of exp(1)
(a) Generate a frequency histogram of 1000 samples from an exp(1) random variable.
(b) Generate a density histogram for the average of 2 independent exp(1) random variables.
(c) Using rexp(), matrix() and colMeans() generate a density histogram for the average of 50 independent exp(1) random variables. Make 10000 sample averages and use a binwidth of 0.1 for this. Look at the spread of the histogram.
(d) Add a graph of the pdf of on your plot in problem (c).

Lösung[Bearbeiten | Quelltext bearbeiten]

Die Angabe wurde von OpenCourseWare kopiert: https://ocw.mit.edu/ans7870/18/18.05/s14/r-code/studio3-sol.r

(a)

lambda = 1
data = rexp(1000,lambda)
binwidth = .5
bins = seq(min(data), max(data)+binwidth, binwidth)
hist(data, breaks=bins, col='yellow', freq=TRUE)

(b)

lambda = 1
data1 = rexp(1000,lambda)
data2 = rexp(1000,lambda)
aveData = (data1+data2)/2
binwidth = .4
bins = seq(min(aveData), max(aveData)+binwidth, binwidth)
hist(aveData, breaks=bins, col='yellow', freq=TRUE)

(c)

lambda = 1
nexponentials = 50
ntrials = 10000
x = rexp(ntrials*nexponentials,lambda)
data = matrix(x, nrow=nexponentials, ncol=ntrials)
aveData = colMeans(data)
binwidth = .05
bins = seq(min(aveData), max(aveData)+binwidth, binwidth)
hist(aveData, breaks=bins, col='yellow', freq=FALSE)

(d)

x = seq(-2,4,.01)
lines(x,dnorm(x,1,1/sqrt(50)), col='red',lwd=3)