Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions perf/mixtures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function current_master(d::AbstractMixtureModel, x)
p = probs(d)
@assert length(p) == K
v = 0.0
@inbounds for i in eachindex(p)
for i in eachindex(p)
pi = p[i]
if pi > 0.0
c = component(d, i)
Expand All @@ -28,7 +28,7 @@ function improved_version(d, x)
p = probs(d)
return sum(enumerate(p)) do (i, pi)
if pi > 0
@inbounds c = component(d, i)
c = component(d, i)
pdf(c, x) * pi
else
zero(eltype(p))
Expand Down Expand Up @@ -59,7 +59,7 @@ function forloop(d, x)
ps = probs(d)
cs = components(d)
s = zero(eltype(ps))
@inbounds for i in eachindex(ps)
for i in eachindex(ps)
if ps[i] > 0
s += ps[i] * pdf(cs[i], x)
end
Expand All @@ -70,13 +70,13 @@ end
function indexed_sum_comp(d, x)
ps = probs(d)
cs = components(d)
@inbounds sum(ps[i] * pdf(cs[i], x) for i in eachindex(ps) if ps[i] > 0)
sum(ps[i] * pdf(cs[i], x) for i in eachindex(ps) if ps[i] > 0)
end

function indexed_boolprod(d, x)
ps = probs(d)
cs = components(d)
@inbounds sum((ps[i] > 0) * (ps[i] * pdf(cs[i], x)) for i in eachindex(ps))
sum((ps[i] > 0) * (ps[i] * pdf(cs[i], x)) for i in eachindex(ps))
end

function indexed_boolprod_noinbound(d, x)
Expand All @@ -89,7 +89,7 @@ function sumcomp_cond(d, x)
ps = probs(d)
cs = components(d)
s = zero(eltype(ps))
@inbounds sum(ps[i] * pdf(cs[i], x) for i in eachindex(ps) if ps[i] > 0)
sum(ps[i] * pdf(cs[i], x) for i in eachindex(ps) if ps[i] > 0)
end

