Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faster method to compute the mean of a vector of Measurements #135

Merged
merged 3 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/MatrixProductBP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ using UnPack: @unpack
using Random: shuffle!, AbstractRNG, GLOBAL_RNG
using SparseArrays: rowvals, nonzeros, nzrange, sparse
using Distributions: Distributions, sample, sample!
using Measurements: Measurement, ±
using Measurements: Measurement, ±, value, uncertainty
using Statistics: mean, std
using Unzip: unzip
using StatsBase: weights, proportions
Expand Down Expand Up @@ -56,7 +56,8 @@ export
continuous_sis_sampler, simulate_queue_sis!,
draw_node_observations!, AtomicVector,
RecursiveBPFactor, DampedFactor, RecursiveTraceFactor, GenericFactor,
RestrictedRecursiveBPFactor
RestrictedRecursiveBPFactor,
mean_with_uncertainty


include("utils.jl")
Expand Down
3 changes: 2 additions & 1 deletion src/sampling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ function autocorrelations(f, sms::SoftMarginSampler; showprogress::Bool=true,
dt = showprogress ? 0.1 : Inf
prog = Progress(N, desc="Autocorrelations from Soft Margin"; dt)

for (a,i) in pairs(sites)
@threads for a in eachindex(sites)
i = sites[a]
for u in axes(r[a], 2), t in max(1,u-maxdist):u-1
q = nstates(bp, i)
mtu_avg = zeros(q, q)
Expand Down
12 changes: 12 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,15 @@ end
sample_noalloc(w) = sample_noalloc(GLOBAL_RNG, w)


# Computes the mean of a vector of statistically independent `Measurement`s. Equivalent to `mean(X)`, but faster
function mean_with_uncertainty(x)
v = mean(value(a) for a in x)
e = sum(abs2, uncertainty(a) for a in x)
s = sqrt(e) / length(x)
return v ± s
end
function mean_with_uncertainty(x::AbstractArray{<:AbstractArray{<:Measurement}})
map(Iterators.product(axes(x[1])...)) do i
mean_with_uncertainty(xx[i...] for xx in x)
end
end
4 changes: 4 additions & 0 deletions test/sampling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@

sms = sample(bp, 10; showprogress=false, rng)
m = marginals(sms)
@test all(m) do mᵢ
mean.(mᵢ) ≈ mean_with_uncertainty.(mᵢ)
end
@test mean.(m) ≈ mean_with_uncertainty.(m)
pb = pair_marginals(sms)

f(x,i) = x-1
Expand Down
Loading