Take a look at the MATLAB code below:
clear; close all; rng(1)
ntrials = 1000;
nsamples = 10000;
%% beta distribution: true mean 0.9
xbetamu = nan(ntrials, 1);
xbetapmu = nan(ntrials, 1);
for i = 1:ntrials
xbeta = betarnd(90, 10, nsamples, 1);
xbetap = betapdf(xbeta, 90, 10);
xbetamu(i) = mean(xbeta);
xbetapmu(i) = sum(xbeta.*xbetap)/sum(xbetap);
end
figure; hist([xbetamu xbetapmu], 100)
title('beta distribution')
legend('sample mean', 'pdf mean')
%% gamma distribution: true mean 4
xgammamu = nan(ntrials, 1);
xgammapmu = nan(ntrials, 1);
for i = 1:ntrials
xgamma = gamrnd(2, 2, nsamples, 1);
xgammap = gampdf(xgamma, 2, 2);
xgammamu(i) = mean(xgamma);
xgammapmu(i) = sum(xgamma.*xgammap)/sum(xgammap);
end
figure; hist([xgammamu xgammapmu], 100)
title('gamma distribution')
legend('sample mean', 'pdf mean')
%% normal: true mean 5
xnormmu = nan(ntrials, 1);
xnormpmu = nan(ntrials, 1);
for i = 1:ntrials
xnorm = normrnd(5, 2, nsamples, 1);
xnormp = normpdf(xnorm, 5, 2);
xnormmu(i) = mean(xnorm);
xnormpmu(i) = sum(xnorm.*xnormp)/sum(xnormp);
end
figure; hist([xnormmu xnormpmu], 100)
title('normal distribution')
legend('sample mean', 'pdf mean')
%% poisson: true mean 5
xpoismu = nan(ntrials, 1);
xpoispmu = nan(ntrials, 1);
for i = 1:ntrials
xpois = poissrnd(5, nsamples, 1);
xpoisp = poisspdf(xpois, 5);
xpoismu(i) = mean(xpois);
xpoispmu(i) = sum(xpois.*xpoisp)/sum(xpoisp);
end
figure; hist([xpoismu xpoispmu], 100)
title('poisson distribution')
legend('sample mean', 'pdf mean')
For any of the distributions with nonzero skewness, the distribution of the pdf-weighted means is shifted in the direction of the skewness with respect to the distribution of the sample mean. It also looks like it has a smaller variance. Here, we used the true parameter values of the distributions; it seems like fitting the distributions would only perform as well or worse since the fitting process itself requires using the samples.
The statement that the sample mean is an unbiased estimator of the mean holds regardless of the distribution of the data:
\begin{align}
\mathbb{E}[\bar{x}] = \frac{1}{n}\sum_{i=1}^n\mathbb{E}[x_i] = \frac{1}{n}\sum_{i=1}^n\theta = \frac{n\theta}{n} = \theta
\end{align}
Since this doesn't depend on the distribution of the data, your estimate will always be unbiased.
On the other hand, if you have outliers, there's a chance you could grossly mis-estimate your mean if you don't have knowledge of the functional form of the distribution (see code and figures below).
%% normal with outliers: true mean somewhere above 5
xnormmu = nan(ntrials, 1);
xnormpmu = nan(ntrials, 1);
for i = 1:ntrials
xnorm = normrnd(5, 2, nsamples, 1);
if logical(binornd(1, 0.1, 1))
xnorm = [xnorm; 20000];
else
xnorm = [xnorm; normrnd(5, 2, 1, 1)];
end
xnormp = normpdf(xnorm, 5, 2);
xnormmu(i) = mean(xnorm);
xnormpmu(i) = sum(xnorm.*xnormp)/sum(xnormp);
end
figure; hist([xnormmu xnormpmu], 100)
title('normal distribution with outliers')
legend('sample mean', 'pdf mean')
%% poisson with outliers: true mean somewhere above 5
xpoismu = nan(ntrials, 1);
xpoispmu = nan(ntrials, 1);
for i = 1:ntrials
xpois = poissrnd(5, nsamples, 1);
if logical(binornd(1, 0.1, 1))
xpois = [xpois; 20000];
else
xpois = [xpois; poissrnd(5, 1, 1)];
end
xpoisp = [poisspdf(xpois, 5)];
xpoismu(i) = mean(xpois);
xpoispmu(i) = sum(xpois.*xpoisp)/sum(xpoisp);
end
figure; hist([xpoismu xpoispmu], 100)
title('poisson distribution with outliers')
legend('sample mean', 'pdf mean')
Here, 0.1 is the probability of having an outlier in your dataset. Approximately 10% of the sample-mean estimates of the population mean are much farther from the true mean than the pdf-weighted estimates. In this case, knowledge of the true distribution more frequently results in a better estimate of the population mean than just using the sample mean. The sample mean is still ``unbiased'' (because you have to weight the means of the datasets with and without outliers to get the true mean, which is no longer just 5 in either of the two above cases). However, since you know that outliers exist and that their values are inflating the mean (which is actually 5), the sample estimate is much worse on average compared to the pdf-fitted mean.