# Expected value and variance of median

Suppose $$Y|\Lambda\sim U(0,\lambda)$$ with $$\Lambda \sim U(0,1)$$. If there is sample with size $$n$$ of $$Y$$ (To simplify, assume $$n$$ is odd, so $$n=2m-1$$). How do I calculate the expected value of median ($$E[Y_{(m)}]$$) and variance of the median ($$Var[Y_{(m)}]$$). Thanks in advance

Note: To clarify, my intention was that $$\{ Y_1, \dots, Y_n \}$$ is generated with the same $$\Lambda$$ value (so there's only 1 value of $$\Lambda$$, not $$n$$ for each value of $$Y_i$$). Sorry for the lack of information on the previous post.

• You can (easily) find the distribution of $Y,$ which directly gives the distribution of the median, and then apply the standard integral formulas, giving a three-step solution. Which step do you need help with?
– whuber
Sep 15, 2023 at 18:04
• Why should it not converge? It is bounded between $0$ and $1$ and there is no reason to suppose it would keep changing direction as $m$ keeps increasing Sep 15, 2023 at 19:27
• Re-stated: Finding the mixture distribution of 𝑌 is okey-dokey. Finding the distribution of the median is okey-dokey too. But, it seems to me the expectation of the median does not have a closed-form. Are you saying that it does? Sep 15, 2023 at 19:33
• @Wolfies By symmetry, the expectation of the median conditional on $\Lambda$ is $\Lambda/2.$ Thus, the expectation of the median of $Y$ must be $1/4.$ // The exact distribution of the median is straightforward to find, because it's a uniform scale mixture of Beta distributions.
– whuber
Sep 15, 2023 at 19:57
• @whuber: I agree with you, but to clarify the difference between the two readings of the questions try with $n=9$ in the R code medA<-function(n){median(runif(n,0,runif(1)))}; simsA<-replicate(10^4,medA(9)); mean(simsA) which gives about $0.25$ while medB<-function(n){median(runif(n,0,runif(n)))}; simsB<-replicate(10^4,medB(9)); mean(simsB) gives about $0.20$. Wolfies has $n$ different $\Lambda$ in each sample, while you and I and Zhanxiong have the same value through that sample Sep 16, 2023 at 23:15

Caveat: The calculation below relies on the (not explicitly stated) condition that the sample $$\{Y_1, \ldots, Y_n\}$$ is drawn independently from $$U(0, \lambda)$$ once $$\Lambda = \lambda$$ was observed. In other words, $$Y_1, \ldots, Y_n$$ are conditionally independent given $$\Lambda = \lambda$$. Note that this sampling mechanism is essentially different from drawing $$n$$ i.i.d. observations from the marginal density (as simulated in @wolfies answer) $$f_Y(y) = -\log y, 0 < y < 1$$. The closed-form solutions with the second interpretation should be much harder -- the density of $$Y_{(m)}$$ if $$Y_1, \ldots, Y_{2m - 1} \text{ i.i.d. } \sim f_Y$$ is \begin{align} g(x) = -\frac{(2m - 1)!}{[(m - 1)!]^2}[x(1 - \log x)(1 - x(1 - \log x))]^{m - 1}\log x, \quad 0 < x < 1. \end{align}

Here we use that the $$k$$-th order statistic of the standard uniform distribution is beta-distributed, i.e., $$U_{(k)} \sim B(k, n + 1 - k)$$, whence $$\lambda^{-1}Y_{(m)}|\Lambda = \lambda \sim B(m, 2m - 1 + 1 - m) = B(m, m)$$. It then follows by the moments of beta distribution that \begin{align} & E[\lambda^{-1}Y_{(m)}|\Lambda = \lambda] = \frac{m}{m + m} = \frac{1}{2}, \\ & \operatorname{Var}(\lambda^{-1}Y_{(m)}|\Lambda = \lambda) = \frac{m^2}{4m^2\cdot(2m + 1)} = \frac{1}{8m + 4}. \end{align} Therefore \begin{align} E[Y_{(m)}|\Lambda] = \frac{1}{2}\Lambda, \quad \operatorname{Var}(Y_{(m)}|\Lambda) = \frac{\Lambda^2}{8m + 4}. \end{align} It then follows by the law of total expectations and variances that \begin{align} & E[Y_{(m)}] = \frac{1}{2}E[\Lambda] = \frac{1}{4}, \\ & \operatorname{Var}(Y_{(m)}) = E[\operatorname{Var}(Y_{(m)}|\Lambda)] + \operatorname{Var}(E[Y_{(m)}|\Lambda]) = \frac{1}{8m + 4}E[\Lambda^2] + \frac{1}{4}\operatorname{Var}(\Lambda) \\ & = \frac{1}{8m + 4}\times\frac{1}{3} + \frac{1}{4}\times \frac{1}{12} = \frac{2m + 5}{96m + 48}. \end{align}

• @wolfies of course not... Did you really understand the argument? Take $n = 2m - 1$ and $k = m$ in the first link in my answer. Here $n$ can be any positive integer. Sep 16, 2023 at 13:45
• At its essence, your solution posits that the expected value of the sample median is a constant (independent of the sample size). I have posted a solution that asserts that it is not a constant. It is very easy to check via simulation whether it is a constant, or not. Sep 16, 2023 at 15:42
• @wolfies I think the difference of your simulation and my calculation above lies in that how the sample $Y$ was drawn (the OP didn't sufficiently clarify it) -- my interpretation is that a sample was drawn given the same $\Lambda = \lambda$, that is $Y_1, \ldots, Y_n$ are (conditionally) independent $U(0, \lambda)$ given $\Lambda = \lambda$, while your simulation is based on the interpretation that $Y_1, \ldots, Y_n \text{ i.i.d. } \sim f(y) = -\log y$. I will clarify it in my answer. Sep 16, 2023 at 16:50
• It occurred to me too that this explains the difference. However, we both agreed that the unconditional pdf of $Y$ is $-\log y$, and that result is not consistent with holding $\lambda$ constant. Sep 16, 2023 at 17:12
• @wolfies Again, that depends on how you interpret the problem. $-\log y$ is the unconditional pdf for ONE $Y$, but not the the joint unconditional pdf of $(Y_1, \ldots, Y_n)$ if we take the first interpretation, under which case the expectation is indeed $1/4$. Sep 16, 2023 at 17:17

As whuber has noted, this problem has 3 stages:

Step 1: The unconditional pdf of $$Y$$

The first stage is comparatively easy (see here for instance) which is to find the unconditional distribution of $$Y$$, i.e. the parameter mixture of two Uniforms.

The unconditional pdf of $$Y$$, say $$f(y)$$ is :

Step 2: The distribution of the sample median of $$Y$$

The OP assumes that a random sample of size $$n$$ is drawn on $$Y$$ where $$n$$ is odd, and so we write $$n=2r-1$$. The sample median, which we denote by $$M$$, corresponds to the middle order statistic in a sample of size $$n$$, which is by definition thus $$Y_{(r)}$$.

The pdf of the $$r^\text{th}$$ order statistic, in a sample of size $$n$$, can be found using the OrderStat[r, f, n] function in the mathStatica package for Mathematica.

The pdf of the sample median is then $$g(m)$$:

with domain of support:

Step 3: The mean and variance of the sample median

Calculating the mean and variance of the sample median does not appear to have an obvious closed form solution, as a general function of the sample size ($$n$$ or equivalently $$r$$). However, for given values of $$r$$, it yields exact solutions. For instance, when $$r = 1, 2, 3, 4, 5$$:

Note that this is the mean of the sample median for sample sizes $$n = 1, 3, 5, 7, 9$$, and corresponds to numerical values of:

{0.25, 0.220486, 0.209634, 0.204037, 0.200629}

Variance:

Similarly, the variance of the sample median, when $$r = 1, 2, 3, 4, 5$$ is:

Note that this is the variance of the sample median for sample sizes of $$n = 1, 3, 5, 7, 9$$, and corresponds to numerical values of:

{0.0486111, 0.0242859, 0.0159245, 0.0117921, 0.00934636}

The following diagram plots the expectation of the sample median, for $$r$$ from 1 to 25 (corresponding to the odd-sized samples from $$n = 1$$ to $$49$$):

Check by Simulation

Here is some quick code to simulate: for any given value of $$r$$, generate 500,000 samples of size $$n = 2r-1$$. Then, for each sample, calculate the sample median, and then find the sample mean of all 500,000 sample medians:

SimExpectedMedian[r_] := (data = Partition[RandomNumber[(2 r - 1)*500000, f], 2 r - 1]; Mean[Map[Median, data]])

For example, here we simulate the solution for $$r = 1, 2, 3, 4$$ and $$5$$:

... which very closely fits the exact solutions derived above (stated here numerically for ease of comparison):

{0.25, 0.220486, 0.209634, 0.204037, 0.200629}

The following diagram plots:

a) the EXACT expectation of the sample median derived above (for $$r = 1$$ to 25): big BLUE dots

b) the SIMULATED mean of the sample median: smaller RED dots

