1
+ """
2
+ NegativeBinomial
3
+
4
+ * Parametrization
5
+ parametrized in r, p
6
+
7
+ * Score
8
+
9
+ * Fisher Information
10
+
11
+ * `time_varying_params` map.
12
+
13
+ * Default link
14
+ """
15
+ NegativeBinomial
16
+
17
+ function score! (score_til:: Matrix{T} , y:: Int , :: Type{NegativeBinomial} , param:: Matrix{T} , t:: Int ) where T
18
+ score_til[t, 1 ] = digamma (y + param[t, 1 ]) - digamma (param[t, 1 ]) + log (param[t, 2 ])
19
+ score_til[t, 2 ] = param[t, 1 ] / param[t, 2 ] - y / (one (T) - param[t, 2 ])
20
+ return
21
+ end
22
+
23
+ function fisher_information! (aux:: AuxiliaryLinAlg{T} , :: Type{NegativeBinomial} , param:: Matrix{T} , t:: Int ) where T
24
+ return error (" Fisher information not implemented for NegativeBinomial distribution." )
25
+ end
26
+
27
+ function log_likelihood (:: Type{NegativeBinomial} , y:: Vector{Int} , param:: Matrix{T} , n:: Int ) where T
28
+ loglik = zero (T)
29
+ for t in 1 : n
30
+ loglik += loggamma (y[t] + param[t, 1 ]) - logfactorial (y[t]) - loggamma (param[t, 1 ]) +
31
+ param[t, 1 ] * log (param[t, 2 ]) + y[t] * log (1 - param[t, 2 ])
32
+ end
33
+ return - loglik
34
+ end
35
+
36
+ # Links
37
+ function link! (param_tilde:: Matrix{T} , :: Type{NegativeBinomial} , param:: Matrix{T} , t:: Int ) where T
38
+ param_tilde[t, 1 ] = link (LogLink, param[t, 1 ], zero (T))
39
+ param_tilde[t, 2 ] = link (LogitLink, param[t, 2 ], zero (T), one (T))
40
+ return
41
+ end
42
+ function unlink! (param:: Matrix{T} , :: Type{NegativeBinomial} , param_tilde:: Matrix{T} , t:: Int ) where T
43
+ param[t, 1 ] = unlink (LogLink, param_tilde[t, 1 ], zero (T))
44
+ param[t, 2 ] = unlink (LogitLink, param_tilde[t, 2 ], zero (T), one (T))
45
+ return
46
+ end
47
+ function jacobian_link! (aux:: AuxiliaryLinAlg{T} , :: Type{NegativeBinomial} , param:: Matrix{T} , t:: Int ) where T
48
+ aux. jac[1 ] = jacobian_link (LogLink, param[t, 1 ], zero (T))
49
+ aux. jac[2 ] = jacobian_link (LogitLink, param[t, 2 ], zero (T), one (T))
50
+ return
51
+ end
52
+
53
+ # utils
54
+ function update_dist (:: Type{NegativeBinomial} , param:: Matrix{T} , t:: Int ) where T
55
+ return NegativeBinomial (param[t, 1 ], param[t, 2 ])
56
+ end
57
+
58
+ function params_sdm (d:: NegativeBinomial )
59
+ return Distributions. params (d)
60
+ end
61
+
62
+ function num_params (:: Type{NegativeBinomial} )
63
+ return 2
64
+ end
0 commit comments