How to normalize a probability density function?

조회 수: 4 (최근 30일)
Elke
Elke 2015년 1월 9일
편집: Amos 2015년 1월 9일
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

답변 (1개)

Amos
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.

카테고리

Help CenterFile Exchange에서 Interpolation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by