Debian Codegraph @

easel/esl_gamma.md

raw symbols SCIP
1 # Gamma distributions
2
3 | | | |
4 |----------------------|------------|---------------------------|
5 | Variate | $x$ | $\mu < x < \infty$ |
6 | Lower bound | $\mu$ | $-\infty < \mu < \infty$ |
7 | Rate (Inverse scale) | $\lambda$ | $\lambda > 0$ |
8 | Shape | $\tau$ | $\tau > 0$ |
9
10
11 | | |
12 |-------------|-------------------------------|
13 | Mean | $\frac{\tau}{\lambda} + \mu$ |
14 | Variance | $\frac{\tau}{\lambda^2}$ |
15
16 The distribution starts at $\mu$ (support is $x > \mu$). We typically
17 know $\mu$. It is often 0; or in the case of fitting a gamma density
18 to a right tail, we know the threshold $\mu$ at which we truncated the
19 tail.
20
21 Probability density function (PDF):
22
23 $$
24 P(x) = \frac{\lambda^{\tau}}{\Gamma(\tau)} (x-\mu)^{\tau-1} e^{-\lambda (x - \mu)}
25 $$
26
27 The cumulative distribution function (CDF) does not have an analytical
28 expression. It is calculated numerically using the incomplete Gamma
29 function (`esl_stats_IncompleteGamma()`).
30
31 Some sources will say $x \geq \mu$, but $P(x=\mu) = 0$. Easel code
32 makes sure $x > \mu$ to avoid $\log P$ terms of $-\infty$.
33
34 ### Maximum likelihood parameter estimation
35
36 #### Complete data; known $\mu$
37
38
39 Given a complete dataset of $n$ observed samples $x_i$ ($i=1..n$) and
40 known $\mu$, `esl_gam_FitComplete()` estimates maximum likelihood
41 parameters $\hat{\tau}$ and $\hat{\lambda}$ using a generalized Newton
42 optimization [Minka00,Minka02].
43
44 The optimization only needs two sufficient statistics, the means
45 $\bar{x} = \frac{1}{n} \sum_i (x_i - \mu)$ and
46 $\overline{\log x} = \frac{1}{n} \sum_i \log (x_i - \mu)$,
47 which we precalculate.
48
49 This method chooses initial starting guesses:
50
51 $$
52 \tau_0 = \frac{0.5}{\log \bar{x} - \overline{\log x}}
53 $$
54 $$
55 \lambda_0 = \frac{\tau_0}{\bar{x}}
56 $$
57
58 and then iterates to convergence:
59
60 $$
61 \frac{1}{\tau_{t+1}} = \frac{1}{\tau_t} + \frac{\log \tau - \log \bar{x} + \overline{\log x} - \Psi(\tau) } { \tau_t - \tau_t^2 \Psi'(\tau_t)}
62 $$
63 $$
64 \lambda_{t+1} = \frac{\tau_{t+1}}{\bar{x}}
65 $$
66
67 The first and second derivatives of $\log \Gamma(x)$ are called
68 $\Psi(x)$ and $\Psi'(x)$, the "digamma" and "trigamma" functions.
69 These are obtained numerically by `esl_stats_Psi()` and
70 `esl_stats_Trigamma()`.
71
72 See the appendix at the end for the derivation of Minka's method.
73
74
75
76 ## Appendix 1: Minka's generalized Newton method
77
78
79 Minka's generalized Newton method works as follows. We aim to maximize
80 the log likelihood of the data:
81
82 $$
83 \log P(\mathbf{x} \mid \tau, \lambda) = n \tau \log \lambda - n \log \Gamma(\tau) + (\tau-1) \sum_i \log(x_i - \mu) - \lambda \sum_i (x_i - \mu)
84 $$
85
86 It's equivalent to maximize the average log likelihood, which we can
87 write in terms of two sufficient statistics, the means $\bar{x} = \frac{1}{n} \sum_i (x_i - \mu)$ and
88 $\overline{\log x} = \frac{1}{n} \sum_i \log (x_i - \mu)$:
89
90 $$
91 \frac{1}{n} \log P(\mathbf{x} \mid \tau, \lambda) = \tau \log \lambda - \log \Gamma(\tau) + (\tau-1) \overline{\log x} - \lambda \bar{x}
92 $$
93
94 Take derivative w.r.t. $\lambda$, set to zero, solve for $\hat{\lambda}$:
95
96 $$
97 \frac{\partial}{\partial \lambda} = \frac{\tau}{\lambda} - \bar{x} = 0
98 $$
99 $$
100 \hat{\lambda} = \frac{\tau}{\bar{x}}
101 $$
102
103 This makes sense because the mean of a Gamma is $\frac{\tau}{\lambda} + \mu$.
104
105 Substitute $\hat{\lambda} = \frac{\tau}{\bar{x}}$ back into the
106 average log likelihood to get an objective function $f(\tau)$ in
107 terms of a single variable $\tau$:
108
109 $$
110 f(\tau) = \frac{1}{n} \log P(\mathbf{x} \mid \tau, \hat{\lambda}) = \tau \log \tau - \tau \log \bar{x} - \log \Gamma(\tau) + (\tau-1) \overline{\log x} - \tau
111 $$
112
113 which is differentiable:
114
115 $$
116 f'(\tau) = \log \tau - \log \bar{x} - \Psi(\tau) + \overline{\log x}
117 $$
118 $$
119 f''(\tau) = \frac{1}{\tau} - \Psi'(\tau)
120 $$
121
122 Newton's method for finding the optimum of $f(x)$ works iteratively,
123 by approximating $f(x)$ locally around a current point $x_t$ by fitting a Gaussian distribution $g(x)$
124 to match $f$ and its first and second derivatives (i.e. $f(x_t) = g(x_t)$, $f'(x_t) = g'(x_t)$, $f''(x_t) = g''(x_t)$),
125 then analytically locating the optimum of $g(x)$ to propose the next point $x_{t+1}$.
126
127 Minka generalizes Newton's method by observing that we may be able to
128 find a much better approximation $g(x)$ for our particular
129 $f(x)$. Here, he proposes the approximation:
130
131 $$
132 g(\tau) = c_0 + c_1 \tau + c_2 \log \tau \approx \frac{1}{n} \log P(\mathbf{x} \mid \tau, \hat{\lambda})
133 $$
134
135 Constructing $g(\tau)$ to locally approximate our objective function $f(\tau)$ at a point $\tau_t$ means:
136
137 $$
138 g(\tau_t) = f(\tau_t) = c_0 + c_1 \tau_t + c_2 \log \tau_t
139 $$
140 $$
141 g'(\tau_t) = f'(\tau_t) = c_1 + \frac{c_2}{\tau_t}
142 $$
143 $$
144 g''(\tau_t) = f''(\tau_t) = -\frac{c_2}{\tau_t^2}
145 $$
146
147 which we can solve for estimates of the three parameters of the local approximation $g(\tau)$:
148
149 $$
150 c_2 = - \tau_t^2 f''(\tau_t)
151 $$
152 $$
153 c_1 = f'(\tau_t) + \tau_t f''(\tau_t)
154 $$
155 $$
156 c_0 = f(\tau_t) - \tau_t f'(\tau_t) + (\log(\tau_t) + 1) \tau_t^2 f''(\tau_t)
157 $$
158
159 The optimum $\hat{\tau}$ of $g(\tau)$ is where $g'(\tau) = 0$ so we didn't even
160 need $c_0$, we only need $c_1$ and $c_2$:
161
162 $$
163 g'(\tau) = c_1 + \frac{c_2}{\tau} = 0
164 $$
165
166 Solving for $\hat{\tau}$ gives $-\frac{c_2}{c_1}$, which is:
167
168 $$
169 \hat{\tau} = \frac{\tau_t^2 f''(\tau_t)}{f'(\tau_t) + \tau_t f''(\tau_t)}
170 $$
171
172 which is more compactly written as:
173
174 $$
175 \frac{1}{\hat{\tau}} = \frac{1}{\tau_t} + \frac{f'(\tau_t)}{\tau_t^2 f''(\tau_t)}
176 $$
177
178 Finally, substituting $f'$ and $f''$ gives us our iterative reestimation equation for our
179 next point $\tau_{t+1}$:
180
181 $$
182 \boxed{
183 \frac{1}{\tau_{t+1}} = \frac{1}{\tau_t} + \frac{\log \tau - \log \bar{x} + \overline{\log x} - \Psi(\tau) } { \tau_t - \tau_t^2 \Psi'(\tau_t)}
184 }
185 $$
186
187 That's our iterative reestimation, but we still need to choose an initial starting point $\tau_0$. Minka observes
188 that $\Psi(\tau) \approx \log(\tau) - \frac{1}{2\tau}$ (from a Stirling approximation
189 $\log \Gamma(\tau) \approx \tau \log \tau - \tau - \frac{1}{2} \tau + \mathrm{const.}$), which
190 we can substitute into $f'(\tau)$, set to zero, and solve:
191
192 $$
193 \boxed{ \tau_0 = \frac{0.5}{\log \bar{x} - \overline{\log x}} \approx \hat{\tau} }
194 $$
195
196
197
198
199
200 ## References
201
202 [Minka00] TP Minka, ["Beyond Newton's
203 Method"](https://tminka.github.io/papers/minka-newton.pdf),
204 unpublished note (2000).
205
206 [Minka02] TP Minka, ["Estimating a Gamma
207 distribution"](https://tminka.github.io/papers/minka-gamma.pdf),
208 unpublished note (2002).