Clear Filters
Clear Filters

Why does ksdensity return density value of 0?

2 views (last 30 days)
Naomi Gray
Naomi Gray on 22 Apr 2022
Answered: Sandeep on 21 Dec 2023
I'm using ksdensity function to estimate the densities of my data, but I have a problem.
[f,xi] = ksdensity(data,pts,'Support','unbounded')
I used the support for desnity as unbound, which allows the density to extend over the whole real line. (as described in the documentation)
I was expecting to get value as small as possible, but it doesn't return values smaller than 1e-19.
It seems like the smallest floating point-number Matlab can give is 2.2251e-308.
f = realmin;
% f = 2.2251e-308
I'm curious as to why this is the case.

Answers (1)

Sandeep
Sandeep on 21 Dec 2023
Hi Naomi Gray,
It is my understanding that you are using 'ksdensity' function to estimate density for a univariate or bivariate data and you are facing difficulties in achieving lower density values.
The behavior you're observing is due to the numerical limitations of floating-point arithmetic in MATLAB, as well as the inherent properties of the 'ksdensity' function.
It is important to understand that while MATLAB's smallest positive normalized floating-point number is indeed approximately 2.2251e-308, the results from functions like ksdensity are affected by both the algorithm's numerical stability and precision limits.
The ksdensity function uses kernel smoothing to estimate the probability density function of a random variable. The 'Support' is specified as 'unbounded' by default, allows the density estimation to extend over the entire real line.
In practice, the density values at the tails of the distribution are expected to be very small, and due to the limitations of floating-point representation, they may reach a point where they are effectively considered zero. The value of 1e-19 that you're observing is likely a practical lower bound for the density estimates given the data and bandwidth used in the kernel density estimation.
I hope it helps!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!