Skip to content

Instantly share code, notes, and snippets.

@jdthorpe
Created May 28, 2019 00:57
Show Gist options
  • Save jdthorpe/240cd669c8c6909d32793883ed9842b2 to your computer and use it in GitHub Desktop.
Save jdthorpe/240cd669c8c6909d32793883ed9842b2 to your computer and use it in GitHub Desktop.
Kernel densties
function kernelDensityEstimator(kernel:{(x:number):number}, X:number[]) {
return function(V:number[]) {
return X.map(function(x) {
return [x, d3.d3.mean(V, function(v:number) { return kernel(x - v); })];
});
};
}
function kernelEpanechnikov(k:number): {(x:number):number}{
return function(v:number):number {
return Math.abs(v /= k) <= 1 ? 0.75 * (1 - v * v) / k : 0;
};
}
function cumulativeKernelEpanechnikov(k:number): {(x:number):number} {
return function(v:number):number {
return Math.abs(v /= k) <= 1 ? 0.5 * (1 + v * v * v) / k : (v < 0 ? 0 : 1);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment