Skip to content

Commit

Permalink
Calculate effective sample size to choose bins, bandwidth
Browse files Browse the repository at this point in the history
  • Loading branch information
sefffal committed Dec 3, 2024
1 parent 655e8d8 commit 0874ab3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 20 deletions.
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
name = "PairPlots"
uuid = "43a3c2be-4208-490b-832a-a21dcd55d7da"
authors = ["William Thompson <wthompson@uvic.ca>"]
version = "2.9.3"
version = "2.10.0"

[deps]
Contour = "d38c429a-6771-53c6-b99e-75d170b6e991"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
KernelDensity = "5ab0869b-81aa-558d-bb23-cbf5423bbe9b"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MCMCDiagnosticTools = "be115224-59cd-429b-ad48-344e309966f0"
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
Measures = "442fdcdd-2543-5da2-b0f3-8c86c306513e"
Missings = "e1d29d7a-bbdc-5cf2-9ac0-f12de2c33e28"
Expand Down Expand Up @@ -40,6 +41,7 @@ DynamicQuantities = "0.14, 1"
KernelDensity = "0.6"
LinearAlgebra = "1"
MCMCChains = "5.0, 6.0"
MCMCDiagnosticTools = "0.3"
Makie = "0.20, 0.21"
Measures = "0.3.1"
Missings = "1"
Expand Down
67 changes: 48 additions & 19 deletions src/PairPlots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ using Printf
using KernelDensity: KernelDensity
using Contour: Contour as ContourLib
using OrderedCollections: OrderedDict
using StatsBase: fit, quantile, Histogram, cor
using StatsBase: fit, quantile, Histogram, cor, std
using Distributions: pdf
using StaticArrays
using PolygonOps
using LinearAlgebra: LinearAlgebra
using LinearAlgebra: normalize
using Missings
using NamedTupleTools
using MCMCDiagnosticTools: MCMCDiagnosticTools

