How to normalize a probability density function?
2 次查看(过去 30 天)
显示 更早的评论
When I run this code, the pdf curve does not line up with the normalized histogram. How can I fix this so that the area under the curve is 1?
rng(0); %initialize seed
x=randn(1000,1); %create matrix of random numbers
width=8/29; %size of bin, uses 29 so there will be 30 total bins
binranges=-4:width:4; %sets range of values
[bincounts]=histc(x,binranges); %counts how many x are in each bin
[norbincts]=bincounts./1000; %normalize so area=1
figure %start image
bar(binranges,norbincts,'histc') %add bars
axis([-4 4 0 0.5]) %resize the axes
hold on %keep same plot
M=mean(x); %mean of matrix
S=std(x); %standard deviation of matrix
gauss=pdf('normal',binranges,M,S); %create curve of gaussian dist
plot(binranges,gauss) %add gauss dist to plot
text='Mean = %f Standard deviation = %f'; %title
str=sprintf(text,M,S); %title
title(str); %title
0 个评论
回答(1 个)
Amos
2015-1-9
编辑:Amos
2015-1-9
As I understand it, the Gauss curve is normalized:
trapz(binranges,gauss),trapz(binranges,norbincts)
leads to an output
ans =
0.9999
ans =
0.2759
It seems that the curve is normalized in the sense that the integral is 1, whereas the histogram is normalized in the sense that the sum is 1 (which makes a difference, as the integral is the sum times the bin width).
In order to get the curve over the bars, you can either multiply the curve by width or divide the bars by width.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interpolation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!