MatLab 中的自定义直方图密度评估

Custom histogram density evaluation in MatLab

MatLab 是否有任何内置函数来评估自定义直方图中随机变量的密度? (我怀疑可能有很多方法可以做到这一点,我只是想看看是否已经有任何内置的 MatLab 功能)。 谢谢。

函数hist为您提供了您正在评估的概率密度的近似值。

如果您想要它的连续表示,this article from the Matlab documentation 解释了如何使用曲线拟合工具箱中的 spline 命令获得一个。基本上,这篇文章解释了如何对直方图进行三次插值。

结果代码是:

y = randn(1,5001); % Replace y by your own dataset

[heights,centers] = hist(y);
hold on
n = length(centers);
w = centers(2)-centers(1);
t = linspace(centers(1)-w/2,centers(end)+w/2,n+1);
dt = diff(t);
Fvals = cumsum([0,heights.*dt]);
F = spline(t, [0, Fvals, 0]);
DF = fnder(F);
hold on
fnplt(DF, 'r', 2)
hold off
ylims = ylim;
ylim([0,ylims(2)]);

一种流行的方法是使用核密度估计。在 Matlab 中执行此操作的最简单方法是使用 ksdensity.