Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How many replicate samples do we need to estimate the mean of a distribution?
mu = 5
stdev = 2
N = 100
data = rnorm(N,mu,stdev)
pdf = function(x) dnorm(x,mu,stdev)
bestmu = function(N,x) sum(x[1:N])/N
op=par(mfrow=c(1,2))
plot(density(data),main = "Comparing density plot from\nsamples with true density")
curve(pdf,from = min(data),to=max(data),add = TRUE,col="red")
estimates = sapply(1:N,bestmu,x = data)
plot(estimates,xlab="Number of measurements",ylab="Best estimate for mean",type="n")
abline(h = mu,col="red",lwd=3)
points(estimates,type="l")
par=op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.