Skip to content

Commit

Permalink
perf improvements - Adaptation
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamed82008 committed Jun 10, 2019
1 parent 9c043ce commit 7bf6d85
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/adaptation/precond.jl
Expand Up @@ -53,9 +53,12 @@ end

function add_sample!(wv::WelfordVar, s::AbstractVector)
wv.n += 1
δ = s .- wv.μ
wv.μ .+= δ ./ wv.n
wv.M .+= δ .* (s .- wv.μ)
@unpack μ, M, n = wv
for i in eachindex(s)
δ = s[i] - μ[i]
μ[i] += δ / n
M[i] += δ * (s[i] - μ[i])
end
end

# https://github.com/stan-dev/stan/blob/develop/src/stan/mcmc/var_adaptation.hpp
Expand Down Expand Up @@ -110,9 +113,10 @@ end

function add_sample!(wc::WelfordCov, s::AbstractVector)
wc.n += 1
δ = s .- wc.μ
wc.μ .+= δ ./ wc.n
wc.M .+= (s .- wc.μ) * δ'
@unpack δ, μ, n, M = wc
δ .= s .- μ
μ .+= δ ./ n
M .+= (s .- μ) .* δ'
end
# Ref: https://github.com/stan-dev/stan/blob/develop/src/stan/mcmc/covar_adaptation.hpp
function get_cov(wc::WelfordCov{T}) where {T<:AbstractFloat}
Expand Down

0 comments on commit 7bf6d85

Please sign in to comment.