Negative values in kernel density estimation

39 ビュー (過去 30 日間)
Sunetra Chituru
Sunetra Chituru 2022 年 9 月 26 日
コメント済み: Sunetra Chituru 2022 年 10 月 6 日
My input data vector 'x' has values ranging between 20 to 500 . when i use the command
[f1,y1]= ksdensity(x),
the out put values are negative. how is this possible?
  2 件のコメント
the cyclist
the cyclist 2022 年 9 月 30 日
Can you upload the data, or a sample that shows the issue? You can use the paper clip icon in the INSERT section of the toolbar.
Sunetra Chituru
Sunetra Chituru 2022 年 9 月 30 日
I have attached the excel sheet containing the data.

サインインしてコメントする。

採用された回答

the cyclist
the cyclist 2022 年 10 月 4 日
(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
[f1,y1]= ksdensity(x,"Support","positive");
% Plot that fit against a histogram of the data
figure
hold on
histogram(x,10:20:210,"Normalization","pdf")
h = plot(y1,f1);
set(h,"LineWidth",2)
legend(["Binned data","K-S density"])
  1 件のコメント
Sunetra Chituru
Sunetra Chituru 2022 年 10 月 6 日
Hi,
thanks for the response.

サインインしてコメントする。

その他の回答 (1 件)

Bala Tripura Bodapati
Bala Tripura Bodapati 2022 年 9 月 30 日
Hi Sunetra
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:
x=20:10:500
[f1,y1]= ksdensity(x,'support','positive')
plot(y1,f1)
Refer the ksdensity documentation for more information.
  1 件のコメント
Sunetra Chituru
Sunetra Chituru 2022 年 9 月 30 日
Hi Tripura,
Thanks for the response. I have implemented the support property , but the distribution obtained seems to be much more skewed when compared to my distribution obtained without the support function. Moreover the values in my data are not anywhere near to zero.

サインインしてコメントする。

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by