You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/AdaptiveRejectionSampling.jl
+23-9Lines changed: 23 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,7 @@
1
1
2
2
"""
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.
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.
46
50
"""
47
51
mutable struct Envelop
48
52
lines::Vector{Line}
@@ -65,7 +69,9 @@ end
65
69
66
70
"""
67
71
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.
69
75
"""
70
76
functionadd_line_segment!(e::Envelop, l::Line)
71
77
# Find the position in sorted array with binary search
@@ -92,7 +98,8 @@ end
92
98
93
99
"""
94
100
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.
96
103
"""
97
104
functionsample(e::Envelop, n::Int)
98
105
# Randomly select lines based on envelop weights
@@ -107,7 +114,8 @@ end
107
114
108
115
"""
109
116
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`.
111
119
"""
112
120
functioneval_envelop(e::Envelop, x::Float64)
113
121
# searchsortedfirst is the proper method for and ordered list
@@ -122,7 +130,8 @@ end
122
130
"""
123
131
Objective(logf::Function, support:)
124
132
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.
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 `δ`.
142
154
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.
144
157
145
158
## Keyword arguments
146
159
- `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
0 commit comments