Skip to content

Instantly share code, notes, and snippets.

@CnrLwlss
Last active November 13, 2019 09:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CnrLwlss/a79a3d039230390fed4622f8e67172cd to your computer and use it in GitHub Desktop.
Save CnrLwlss/a79a3d039230390fed4622f8e67172cd to your computer and use it in GitHub Desktop.
How many replicate samples do we need to estimate the mean of a distribution? 2-panel plot, random output.
mu = 5
stdev = 2
N = 10000
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,from=min(data), to=max(data)),main = paste("Comparing density plot from",N,"\nsamples with true density"),lwd=2)
curve(pdf,from = min(data),to=max(data),add = TRUE,col="red",lwd=3)
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",lwd=2)
par=op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment