Two-dimensional kernel density estimation.
npm install @stdlib/stats-kde2dWe believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js. The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases. When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there. To join us in bringing numerical computing to the web, get started by checking us out on GitHub, and please consider financially supporting stdlib. We greatly appreciate your continued support!
About stdlib...
[![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url]
> Two-dimensional kernel density estimation.
``bash`
npm install @stdlib/stats-kde2d
`javascript`
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][mdn-array] or [typed-arrays][mdn-typed-array] x and y. When these arguments are supplied, the arrays are coerced into a Matrix-like object.
`javascript
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{
}
*/
`
#### kde2d( arr\[, opts] )
The function has the ability to handle [ndarrays][nd-array]. 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.
`javascript
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{
}
*/
`
The function accepts the following options:
- h: NumberArray of length 2 indicating the X and Y bandwidth values, respectively.integer
- n: a positive indicating the number of partitions to create in the grid. Default: 25.number
- xMin: a indicating the lower bound of X. Must be strictly less than xMax. Will default to the minimum value of X.number
- xMax: a indicating the upper bound of X. Must be strictly greater than xMin. Will default to the maximum value of X.number
- yMin: a indicating the lower bound of Y. Must be strictly less than yMax. Will default to the minimum value of Y.number
- yMax: a indicating the upper bound of Y. Must be strictly greater than yMin. Will default to the maximum value of Y.string
- kernel: a 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.
`javascript
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{
}
*/
`
By default, we use 25 partitions. To change the number of partitions, set the n option.
`javascript
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{
}
*/
`
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:
`javascript
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{
}
*/
`
`javascript
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, ...]}
}
*/
`
*
This package is part of [stdlib][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][stdlib], see the main project [repository][stdlib].
#### Community
[![Chat][chat-image]][chat-url]
---
See [LICENSE][stdlib-license].
Copyright © 2016-2026. The Stdlib [Authors][stdlib-authors].
[npm-image]: http://img.shields.io/npm/v/@stdlib/stats-kde2d.svg
[npm-url]: https://npmjs.org/package/@stdlib/stats-kde2d
[test-image]: https://github.com/stdlib-js/stats-kde2d/actions/workflows/test.yml/badge.svg?branch=v0.2.2
[test-url]: https://github.com/stdlib-js/stats-kde2d/actions/workflows/test.yml?query=branch:v0.2.2
[coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/stats-kde2d/main.svg
[coverage-url]: https://codecov.io/github/stdlib-js/stats-kde2d?branch=main
[chat-image]: https://img.shields.io/badge/zulip-join_chat-brightgreen.svg
[chat-url]: https://stdlib.zulipchat.com
[stdlib]: https://github.com/stdlib-js/stdlib
[stdlib-authors]: https://github.com/stdlib-js/stdlib/graphs/contributors
[umd]: https://github.com/umdjs/umd
[es-module]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules
[deno-url]: https://github.com/stdlib-js/stats-kde2d/tree/deno
[deno-readme]: https://github.com/stdlib-js/stats-kde2d/blob/deno/README.md
[umd-url]: https://github.com/stdlib-js/stats-kde2d/tree/umd
[umd-readme]: https://github.com/stdlib-js/stats-kde2d/blob/umd/README.md
[esm-url]: https://github.com/stdlib-js/stats-kde2d/tree/esm
[esm-readme]: https://github.com/stdlib-js/stats-kde2d/blob/esm/README.md
[branches-url]: https://github.com/stdlib-js/stats-kde2d/blob/main/branches.md
[stdlib-license]: https://raw.githubusercontent.com/stdlib-js/stats-kde2d/main/LICENSE
[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays
[nd-array]: https://github.com/stdlib-js/stdlib/blob/develop/lib/node_modules/@stdlib/ndarray/ctor/README.md