Skip to content

Merge ARSampling.jl (JuliaRegistries/General/151430)#26

Merged
Eliassj merged 9 commits into
JuliaStats:masterfrom
Eliassj:master
Apr 19, 2026
Merged

Merge ARSampling.jl (JuliaRegistries/General/151430)#26
Eliassj merged 9 commits into
JuliaStats:masterfrom
Eliassj:master

Conversation

@Eliassj

@Eliassj Eliassj commented Apr 10, 2026

Copy link
Copy Markdown
Member

For background see JuliaRegistries/General/151430

This pull request merges a redesign of both the implementation and API of sampling which aims to:

  • Reduce allocations (sampling is now allocation-free, adding segments is allocation-free for the first 50).
  • Significantly improve sampling speed.
  • Allow types other than Float64 (such as BigFloat etc).
  • Allow other AD backends through DifferentiationInterface.jl.

Changes to public API:

  • Target distribution and its gradient is specified through the new Objective struct which is then passed to the sampler. It allows specifiyng a manual gradient or automatically through an AD backend of choice with the default being ForwardDiff.jl. Target functions are now expected to be in their log form when passed to Objective.
  • The sampler is created mostly like before with some changes in the expected types of arguments.
  • Maximum number of segments is specified when drawing samples instead of at sampler creation.
  • As it currently stands the sampler is named ARSampler instead of RejectionSampler and the sampling function is named sample! instead of run_sampler!.

Example of new interface

For more examples see: https://eliassj.github.io/ARSampling.jl/dev/

using AdaptiveRejectionSampling: Objective, ARSampler, sample!
using DifferentiationInterface, Enzyme
using Chairmarks

# Sampling from a Beta distribution truncated to 0.5--1.0
const alpha::Float64 = 2.5
const beta::Float64 = 5.0

f(x) = x^(alpha - 1) * (1 - x)^(beta - 1)
f_log(x) = log(f(x))
const obj = Objective(f_log; adbacked = AutoEnzyme())
const sam_bounded = ARSampler(obj, (0.5, 1.0), [0.8])

@time samples_bounded = sample!(sam_bounded, 100000; max_segments = 10)

TODO

  • Finish updating README and benchmarks.
  • Rename the sampler / sampling function or align it with the rand-interface.

@Eliassj Eliassj requested a review from mauriciogtec April 11, 2026 17:03
@Eliassj

Eliassj commented Apr 15, 2026

Copy link
Copy Markdown
Member Author

@mauriciogtec is it ok if I merge this?

Comment thread test/runtests.jl


@testset "Truncated" begin
f_gamma(x, α, β) = β^α * x^(α - 1) * exp(-β * x) / gamma(α)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: f_gamma does not seem to be used?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will fix!

@mauriciogtec

Copy link
Copy Markdown
Collaborator

@Eliassj you can merge. Should we bump up the version?

@Eliassj

Eliassj commented Apr 19, 2026

Copy link
Copy Markdown
Member Author

@mauriciogtec Thanks! I'll just finish updating the README and check that the CI / docs is working and we should be ready to bump. What is your stance on moving the package to StatsJulia?

@Eliassj Eliassj merged commit 385f824 into JuliaStats:master Apr 19, 2026
1 check failed
@mauriciogtec

Copy link
Copy Markdown
Collaborator

I support the transfer. What are the steps?

@Eliassj

Eliassj commented Apr 23, 2026

Copy link
Copy Markdown
Member Author

I believe we contact someone in the organization @DilumAluthge? (JuliaRegistries/General#151430 (comment))

@DilumAluthge

Copy link
Copy Markdown
Member

Sure, I'm happy to help transfer this repo to JuliaStats. The process is this:

  1. The owner of this repo (mauriciogtec) transfers this repo to me (DilumAluthge).
  2. I transfer this repo to JuliaStats.
  3. I restore @mauriciogtec's admin permissions on the repo.
  4. I can then add anyone else that needs access to the repo.

@mauriciogtec As a first step, can you transfer this repo to me?

@mauriciogtec

Copy link
Copy Markdown
Collaborator

@DilumAluthge I have made transfer request.

@DilumAluthge

Copy link
Copy Markdown
Member
  1. I have transferred the repo to JuliaStats.
  2. I have restored @mauriciogtec's admin permissions on the repo.

@mauriciogtec Do you want me to give anyone else admin (or maintain, or write/commit) permissions on the repo?

@mauriciogtec

Copy link
Copy Markdown
Collaborator

@DilumAluthge Please also give admin permissions to @Eliassj

@DilumAluthge

Copy link
Copy Markdown
Member

@Eliassj I've given you admin permissions.

@DilumAluthge

Copy link
Copy Markdown
Member

The last piece we need is to update the repo URL in the registry: JuliaRegistries/General#154331

@Eliassj

Eliassj commented Apr 29, 2026

Copy link
Copy Markdown
Member Author

@DilumAluthge Is there anything organization-specific one should be aware of (such as style of docs/how these should be deployed) when maintaining a package in JuliaStats?

@DilumAluthge

Copy link
Copy Markdown
Member

I'm not aware of any styles we enforce at the organization-wide level, I think most things we defer to the maintainer of each package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants