Skip to content

Commit f87a5a6

Browse files
author
Closed-Limelike-Curves
committed
Aligned names of ess functions
1 parent 765055f commit f87a5a6

File tree

4 files changed

+67
-11
lines changed

4 files changed

+67
-11
lines changed

.lh/src/ESS.jl.json

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"activeCommit": 0,
44
"commits": [
55
{
6-
"activePatchIndex": 4,
6+
"activePatchIndex": 11,
77
"patches": [
88
{
99
"date": 1626309971342,
@@ -24,6 +24,34 @@
2424
{
2525
"date": 1626314567863,
2626
"content": "Index: \n===================================================================\n--- \n+++ \n@@ -1,8 +1,7 @@\n using FFTW\n using MCMCDiagnosticTools\n using LoopVectorization\n-using TensorOperations\n using Tullio\n \n export relative_eff, psis_n_eff\n \n"
27+
},
28+
{
29+
"date": 1626454088792,
30+
"content": "Index: \n===================================================================\n--- \n+++ \n@@ -2,14 +2,15 @@\n using MCMCDiagnosticTools\n using LoopVectorization\n using Tullio\n \n-export relative_eff, psis_n_eff\n+export relative_eff, psis_ess\n \n \"\"\"\n relative_eff(sample::AbstractArray{AbstractFloat, 3}; method=FFTESSMethod())\n \n-Compute the MCMC effective sample size divided by the nominal sample size.\n+Calculate the relative efficiency of an MCMC chain, i.e. the effective sample size divided\n+by the nominal sample size.\n \"\"\"\n function relative_eff(\n sample::AbstractArray{T,3}; method=FFTESSMethod()\n ) where {T<:AbstractFloat}\n@@ -22,28 +23,37 @@\n return r_eff\n end\n \n \"\"\"\n- function psis_n_eff(\n+ function psis_ess(\n weights::AbstractVector{T},\n r_eff::AbstractVector{T}\n ) -> AbstractVector{T}\n+\n+Calculate the (approximate) effective sample size of a PSIS sample, using the correction in\n+Vehtari et al. 2019.\n+\n+# Arguments\n+\n+- `weights`: A set of importance sampling weights derived from PSIS.\n+- `r_eff`: The relative efficiency of the MCMC chains from which PSIS samples were derived.\n+See `?relative_eff` to calculate `r_eff`.\n \"\"\"\n-function psis_n_eff(\n+function psis_ess(\n weights::AbstractVector{T}, r_eff::AbstractVector{T}\n ) where {T<:AbstractFloat}\n @tullio sum_of_squares := weights[x]^2\n return r_eff ./ sum_of_squares\n end\n \n-function psis_n_eff(\n+function psis_ess(\n weights::AbstractMatrix{T}, r_eff::AbstractVector{T}\n ) where {T<:AbstractFloat}\n @tullio sum_of_squares[x] := weights[x, y]^2\n return @tturbo r_eff ./ sum_of_squares\n end\n \n-function psis_n_eff(weights::AbstractArray{T}) where {T<:AbstractFloat}\n+function psis_ess(weights::AbstractArray{T}) where {T<:AbstractFloat}\n @warn \"PSIS ESS not adjusted based on MCMC ESS. MCSE and ESS estimates \" *\n \"will be overoptimistic if samples are autocorrelated.\"\n- return psis_n_eff(weights, ones(size(weights)))\n+ return psis_ess(weights, ones(size(weights)))\n end\n"
31+
},
32+
{
33+
"date": 1626454315198,
34+
"content": "Index: \n===================================================================\n--- \n+++ \n@@ -56,4 +56,8 @@\n @warn \"PSIS ESS not adjusted based on MCMC ESS. MCSE and ESS estimates \" *\n \"will be overoptimistic if samples are autocorrelated.\"\n return psis_ess(weights, ones(size(weights)))\n end\n+\n+function psis_n_eff(args...) \n+ psis_ess\n+end\n\\ No newline at end of file\n"
35+
},
36+
{
37+
"date": 1626454380380,
38+
"content": "Index: \n===================================================================\n--- \n+++ \n@@ -2,9 +2,9 @@\n using MCMCDiagnosticTools\n using LoopVectorization\n using Tullio\n \n-export relative_eff, psis_ess\n+export relative_eff, psis_ess, psis_n_eff\n \n \"\"\"\n relative_eff(sample::AbstractArray{AbstractFloat, 3}; method=FFTESSMethod())\n \n"
39+
},
40+
{
41+
"date": 1626454430031,
42+
"content": "Index: \n===================================================================\n--- \n+++ \n@@ -51,13 +51,13 @@\n @tullio sum_of_squares[x] := weights[x, y]^2\n return @tturbo r_eff ./ sum_of_squares\n end\n \n-function psis_ess(weights::AbstractArray{T}) where {T<:AbstractFloat}\n+function psis_ess(weights::AbstractMatrix{T}) where {T<:AbstractFloat}\n @warn \"PSIS ESS not adjusted based on MCMC ESS. MCSE and ESS estimates \" *\n \"will be overoptimistic if samples are autocorrelated.\"\n return psis_ess(weights, ones(size(weights)))\n end\n \n-function psis_n_eff(args...) \n- psis_ess\n+function psis_n_eff(args...; kwargs...) \n+ return psis_ess(args...; kwargs...)\n end\n\\ No newline at end of file\n"
43+
},
44+
{
45+
"date": 1626454496697,
46+
"content": "Index: \n===================================================================\n--- \n+++ \n@@ -57,7 +57,7 @@\n \"will be overoptimistic if samples are autocorrelated.\"\n return psis_ess(weights, ones(size(weights)))\n end\n \n-function psis_n_eff(args...; kwargs...) \n+function psis_n_eff(args...; kwargs...) # Alias for compatibility with R version\n return psis_ess(args...; kwargs...)\n end\n\\ No newline at end of file\n"
47+
},
48+
{
49+
"date": 1626454514402,
50+
"content": "Index: \n===================================================================\n--- \n+++ \n@@ -2,9 +2,9 @@\n using MCMCDiagnosticTools\n using LoopVectorization\n using Tullio\n \n-export relative_eff, psis_ess, psis_n_eff\n+export relative_eff, psis_ess\n \n \"\"\"\n relative_eff(sample::AbstractArray{AbstractFloat, 3}; method=FFTESSMethod())\n \n@@ -56,8 +56,4 @@\n @warn \"PSIS ESS not adjusted based on MCMC ESS. MCSE and ESS estimates \" *\n \"will be overoptimistic if samples are autocorrelated.\"\n return psis_ess(weights, ones(size(weights)))\n end\n-\n-function psis_n_eff(args...; kwargs...) # Alias for compatibility with R version\n- return psis_ess(args...; kwargs...)\n-end\n\\ No newline at end of file\n"
51+
},
52+
{
53+
"date": 1626454525488,
54+
"content": "Index: \n===================================================================\n--- \n+++ \n@@ -2,9 +2,9 @@\n using MCMCDiagnosticTools\n using LoopVectorization\n using Tullio\n \n-export relative_eff, psis_ess\n+export relative_eff, psis_ess, psis_n_eff\n \n \"\"\"\n relative_eff(sample::AbstractArray{AbstractFloat, 3}; method=FFTESSMethod())\n \n@@ -56,4 +56,8 @@\n @warn \"PSIS ESS not adjusted based on MCMC ESS. MCSE and ESS estimates \" *\n \"will be overoptimistic if samples are autocorrelated.\"\n return psis_ess(weights, ones(size(weights)))\n end\n+\n+function psis_n_eff(args...; kwargs...) # Alias for compatibility with R version\n+ return psis_ess(args...; kwargs...)\n+end\n\\ No newline at end of file\n"
2755
}
2856
],
2957
"date": 1626309971342,

.lh/src/ImportanceSampling.jl.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"activeCommit": 0,
44
"commits": [
55
{
6-
"activePatchIndex": 33,
6+
"activePatchIndex": 36,
77
"patches": [
88
{
99
"date": 1626309654641,
@@ -140,6 +140,18 @@
140140
{
141141
"date": 1626452759502,
142142
"content": "Index: \n===================================================================\n--- \n+++ \n@@ -1,6 +1,5 @@\n using LoopVectorization\n-using Polyester\n using Tullio\n \n \n const LIKELY_ERROR_CAUSES = \"\"\"\n"
143+
},
144+
{
145+
"date": 1626453064337,
146+
"content": "Index: \n===================================================================\n--- \n+++ \n@@ -105,9 +105,9 @@\n end\n \n \n \"\"\"\n- do_psis_i!(is_ratios::AbstractVector{AbstractFloat}, tail_length::Integer)::T\n+ do_psis_i!(is_ratios::AbstractVector{AbstractFloat}, tail_length::Integer) -> T\n \n Do PSIS on a single vector, smoothing its tail values.\n \n # Arguments\n"
147+
},
148+
{
149+
"date": 1626453089719,
150+
"content": "Index: \n===================================================================\n--- \n+++ \n@@ -242,8 +242,9 @@\n return r_eff\n end\n end\n \n+\n \"\"\"\n Make sure all inputs to `psis` are valid.\n \"\"\"\n function check_input_validity_psis(\n@@ -266,8 +267,9 @@\n end\n return nothing\n end\n \n+\n \"\"\"\n Check the tail to make sure a GPD fit is possible.\n \"\"\"\n function check_tail(tail::AbstractVector{T}) where {T<:AbstractFloat}\n"
151+
},
152+
{
153+
"date": 1626454092733,
154+
"content": "Index: \n===================================================================\n--- \n+++ \n@@ -71,9 +71,9 @@\n end\n \n @tullio norm_const[i] := weights[i, j]\n weights .= weights ./ norm_const\n- ess = psis_n_eff(weights, r_eff)\n+ ess = psis_ess(weights, r_eff)\n \n weights = reshape(weights, dims)\n \n if log_weights\n"
143155
}
144156
],
145157
"date": 1626309654640,

src/ESS.jl

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ using MCMCDiagnosticTools
33
using LoopVectorization
44
using Tullio
55

6-
export relative_eff, psis_n_eff
6+
export relative_eff, psis_ess, psis_n_eff
77

88
"""
99
relative_eff(sample::AbstractArray{AbstractFloat, 3}; method=FFTESSMethod())
1010
11-
Compute the MCMC effective sample size divided by the nominal sample size.
11+
Calculate the relative efficiency of an MCMC chain, i.e. the effective sample size divided
12+
by the nominal sample size.
1213
"""
1314
function relative_eff(
1415
sample::AbstractArray{T,3}; method=FFTESSMethod()
@@ -23,27 +24,40 @@ function relative_eff(
2324
end
2425

2526
"""
26-
function psis_n_eff(
27+
function psis_ess(
2728
weights::AbstractVector{T},
2829
r_eff::AbstractVector{T}
2930
) -> AbstractVector{T}
31+
32+
Calculate the (approximate) effective sample size of a PSIS sample, using the correction in
33+
Vehtari et al. 2019.
34+
35+
# Arguments
36+
37+
- `weights`: A set of importance sampling weights derived from PSIS.
38+
- `r_eff`: The relative efficiency of the MCMC chains from which PSIS samples were derived.
39+
See `?relative_eff` to calculate `r_eff`.
3040
"""
31-
function psis_n_eff(
41+
function psis_ess(
3242
weights::AbstractVector{T}, r_eff::AbstractVector{T}
3343
) where {T<:AbstractFloat}
3444
@tullio sum_of_squares := weights[x]^2
3545
return r_eff ./ sum_of_squares
3646
end
3747

38-
function psis_n_eff(
48+
function psis_ess(
3949
weights::AbstractMatrix{T}, r_eff::AbstractVector{T}
4050
) where {T<:AbstractFloat}
4151
@tullio sum_of_squares[x] := weights[x, y]^2
4252
return @tturbo r_eff ./ sum_of_squares
4353
end
4454

45-
function psis_n_eff(weights::AbstractArray{T}) where {T<:AbstractFloat}
55+
function psis_ess(weights::AbstractMatrix{T}) where {T<:AbstractFloat}
4656
@warn "PSIS ESS not adjusted based on MCMC ESS. MCSE and ESS estimates " *
4757
"will be overoptimistic if samples are autocorrelated."
48-
return psis_n_eff(weights, ones(size(weights)))
58+
return psis_ess(weights, ones(size(weights)))
4959
end
60+
61+
function psis_n_eff(args...; kwargs...) # Alias for compatibility with R version
62+
return psis_ess(args...; kwargs...)
63+
end

src/ImportanceSampling.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function psis(
7272

7373
@tullio norm_const[i] := weights[i, j]
7474
weights .= weights ./ norm_const
75-
ess = psis_n_eff(weights, r_eff)
75+
ess = psis_ess(weights, r_eff)
7676

7777
weights = reshape(weights, dims)
7878

@@ -106,7 +106,7 @@ end
106106

107107

108108
"""
109-
do_psis_i!(is_ratios::AbstractVector{AbstractFloat}, tail_length::Integer)::T
109+
do_psis_i!(is_ratios::AbstractVector{AbstractFloat}, tail_length::Integer) -> T
110110
111111
Do PSIS on a single vector, smoothing its tail values.
112112
@@ -243,6 +243,7 @@ function _generate_r_eff(weights, dims, r_eff, source)
243243
end
244244
end
245245

246+
246247
"""
247248
Make sure all inputs to `psis` are valid.
248249
"""
@@ -267,6 +268,7 @@ function check_input_validity_psis(
267268
return nothing
268269
end
269270

271+
270272
"""
271273
Check the tail to make sure a GPD fit is possible.
272274
"""

0 commit comments

Comments
 (0)