-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamma.jl
executable file
·289 lines (215 loc) · 8.57 KB
/
gamma.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/env julia
using StatsFuns, Statistics, SpecialFunctions, LinearAlgebra
"""
funjacGamUnr(alphavec::Matrix{BigFloat}, nvec::Matrix{Int64},
yarr::Matrix{BigFloat}, ybarvec::Matrix{BigFloat})
Compute the inverse of the Jacobian and the matrix of function values for
computing the unrestricted maximum likelihood estiamtor (MLE) of ``\\alpha_i``.
Parameters
----------
`alphavec::Matrix{BigFloat}`: m x 1 matrix of values of our current estimate
of the MLE of ``\\alpha_i``.
`nvec::Matrix{Int64}`: m x 1 matrix of sample sizes of each group.
`yarr::Matrix{Float64}`: m x ni matrix of observations in rows that correspond
to different values of the grouping variable.
`ybarvec::Matrix{Float64}`: m x 1 matrix of sample means.
Returns
-------
`Jinv::Diagonal{BigFloat, Vector{BigFloat}}`: inverse of the Jacobian for the
unrestricted MLE problem.
`F::Matrix{BigFloat}`: m x 1 matrix of function values for the unrestricted MLE
problem.
"""
function funjacGamUnr(m, alphavec, nvec, yarr, ybarvec)
# Jinv is a diagonal matrix composed of elements of
# 1/(partial g_k/partial alpha_i)
# Alphavec must be converted to Float64 for polygamma,
# for some reason it cannot handle BigFloat.
Jinv = Diagonal(vec((nvec .* (alphavec.^(-1)-
polygamma.(1, Float64.(alphavec)))).^(-1)))
# Defining logsum to make F's definition shorter
logsum = reshape(sum(log.(yarr), dims=2), (m, 1))
F = - nvec .* (polygamma.(0, Float64.(alphavec)) +
log.(ybarvec .* alphavec.^(-1))) + logsum
return Jinv, F
end
"""
funjacGamNull(alpha::BigFloat, n::Int64, yarr::Matrix{BigFloat},
ybar::BigFloat)
Compute the derivative and function value required to compute the maximum
likelihood estimator (MLE) of ``\\alpha`` under the null hypothesis.
Parameters
----------
`alpha::BigFloat`: current estimate of the MLE of `\\alpha`.
`n::Int64`: total number of observations.
`yarr::Matrix{BigFloat}`: m x ni matrix of observations with different rows
corresponding to different values of the grouping variable.
`ybar::BigFloat`: mean of all observations.
Returns
-------
`J::BigFloat`: function derivative value.
`F::BigFloat`: function value.
"""
function funjacGamNull(alpha, n, yarr, ybar)
J = n * (1/alpha - polygamma(1, Float64(alpha)))
F = -n * (polygamma(0, Float64(alpha)) + log(ybar/alpha)) + sum(log.(yarr))
return J, F
end
"""
newtonsGamUnr(m::Int64, alphavec::Matrix{BigFloat}, nvec::Matrix{Int64},
yarr::Matrix{BigFloat}, ybarvec::Matrix{BigFloat}, itMax::Int64,
tol::Float64)
Estimate the unrestricted maximum likelihood estimator (MLE) of ``\\alpha_i``
using Newton's method.
Parameters
----------
`m::Int64`: number of groups.
`alphavec::Matrix{BigFloat}`: m x 1 matrix of values of our initial estimate
of the MLE of ``\\alpha_i``.
`nvec::Matrix{Int64}`: m x 1 matrix of sample sizes of each group.
`yarr::Matrix{Float64}`: m x ni matrix of observations in rows that correspond
to different values of the grouping variable.
`ybarvec::Matrix{Float64}`: m x 1 matrix of sample means.
`itMax::Int64`: maximum number of iterations of Newton's method that can be
used.
`tol::Float64`: relative error tolerance.
Returns
-------
`alphavec::Matrix{BigFloat}`: m x 1 matrix of values of our improved estimate
of the MLE of ``\\alpha_i``.
"""
function newtonsGamUnr(m, alphavec, nvec, yarr, ybarvec, itMax, tol)
# First iteration of Newton's method
Jinv, F = funjacGamUnr(m, alphavec, nvec, yarr, ybarvec)
eps = -Jinv * F
alphavec += eps
epsRel = eps .* alphavec.^(-1)
diff = sqrt(sum(epsRel.^2)/m)
# Initialize our counter
iteration = 1
# Iterate until we get a satisfactorily accurate estimate for the MLE
while ((tol < diff) && (iteration < itMax))
Jinv, F = funjacGamUnr(m, alphavec, nvec, yarr, ybarvec)
eps = -Jinv * F
alphavec += eps
epsRel = eps .* alphavec.^(-1)
diff = sqrt(sum((epsRel.^2)/m))
iteration += 1
end
return alphavec
end
"""
newtonsGamNull(alpha::BigFloat, n::Int64, yarr::Matrix, ybar::Float64,
itMax::Int64, tol::Float64)
Estimate the maximum likelihood estimator (MLE) of alpha under the null using
Newton's method.
Parameters
----------
`alpha::BigFloat`: initial estimate of the MLE of `\\alpha`.
`n::Int64`: total number of observations.
`yarr::Matrix{BigFloat}`: m x ni matrix of observations with different rows
corresponding to different values of the grouping variable.
`ybar::BigFloat`: mean of all observations.
Returns
-------
`alpha::BigFloat`: improved estimate of the MLE of `\\alpha`.
"""
function newtonsGamNull(alpha, n, yarr, ybar, itMax, tol)
# First iteration of Newton's
J, F = funjacGamNull(alpha, n, yarr, ybar)
eps = -F/J
alpha += eps
# Initialize iteration counter
iteration = 1
# Iterate until we get a satisfactorily accurate for the MLE
while ((tol < eps/alpha) && (iteration < itMax))
J, F = funjacGamNull(alpha, n, yarr, ybar)
eps = -F/J
alpha += eps
iteration +=1
end
return alpha
end
"""
gammaTest(m::Int64, n::Int64, ni::Int64, alphavec::Matrix{BigFloat},
nvec::Matrix{Int64}, group::Vector{Int64}, yarr::Matrix{BigFloat},
ybar::BigFloat, ybarvec::Matrix{BigFloat})
Perform the gamma likelihood-ratio test and return the maximum likelihood
estimator (MLEs), likelihood ratio, test statistic and p-value.
Parameters
----------
`m::Int64`: number of groups.
`n::Int64`: total number of observations.
`ni::Int64`: maximum sample size.
`alphavec::Matrix{BigFloat}`: m x 1 matrix of MLE of ``\\alpha_i``.
`nvec::Matrix{Int64}`: m x 1 matrix of sample sizes for each group.
`group::Vector{Int64}`: vector of values of the grouping variable.
`yarr::Matrix{BigFloat}`: m x ni matrix of observed values of dependent
variable.
`ybar::BigFloat`: overall mean of dependent variable.
`ybarvec::Matrix{BigFloat}`: m x 1 matrix of the mean of each sample
(treatment group).
Returns
-------
`alpha::BigFloat`: the MLE of ``\\alpha`` under the null.
`beta::BigFloat`: the MLE of ``\\beta`` under the null.
`alphavec::BigFloat`: the unrestricted MLE of ``\\alpha_i``.
`betavec::BigFloat`: the unrestricted MLE of ``\\beta_i``.
`lam::BigFloat`: ``\\lambda``, the likelihood-ratio.
`stat::BigFloat`: ``-2\\ln(\\lambda)``, our test statistic.
`pval::BigFloat`: p-value of our test.
"""
function gammaTest(m, n, ni, alphavec, nvec, group, yarr, ybar, ybarvec, itMax, tol)
# Estimate unrestricted MLEs
alphavec = newtonsGamUnr(m, alphavec, nvec, yarr, ybarvec, itMax, tol)
betavec = alphavec.^(-1) .* ybarvec
# Estimate MLEs under null
alpha = 1
alpha = newtonsGamNull(alpha, n, yarr, ybar, itMax, tol)
beta = ybar/alpha
# Likelihood ratio
lam = (gamma(alpha) * (ybar/alpha)^(alpha))^(-n)
lam *= prod(prod(yarr.^(alpha*ones(length(alphavec), 1)-alphavec), dims=2))
lam *= prod((gamma.(alphavec) .* (ybarvec.*alphavec.^(-1)).^(alphavec)).^(nvec))
# Test statistic, -2 ln(lambda)
stat = -2*log(lam)
# Obtain p-value keeping in mind that under the null our test statistic
# should asymptotically follow a chi-squared distribution with 2m-2 df
pval = 1-chisqcdf(2*m-2, Float64(stat))
return alpha, beta, alphavec, betavec, lam, stat, pval
end
"""
printGamma(m::Int64, alpha::BigFloat, beta::BigFloat,
alphavec::Matrix{BigFloat}, betavec::Matrix{BigFloat}, lam::BigFloat,
stat::BigFloat, pval::BigFloat)
Print the results of the gamma likelihood-ratio test.
Parameters
----------
`m::Int64`: the number of groups.
`alpha::BigFloat`: the maximum likelihood estimator (MLE) of ``\\alpha``
under the null.
`beta::BigFloat`: the MLE of ``\\beta`` under the null.
`alphavec::Matrix{BigFloat}`: m x 1 matrix of the unrestricted MLE of
``\\alpha_i``.
`betavec::Matrix{BigFloat}`: m x 1 matrix of the unrestricted MLE of
``\\beta_i``.
`lam::BigFloat`: ``\\lambda``, the likelihood-ratio.
`stat::BigFloat`: ``-2\\ln{\\lambda}``, the test statistic.
`pval::BigFloat`: p-value of our test.
Returns
-------
Nothing.
"""
function printGamma(m, alpha, beta, alphavec, betavec, lam, stat, pval)
# Printing important data
println("For gamma model:")
println("alpha (null) = ", Float64(alpha))
println("beta (null) = ", Float64(beta))
println("alpha_i = ", Float64.(alphavec))
println("beta_i = ", Float64.(betavec))
println("lambda = ", lam)
println("Test statistic = ", Float64(stat))
println("Degrees of freedom = ", 2*m-2)
println("P-value = ", pval)
println("--------------------------------------------------")
end