data = vcat(
rand(Normal(-1.0, 0.5), 500),
rand(Normal( 2.0, 0.5), 1000)
)
hist = append!(Histogram(-2:0.1:4), data)
function fit_function(p::NamedTuple{(:a, :mu, :sigma)}, x::Real)
p.a[1] * pdf(Normal(p.mu[1], p.sigma), x) +
p.a[2] * pdf(Normal(p.mu[2], p.sigma), x)
end
true_par_values = (a = [500, 1000], mu = (-1.0, 2.0), sigma = 0.5)
using BAT, IntervalSets
likelihood = let h = hist, f = fit_function
# Histogram counts for each bin as an array:
observed_counts = h.weights
# Histogram binning:
bin_edges = h.edges[1]
bin_edges_left = bin_edges[1:end-1]
bin_edges_right = bin_edges[2:end]
bin_widths = bin_edges_right - bin_edges_left
bin_centers = (bin_edges_right + bin_edges_left) / 2
params -> begin
# Log-likelihood for a single bin:
function bin_log_likelihood(i)
# Simple mid-point rule integration of fit function `f` over bin:
expected_counts = bin_widths[i] * f(params, bin_centers[i])
logpdf(Poisson(expected_counts), observed_counts[i])
end
# Sum log-likelihood over bins:
idxs = eachindex(observed_counts)
ll_value = bin_log_likelihood(idxs[1])
for i in idxs[2:end]
ll_value += bin_log_likelihood(i)
end
# Wrap `ll_value` in `LogDVal` so BAT knows it's a log density-value.
return LogDVal(ll_value)
end
end
I just went through the tutoroial for BAT.jl, and am that LogDVal is not defined. Am I missing a parackage, or is a function not defined?