3 回答

TA貢獻1815條經驗 獲得超6個贊
我對此的回答與您對先前問題的回答相同。對于概率密度函數,整個空間的積分為1。除以總和不會得到正確的密度。為了獲得正確的密度,必須除以面積。為了說明我的觀點,請嘗試以下示例。
[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
您可以自己查看哪種方法與正確答案(紅色曲線)相符。
標準化直方圖的另一種方法(比方法2更直接)是除以sum(f * dx)表示概率密度函數的積分,即
% 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貢獻1871條經驗 獲得超8個贊
自2014b起,Matlab將這些規范化例程本機嵌入在histogram函數中(有關此函數提供的6個例程,請參閱幫助文件)。這是一個使用PDF歸一化的示例(所有bin的總和為1)。
data = 2*randn(5000,1) + 5; % generate normal random (m=5, std=2)
h = histogram(data,'Normalization','pdf') % PDF normalization
對應的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)
在此處輸入圖片說明
改進很可能歸因于實際問題和接受的答案的成功!
編輯-使用hist和histc被不建議現在,和histogram應改為使用。請注意,使用此新功能創建垃圾箱的6種方法均不會產生垃圾箱 hist并histc產生。有一個Matlab腳本可以更新以前的代碼以適應 histogram調用方式(bin邊而不是bin中心-link)。這樣,可以比較pdf @abcd(trapz和sum)和Matlab(pdf)的規范化方法。
3 pdf歸一化方法給出的結果幾乎相同(在的范圍內eps)。
測試:
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規范化與以前的規范化之間的最大差是5.5511e-17。

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