Your example. Suppose $P(X = 20) = 0.6$ and $P(X = 40) = 0.4.$ Then $\mu= E(X) = 20(.6)+40(.4) = 12+16 = 28.$ Also, $\eta = Med(X) = 20$ because
$P(X < 20) = 0 < 0.5$ and $P(X > 20) = 0.4 < 0.5.$
Now we use R to sample $n=100\,000$ observations $X_1, X_2, \dots, X_n$
from this distribution.
set.seed(2020)
x = sample(c(20,40), 10^5, rep=T, p=c(.6,.4))
A tabulation of results shows that we have about 60% 20
s and
about 40% 40
s.
table(x)/10^6
x
20 40
0.60473 0.39527
A summary of sample statistics shows that the median of this large sample $H = 20.0$ is the same as the median $\eta=20$ of the distribution; and the mean $A = \bar X = 27.91$ is very nearly
the same as the mean $\mu = 28$ of the population.
summary(x); var(x)
Min. 1st Qu. Median Mean 3rd Qu. Max.
20.00 20.00 20.00 27.91 40.00 40.00
[1] 95.61361 # sample variance S^2
The general principle is that for increasingly large samples, the
sample mean converges to the population mean and the sample median
converges to the population variance.
Furthermore, the population variance is
$$\sigma^2= Var(X_i) = P(X=20)(20-\mu)^2 + P(X=40)(40-mu)^2 \\
= 0.6(20-28)^2 + 0.5(40-28)^2 = 96.$$ And the sample variance
$S^2 =95.61$ has nearly the same numerical value.
Binomial Example. This same kind of convergence holds true for more complicated
distributions. For example, The number $Y$ of heads in ten tosses of a fair coin has the distribution $\mathsf{Binom}(n=10, p=1/2).$ This distribution has $\mu_Y = E(Y) = np =10(1/2) = 5,$ median $\eta_Y = 1/2,$ and variance
$\sigma^2_Y = Var(Y) = np(1-p) = 5/2 = 2.5.$
Suppose we use R to sample $m = 100\,000$ results $Y_i.$
set.seed(1224)
y = rbinom(10^5, 10, .5)
summary(y); var(y)
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.000 4.000 5.000 4.988 6.000 10.000
[1] 2.483412
The sample mean $\bar Y = 4.988 \approx \mu_Y = 5,$
Also, the sample median $5.000$ matches the population median
$\eta = 5,$ and the sample variance $S_Y^2 = 2.4835 \approx \sigma_Y^2 = 2.5.$
Moreover, if we make a histogram of the $m = 100\,000$ simulated values of $Y$ (blue), the heights of the histogram bars nearly match the probabilities that $Y$ takes the
corresponding values (red circles).
hist(y, prob=T, br=-.5:10.5, col="skyblue2", main="BINOM(10, .5)")
k = 0:10; pdf = dbinom(k, 10, .5)
points(k, pdf, col="red")