-
Notifications
You must be signed in to change notification settings - Fork 1
/
Gaussian.jl
50 lines (43 loc) · 1.43 KB
/
Gaussian.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
## Calculate sufficient statistics given samples
function add_ss!(ss::Vector{Float64}, x::Vector, hp::IsotropicGaussian)
assert(length(ss)==length(x))
@simd for d in eachindex(x)
@inbounds ss[d] += x[d]
end
end
function sub_ss!(ss::Vector{Float64}, x::Vector, hp::IsotropicGaussian)
assert(length(ss)==length(x))
@simd for d in eachindex(x)
@inbounds ss[d] -= x[d]
end
end
function loghx(x::Matrix, hp::IsotropicGaussian)
# x can be vector or matrix
N = size(x,2)
d = hp.dd
σ = hp.σ
# -sumabs2(x)/2σ^2 - N*d*(log(σ)+log(2π)/2)
-sum(abs2,x)/2σ^2 - N*d*(log(σ)+log(2π)/2)
end
## log partition function b
function _b(c::AbstractCluster, hp::IsotropicGaussian)
κ = hp.κ0 + hp.c * c.n
# sumabs2(c.ss)/2κ/hp.σ^4 + hp.dim/2*log(2π/κ)
sum(abs2,c.ss)/2κ/hp.σ^4 + hp.dd/2*log(2π/κ)
end
## when c is empty
_b(hp::IsotropicGaussian) = hp.dd/2*log(2π/hp.κ0)
## b(x+C)-b(C)
function diff_b(x::Vector, c::DataCluster, hp::IsotropicGaussian)
β = c.ss
κ = hp.κ0 + hp.c * c.n
t = κ + hp.c
# (sumabs2(x)/2 - sumabs2(β)*hp.c/2κ + dot(β,x))/t/hp.σ^4 + hp.dim/2*log(κ/t)
(sum(abs2,x)/2 - sum(abs2,β)*hp.c/2κ + dot(β,x))/t/hp.σ^4 + hp.dd/2*log(κ/t)
end
## when c is empty
function diff_b(x::Vector, hp::IsotropicGaussian)
t = hp.κ0 + hp.c
# sumabs2(x)/2t/hp.σ^4 + hp.dim/2*log(hp.κ0/t)
sum(abs2,x)/2t/hp.σ^4 + hp.dd/2*log(hp.κ0/t)
end