Calculating Bias in R Write a simulation experiment to estimate the bias of the estimator λˆ= 1/ X¯ by sampling using x=rexp(n,rate=5) and recording the values of 1/mean(x). You should find that the bias is λ/n−1. Here we’ve used λ = 5 but the result will hold for any λ. Here is my solution ( I dont get λ/n−1). Am I doing something wrong here?
set.seed(1)
lambda <- 5
x <- rexp(n= 1e5, rate = lambda )
samp.mean <- mean(x)
lam.est <- 1/samp.mean
lam.est ##4.986549

bias <- abs(lambda - lam.est)
bias ##0.01345146


Your issue stems from the fact that you only ran one simulation. Recall that the formal definition of the bias of an estimator is :
$$ Bias(\hat{\theta}) = E(\hat{\theta})-\theta $$
In a simulation you approximate the expected value of your estimator by its sample mean.
Here is some R code to get what you want :
> set.seed(1)
> lambda <- 5
> n <- 50
> X <- matrix(rexp(n*1e5, rate = lambda),n)
> samp.mean <- apply(X,2,mean)
> lam.est <- 1/samp.mean
> 
> bias <- abs(lambda - mean(lam.est))
> bias
[1] 0.1001163
> # Approximately equal to lambda/(n-1) and will be if n -> infty  

