Skip to content

Commit

Permalink
Simplified unbinned histogram mean/std algorithm, changed to more num…
Browse files Browse the repository at this point in the history
…erically stable Welford variance
  • Loading branch information
pl0xz0rz committed Nov 7, 2023
1 parent 1d37097 commit 7ca323f
Showing 1 changed file with 17 additions and 27 deletions.
44 changes: 17 additions & 27 deletions RootInteractive/InteractiveDrawing/bokeh/HistoNdProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,19 +167,30 @@ export class HistoNdProfile extends ColumnarDataSource {
entries_total += entries
if(entries > 0){
let mean = 0
let std = 0
let entries_ = 0
if(unbinned){
let old_mean = 0
for (let y = 0; y < stride_high; y+= stride_low) {
const index_low = x+y+z ? global_cumulative_histogram![x+y+z-1] : 0
const index_high = global_cumulative_histogram![x+y+z]
if(sorted_weights == null){
for(let i=index_low; i < index_high; ++i){
if (isFinite(sorted_entries![i]))
mean += sorted_entries![i]
if (isFinite(sorted_entries![i])){
entries_ ++
old_mean = mean
mean += (sorted_entries![i] - mean) / entries_
std += (sorted_entries![i] - mean) * (sorted_entries![i] - old_mean)
}
}
} else {
for(let i=index_low; i < index_high; ++i){
if (isFinite(sorted_entries![i]))
mean += sorted_entries![i] * sorted_weights[i]
if (isFinite(sorted_entries![i])){
entries_ += sorted_weights[i]
old_mean = mean
mean += (sorted_entries![i] - mean) * sorted_weights[i] / entries_
std += (sorted_entries![i] - mean) * (sorted_entries![i] - old_mean) * sorted_weights[i]
}
}
}
}
Expand All @@ -188,29 +199,8 @@ export class HistoNdProfile extends ColumnarDataSource {
for (let y = 0; y < stride_high; y+= stride_low) {
mean += bin_count[x+y+z] * bin_centers[x+y+z]
}
}
let std = 0
mean /= entries
if(unbinned){
for (let y = 0; y < stride_high; y+= stride_low) {
const index_low = x+y+z ? global_cumulative_histogram![x+y+z-1] : 0
const index_high = global_cumulative_histogram![x+y+z]
if(sorted_weights == null){
for(let i=index_low; i < index_high; ++i){
if (isFinite(sorted_entries![i]))
std += sorted_entries![i] * sorted_entries![i]
}
} else {
for(let i=index_low; i < index_high; ++i){
if (isFinite(sorted_entries![i]))
std += sorted_entries![i] * sorted_entries![i] * sorted_weights[i]
}
}
}
std -= mean*mean*entries
} else
{
for (let y = 0; y < stride_high; y+= stride_low) {
mean /= entries
for (let y = 0; y < stride_high; y+= stride_low) {
std += (bin_centers[x+y+z] - mean) * (bin_centers[x+y+z] - mean) * bin_count[x+y+z]
}
}
Expand Down

0 comments on commit 7ca323f

Please sign in to comment.