Skip to content

Commit a28b21a

Browse files
committed
tweak default max proposal limit and improve max proposal error msg
1 parent 48059bc commit a28b21a

File tree

7 files changed

+24
-18
lines changed

7 files changed

+24
-18
lines changed

src/SliceSampling.jl

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ export sample, MCMCThreads, MCMCDistributed, MCMCSerial
2020
# Interfaces
2121
abstract type AbstractSliceSampling <: AbstractMCMC.AbstractSampler end
2222

23+
const DEFAULT_MAX_PROPOSALS = 10_000
24+
2325
"""
2426
struct Transition
2527
@@ -52,10 +54,9 @@ Return the initial sample for the `model` using the random number generator `rng
5254
"""
5355
function initial_sample(::Random.AbstractRNG, ::Any)
5456
error(
55-
"`initial_sample` is not implemented but an initialization wasn't provided. " *
57+
"`initial_sample` is not implemented but an initialization wasn't provided. ",
5658
"Consider supplying an initialization to `initial_params`."
5759
)
58-
println("fuck!!!")
5960
end
6061

6162
# If target is from `LogDensityProblemsAD`, unwrap target before calling `initial_sample`.
@@ -66,10 +67,15 @@ initial_sample(
6667
) = initial_sample(rng, parent(wrap))
6768

6869
function exceeded_max_prop(max_prop::Int)
69-
error("Exceeded maximum number of proposal $(max_prop).\n",
70-
"Here are possible causes:\n",
71-
"- The model might be broken or pathologic.\n",
72-
"- There might be a bug in the sampler.")
70+
error("Exceeded maximum number of proposal $(max_prop), ",
71+
"which indicates an acceptance rate less than $(1/max_prop*100)%. ",
72+
"A quick fix is to increase `max_prop`, ",
73+
"but an acceptance rate that is too low often indicates that there is a problem. ",
74+
"Here are some possible causes:\n",
75+
"- The model might be broken or degenerate. (most likely cause)\n",
76+
"- The initialization is pathologic. (try supplying a different `initial_params`)\n",
77+
"- There might be a bug in the sampler. (if this is suspected, file an issue to `SliceSampling`)\n"
78+
)
7379
end
7480

7581
## Univariate Slice Sampling Algorithms

