Skip to content

Commit

Permalink
Fixes normalization for groupedhist (#465)
Browse files Browse the repository at this point in the history
* fixes normalization for groupedhist

* adds weighting by the group probability

* Update src/hist.jl

Co-authored-by: Seth Axen <[email protected]>

* adds ntot

* Update src/hist.jl

Co-authored-by: Seth Axen <[email protected]>

* adds suggested performance tweaks
version iterate

Co-authored-by: Valentin Kaisermayer <[email protected]>
Co-authored-by: Seth Axen <[email protected]>
  • Loading branch information
3 people authored Sep 29, 2021
1 parent 6a0ee14 commit b722dab
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "StatsPlots"
uuid = "f3b207a7-027a-5e70-b257-86293d7955fd"
version = "0.14.27"
version = "0.14.28"

[deps]
Clustering = "aaaa29a8-35af-508c-8bc3-b662a17a0fe5"
Expand Down
11 changes: 8 additions & 3 deletions src/hist.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,27 @@ Plots.group_as_matrix(g::GroupedHist) = true
edges = h.edges[1]
bar_width --> mean(map(i -> edges[i+1] - edges[i], 1:nbins))
x = map(i -> (edges[i] + edges[i+1])/2, 1:nbins)

if group === nothing
y = reshape(h.weights, nbins, 1)
else
gb = RecipesPipeline._extract_group_attributes(group)
labels, idxs = getfield(gb, 1), getfield(gb, 2)
ngroups = length(labels)

ntot = count(x->!isnan(x), v)

# compute weights (frequencies) by group using those edges
y = fill(NaN, nbins, ngroups)
for i in 1:ngroups
groupinds = idxs[i]
v_i = filter(x->!isnan(x), v[:,i])
w_i = weights == nothing ? nothing : weights[groupinds]
h_i = Plots._make_hist((v_i,), h.edges; normed = false, weights = w_i)
y[:,i] = normed ? h_i.weights / sum(h.weights) : h_i.weights
if normed
y[:,i] .= h_i.weights .* (length(v_i) / ntot / sum(h_i.weights))
else
y[:,i] .= h_i.weights
end
end
end

Expand Down

2 comments on commit b722dab

@sethaxen
Copy link
Member

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/45773

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 v0.14.28 -m "<description of version>" b722dabc046326b3ae3c141d06bf6a75a84179b3
git push origin v0.14.28

Please sign in to comment.