From a6f06212e9c990947c7d409d8cdddba374a00af8 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Tue, 5 Nov 2024 01:13:23 +0000 Subject: [PATCH] Update GLOBAL_RNG to Random.default_rng() --- src/AdvancedHMC.jl | 3 +-- src/abstractmcmc.jl | 12 ++++++------ src/metric.jl | 4 ++-- src/sampler.jl | 5 +++-- src/trajectory.jl | 8 ++++---- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/AdvancedHMC.jl b/src/AdvancedHMC.jl index fcaab095..1895f8cd 100644 --- a/src/AdvancedHMC.jl +++ b/src/AdvancedHMC.jl @@ -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 diff --git a/src/abstractmcmc.jl b/src/abstractmcmc.jl index 40585909..c3a73778 100644 --- a/src/abstractmcmc.jl +++ b/src/abstractmcmc.jl @@ -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; @@ -92,7 +92,7 @@ function AbstractMCMC.sample( end function AbstractMCMC.sample( - rng::Random.AbstractRNG, + rng::AbstractRNG, model::AbstractMCMC.LogDensityModel, sampler::AbstractHMCSampler, parallel::AbstractMCMC.AbstractMCMCEnsemble, @@ -318,7 +318,7 @@ end ######### function make_step_size( - rng::Random.AbstractRNG, + rng::AbstractRNG, spl::HMCSampler, hamiltonian::Hamiltonian, initial_params, @@ -329,7 +329,7 @@ function make_step_size( end function make_step_size( - rng::Random.AbstractRNG, + rng::AbstractRNG, spl::AbstractHMCSampler, hamiltonian::Hamiltonian, initial_params, @@ -340,7 +340,7 @@ function make_step_size( end function make_step_size( - rng::Random.AbstractRNG, + rng::AbstractRNG, integrator::AbstractIntegrator, T::Type, hamiltonian::Hamiltonian, @@ -356,7 +356,7 @@ function make_step_size( end function make_step_size( - rng::Random.AbstractRNG, + rng::AbstractRNG, integrator::Symbol, T::Type, hamiltonian::Hamiltonian, diff --git a/src/metric.jl b/src/metric.jl index 2afbd629..017f5ac5 100644 --- a/src/metric.jl +++ b/src/metric.jl @@ -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( @@ -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) diff --git a/src/sampler.jl b/src/sampler.jl index 7d1b7eb5..0d898719 100644 --- a/src/sampler.jl +++ b/src/sampler.jl @@ -118,7 +118,7 @@ sample( progress::Bool = false, (pm_next!)::Function = pm_next!, ) = sample( - GLOBAL_RNG, + Random.default_rng(), h, κ, θ, @@ -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` @@ -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) diff --git a/src/trajectory.jl b/src/trajectory.jl index 4b17422f..56aedd1a 100644 --- a/src/trajectory.jl +++ b/src/trajectory.jl @@ -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) @@ -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 ### @@ -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."