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

@stdlib/stats-kde2d

v0.2.1

Published

Two-dimensional kernel density estimation.

Downloads

72

Readme

kde2d

NPM version Build Status Coverage Status

Two-dimensional kernel density estimation.

Installation

npm install @stdlib/stats-kde2d

Usage

var kde2d = require( '@stdlib/stats-kde2d' );

kde2d( x, y[, opts] )

By default, the function computes two-dimensional normal kernel density estimation for data provided in arrays or typed-arrays x and y. When these arguments are supplied, the arrays are coerced into a Matrix-like object.

var x = [ 0.6333, 0.8643, 1.0952, 1.3262, 1.5571,
    1.7881, 2.019, 2.25, 2.481, 2.7119 ];
var y = [ -0.0468, 0.8012, 1.6492, 2.4973, 3.3454,
    4.1934, 5.0415, 5.8896, 6.7376, 7.5857 ];

var out = kde2d( x, y );
/* e.g., returns
    {
        'x': [ ~0.633, ~0.72, ... ],
        'y': [ ~-0.047, ~0.271 ... ],
        'z': ndarray{ <Float64Array>[ ~0.0455, ... ]}
    }
*/

kde2d( arr[, opts] )

The function has the ability to handle ndarrays. Specifically the ndarray must be constructed so that there are two columns present, the first column containing the x values and the second column containing the y values.

Note that for the output the x and y properties refer to the equally spaced gridpoints of X and Y used to calculate z.

var ndarray = require( '@stdlib/ndarray-ctor' );

var x = [ 0.6333, 0.8643, 1.0952, 1.3262, 1.5571,
    1.7881, 2.019, 2.25, 2.481, 2.7119 ];
var y = [ -0.0468, 0.8012, 1.6492, 2.4973, 3.3454,
    4.1934, 5.0415, 5.8896, 6.7376, 7.5857 ];

var buffer = x.concat( y );
var n = x.length;
var shape = [ n, 2 ];
var strides = [ 1, n ];
var offset = 0;
var order = 'column-major';

var arr = ndarray( 'generic', buffer, shape, strides, offset, order );

var out = kde2d( arr );
/* e.g., returns
    {
        'x': [ ~0.633, ~0.72, ... ],
        'y': [ ~-0.047,~ 0.271, ... ],
        'z': ndarray{ <Float64Array>[0.04547178438418015, ... ]}
    }
*/

The function accepts the following options:

  • h: NumberArray of length 2 indicating the X and Y bandwidth values, respectively.
  • n: a positive integer indicating the number of partitions to create in the grid. Default: 25.
  • xMin: a number indicating the lower bound of X. Must be strictly less than xMax. Will default to the minimum value of X.
  • xMax: a number indicating the upper bound of X. Must be strictly greater than xMin. Will default to the maximum value of X.
  • yMin: a number indicating the lower bound of Y. Must be strictly less than yMax. Will default to the minimum value of Y.
  • yMax: a number indicating the upper bound of Y. Must be strictly greater than yMin. Will default to the maximum value of Y.
  • kernel: a string or function indicating the kernel to be used when calculating the estimation. If a string is supplied then it will be matched to a pre-defined kernel function. Otherwise you may supply a function to support custom kernels. Will default to the gaussian kernel.

By default, the bandwidth argument is set by a builtin function. To choose different bandwidth values, set the h option. Note that if you use a custom bandwidth for one axis, you must also use a custom bandwidth for the other axis.

var x = [ 0.6333, 0.8643, 1.0952, 1.3262, 1.5571,
    1.7881, 2.019, 2.25, 2.481, 2.7119 ];
var y = [ -0.0468, 0.8012, 1.6492, 2.4973, 3.3454,
    4.1934, 5.0415, 5.8896, 6.7376, 7.5857 ];

var out = kde2d( x, y, {
    'h': [ 0.05, 0.1 ]
});
/* e.g., returns
    {
        'x': [ 0.148, 0.3772, ... ],
        'y': [ -1.1511, -0.253, ... ],
        'z': ndarray{ <Float64Array>[ 6.344e-154, 1.93e-171, ... ]}
    }
*/

By default, we use 25 partitions. To change the number of partitions, set the n option.

var x = [ 0.6333, 0.8643, 1.0952, 1.3262, 1.5571,
    1.7881, 2.019, 2.25, 2.481, 2.7119 ];
var y = [ -0.0468, 0.8012, 1.6492, 2.4973, 3.3454,
    4.1934, 5.0415, 5.8896, 6.7376, 7.5857 ];

var out = kde2d( x, y, {
    'n': 15
});
/* e.g., returns
    {
        'x': [ 0.0623, 0.452, ... ],
        'y': [ 0.1378, 1.6266, ... ],
        'z': ndarray{ <Float64Array>[1.211e-7, 5.76e-7, ... ]}
    }
*/

As a default choice, the kde2d function sets the xMin, xMax, yMin and yMax values to be the minimum and maximum of the X and Y arrays or columns of the supplied arguments. We may change the options as follows:

var x = [ 0.6333, 0.8643, 1.0952, 1.3262, 1.5571,
    1.7881, 2.019, 2.25, 2.481, 2.7119 ];
var y = [ -0.0468, 0.8012, 1.6492, 2.4973, 3.3454,
    4.1934, 5.0415, 5.8896, 6.7376, 7.5857 ];

var out = kde2d( x, y, {
    'xMin': 0.0,
    'xMax': 2.5,
    'yMin': 0.0,
    'yMax': 6.0
});
/* e.g., returns
    {
        'x': [ 0, 0.1041, ... ],
        'y': [ 0, 0.25, ... ],
        'z': ndarray{ <Float64Array>[ 1.762e-8, 2.94e-8, ... ]}
    }
*/

Examples

var normal = require( '@stdlib/random-base-normal' );
var kde2d = require( '@stdlib/stats-kde2d' );

var randX;
var randY;
var out;
var i;
var x;
var y;
var n;

n = 100;

x = new Array( n );
y = new Array( n );

randX = normal.factory( 3.0, 1.2 );
randY = normal.factory( 10.0, 4.5 );

for ( i = 0; i < n; i++ ) {
    x[ i ] = randX();
    y[ i ] = randY();
}

out = kde2d( x, y );
/* e.g., returns
    {
        'x': [0.022, 0.2614, ...],
        'y': [-4.533, 3.602, ...],
        'z': ndarray { Float64Array [9.8266e-11, 6.45e-9, ...]}
}
*/

Notice

This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.

For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.

Community

Chat


License

See LICENSE.

Copyright

Copyright © 2016-2024. The Stdlib Authors.