"""
AbstractSeries
Expand Down Expand Up @@ -930,23 +931,18 @@ function pairplot(
if !anydiag_viz
M -= 1
end
if display_bottomleft_axes && !display_topright_axes
pos = grid[M == 1 ? 1 : end-1, M <= 2 ? 2 : M ]
elseif !display_bottomleft_axes && display_topright_axes
# TODO
pos = grid[M == 1 ? 1 : end, N <= 2 ? 2 : M-1 ]
else
# Extend the grid out sideways to accomodate
pos = grid[begin,end+1]
end
pos = grid[begin,end+1]

Makie.Legend(
pos,
collect(legend_entries),
collect(legend_strings);
tellwidth=false,
# tellwidth=false,
tellheight=false,
valign = display_bottomleft_axes ? :bottom : :top,
halign = display_bottomleft_axes ? :left : :right,
# valign = display_bottomleft_axes ? :bottom : :top,
# halign = display_bottomleft_axes ? :left : :right,
valign=:top,
halign=:right,
legend...
)
end
Expand Down Expand Up @@ -1021,7 +1017,8 @@ function diagplot(ax::Makie.Axis, viz::MarginHist, series::AbstractSeries, colna
else
# Determine the number of bins to use, and allow series or
# visualization layer to override.
bins = max(7, ceil(Int, 1.8log2(length(dat))) + 1)
n_ess_dat = MCMCDiagnosticTools.ess(dat)
bins = max(7, ceil(Int, 1.8log2(n_ess_dat)) + 1)
bins = get(series.kwargs, :bins, bins)
bins = get(viz.kwargs, :bins, bins)
end
Expand Down Expand Up @@ -1069,7 +1066,8 @@ function diagplot(ax::Makie.Axis, viz::MarginStepHist, series::AbstractSeries, c
else
# Determine the number of bins to use, and allow series or
# visualization layer to override.
bins = max(7, ceil(Int, 1.8log2(length(dat))) + 1)
n_ess_dat = MCMCDiagnosticTools.ess(dat)
bins = max(7, ceil(Int, 1.8log2(n_ess_dat)) + 1)
bins = get(series.kwargs, :bins, bins)
bins = get(viz.kwargs, :bins, bins)
end
Expand Down Expand Up @@ -1118,7 +1116,7 @@ function diagplot(ax::Makie.Axis, viz::MarginDensity, series::AbstractSeries, co
# viz.kwargs...,
# )

k = KernelDensity.kde(dat, bandwidth=KernelDensity.default_bandwidth(dat).*viz.bandwidth)
k = KernelDensity.kde(dat, bandwidth=default_bandwidth_ess(dat).*viz.bandwidth)
ik = KernelDensity.InterpKDE(k)

exx = extrema(dat)
Expand Down Expand Up @@ -1232,7 +1230,8 @@ function bodyplot(ax::Makie.Axis, viz::HexBin, series::AbstractSeries, colname_r
else
# Determine the number of bins to use, and allow series or
# visualization layer to override.
xbins = max(7, ceil(Int, 1.8log2(length(X))) + 1)
n_ess_dat = MCMCDiagnosticTools.ess(X)
xbins = max(7, ceil(Int, 1.8log2(n_ess_dat)) + 1)
xbins = get(series.kwargs, :bins, xbins)
xbins = get(viz.kwargs, :bins, xbins)
end
Expand All @@ -1241,7 +1240,8 @@ function bodyplot(ax::Makie.Axis, viz::HexBin, series::AbstractSeries, colname_r
else
# Determine the number of bins to use, and allow series or
# visualization layer to override.
ybins = max(7, ceil(Int, 1.8log2(length(Y))) + 1)
n_ess_dat = MCMCDiagnosticTools.ess(Y)
ybins = max(7, ceil(Int, 1.8log2(n_ess_dat)) + 1)
ybins = get(series.kwargs, :bins, ybins)
ybins = get(viz.kwargs, :bins, ybins)
end
Expand Down Expand Up @@ -1312,7 +1312,7 @@ function prep_contours(series::AbstractSeries, sigmas, colname_row, colname_col;
xdat = ustrip(disallowmissing(getcolumn(series, colname_col)))
ydat = ustrip(disallowmissing(getcolumn(series, colname_row)))
dat = (xdat, ydat)
k = KernelDensity.kde(dat, bandwidth=KernelDensity.default_bandwidth(dat).*bandwidth)
k = KernelDensity.kde(dat, bandwidth=(default_bandwidth_ess(xdat),default_bandwidth_ess(ydat)).*bandwidth)
ik = KernelDensity.InterpKDE(k)

exx = extrema(xdat)
Expand Down Expand Up @@ -1502,6 +1502,35 @@ function bodyplot(ax::Makie.Axis, viz::BodyLines, series::AbstractSeries, colnam
end
end

# Bandwidth calculations using ESS instead of sample size
# Adapted from KernelDensity.jl
# Silverman's rule of thumb for KDE bandwidth selection
function default_bandwidth_ess(data::AbstractVector{<:Real}, alpha::Float64 = 0.9)
# Determine length of data
ndata = length(data)
ndata <= 1 && return alpha

n_ess = MCMCDiagnosticTools.ess(data)

# Calculate width using variance and IQR
var_width = std(data)
q25, q75 = quantile(data, [0.25, 0.75])
quantile_width = (q75 - q25) / 1.34

# Deal with edge cases with 0 IQR or variance
width = min(var_width, quantile_width)
if width == 0.0
if var_width == 0.0
width = 1.0
else
width = var_width
end
end

# Set bandwidth using Silverman's rule of thumb
return alpha * width * n_ess^(-0.2)
end


# Default histogram calculations
"""
Expand Down

2 comments on commit 0874ab3

@sefffal
Copy link
Owner Author

@sefffal sefffal commented on 0874ab3 Dec 3, 2024

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/120610

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v2.10.0 -m "<description of version>" 0874ab33073833b70ab6351fd67e3672f32828cc
git push origin v2.10.0

Please sign in to comment.