distributions = [
Expand Down
10 changes: 7 additions & 3 deletions perf/samplers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,13 @@ if haskey(ENV, "binomial") && ENV["binomial"] != "skip"
nvals = haskey(ENV, "CI") ? [2] : 2 .^ (1:12)
pvals = haskey(ENV, "CI") ? [0.3] : [0.3, 0.5, 0.9]
for n in nvals, p in pvals
s = ST(n, p)
b = @benchmark rand($mt, $s)
@info "(n,p): $((n,p)), result: $b"
try
s = ST(n, p)
b = @benchmark rand($mt, $s)
@info "(n,p): $((n,p)), result: $b"
catch e
@warn "Failed for (n,p): $((n,p)), error: $e"
end
end
end
end
Expand Down
10 changes: 5 additions & 5 deletions src/cholesky/lkjcholesky.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function insupport(d::LKJCholesky, R::LinearAlgebra.Cholesky)
(isreal(factors) && size(factors, 1) == p) || return false
iinds, jinds = axes(factors)
# check that the diagonal of U'*U or L*L' is all ones
@inbounds if R.uplo === 'U'
if R.uplo === 'U'
for (j, jind) in enumerate(jinds)
col_iinds = view(iinds, 1:j)
sum(abs2, view(factors, col_iinds, jind)) ≈ 1 || return false
Expand Down Expand Up @@ -247,12 +247,12 @@ function _lkj_cholesky_onion_tri!(
β = η + (d - 2)//2
# 1. Initialization
w0 = 2 * rand(rng, Beta(β, β)) - 1
@inbounds if uplo === :L
if uplo === :L
A[2, 1] = w0
else
A[1, 2] = w0
end
@inbounds A[2, 2] = sqrt(1 - w0^2)
A[2, 2] = sqrt(1 - w0^2)
# 2. Loop, each iteration k adds row/column k+1
for k in 2:(d - 1)
# (a)
Expand All @@ -261,11 +261,11 @@ function _lkj_cholesky_onion_tri!(
y = rand(rng, Beta(k//2, β))
# (c)-(e)
# w is directionally uniform vector of length √y
@inbounds w = @views uplo === :L ? A[k + 1, 1:k] : A[1:k, k + 1]
w = @views uplo === :L ? A[k + 1, 1:k] : A[1:k, k + 1]
Random.randn!(rng, w)
rmul!(w, sqrt(y) / norm(w))
# normalize so new row/column has unit norm
@inbounds A[k + 1, k + 1] = sqrt(1 - y)
A[k + 1, k + 1] = sqrt(1 - y)
end
# 3.
return A
Expand Down
12 changes: 6 additions & 6 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,12 @@ See also: [`logpdf`](@ref).
ntuple(i -> size(x, i), Val(N)) == size(d) ||
throw(DimensionMismatch("inconsistent array dimensions"))
end
return @inbounds map(Base.Fix1(pdf, d), eachvariate(x, variate_form(typeof(d))))
return map(Base.Fix1(pdf, d), eachvariate(x, variate_form(typeof(d))))
end
end

function _pdf(d::Distribution{ArrayLikeVariate{N}}, x::AbstractArray{<:Real,N}) where {N}
return exp(@inbounds logpdf(d, x))
return exp(logpdf(d, x))
end

"""
Expand Down Expand Up @@ -276,7 +276,7 @@ See also: [`pdf`](@ref), [`gradlogpdf`](@ref).
ntuple(i -> size(x, i), Val(N)) == size(d) ||
throw(DimensionMismatch("inconsistent array dimensions"))
end
return @inbounds map(Base.Fix1(logpdf, d), eachvariate(x, variate_form(typeof(d))))
return map(Base.Fix1(logpdf, d), eachvariate(x, variate_form(typeof(d))))
end
end

Expand Down Expand Up @@ -393,7 +393,7 @@ function _pdf!(
d::Distribution{<:ArrayLikeVariate},
x::AbstractArray{<:Real},
)
@inbounds logpdf!(out, d, x)
logpdf!(out, d, x)
map!(exp, out, out)
return out
end
Expand Down Expand Up @@ -443,7 +443,7 @@ function _logpdf!(
d::Distribution{<:ArrayLikeVariate},
x::AbstractArray{<:Real},
)
@inbounds map!(Base.Fix1(logpdf, d), out, eachvariate(x, variate_form(typeof(d))))
map!(Base.Fix1(logpdf, d), out, eachvariate(x, variate_form(typeof(d))))
return out
end

Expand Down Expand Up @@ -472,7 +472,7 @@ Base.@propagate_inbounds @inline function loglikelihood(
ntuple(i -> size(x, i), Val(N)) == size(d) ||
throw(DimensionMismatch("inconsistent array dimensions"))
end
return @inbounds sum(Base.Fix1(logpdf, d), eachvariate(x, ArrayLikeVariate{N}))
return sum(Base.Fix1(logpdf, d), eachvariate(x, ArrayLikeVariate{N}))
end
end
Base.@propagate_inbounds function loglikelihood(
Expand Down
20 changes: 10 additions & 10 deletions src/genericrand.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,38 +26,38 @@ rand(rng::AbstractRNG, s::Sampleable, dim1::Int, moredims::Int...) =

# default fallback (redefined for univariate distributions)
function rand(rng::AbstractRNG, s::Sampleable{<:ArrayLikeVariate})
return @inbounds rand!(rng, s, Array{eltype(s)}(undef, size(s)))
return rand!(rng, s, Array{eltype(s)}(undef, size(s)))
end

# multiple samples
function rand(rng::AbstractRNG, s::Sampleable{Univariate}, dims::Dims)
out = Array{eltype(s)}(undef, dims)
return @inbounds rand!(rng, sampler(s), out)
return rand!(rng, sampler(s), out)
end
function rand(
rng::AbstractRNG, s::Sampleable{<:ArrayLikeVariate}, dims::Dims,
)
sz = size(s)
ax = map(Base.OneTo, dims)
out = [Array{eltype(s)}(undef, sz) for _ in Iterators.product(ax...)]
return @inbounds rand!(rng, sampler(s), out, false)
return rand!(rng, sampler(s), out, false)
end

# these are workarounds for sampleables that incorrectly base `eltype` on the parameters
function rand(rng::AbstractRNG, s::Sampleable{<:ArrayLikeVariate,Continuous})
return @inbounds rand!(rng, sampler(s), Array{float(eltype(s))}(undef, size(s)))
return rand!(rng, sampler(s), Array{float(eltype(s))}(undef, size(s)))
end
function rand(rng::AbstractRNG, s::Sampleable{Univariate,Continuous}, dims::Dims)
out = Array{float(eltype(s))}(undef, dims)
return @inbounds rand!(rng, sampler(s), out)
return rand!(rng, sampler(s), out)
end
function rand(
rng::AbstractRNG, s::Sampleable{<:ArrayLikeVariate,Continuous}, dims::Dims,
)
sz = size(s)
ax = map(Base.OneTo, dims)
out = [Array{float(eltype(s))}(undef, sz) for _ in Iterators.product(ax...)]
return @inbounds rand!(rng, sampler(s), out, false)
return rand!(rng, sampler(s), out, false)
end

"""
Expand Down Expand Up @@ -113,7 +113,7 @@ function _rand!(
s::Sampleable{<:ArrayLikeVariate},
x::AbstractArray{<:Real},
)
@inbounds for xi in eachvariate(x, variate_form(typeof(s)))
for xi in eachvariate(x, variate_form(typeof(s)))
rand!(rng, s, xi)
end
return x
Expand All @@ -125,7 +125,7 @@ Base.@propagate_inbounds function rand!(
x::AbstractArray{<:AbstractArray{<:Real,N}},
) where {N}
sz = size(s)
allocate = !all(isassigned(x, i) && size(@inbounds x[i]) == sz for i in eachindex(x))
allocate = !all(isassigned(x, i) && size(x[i]) == sz for i in eachindex(x))
return rand!(rng, s, x, allocate)
end

Expand Down Expand Up @@ -160,11 +160,11 @@ function _rand!(
allocate::Bool,
) where {N}
if allocate
@inbounds for i in eachindex(x)
for i in eachindex(x)
x[i] = rand(rng, s)
end
else
@inbounds for xi in x
for xi in x
rand!(rng, s, xi)
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/matrix/wishart.jl
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ function _wishart_genA!(rng::AbstractRNG, A::AbstractMatrix, df::Real)
T = eltype(A)
z = zero(T)
axes1 = axes(A, 1)
@inbounds for (j, jdx) in enumerate(axes(A, 2)), (i, idx) in enumerate(axes1)
for (j, jdx) in enumerate(axes(A, 2)), (i, idx) in enumerate(axes1)
A[idx, jdx] = if i < j
z
elseif i > j
Expand Down
12 changes: 6 additions & 6 deletions src/mixtures/mixturemodel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ function _mixpdf!(r::AbstractArray, d::AbstractMixtureModel, x)
p = probs(d)
fill!(r, 0.0)
t = Array{eltype(p)}(undef, size(r))
@inbounds for i in eachindex(p)
for i in eachindex(p)
pi = p[i]
if pi > 0.0
if d isa UnivariateMixture
Expand All @@ -321,7 +321,7 @@ function _mixlogpdf!(r::AbstractArray, d::AbstractMixtureModel, x)
n = length(r)
Lp = Matrix{eltype(p)}(undef, n, K)
m = fill(-Inf, n)
@inbounds for i in eachindex(p)
for i in eachindex(p)
pi = p[i]
if pi > 0.0
lpri = log(pi)
Expand All @@ -346,7 +346,7 @@ function _mixlogpdf!(r::AbstractArray, d::AbstractMixtureModel, x)
end

fill!(r, 0.0)
@inbounds for i = 1:K
for i = 1:K
if p[i] > 0.0
lp_i = view(Lp, :, i)
for j = 1:n
Expand All @@ -355,7 +355,7 @@ function _mixlogpdf!(r::AbstractArray, d::AbstractMixtureModel, x)
end
end

@inbounds for j = 1:n
for j = 1:n
r[j] = log(r[j]) + m[j]
end
return r
Expand Down Expand Up @@ -479,9 +479,9 @@ rand(rng::AbstractRNG, d::MixtureModel{Univariate}) =

# multivariate mixture sampler for a vector
_rand!(rng::AbstractRNG, s::MixtureSampler{Multivariate}, x::AbstractVector{<:Real}) =
@inbounds rand!(rng, s.csamplers[rand(rng, s.psampler)], x)
rand!(rng, s.csamplers[rand(rng, s.psampler)], x)
# if only a single sample is requested, no alias table is created
_rand!(rng::AbstractRNG, d::MixtureModel{Multivariate}, x::AbstractVector{<:Real}) =
@inbounds rand!(rng, component(d, rand(rng, d.prior)), x)
rand!(rng, component(d, rand(rng, d.prior)), x)

sampler(d::MixtureModel) = MixtureSampler(d)
38 changes: 19 additions & 19 deletions src/multivariate/dirichlet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ function cov(d::Dirichlet)
αj = α[j]
αjc = αj * c
for i in 1:(j-1)
@inbounds C[i,j] = C[j,i]
C[i,j] = C[j,i]
end
@inbounds C[j,j] = (α0 - αj) * αjc
C[j,j] = (α0 - αj) * αjc
for i in (j+1):k
@inbounds C[i,j] = - α[i] * αjc
C[i,j] = - α[i] * αjc
end
end

Expand Down Expand Up @@ -158,7 +158,7 @@ function _rand!(rng::AbstractRNG,
d::Union{Dirichlet,DirichletCanon},
x::AbstractVector{<:Real})
for (i, αi) in zip(eachindex(x), d.alpha)
@inbounds x[i] = rand(rng, Gamma(αi))
x[i] = rand(rng, Gamma(αi))
end
lmul!(inv(sum(x)), x) # this returns x
end
Expand Down Expand Up @@ -193,7 +193,7 @@ function suffstats(::Type{<:Dirichlet}, P::AbstractMatrix{Float64})
slogp = zeros(K)
for i = 1:n
for k = 1:K
@inbounds slogp[k] += log(P[k,i])
slogp[k] += log(P[k,i])
end
end
DirichletStats(slogp, n)
Expand All @@ -211,10 +211,10 @@ function suffstats(::Type{<:Dirichlet}, P::AbstractMatrix{Float64},
slogp = zeros(K)

for i = 1:n
@inbounds wi = w[i]
wi = w[i]
tw += wi
for k = 1:K
@inbounds slogp[k] += log(P[k,i]) * wi
slogp[k] += log(P[k,i]) * wi
end
end
DirichletStats(slogp, tw)
Expand All @@ -229,8 +229,8 @@ function _dirichlet_mle_init2(μ::Vector{Float64}, γ::Vector{Float64})

α0 = 0.
for k = 1:K
@inbounds μk = μ[k]
@inbounds γk = γ[k]
μk = μ[k]
γk = γ[k]
ak = (μk - γk) / (γk - μk * μk)
α0 += ak
end
Expand Down Expand Up @@ -262,12 +262,12 @@ function dirichlet_mle_init(P::AbstractMatrix{Float64}, w::AbstractArray{Float64
tw = 0.0

for i in 1:n
@inbounds wi = w[i]
wi = w[i]
tw += wi
for k in 1:K
pk = P[k, i]
@inbounds μ[k] += pk * wi
@inbounds γ[k] += pk * pk * wi
μ[k] += pk * wi
γ[k] += pk * pk * wi
end
end

Expand Down Expand Up @@ -310,12 +310,12 @@ function fit_dirichlet!(elogp::Vector{Float64}, α::Vector{Float64};
iqs = 0.

for k = 1:K
@inbounds ak = α[k]
@inbounds g[k] = gk = digam_α0 - digamma(ak) + elogp[k]
@inbounds iq[k] = - 1.0 / trigamma(ak)
ak = α[k]
g[k] = gk = digam_α0 - digamma(ak) + elogp[k]
iq[k] = - 1.0 / trigamma(ak)

@inbounds b += gk * iq[k]
@inbounds iqs += iq[k]
b += gk * iq[k]
iqs += iq[k]

agk = abs(gk)
if agk > gnorm
Expand All @@ -327,8 +327,8 @@ function fit_dirichlet!(elogp::Vector{Float64}, α::Vector{Float64};
# update α

for k = 1:K
@inbounds α[k] -= (g[k] - b) * iq[k]
@inbounds if α[k] < 1.0e-12
α[k] -= (g[k] - b) * iq[k]
if α[k] < 1.0e-12
α[k] = 1.0e-12
end
end
Expand Down
Loading
Loading