Skip to content

Commit

Permalink
Reduce allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
devmotion committed Jun 17, 2021
1 parent f7cb42f commit 37980b2
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions src/mcse.jl
Expand Up @@ -21,35 +21,45 @@ function mcse_bm(x::AbstractVector{<:Real}; size::Int=floor(Int, sqrt(length(x))
n = length(x)
m = min(div(n, 2), size)
m == size || @warn "batch size was reduced to $m"
mbar = [Statistics.mean(@view(x[(i + 1):(i + m)])) for i in 0:m:(n - m)]
return StatsBase.sem(mbar)
mcse = StatsBase.sem(Statistics.mean(@view(x[(i + 1):(i + m)])) for i in 0:m:(n - m))
return mcse
end

function mcse_imse(x::AbstractVector{<:Real})
n = length(x)
m = div(n - 2, 2)
x_ = map(Float64, x)
ghat = StatsBase.autocov(x_, [0, 1])
lags = [0, 1]
ghat = StatsBase.autocov(x, lags)
Ghat = sum(ghat)
value = -ghat[1] + 2 * Ghat
for i in 1:m
Ghat = min(Ghat, sum(StatsBase.autocov(x_, [2 * i, 2 * i + 1])))
@inbounds value = Ghat + ghat[2]
@inbounds for i in 2:2:(n - 2)
lags[1] = i
lags[2] = i + 1
StatsBase.autocov!(ghat, x, lags)
Ghat = min(Ghat, sum(ghat))
Ghat > 0 || break
value += 2 * Ghat
end
return sqrt(value / n)

mcse = sqrt(value / n)

return mcse
end

function mcse_ipse(x::AbstractVector{<:Real})
n = length(x)
m = div(n - 2, 2)
x_ = map(Float64, x)
ghat = StatsBase.autocov(x_, [0, 1])
value = ghat[1] + 2 * ghat[2]
for i in 1:m
Ghat = sum(StatsBase.autocov(x_, [2 * i, 2 * i + 1]))
lags = [0, 1]
ghat = StatsBase.autocov(x, lags)
@inbounds value = ghat[1] + 2 * ghat[2]
@inbounds for i in 2:2:(n - 2)
lags[1] = i
lags[2] = i + 1
StatsBase.autocov!(ghat, x, lags)
Ghat = sum(ghat)
Ghat > 0 || break
value += 2 * Ghat
end
return sqrt(value / n)

mcse = sqrt(value / n)

return mcse
end

0 comments on commit 37980b2

Please sign in to comment.