Going on with measures of location, the mean value is not enough. Indeed, what if we have hundreds of similar values and one very-very large value? Their mean value will be pretty higher than these similar values. This is not very informative if we don't know the actual values, but know only the mean value? We would think that the values are similar to this high value, right? In this topic, we will introduce some more of them: the median and the quantiles, and learn how to estimate them from a large set of realizations.
Median
The median of a random variable is a number that divides its distribution into two parts: the left part, which has a probability of at least , and the right part, which has a probability of at least . In other words, is the number such that
The fact that the median divides the distribution of a random variable in half provides a very simple way to estimate it using a set of realizations of this random variable:
Sort the realizations in ascending order.
Find the central element of the resulting sequence if there is an odd number of realizations, or the average of the two central elements if there is an even number of realizations.
Median in Python
In Python, the median can be calculated using the numpy.median method. For example, for the salaries discussed above, the following values are obtained:
import numpy as np
first = [50000] * 10
second = [275000] + [25000] * 9
print(np.median(first), np.median(second))50000.0 25000.0As we can see, the median correctly reflects the fact that a typical employee in the first company earns twice as much as his colleague from the second company.
As with the expected value, the median also satisfies an analogue of the law of large numbers: the more realizations, the more accurate the estimate of the median will be.
The median is convenient to use in situations where we need to understand the typical behavior of a random variable. However, in life, there are often problems where we need to locate rare values of a random variable. For example, suppose we want to build a pedestrian bridge over a road and need to determine its height. If we make the bridge too low, many vehicles won't be able to pass underneath and will have to take a detour. On the other hand, if we make it too high, we'll spend a lot of money on construction. Thus, we need to find a balance between the number of vehicles that will have to take a detour and the cost of the bridge. To solve such problems, it's useful to consider another measure of location: quantile.
Quantiles
Quantiles are a natural generalization of the median. If the median divides the distribution in half, then the -quantile divides the distribution into pieces of probability and . More formally, a number , where , is called the -quantile of a random variable if
In this notation the median is -quantile: .
In Python, the quantile can be calculated using the numpy.quantile method. For example, let's estimate 0.25-quantile of the random variable distributed uniformly on :
import numpy as np
rng = np.random.default_rng()
# generate 100000 realizations of the uniform random variable
sample = rng.uniform(size=100)
np.quantile(sample, 0.25)0.2509581679576445Let's now return to the pedestrian bridge problem from the previous section. How can quantiles help us in this problem? Suppose we have estimated that we can afford a bridge height that is higher than 99.9% of the cars that will pass under it. In other words, we can build a bridge that will force no more than 0.1% of cars to detour. Then, to determine the minimum height of the bridge, we can gather information about the heights of cars and find their 0.999-quantile.
Conclusion
Measures of location help compactly answer the question "what values does a random variable take on?" Depending on the problem, different measures can be useful. For example, suppose we are studying the salaries of residents in a city.
If we are interested in the total tax paid by residents, it is useful to estimate the expected value of salary: tax is proportional to salary, so the total tax is proportional to the total salary, which in turn equals to the total population times the expected value of salary. The expected value can be estimated using
numpy.mean.If we are interested in the level of prosperity in the city, it is useful to estimate the median salary: it will reflect the income of a typical resident, and the higher it is, the better the prosperity of the city as a whole. The median can be estimated using
numpy.median.If we are interested in the level of poverty, we can estimate the -quantile with a small value of . For example, the 0.1-quantile of salary will show the boundary below which 10% of the poorest residents are located. If this boundary is high, it means that the level of poverty is high, and vice versa. The quantile can be estimated using
numpy.quantile.