Skip to content

Violin span fix #2650

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 22, 2018
Merged
Show file tree
Hide file tree
Changes from 5 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
37 changes: 28 additions & 9 deletions src/traces/violin/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,13 @@ module.exports = function calc(gd, trace) {
for(var i = 0; i < cd.length; i++) {
var cdi = cd[i];
var vals = cdi.pts.map(helpers.extractVal);
var len = vals.length;

// sample standard deviation
var ssd = Lib.stdev(vals, len - 1, cdi.mean);
var bandwidthDflt = ruleOfThumbBandwidth(vals, ssd, cdi.q3 - cdi.q1);
var bandwidth = cdi.bandwidth = trace.bandwidth || bandwidthDflt;
var bandwidth = cdi.bandwidth = calcBandwidth(trace, cdi, vals);
var span = cdi.span = calcSpan(trace, cdi, valAxis, bandwidth);

// step that well covers the bandwidth and is multiple of span distance
var dist = span[1] - span[0];
var n = Math.ceil(dist / (Math.min(bandwidthDflt, bandwidth) / 3));
var n = Math.ceil(dist / (bandwidth / 3));
var step = dist / n;

if(!isFinite(step) || !isFinite(n)) {
Expand All @@ -75,13 +71,36 @@ module.exports = function calc(gd, trace) {
return cd;
};

// Default to Silveman's rule of thumb:
// Default to Silveman's rule of thumb
// - https://stats.stackexchange.com/a/6671
// - https://en.wikipedia.org/wiki/Kernel_density_estimation#A_rule-of-thumb_bandwidth_estimator
// - https://github.com/statsmodels/statsmodels/blob/master/statsmodels/nonparametric/bandwidths.py
function ruleOfThumbBandwidth(vals, ssd, iqr) {
function silvermanRule(len, ssd, iqr) {
var a = Math.min(ssd, iqr / 1.349);
return 1.059 * a * Math.pow(vals.length, -0.2);
return 1.059 * a * Math.pow(len, -0.2);
}

function calcBandwidth(trace, cdi, vals) {
var span = cdi.max - cdi.min;

// Limit how small the bandwidth can be.
//
// Silverman's rule of thumb can be "very" small
// when IQR does a poor job at describing the spread
// of the distribution.
// We also want to limit custom bandwidths
// to not blow up kde computations.

if(trace.bandwidth) {
return Math.max(trace.bandwidth, span / 1e4);
Copy link
Collaborator

Choose a reason for hiding this comment

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

From slack, for the record:

@etpinard: limiting custom bandwidth at (span / 1e5) is still pretty slow. ok if I increase it to span / 1e4 ?

Me: sure, that’s still more than 1 bw/px. I’m not quite so worried about it though, at that point the user has asked for something explicitly ridiculous… but I guess we don’t want our mock to take forever to render!

} else {
var len = vals.length;
var ssd = Lib.stdev(vals, len - 1, cdi.mean);
return Math.max(
silvermanRule(len, ssd, cdi.q3 - cdi.q1),
span / 100
);
}
}

function calcSpan(trace, cdi, valAxis, bandwidth) {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading