npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

kde-simple

v0.0.18

Published

simple 1D kernel density estimation (KDE) with gaussian and triangular kernels

Downloads

31

Readme

kde-simple

simple 1D kernel density estimation (KDE) with gaussian and triangular kernels

Installation

npm i kde-simple

Usage

var {kernelDensityEstimate, getRange, generateRandomFromKDE, calculateBandwidth, getPeakValue } = require('kde-simple');

var useTriangularKernel = true; //if false, uses gaussian kernel
var bandwidth = 1;

//weights affect the size of the samples relative to e/o, but
//the area under the KDE always sums to 1 regardless of weights
var samples = [
    {weight:2,x:0}, //default weight is 1.
    {weight:1,x:2},
    // {weight:2,x:-2}
];

//calculateBandwidth_scott(samples) = bandwidth via Scott's rule for 1D data [based on weights]
//calculateBandwidth_silverman(samples) = bandwidth via Silverman's rule for 1D data [based on x values]
//getPeakValue(samples, bandwidth, useTriangular, range) => description of peak of KDE
// format like { x: 0.266, val: 1.1388, sampleIndex: 796 }

//optional - get y min/max values [min, max] for this kde. 
// this can use this to normalize results by height instead of area
var range = getRange(samples, bandwidth, useTriangularKernel);

var pts =[];
for(var x=-3; x<5; x+=0.02){
    pts.push(
        {
            x: x,
            //get values normalized by area [total area = 1]
            y: kernelDensityEstimate(samples, x, bandwidth, useTriangularKernel) //returns values normalized by total area = 1
            //alt: returns values normalized by height[max height = 1]
            //y: kernelDensityEstimate(samples, x, bandwidth, useTriangularKernel, range) 
        }
    );
}

//save the result as a scatter chart...

//var scs = require('scatter-chart-simple');
//var title = `${samples.length} ${useTriangularKernel ? 'triangle' : 'gaussian'}s bw ${bandwidth}`;
//var resultFile = `./${title}.png`.replace(/ /g,'_');
//var width = 320;
//var height = 240;
//var bgColor = 'white';
//var fgColor = 'rgb(255, 99, 132)'
//scs.plotSingleDataChart(resultFile, pts, title, width, height, bgColor, fgColor)


//you can also extract a random number using the KDE as a probability distribution 
// -- uses rejection sampling 

var samplesForKDE = [
    {weight: 1, x:1}
]

var results = []

for(var i=0;i<100000;i++){
    var bandwidth = 0.1;
    results.push(generateRandomFromKDE(samplesForKDE,bandwidth));
}

console.log(require('histogram-simple')(results).toString(80));

// 0.8000: ##
// 0.8400: #####
// 0.8800: ########
// 0.9200: ###########
// 0.9600: ############
// 1.0000: ############
// 1.0400: ###########
// 1.0800: ########
// 1.1200: #####
// 1.1600: ##

//get 1st and 2nd derivatives using central differences over distance h 
//outputs { firstDerivative: value1, secondDerivative: value2 }
//  .getKDEDerivatives(data, point, bandwidth, useTriangular, range, h=0.0001)

Notice how changing the bandwidth changes the height of the result. The total area is always 1.

graphA

graphA2

graphC

graphB

If you want values spanning 0...1 then make sure to include the range parameter to normalize results by height instead of area [see code above]

graphE

graphF

stonks