Skip to content

Commit 5bc1a04

Browse files
Merge pull request #54 from ArndtLab/histogram-bug
Histogram bug
2 parents a3b8c6d + afcc546 commit 5bc1a04

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

src/HetDister.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,23 +102,24 @@ function CustomEdgeVector(; lo = 1, hi = 10, nbins::Integer)
102102
end
103103

104104
"""
105-
adapt_histogram(segments::AbstractVector{<:Integer}; lo::Int=1, hi::Int=50_000_000, nbins::Int=200)
105+
adapt_histogram(segments::AbstractVector{<:Integer}; lo::Int=1, hi::Int=50_000_000, nbins::Int=800)
106106
107107
Build an histogram from `segments` logbinned between `lo` and `hi`
108108
with `nbins` bins.
109109
110110
The upper limit is adapted to ensure logspacing with the requested `nbins`.
111111
"""
112-
function adapt_histogram(segments::AbstractVector{<:Integer}; lo::Int=1, hi::Int=50_000_000, nbins::Int=200)
112+
function adapt_histogram(segments::AbstractVector{<:Integer}; lo::Int=1, hi::Int=50_000_000, nbins::Int=800)
113113
h_obs = Histogram(CustomEdgeVector(;lo, hi, nbins))
114114
@assert !isempty(segments)
115115
append!(h_obs, segments)
116-
l = findlast(h_obs.weights .> 1) + 1
117-
while h_obs.edges[1].edges[l+1] + 1 < hi
116+
l = findlast(h_obs.weights .> 1)
117+
isnothing(l) && return h_obs
118+
while h_obs.edges[1].edges[l+1] < hi
118119
hi = h_obs.edges[1].edges[l+1]
119120
h_obs = Histogram(CustomEdgeVector(;lo, hi, nbins))
120121
append!(h_obs, segments)
121-
l = findlast(h_obs.weights .> 1) + 1
122+
l = findlast(h_obs.weights .> 1)
122123
end
123124
return h_obs
124125
end

src/Spectra/SMCpIntegrals.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ function prordn!(res::AbstractMatrix{<:Real}, jprt::AbstractMatrix{<:Real},
202202
s += temp[k,j] * exp(-2rate * (rs[i]-edges[k+1]) * ts[j]) * (- expm1(-2rate * w * ts[j])) / 2ts[j]
203203
end
204204
w = edges[i+1] - edges[i]
205-
if w <= 1
205+
if w < 1
206206
s += temp[i,j] * (- expm1(-2rate * w * ts[j])) / 2ts[j]
207207
else
208208
w = rs[i] - edges[i]

src/corrections.jl

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ function demoinfer(h_obs::Histogram{T,1,E}, epochrange::AbstractRange{<:Integer}
6565
fits = map(r->r.f, results),
6666
chains = map(r->r.chain, results),
6767
corrections = map(r->r.corrections, results),
68-
h_obs = results[1].h_obs
68+
h_obs = results[1].h_obs,
69+
yth = map(r->r.yth, results),
70+
deltas = map(r->r.deltas, results)
6971
)
7072
end
7173

@@ -83,10 +85,12 @@ function demoinfer(h_obs::Histogram{T,1,E}, epochs::Int, fop_::FitOptions;
8385

8486
chain = []
8587
corrections = []
88+
deltas = []
8689

8790
ho_mod.weights .= h_obs.weights
8891
corr = zeros(eltype(h_obs.weights), length(h_obs.weights))
8992
f = nothing
93+
yth = nothing
9094
for iter in 1:iters
9195
fits = pre_fit!(fop, ho_mod, epochs; require_convergence = false)
9296
f = fits[end]
@@ -109,7 +113,9 @@ function demoinfer(h_obs::Histogram{T,1,E}, epochs::Int, fop_::FitOptions;
109113
if iter > 1
110114
deltacorr = (corrections[iter] .- corrections[iter-1]) ./ corrections[iter-1]
111115
deltacorr[isnan.(deltacorr)] .= 0.
112-
if maximum(abs.(deltacorr)) < reltol
116+
delta = maximum(abs.(deltacorr))
117+
push!(deltas, delta)
118+
if delta < reltol
113119
break
114120
end
115121
end
@@ -119,7 +125,9 @@ function demoinfer(h_obs::Histogram{T,1,E}, epochs::Int, fop_::FitOptions;
119125
f,
120126
chain,
121127
corrections,
122-
h_obs
128+
h_obs,
129+
yth,
130+
deltas
123131
)
124132
end
125133

test/runtests.jl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,10 @@ end
9393

9494
@testset "Test core functionality" for (mu,rho,TN) in zip(mus, rhos, TNs)
9595

96-
h = Histogram(LogEdgeVector(lo = 1, hi = 1_000_000, nbins = 200))
9796
ibs_segments = get_sim(TN, mu, rho)
98-
append!(h, ibs_segments)
97+
h = adapt_histogram(ibs_segments; nbins = 200)
98+
@test length(h.weights) == 200
99+
@test h.weights[end] > 1
99100

100101
stat = pre_fit(h, 2, mu, rho, 10, 100, sum(ibs_segments); require_convergence = false)
101102
@test isassigned(stat, 1)
@@ -113,7 +114,11 @@ end
113114
iters = 1
114115
)
115116
@test length(res.chains) == length(TN)÷2
117+
@test length(res.yth) == length(TN)÷2
116118
@test all(length.(res.chains) .== 1)
119+
@test all(length.(res.corrections) .== 1)
120+
@test all(length.(res.deltas) .== 0)
121+
@test all(length.(res.yth) .== length(res.h_obs.weights))
117122
@test !any(isinf.(evd.(res.fits)))
118123
best = compare_models(res.fits)
119124
@test !isnothing(best)
@@ -139,7 +144,7 @@ end
139144
@testset "fitting procedure" begin
140145
@testset "exhaustive pre-fit $(length(TN)÷2) epochs, mu $mu, rho $rho" for (mu,rho,TN) in itr
141146
ibs_segments = get_sim(TN, mu, rho)
142-
h = adapt_histogram(ibs_segments)
147+
h = adapt_histogram(ibs_segments; nbins = 200)
143148
Ltot = sum(ibs_segments)
144149
fop = FitOptions(Ltot, mu, rho; maxnts = 8, force = false)
145150
fits = pre_fit!(fop, h, 8; require_convergence = false)
@@ -153,7 +158,7 @@ end
153158
@testset "Iterative fit" begin
154159
mu, rho, TN = mus[1], rhos[1], TNs[3]
155160
ibs_segments = get_sim(TN, mu, rho)
156-
h = adapt_histogram(ibs_segments)
161+
h = adapt_histogram(ibs_segments; nbins = 200)
157162
Ltot = sum(ibs_segments)
158163
pfits = pre_fit(h, 5, mu, rho, 10, 100, Ltot; require_convergence = false)
159164
fop = FitOptions(Ltot, mu, rho; order = 10, ndt = 800)

0 commit comments

Comments
 (0)