Skip to content

Commit 4615303

Browse files
author
mauriciogtec
committed
Finished README with new features
1 parent f4e3b0c commit 4615303

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/AdaptiveRejectionSampling.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ the density assigned to the point `x`.
118118
function eval_envelop(e::Envelop, x::Float64)
119119
# searchsortedfirst is the proper method for and ordered list
120120
pos = searchsortedfirst(e.cutpoints, x)
121-
if pos == 1 || pos == length(e.cutpoints + 2)
121+
if pos == 1 || pos == length(e.cutpoints) + 1
122122
return 0.0
123123
else
124124
a, b = e.lines[pos - 1].slope, e.lines[pos - 1].intercept
@@ -175,7 +175,7 @@ mutable struct RejectionSampler
175175
max_failed_rate::Float64 = 0.001
176176
) = begin
177177
@assert support[1] < support[2] "invalid support, not an interval"
178-
logf(x) = log(f(x) * float(support[1] <= x <= support[2]))
178+
logf(x) = log(f(x))
179179
objective = Objective(logf)
180180
x1, x2 = init
181181
@assert x1 < x2 "cutpoints must be ordered"
@@ -198,7 +198,8 @@ mutable struct RejectionSampler
198198
) = begin
199199
logf(x) = log(f(x))
200200
grad(x) = ForwardDiff.derivative(logf, x)
201-
grid = search_range[1]:δ:search_range[2]
201+
grid_lims = max(search_range[1], support[1]), min(search_range[2], support[2])
202+
grid = grid_lims[1]:δ:grid_lims[2]
202203
i1, i2 = findfirst(grad.(grid) .> 0.), findfirst(grad.(grid) .< 0.)
203204
@assert (i1 > 0) && (i2 > 0) "couldn't find initial points, please provide them or change `search_range`"
204205
x1, x2 = grid[i1], grid[i2]
@@ -233,5 +234,4 @@ function run_sampler!(s::RejectionSampler, n::Int)
233234
end
234235
out
235236
end
236-
237237
end #

0 commit comments

Comments
 (0)