Negative values in kernel density estimation
29 views (last 30 days)
the cyclist on 4 Oct 2022
(Sorry for the delayed response. I didn't get a notification that you had replied to my comment.)
The K-S density looks like a pretty appropriate fit to me, and your data are indeed skewed. I don't see an issue.
% Read data locally
% tbl = readtable("Book1(Mq).xlsx");
% Read data online
tbl = readtable("https://www.mathworks.com/matlabcentral/answers/uploaded_files/1141175/Book1(Mq).xlsx");
% Pull the data from the table into a numeric array, for convenience
x = tbl.Var1;
% Fit the K-S density, assuming support for only positive values
% Plot that fit against a histogram of the data
h = plot(y1,f1);
legend(["Binned data","K-S density"])
More Answers (1)
Bala Tripura Bodapati on 30 Sep 2022
It is my understanding that the output values returned by 'ksdensity' function are negative though the input vector contains positive values.
A 'normal kernel function' is the default function used by ‘ksdensity' function to return the probability density estimate. If your data has values near zero, you'll naturally get some overlap into the negative side as the individual kernels are summed.
As a workaround, the 'support' property can be set to 'positive' to restrict the density to positive values. The following code illustrates the suggested workaround:
Refer the ksdensity documentation for more information.