The simulated and exact results are so close that red and blue dots lie on top of each other.

• I read the original question as saying $\Lambda$ is the same value for all $n$ samples of $Y$. Repeat the experiment and $\Lambda$ will change. You seem to read it as having $n$ different values of $\Lambda$ in the sample of $Y$. Sep 16, 2023 at 23:20

A. The marginal distribution function of $$Y$$ is

$$F_Y(y) = y\cdot(1-\ln(y)).$$

To find the population median, we have $$m_Y:= F_Y(m) = \frac 12 \implies m_Y\cdot(1-\ln(m_Y)) = \frac 12 \implies m_Y: 1-\ln(m_Y)-\frac 1 {2m_Y} = 0.$$ This implicit equation is monotonic and it is satisfied for $$m_Y \approx 0.18668235.$$

This is the median of the marginal distribution of $$Y$$, and we see that the expected value of the sample median from draws from this marginal distribution (@wolfies answer), appears to tend downwards from the value $$0.25$$ as the sample size increases, so we can expect it to become unbiased at the limit.

B. The conditional distribution function of $$Y$$, evaluated at the conditional median $$m^c_Y$$ is

$$\Pr\big(Y \leq m^c_Y \mid \Lambda = \lambda\big) = \frac {m^c_Y}{\lambda} = \frac 12 \\ \implies m^c_Y = \frac {\lambda} 2.$$

Viewed as a function of $$\Lambda$$, this is a random variable, and so, the expected value of the conditional median is, (with $$f_{\Lambda}(\lambda)$$ the density of $$\Lambda$$)

$$E[m^c_Y] = \int_0^1 f_{\Lambda}(\lambda)\cdot m^c_Y d\lambda = \int_0^1 1\cdot \frac {\lambda} 2\,d\lambda = \frac {\lambda^2} 4 \Big |^1_0 = \frac 1 4.$$

We see that

• the "expected value of the conditional median"

equals

• the "unconditional expected value of the sample median drawn from the conditional distribution" (@Zhanxiong answer).

This tells us that the sample conditional median is unbiased for the population conditional median, already for finite samples.

--

Why the difference in performance? Why, when we draw from the marginal distribution of $$Y$$, we get unbiasedness of the sample median only asymptotically, while when we draw from a conditional (without needing to know the value of $$\lambda$$, just that it has been fixed), we get unbiasedness immediately? Contemplate.