Skip to content

Commit c00c251

Browse files
author
mauriciogtec
committed
documentation
1 parent d19f924 commit c00c251

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

src/AdaptiveRejectionSampling.jl

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
"""
3-
A log conconcave function is majorized with a piecewise envelop, which on the original scale is piecewise exponential. As the resulting extremely precise envelop adapts, the rejection rate dramatically decreases.
3+
A log conconcave function is majorized with a piecewise envelop, which on the original scale is piecewise
4+
exponential. As the resulting extremely precise envelop adapts, the rejection rate dramatically decreases.
45
"""
56
module AdaptiveRejectionSampling
67
# ------------------------------
@@ -42,7 +43,10 @@ end
4243

4344
"""
4445
Envelop(lines::Vector{Line}, support::Tuple{Float64, Float64})
45-
A piecewise linear function with k segments defined by the lines `L_1, ..., L_k` and cutpoints `c_1, ..., c_k+1` with `c1 = support[1]` and `c2 = support[2]`. A line L_k is active in the segment [c_k, c_k+1], and it's assigned a weight w_k based on [exp_integral](@ref). The weighted integral over c_1 to c_k+1 is one, so that the envelop is interpreted as a density.
46+
A piecewise linear function with k segments defined by the lines `L_1, ..., L_k` and cutpoints
47+
`c_1, ..., c_k+1` with `c1 = support[1]` and `c2 = support[2]`. A line L_k is active in the segment
48+
[c_k, c_k+1], and it's assigned a weight w_k based on [exp_integral](@ref). The weighted integral
49+
over c_1 to c_k+1 is one, so that the envelop is interpreted as a density.
4650
"""
4751
mutable struct Envelop
4852
lines::Vector{Line}
@@ -65,7 +69,9 @@ end
6569

6670
"""
6771
add_line_segment!(e::Envelop, l::Line)
68-
Adds a new line segment to an envelop based on the value of its slope (slopes must be decreasing always in the envelop). The cutpoints are automatically determined by intersecting the line with the adjacent lines.
72+
Adds a new line segment to an envelop based on the value of its slope (slopes must be decreasing
73+
always in the envelop). The cutpoints are automatically determined by intersecting the line with
74+
the adjacent lines.
6975
"""
7076
function add_line_segment!(e::Envelop, l::Line)
7177
# Find the position in sorted array with binary search
@@ -92,7 +98,8 @@ end
9298

9399
"""
94100
sample(p::Envelop, n::Int)
95-
Samples `n` elements iid from the density defined by the envelop `e` with it's exponential weights. See [`Envelop`](@ref) for details.
101+
Samples `n` elements iid from the density defined by the envelop `e` with it's exponential weights.
102+
See [`Envelop`](@ref) for details.
96103
"""
97104
function sample(e::Envelop, n::Int)
98105
# Randomly select lines based on envelop weights
@@ -107,7 +114,8 @@ end
107114

108115
"""
109116
eval_envelop(e::Envelop, x::Float64)
110-
Eval point a point `x` in the piecewise linear function defined by `e`. Necessary for evaluating the density assigned to the point `x`.
117+
Eval point a point `x` in the piecewise linear function defined by `e`. Necessary for evaluating
118+
the density assigned to the point `x`.
111119
"""
112120
function eval_envelop(e::Envelop, x::Float64)
113121
# searchsortedfirst is the proper method for and ordered list
@@ -122,7 +130,8 @@ end
122130
"""
123131
Objective(logf::Function, support:)
124132
Objective(logf::Function, grad::Function)
125-
Convenient structure to store the objective function to be sampled. It must receive the logarithm of f and not f directly. It uses automatic differentiation by default, but the user can provide the derivative optionally.
133+
Convenient structure to store the objective function to be sampled. It must receive the logarithm of
134+
f and not f directly. It uses automatic differentiation by default, but the user can provide the derivative optionally.
126135
"""
127136
struct Objective
128137
logf::Function
@@ -138,13 +147,18 @@ end
138147
"""
139148
RejectionSampler(f::Function, support::Tuple{Float64, Float64}[ ,δ::Float64])
140149
RejectionSampler(f::Function, support::Tuple{Float64, Float64}, init::Tuple{Float64, Float64})
141-
An adaptive rejection sampler to obtain iid samples from a logconcave function `f`, supported in the domain `support` = (support[1], support[2]). To create the object, two initial points `init = init[1], init[2]` such that `loff'(init[1]) > 0` and `logf'(init[2]) < 0` are necessary. If they are not provided, the constructor will perform a greedy search based on `δ`.
150+
An adaptive rejection sampler to obtain iid samples from a logconcave function `f`, supported in the
151+
domain `support` = (support[1], support[2]). To create the object, two initial points `init = init[1], init[2]`
152+
such that `loff'(init[1]) > 0` and `logf'(init[2]) < 0` are necessary. If they are not provided, the constructor
153+
will perform a greedy search based on `δ`.
142154
143-
The argument `support` must be of the form `(-Inf, Inf), (-Inf, a), (b, Inf), (a,b)`, and it represent the interval in which f has positive value, and zero elsewhere.
155+
The argument `support` must be of the form `(-Inf, Inf), (-Inf, a), (b, Inf), (a,b)`, and it represent the
156+
interval in which f has positive value, and zero elsewhere.
144157
145158
## Keyword arguments
146159
- `max_segments::Int = 10` : max size of envelop, the rejection-rate is usually slow with a small number of segments
147-
- `max_failed_factor::Float64 = 0.001`: level at which throw an error if one single sample has a rejection rate exceeding this value
160+
- `max_failed_factor::Float64 = 0.001`: level at which throw an error if one single sample has a rejection rate
161+
exceeding this value
148162
"""
149163
mutable struct RejectionSampler
150164
objective::Objective

0 commit comments

Comments
 (0)