केएल डाइवर्जेंस फॉर्म के अभिन्न अंग का अंतर है
$$ \ eqalign {I (a, b, c, d) & = \ int_0 ^ {\ infty} \ log \ left (\ frac {e ^ {- x / a} x ^ {b-1}} {a ^ b \ Gamma (b)} \ right) \ frac {e ^ {- x / c} x ^ {d-1}} {c ^ d \ Gamma (d)} dx \
& = - \ frac {1} {a} \ int_0 ^ \ infty \ frac {x ^ de ^ {- x / c}} {c ^ d \ Gamma (d)} \, dx - \ log (^ b) \ Gamma (b)) \ int_0 ^ \ infty \ frac {e ^ {- x / c} x ^ {d-1}} {c ^ d \ Gamma (d)} \ _, dx \ & \ quad + (b) 1) \ int_0 ^ \ infty \ log (x) \ frac {e ^ {- x / c} x ^ {d-1}} {c ^ d \ Gamma (d)} \, dx \
& = - \ frac {cd} {a} - \ log (a ^ b \ Gamma (b)) + (b-1) \ int_0 ^ \ infty \ log (x) \ frac {e ^ {- x / c } x ^ {d-1}} {c ^ d \ Gamma (d)} \, dx}
हमें बस दाहिने हाथ के अभिन्न अंग से निपटना है, जिसे देखने से प्राप्त होता है
∂∂dΓ(d)====∂∂d∫∞0e−x/cxd−1cddx∂∂d∫∞0e−x/c(x/c)d−1cdx∫∞0e−x/cxd−1cdlogxcdx∫∞0log(x)e−x/cxd−1cddx−log(c)Γ(d).
जहां से
b−1Γ(d)∫∞0log(x)e−x/c(x/c)d−1dx=(b−1)Γ′(d)Γ(d)+(b−1)log(c).
Plugging into the preceding yields
I(a,b,c,d)=−cda−log(abΓ(b))+(b−1)Γ′(d)Γ(d)+(b−1)log(c).
The KL divergence between Γ(c,d) and Γ(a,b) equals I(c,d,c,d)−I(a,b,c,d), which is straightforward to assemble.
Implementation Details
Gamma functions grow rapidly, so to avoid overflow don't compute Gamma and take its logarithm: instead use the log-Gamma function that will be found in any statistical computing platform (including Excel, for that matter).
Γ′(d)/Γ(d)Γ,ψ, the digamma function. If it's not available to you, there are relatively simple ways to approximate it, as described in the Wikipedia article.
Here, to illustrate, is a direct R
implementation of the formula in terms of I. This does not exploit an opportunity to simplify the result algebraically, which would make it a little more efficient (by eliminating a redundant calculation of ψ).
#
# `b` and `d` are Gamma shape parameters and
# `a` and `c` are scale parameters.
# (All, therefore, must be positive.)
#
KL.gamma <- function(a,b,c,d) {
i <- function(a,b,c,d)
- c * d / a - b * log(a) - lgamma(b) + (b-1)*(psigamma(d) + log(c))
i(c,d,c,d) - i(a,b,c,d)
}
print(KL.gamma(1/114186.3, 202, 1/119237.3, 195), digits=12)