Jump to content

User:Dicklyon/Gamma median

From Wikipedia, the free encyclopedia

The median of a gamma distribution does not have a closed-form formula, but has some known simple special cases and asymptotic behavior.

On Talk:Gamma distribution we have been talking about approximations for the median of a gamma distribution (well, more accurately, I've been mostly talking to myself there). Here I will discuss some approximations I have come up with for the low-k regime. Per WP:NOR, this is not ready for article space.

The known Laurent series works well at high k, but not for middle and low k. Approximations in red, absolute errors in blue, on log-log scales. The noted parameter is the power of k in the denominator of the last term used.

The gamma distribution pdf is , but we'll use θ = 1 because both the mean and median scale with the scale parameter. So we'll work with .

The median of this pdf is the heavy black curve in the figures (computed by iteratively solving for 0.5 == gammainc(median, k) in Matlab using Newton–Raphson iteration).

Approximations for high k

[edit]

The Laurent series described in the gamma distribution article, originally by Choi and then extended and corrected by Berg and Pedersen, is increasingly accurate with more terms for high enough k, but is not very good around k = 1, and diverges below that.

Approximations for low k

[edit]
A graph illustrating how some approximations to the median of a gamma distribution, with shape parameter k < 1, might be calculated. The correct median value for this shape is about 0.22747.

To find the median, we just need to solve for ν in – but that's hard to solve. That integral is called the lower incomplete gamma function, and it can be computed as a sum of easy-to-compute terms by expressing the exponential as a power series.

But that expression can't be solved for ν, except numerically.

What we can do, however, is take just the first (n = 0) term, the one that treats ex ≈ 1, which is actually pretty accurate when k is low enough. The modified pdf is the upper (dashed) curve in the figure, . This is not a probability distribution, as it doesn't integrate to 1 (in fact its complete integral is infinite for all k). Nevertheless, it is an OK approximation to the pdf in the region below the median, when the median is much less than 1, as the figure suggests.

So, let's integrate, keeping just one term, to find a first approximation to the median, ν0:

Some approximations to the median of the gamma distribution (red) and their errors (blue). Curve c is what we here call , d is , and e is . The curves a and b are simple expansions about 0 of c, per Wolfram; and a was derived by a roundabout but rigorous process by Berg and Peterson (2006).[1]

When ν is very small, is an excellent approximation. But as the figure shows, the vertically-hashed area between curves is excess area counted in the integral. If we can estimate that area, and increase the estimate of to include that much more area to the right (the horizontally-hashed area), then we can get a better estimate. So let's take the next (negative) term in the series to estimate the extra area.

Call the extra area that we counted :

And consider just the first (n = 1) term:

And take the area to the right to be a rectangle of width and height .

So our next approximation is:

Or we could write it out without the intermediate ν0 as

The error in estimating ΔA and the error in getting Δx from ΔA are in opposite directions, so they partially cancel. To get a next better estimate, we can improve the estimate of ΔA, or the conversion to Δx, or both. But to get an improvement, we again want opposite signs of the errors. With two more terms for ΔA, and a trapezoidal estimate for the area to the right, we get a good result:

And for the righthand area that should equal this, we can take the height about in the middle instead of at the left edge; we can get that by evaluating the pdf half way between ν0 and ν1, or we can extrapolate from the height at ν0 using the derivative there, by half of the previously computed Δx, which is what we decided to do.

where the logarithmic derivative is:

Therefore:

Then we have the next approximation to the median:

It's complicated, but it's a ridiculously good approximation at low values of k, and it points toward how a series of better approximations might be derived, using more terms from the exponential and higher-order derivatives.

These are essentially just simple approximations to Newton–Raphson steps that one could do numerically, for very quick convergence when the starting point is not too bad. But it's nice that they can produce "closed form" approximations, not just numerical recipes.

Approximations for medium-to-high k

[edit]
Approximations that work well at high k and reasonably well somewhat below k = 1

Banneheka & Ekanayake (2009) provided the rational function as an approximation for the median at high enough k.[2] Generalizing the 0.2 to be a function c(k) leads to a variety of improved approximations that work down to lower values of the shape factor, and potentially down to 0 if a better form is found.

Using c = 8/45 = 0.1777777 leads to the right behavior for very large k, matching the 8/(9⋅45 k) term in the Laurent series, so we arranged this series of approximations to use that first coefficient. The others are least-squares fitted to an ideal c computed as a function of k using the numerical median values. To get a good fit, the lower limit of k used is adjusted for each N.

Unlike the original Laurent series, in this one adding more terms makes it work to lower values of k.

Approximations across all k

[edit]
Approximations that work well across a wide range of k. With three fitted coefficients, the worst-case absolute and relative errors are both below 0.01 everywhere, and the the high-k asymptote approaches zero absolute error. More digits are needed with more terms, but even with high precision the results do not improve rapidly with more terms.

To get a functional form with low relative error across all k, it seems we need to combine the above strategies. E.g. use 2-1/k to get the low-k shape, and a Laurent series to make it fit everywhere.

Since 2-1/k approaches 1 - log(2)/k at high k (according to Wolfram), multiplying by k + log(2) - 1/3 makes an approximation that approaches k - 1/3 at high k, which is a good place to start. Beyond that, we find coefficients empirically, by least-squares fit. The error curves shown correspond to the rounded coefficients stated on the figure.

By using negative powers of k + 0.5 instead of k, we allow convergence of the series down to 0. The 0.5 offset seems a bit better than other values tried, but has not been optimized.

Another series of approximations built on powers of 2-1/k and those powers times k. These are actually quite excellent in the middle. By constraining the sum of the d coefficients, we get asymptotically zero relative error at high k, but not zero absolute error. At order 3, the relative error is less than 0.01 everywhere (or at least down to the lowest values of k considered.

From looking at the small-k approximations, it seems that maybe a polynomial in (0.5 Γ(k + 1))-1/k might work. But that term is proportional to k at the high end, so powers of it wouldn't be useful. And being tempted to get rid of the gamma function, which is itself not really an easy closed-form expression, we can try polynomials in 2-1/k instead. However, since the latter flattens out at 1, and we want to end up with a result close to k, we included terms multiplied by k, yielding very good least-squares fits with not too many coefficients.

Trying to be smarter about this

[edit]
Approximations to the median of a gamma distribution, using interpolators between the low-k and high-k coeffcients of 1 and k to multiply by. With this approach the absolute (blue) and relative (magenta) errors both go to zero at low and high k, and with the particular interpolators – sigmoids of log k (dashed) and Gompertz functions of log k (solid) – the absolute and relative errors are everywhere less than 0.00228 for sigmoid and 0.00184 for Gompertz function.

The is useful. It needs a factor approaching at the low end, and approaching at the high end, as pointed out in earlier sections. If we make an appropriate interpolator between those coefficients of 1 and k, e.g. using a logistic sigmoid of , we will be able to approach the known asymptotes at both ends, such that both absolute and relative errors will tend toward zero for low and high k.

The coefficient of 1 changes from 0.5615 to 0.3598, while the coefficient of k changes from 0.4618 to 1.0. Finding a pair of interpolators jointly by searching over four parameters (width and centers of two sigmoids) is easy by brute-force search. We minimized the max of absolute and relative errors to arrive at these (sigmoidal in log k) interpolators, which yield absolute and relative errors everywhere less than 0.00228:

The formula for the smarter everywhere-good median approximation is then:

Optimized interpolation coefficients: for the constant factor (blue) and for the proportional-to-k factor (red), for logistic sigmoid (dashed) and Gompertz function (solid)
Adding the single-interpolator result (dash-dot), and removing the Gompertz curves, with the range of k extended at the low end to see the behavior.

The logistic sigmoid leads to a rather odd-looking best fit, as the plot of interpolators shows (red dashed curve, the sigmoid for the k coefficient, in particular). I actually ran this optimization down to to make sure the relative error is not bouncing back up, and it does eventually like the low-k coefficients down there.

Gompertz function as better interpolator

[edit]

The Gompertz function is a good asymmetric function that ends up working just a bit better as an interpolator for max relative error (less than 0.00184), but considerably better for max absolute error (less than 0.000612) at the same time, and gives a more sensible-looking pair of interpolating curves (solid curves in the two figures).

These interpolators work with the same formula for the median approximation given above.

As 4-parameter fits go, this is the best.

Single-interpolator version

[edit]

I tried one interpolator for both terms, but that was a lot worse. The graph of interpolators suggests that a single interpolator for the constant factor, and just fixing the k factor to the high-k value might be about as good. Indeed, that worked OK (and the same trick with Gompertz function did not). The best interpolator is not visually different from the (blue dashed) one plotted:

Then we have this simpler expression, with absolute and relative errors less then 0.00271 with just two fitted parameters:

Having the wrong coefficient of k doesn't hurt very much for very small k – the relative error just doesn't go to zero as quickly as it might.

Rewrite it this way:

Better single interpolator function: Gudermannian

[edit]
Compared to the previous single logistic sigmoid interpolator (dash-dot curves), the arctan or Gudermannian interpolator (solid curves) gives a better fit (less than half as much max relative error, below 0.00132).

Alternatively, we can numerically find the ideal single interpolator, see what it looks like, and seek a better functional form than a sigmoid to approximate it. The difference from a logistic sigmoid is small, but enough that we can see which direction to move, among the various sigmoid functions illustrated in Wikipedia, and it looks like the Gudermannian function would be a step in the right direction.

For the Gudermannian of log k we use the arctan(k) formula, but with optimized offset and scaling, this way:

This actually yields less than half as much max relative error, below 0.00132, and max absolute error below 0.00122. Using the same base formula from above, the new best two-parameter approximation is:

I was not able to reduce the maximum error further by using two Gudermannian interpolators.

References

[edit]
  1. ^ Berg, Christian and Pedersen, Henrik L. (March 2006). "The Chen–Rubin conjecture in a continuous setting" (PDF). Methods and Applications of Analysis. 13 (1): 63–88. Retrieved 1 April 2020.{{cite journal}}: CS1 maint: multiple names: authors list (link)
  2. ^ Banneheka BMSG, Ekanayake GEMUPD (2009) "A new point estimator for the median of gamma distribution". Viyodaya J Science, 14:95–103