-
Notifications
You must be signed in to change notification settings - Fork 1
/
Multinomial.jl
65 lines (57 loc) · 1.81 KB
/
Multinomial.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
## Calculate sufficient statistics given samples
function add_ss!(ss::Vector{Float64}, x::SparseVector{Int,Int}, hp::MultinomialDir)
@simd for i in eachindex(x.nzind)
@inbounds ss[x.nzind[i]] += x.nzval[i]
end
end
function sub_ss!(ss::Vector{Float64}, x::SparseVector{Int,Int}, hp::MultinomialDir)
@simd for i in eachindex(x.nzind)
@inbounds ss[x.nzind[i]] -= x.nzval[i]
end
end
function loghx(X::SparseVector{Int}, hp::MultinomialDir)
N = sum(X)
return sum(lgamma, 1+N) - sum(lgamma, 1+X.nzval)
end
function loghx(X::SparseMatrixCSC{Int,Int}, hp::MultinomialDir)
# each column is a document
N = sum(X,1)
return sum(lgamma, 1+N) - sum(lgamma, 1+X.nzval)
end
## log partition function b
function _b(c::AbstractCluster, hp::MultinomialDir)
slg = 0.0
for i in eachindex(hp.γ)
@inbounds slg += lgamma(hp.γ[i] + c.ss[i])
end
return slg - lgamma(sum(hp.γ) + sum(c.ss))
end
## when c is empty
_b(hp::MultinomialDir) = sum(lgamma,hp.γ) - lgamma(sum(hp.γ))
## b(x+C)-b(C)
function diff_b(x::SparseVector{Int,Int}, c::DataCluster, hp::MultinomialDir)
sl = 0.0
@simd for i in eachindex(x.nzind)
@inbounds ind = x.nzind[i]
@inbounds xi = x.nzval[i]
@inbounds γi = hp.γ[ind]
@inbounds ssi = c.ss[ind]
sl += lgamma(xi+γi+ssi) - lgamma(γi+ssi)
end
s1 = sum(hp.γ)+sum(c.ss)
s2 = sum(x.nzval)
return sl - lgamma(s1+s2) + lgamma(s1)
end
## when c is empty
function diff_b(x::SparseVector{Int,Int}, hp::MultinomialDir)
sl = 0.0
@simd for i in eachindex(x.nzind)
@inbounds ind = x.nzind[i]
@inbounds xi = x.nzval[i]
@inbounds γi = hp.γ[ind]
sl += lgamma(xi+γi) - lgamma(γi)
end
s1 = sum(hp.γ)
s2 = sum(x.nzval)
return sl - lgamma(s1+s2) + lgamma(s1)
end