src/multivariate/gibbspolar.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ Gibbsian polar slice sampling algorithm by P. Schär, M. Habeck, and D. Rudolf [
99
1010
# Keyword Arguments
1111
- `w::Real`: Initial window size for the radius shrinkage procedure
12-
- `max_proposals::Int`: Maximum number of proposals allowed until throwing an error (default: `typemax(Int)`).
12+
- `max_proposals::Int`: Maximum number of proposals allowed until throwing an error (default: `10^6`).
1313
"""
1414
struct GibbsPolarSlice{W <: Real} <: AbstractMultivariateSliceSampling
1515
w::W
1616
max_proposals::Int
1717
end
1818

19-
GibbsPolarSlice(w::Real; max_proposals::Int = typemax(Int)) = GibbsPolarSlice(w, max_proposals)
19+
GibbsPolarSlice(w::Real; max_proposals::Int = 10^6) = GibbsPolarSlice(w, max_proposals)
2020

2121
struct GibbsPolarSliceState{T <: Transition, R <: Real, D <: AbstractVector}
2222
"Current [`Transition`](@ref)."

src/multivariate/latent.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ Latent slice sampling algorithm by Li and Walker[^LW2023].
88
- `beta::Real`: Beta parameter of the Gamma distribution of the auxiliary variables.
99
1010
# Keyword Arguments
11-
- `max_proposals::Int`: Maximum number of proposals allowed until throwing an error (default: `typemax(Int)`).
11+
- `max_proposals::Int`: Maximum number of proposals allowed until throwing an error (default: `$(DEFAULT_MAX_PROPOSALS)`).
1212
"""
1313
struct LatentSlice{B <: Real} <: AbstractMultivariateSliceSampling
1414
beta ::B
1515
max_proposals::Int
1616
end
1717

18-
function LatentSlice(beta::Real; max_proposals::Int = typemax(Int))
18+
function LatentSlice(beta::Real; max_proposals::Int = DEFAULT_MAX_PROPOSALS)
1919
@assert beta > 0 "Beta must be strictly positive"
2020
LatentSlice(beta, max_proposals)
2121
end

src/univariate/doublingout.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Univariate slice sampling by automatically adapting the initial interval through
99
1010
# Keyword Arguments
1111
- `max_doubling_out`: Maximum number of "doubling outs" (default: 8).
12-
- `max_proposals::Int`: Maximum number of proposals allowed until throwing an error (default: `typemax(Int)`).
12+
- `max_proposals::Int`: Maximum number of proposals allowed until throwing an error (default: `$(DEFAULT_MAX_PROPOSALS)`).
1313
"""
1414
struct SliceDoublingOut{W <: Real} <: AbstractUnivariateSliceSampling
1515
window ::W
@@ -20,7 +20,7 @@ end
2020
function SliceDoublingOut(
2121
window ::Real;
2222
max_doubling_out::Int = 8,
23-
max_proposals ::Int = typemax(Int),
23+
max_proposals ::Int = DEFAULT_MAX_PROPOSALS,
2424
)
2525
@assert window > 0
2626
SliceDoublingOut(window, max_doubling_out, max_proposals)

src/univariate/fixedinterval.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Univariate slice sampling with a fixed initial interval (Scheme 2 by Neal[^N2003
88
- `window::Real`: Proposal window.
99
1010
# Keyword Arguments
11-
- `max_proposals::Int`: Maximum number of proposals allowed until throwing an error (default: `typemax(Int)`).
11+
- `max_proposals::Int`: Maximum number of proposals allowed until throwing an error (default: `$(DEFAULT_MAX_PROPOSALS)`).
1212
"""
1313
struct Slice{W <: Real} <: AbstractUnivariateSliceSampling
1414
window ::W
@@ -17,7 +17,7 @@ end
1717

1818
function Slice(
1919
window ::Real;
20-
max_proposals::Int = typemax(Int),
20+
max_proposals::Int = DEFAULT_MAX_PROPOSALS,
2121
)
2222
@assert window > 0
2323
Slice(window, max_proposals)

src/univariate/steppingout.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Univariate slice sampling by automatically adapting the initial interval through
99
1010
# Keyword Arguments
1111
- `max_stepping_out::Int`: Maximum number of "stepping outs" (default: 32).
12-
- `max_proposals::Int`: Maximum number of proposals allowed until throwing an error (default: `typemax(Int)`).
12+
- `max_proposals::Int`: Maximum number of proposals allowed until throwing an error (default: `$(DEFAULT_MAX_PROPOSALS)`).
1313
"""
1414
struct SliceSteppingOut{W <: Real} <: AbstractUnivariateSliceSampling
1515
window ::W
@@ -20,7 +20,7 @@ end
2020
function SliceSteppingOut(
2121
window ::Real;
2222
max_stepping_out::Int = 32,
23-
max_proposals ::Int = typemax(Int),
23+
max_proposals ::Int = DEFAULT_MAX_PROPOSALS,
2424
)
2525
@assert window > 0
2626
SliceSteppingOut(window, max_stepping_out, max_proposals)

test/multivariate.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ end
7878
LatentSlice(5),
7979

8080
# Gibbsian polar slice sampling
81-
GibbsPolarSlice(10),
81+
GibbsPolarSlice(100),
8282
]
8383
@testset "initial_params" begin
8484
model = MultiModel(1.0, 1.0, [0.0])
@@ -87,7 +87,7 @@ end
8787

8888
θ0 = [1.0, 0.1]
8989
chain = sample(
90-
model,
90+
model,
9191
sampler,
9292
10;
9393
initial_params=θ0,

0 commit comments

Comments
 (0)