@@ -12,43 +12,62 @@ This package is useful for efficient sampling from log-concave univariate densit
1212## Examples
1313
1414``` julia
15- using AdaptiveRejectionSampling
16- using Plots
15+ using AdaptiveRejectionSampling: Objective, ARSampler, sample!, eval_hull, abscissae
16+ using SpecialFunctions: gamma
17+ using CairoMakie
1718```
1819
19-
2020### Sampling from a shifted normal distribution
2121
2222
2323``` julia
24- # Define function to be sampled
25- μ, σ = 1.0 , 2.0
26- f (x) = exp (- 0.5 (x - μ)^ 2 / σ^ 2 ) / sqrt (2pi * σ^ 2 )
27- support = (- Inf , Inf )
24+ const μ, σ = 1.0 , 2.0
2825
29- # Build the sampler and simulate 10,000 samples
30- sampler = RejectionSampler (f, support, max_segments = 5 )
31- @time sim = run_sampler! (sampler, 10000 );
26+ function bench ()
27+ # Define function to be sampled
28+ f (x) = log (exp (- 0.5 (x - μ)^ 2 / σ^ 2 ) / sqrt (2pi * σ^ 2 ))
29+ support = (- 10. , 10. )
30+
31+ # Build the sampler and simulate 10,000 samples
32+ obj = Objective (f)
33+ sampler = ARSampler (obj, support)
34+ b = @be deepcopy (sampler) sample! (_, 10000 , true , 10 );
35+ return sampler, b
36+ end
37+ s, b = bench ();
38+ b
3239```
3340
34- 0.010434 seconds (192.15 k allocations: 3.173 MiB)
35-
41+ Benchmark: 126 samples with 1 evaluation
42+ min 616.951 μs (17 allocs: 80.523 KiB)
43+ median 641.532 μs (17 allocs: 80.523 KiB)
44+ mean 749.812 μs (17.10 allocs: 80.625 KiB, 0.75% gc time)
45+ max 13.744 ms (20 allocs: 83.711 KiB, 94.06% gc time)
3646
3747Let's verify the result
3848
3949
4050``` julia
4151# Plot the results and compare to target distribution
52+ f (x) = log (exp (- 0.5 (x - μ)^ 2 / σ^ 2 ) / sqrt (2pi * σ^ 2 ))
4253x = range (- 10.0 , 10.0 , length= 100 )
43- envelop = [eval_envelop (sampler. envelop, xi) for xi in x]
44- target = [f (xi) for xi in x]
45-
46- histogram (sim, normalize = true , label = " Histogram" )
47- plot! (x, [target envelop], width = 2 , label = [" Normal(μ, σ)" " Envelop" ])
54+ envelop = eval_hull .(s. upper_hull, x)
55+ target = f .(x)
56+ samples = sample! (s, 10000 , max_segments = 5 )
57+
58+ fig, ax, p = hist (samples, bins= 25 , normalization = :pdf , label = " Samples" );
59+ lines! (ax, - 10 .. 10 , x -> exp (f (x)), label = " Target" , color = :orange )
60+ lines! (ax, x, exp .(envelop), label = " Envelop" , color = :black , alpha = 0.8 )
61+ absc = ARS. abscissae (s. upper_hull)
62+ scatter! (ax, absc, exp .(eval_hull .(s. upper_hull, absc)), label = " Abscissae" , color = :red )
63+ axislegend (ax)
64+ fig
65+ # histogram(sim, normalize = true, label = "Histogram")
66+ # plot!(x, [target envelop], width = 2, label = ["Normal(μ, σ)" "Envelop"])
4867```
4968
5069
51- ![ ] ( img/example1.png )
70+ ![ ] ( img/example1.svg )
5271
5372
5473### Let's try a Gamma
@@ -59,24 +78,28 @@ plot!(x, [target envelop], width = 2, label = ["Normal(μ, σ)" "Envelop"])
5978f (x) = β^ α * x^ (α- 1 ) * exp (- β* x) / gamma (α)
6079support = (0.0 , Inf )
6180
62- # Build the sampler and simulate 10,000 samples
63- sampler = RejectionSampler (f, support)
64- @time sim = run_sampler! (sampler, 10000 )
65-
66- # Plot the results and compare to target distribution
67- x = range (0.0 , 5.0 , length= 100 )
68- envelop = [eval_envelop (sampler. envelop, xi) for xi in x]
69- target = [f (xi) for xi in x]
70-
71- histogram (sim, normalize = true , label = " Histogram" )
72- plot! (x, [target envelop], width = 2 , label = [" Gamma(α, β)" " Envelop" ])
81+ obj = Objective (x -> log (f (x)))
82+ sam = ARSampler (obj, support)
83+ @time sim = sample! (sam, 10000 , max_segments = 5 )
84+
85+ # Verify result
86+ x = range (0.0 , 8.0 , length= 100 )
87+ envelop = eval_hull .(sam. upper_hull, x)
88+ target = f .(x)
89+ mx = maximum (sim)
90+ fig, ax, p = hist (sim, bins= 50 , normalization = :pdf , label = " Samples" );
91+ lines! (ax, 0 .. mx, y -> f (y), label = " Target" , color = :orange )
92+ lines! (ax, 0 .. mx, y -> exp (eval_hull (sam. upper_hull, y)), label = " Envelop" , color = :black , alpha = 0.8 )
93+ absc = abscissae (sam. upper_hull)
94+ scatter! (ax, absc, exp .(eval_hull .(sam. upper_hull, absc)), label = " Abscissae" , color = :red )
95+ axislegend (ax)
96+ fig
7397```
7498
75- 0.007299 seconds (182.00 k allocations: 3.027 MiB)
76-
99+ 0.000772 seconds (100 allocations: 85.211 KiB)
77100
78101
79- ![ ] ( img/example2.png )
102+ ![ ] ( img/example2.svg )
80103
81104### Truncated distributions and unknown normalization constant
82105
0 commit comments