3 回答

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超6個(gè)贊
我對(duì)此的回答與您對(duì)先前問題的回答相同。對(duì)于概率密度函數(shù),整個(gè)空間的積分為1。除以總和不會(huì)得到正確的密度。為了獲得正確的密度,必須除以面積。為了說明我的觀點(diǎn),請(qǐng)嘗試以下示例。
[f, x] = hist(randn(10000, 1), 50); % Create histogram from a normal distribution.
g = 1 / sqrt(2 * pi) * exp(-0.5 * x .^ 2); % pdf of the normal distribution
% METHOD 1: DIVIDE BY SUM
figure(1)
bar(x, f / sum(f)); hold on
plot(x, g, 'r'); hold off
% METHOD 2: DIVIDE BY AREA
figure(2)
bar(x, f / trapz(x, f)); hold on
plot(x, g, 'r'); hold off
您可以自己查看哪種方法與正確答案(紅色曲線)相符。
標(biāo)準(zhǔn)化直方圖的另一種方法(比方法2更直接)是除以sum(f * dx)表示概率密度函數(shù)的積分,即
% METHOD 3: DIVIDE BY AREA USING sum()
figure(3)
dx = diff(x(1:2))
bar(x, f / sum(f * dx)); hold on
plot(x, g, 'r'); hold off

TA貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超8個(gè)贊
自2014b起,Matlab將這些規(guī)范化例程本機(jī)嵌入在histogram函數(shù)中(有關(guān)此函數(shù)提供的6個(gè)例程,請(qǐng)參閱幫助文件)。這是一個(gè)使用PDF歸一化的示例(所有bin的總和為1)。
data = 2*randn(5000,1) + 5; % generate normal random (m=5, std=2)
h = histogram(data,'Normalization','pdf') % PDF normalization
對(duì)應(yīng)的PDF是
Nbins = h.NumBins;
edges = h.BinEdges;
x = zeros(1,Nbins);
for counter=1:Nbins
midPointShift = abs(edges(counter)-edges(counter+1))/2;
x(counter) = edges(counter)+midPointShift;
end
mu = mean(data);
sigma = std(data);
f = exp(-(x-mu).^2./(2*sigma^2))./(sigma*sqrt(2*pi));
兩者一起給
hold on;
plot(x,f,'LineWidth',1.5)
在此處輸入圖片說明
改進(jìn)很可能歸因于實(shí)際問題和接受的答案的成功!
編輯-使用hist和histc被不建議現(xiàn)在,和histogram應(yīng)改為使用。請(qǐng)注意,使用此新功能創(chuàng)建垃圾箱的6種方法均不會(huì)產(chǎn)生垃圾箱 hist并histc產(chǎn)生。有一個(gè)Matlab腳本可以更新以前的代碼以適應(yīng) histogram調(diào)用方式(bin邊而不是bin中心-link)。這樣,可以比較pdf @abcd(trapz和sum)和Matlab(pdf)的規(guī)范化方法。
3 pdf歸一化方法給出的結(jié)果幾乎相同(在的范圍內(nèi)eps)。
測(cè)試:
A = randn(10000,1);
centers = -6:0.5:6;
d = diff(centers)/2;
edges = [centers(1)-d(1), centers(1:end-1)+d, centers(end)+d(end)];
edges(2:end) = edges(2:end)+eps(edges(2:end));
figure;
subplot(2,2,1);
hist(A,centers);
title('HIST not normalized');
subplot(2,2,2);
h = histogram(A,edges);
title('HISTOGRAM not normalized');
subplot(2,2,3)
[counts, centers] = hist(A,centers); %get the count with hist
bar(centers,counts/trapz(centers,counts))
title('HIST with PDF normalization');
subplot(2,2,4)
h = histogram(A,edges,'Normalization','pdf')
title('HISTOGRAM with PDF normalization');
dx = diff(centers(1:2))
normalization_difference_trapz = abs(counts/trapz(centers,counts) - h.Values);
normalization_difference_sum = abs(counts/sum(counts*dx) - h.Values);
max(normalization_difference_trapz)
max(normalization_difference_sum)
在此處輸入圖片說明
新的PDF規(guī)范化與以前的規(guī)范化之間的最大差是5.5511e-17。

TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超7個(gè)贊
hist不僅可以繪制直方圖,還可以返回每個(gè)bin中元素的數(shù)量,因此您可以獲取該計(jì)數(shù),將每個(gè)bin除以總數(shù)并使用來繪制結(jié)果,以對(duì)其進(jìn)行歸一化bar。例:
Y = rand(10,1);
C = hist(Y);
C = C ./ sum(C);
bar(C)
或者如果您想要單線:
bar(hist(Y) ./ sum(hist(Y)))
編輯:此解決方案回答了問題:如何使所有垃圾箱的總和等于1。僅當(dāng)bin大小相對(duì)于數(shù)據(jù)方差較小時(shí),這種近似才有效。這里使用的總和對(duì)應(yīng)一個(gè)簡(jiǎn)單的正交公式,可以使用更復(fù)雜的公式,如RMtrapz所建議的
- 3 回答
- 0 關(guān)注
- 1353 瀏覽
添加回答
舉報(bào)