Skip to content

Commit

Permalink
Update GLOBAL_RNG to Random.default_rng()
Browse files Browse the repository at this point in the history
  • Loading branch information
penelopeysm committed Nov 5, 2024
1 parent 0272874 commit a6f0621
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
3 changes: 1 addition & 2 deletions src/AdvancedHMC.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ using Statistics: mean, var, middle
using LinearAlgebra:
Symmetric, UpperTriangular, mul!, ldiv!, dot, I, diag, cholesky, UniformScaling
using StatsFuns: logaddexp, logsumexp
import Random
using Random: GLOBAL_RNG, AbstractRNG
import Random: Random, AbstractRNG
using ProgressMeter: ProgressMeter
using SimpleUnPack: @unpack

Expand Down
12 changes: 6 additions & 6 deletions src/abstractmcmc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ A convenient wrapper around `AbstractMCMC.sample` avoiding explicit construction
"""

function AbstractMCMC.sample(
rng::Random.AbstractRNG,
rng::AbstractRNG,
model::AbstractMCMC.LogDensityModel,
sampler::AbstractHMCSampler,
N::Integer;
Expand Down Expand Up @@ -92,7 +92,7 @@ function AbstractMCMC.sample(
end

function AbstractMCMC.sample(
rng::Random.AbstractRNG,
rng::AbstractRNG,
model::AbstractMCMC.LogDensityModel,
sampler::AbstractHMCSampler,
parallel::AbstractMCMC.AbstractMCMCEnsemble,
Expand Down Expand Up @@ -318,7 +318,7 @@ end
#########

function make_step_size(
rng::Random.AbstractRNG,
rng::AbstractRNG,
spl::HMCSampler,
hamiltonian::Hamiltonian,
initial_params,
Expand All @@ -329,7 +329,7 @@ function make_step_size(
end

function make_step_size(
rng::Random.AbstractRNG,
rng::AbstractRNG,
spl::AbstractHMCSampler,
hamiltonian::Hamiltonian,
initial_params,
Expand All @@ -340,7 +340,7 @@ function make_step_size(
end

function make_step_size(
rng::Random.AbstractRNG,
rng::AbstractRNG,
integrator::AbstractIntegrator,
T::Type,
hamiltonian::Hamiltonian,
Expand All @@ -356,7 +356,7 @@ function make_step_size(
end

function make_step_size(
rng::Random.AbstractRNG,
rng::AbstractRNG,
integrator::Symbol,
T::Type,
hamiltonian::Hamiltonian,
Expand Down
4 changes: 2 additions & 2 deletions src/metric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ Base.rand(
kinetic::AbstractKinetic,
) = _rand(rng, metric, kinetic)
Base.rand(metric::AbstractMetric, kinetic::AbstractKinetic) =
rand(GLOBAL_RNG, metric, kinetic)
rand(Random.default_rng(), metric, kinetic)

# ignore θ by default unless defined by the specific kinetic (i.e. not position-dependent)
Base.rand(
Expand All @@ -154,4 +154,4 @@ Base.rand(
θ::AbstractVecOrMat,
) = rand(rng, metric, kinetic)
Base.rand(metric::AbstractMetric, kinetic::AbstractKinetic, θ::AbstractVecOrMat) =
rand(metric, kinetic)
rand(Random.default_rng(), metric, kinetic)
5 changes: 3 additions & 2 deletions src/sampler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ sample(
progress::Bool = false,
(pm_next!)::Function = pm_next!,
) = sample(
GLOBAL_RNG,
Random.default_rng(),
h,
κ,
θ,
Expand Down Expand Up @@ -146,7 +146,7 @@ sample(
)
Sample `n_samples` samples using the proposal `κ` under Hamiltonian `h`.
- The randomness is controlled by `rng`.
- If `rng` is not provided, `GLOBAL_RNG` will be used.
- If `rng` is not provided, `Random.default_rng()` will be used.
- The initial point is given by `θ`.
- The adaptor is set by `adaptor`, for which the default is no adaptation.
- It will perform `n_adapts` steps of adaptation, for which the default is the minimum of `1_000` and 10% of `n_samples`
Expand Down Expand Up @@ -181,6 +181,7 @@ function sample(
nothing
time = @elapsed for i = 1:n_samples
# Make a transition
# i == 2 && error(κ.τ.integrator)
t = transition(rng, h, κ, t.z)
# Adapt h and κ; what mutable is the adaptor
tstat = stat(t)
Expand Down
8 changes: 4 additions & 4 deletions src/trajectory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
####
#### Developers' Notes
####
#### Not all functions that use `rng` require a fallback function with `GLOBAL_RNG`
#### Not all functions that use `rng` require a fallback function with `Random.default_rng()`
#### as default. In short, only those exported to other libries need such a fallback
#### function. Internal uses shall always use the explict `rng` version. (Kai Xu 6/Jul/19)

Expand Down Expand Up @@ -241,10 +241,10 @@ $(SIGNATURES)
Make a MCMC transition from phase point `z` using the trajectory `τ` under Hamiltonian `h`.
NOTE: This is a RNG-implicit fallback function for `transition(GLOBAL_RNG, τ, h, z)`
NOTE: This is a RNG-implicit fallback function for `transition(Random.default_rng(), τ, h, z)`
"""
function transition::Trajectory, h::Hamiltonian, z::PhasePoint)
return transition(GLOBAL_RNG, τ, h, z)
return transition(Random.default_rng(), τ, h, z)
end

###
Expand Down Expand Up @@ -834,7 +834,7 @@ function find_good_stepsize(
θ::AbstractVector{<:AbstractFloat};
max_n_iters::Int = 100,
)
return find_good_stepsize(GLOBAL_RNG, h, θ; max_n_iters = max_n_iters)
return find_good_stepsize(Random.default_rng(), h, θ; max_n_iters = max_n_iters)
end

"Perform MH acceptance based on energy, i.e. negative log probability."
Expand Down

0 comments on commit a6f0621

Please sign in to comment.