http://www.mathworks.com/matlabcentral/newsreader/view_thread/247501
MATLAB Central Newsreader  Normalizing a PDF
Feed for thread: Normalizing a PDF
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Wed, 25 Mar 2009 16:25:42 +0000
Normalizing a PDF
http://www.mathworks.com/matlabcentral/newsreader/view_thread/247501#637648
jrenfree
Hi all,<br>
<br>
What's the best way to normalize a PDF? Here's an example of some<br>
code for a PDF that I'm trying to normalize:<br>
<br>
****************************************<br>
function [Wr,x] = getPDF<br>
e = 0.01;<br>
b = linspace(.005,1,1e4);<br>
x = e./b; % Values to evaluate for PDF<br>
<br>
% Define PDF function parameters<br>
gamma = 4;<br>
sigma = 1e4;<br>
<br>
Wr = riceanpdf(x,gamma,sigma); % get PDF<br>
<br>
trapz(x,Wr)<br>
<br>
function Wr = riceanpdf(e,gamma,sigma)<br>
%% Ricean pdf<br>
<br>
q = 2.*e.*sqrt(gamma.*(1+gamma))./sqrt(sigma);<br>
<br>
term1 = 2.*e.*(1+gamma)./sigma;<br>
term2 = exp(1.*((1+gamma).*e.^2+gamma.*sigma)./sigma);<br>
term3 = besseli(0,q);<br>
<br>
Wr = term1.*term2.*term3;<br>
<br>
Wr(isnan(Wr)) = 0;<br>
**********************************************<br>
<br>
When I do trapz(x,Wr), which should be the numerical integration, I'm<br>
getting a value of .4351 (which I'm not even sure is possible since<br>
Wr has no negative values). How would I normalize this PDF so that<br>
the integration of it, or trapz(x,Wr), is 1?<br>
<br>
I've done it for other PDFs by doing something like:<br>
<br>
newWr = length(x).*Wr./(sum(Wr).*max(x));<br>
<br>
But that doesn't work in this case since the spacing in the xvector<br>
are not constant.<br>
<br>
Thanks!

Wed, 25 Mar 2009 16:59:01 +0000
Re: Normalizing a PDF
http://www.mathworks.com/matlabcentral/newsreader/view_thread/247501#637660
Roger Stafford
jrenfree <jrenfree@gmail.com> wrote in message <ab57211a90f64758b3341b95f04ed3e1@y34g2000prb.googlegroups.com>...<br>
> Hi all,<br>
> <br>
> What's the best way to normalize a PDF? Here's an example of some<br>
> code for a PDF that I'm trying to normalize:<br>
> <br>
> ****************************************<br>
> function [Wr,x] = getPDF<br>
> e = 0.01;<br>
> b = linspace(.005,1,1e4);<br>
> x = e./b; % Values to evaluate for PDF<br>
> <br>
> % Define PDF function parameters<br>
> gamma = 4;<br>
> sigma = 1e4;<br>
> <br>
> Wr = riceanpdf(x,gamma,sigma); % get PDF<br>
> <br>
> trapz(x,Wr)<br>
> <br>
> function Wr = riceanpdf(e,gamma,sigma)<br>
> %% Ricean pdf<br>
> <br>
> q = 2.*e.*sqrt(gamma.*(1+gamma))./sqrt(sigma);<br>
> <br>
> term1 = 2.*e.*(1+gamma)./sigma;<br>
> term2 = exp(1.*((1+gamma).*e.^2+gamma.*sigma)./sigma);<br>
> term3 = besseli(0,q);<br>
> <br>
> Wr = term1.*term2.*term3;<br>
> <br>
> Wr(isnan(Wr)) = 0;<br>
> **********************************************<br>
> <br>
> When I do trapz(x,Wr), which should be the numerical integration, I'm<br>
> getting a value of .4351 (which I'm not even sure is possible since<br>
> Wr has no negative values). How would I normalize this PDF so that<br>
> the integration of it, or trapz(x,Wr), is 1?<br>
> <br>
> I've done it for other PDFs by doing something like:<br>
> <br>
> newWr = length(x).*Wr./(sum(Wr).*max(x));<br>
> <br>
> But that doesn't work in this case since the spacing in the xvector<br>
> are not constant.<br>
> <br>
> Thanks!<br>
<br>
Your variable of integration, x, is going backwards, which is why you are getting negative answers. When you write x = e/b with b increasing, x must decrease.<br>
<br>
This also has the effect of making the spacing in x very nonuniform, so 'trapz' could have accuracy difficulties at the beginning where the jumps in x are largest. The first jump is 0.04 which is very much larger than the last one at 0.000001 . Why are you using this kind of spacing and why go backwards?<br>
<br>
Roger Stafford

Wed, 25 Mar 2009 17:25:33 +0000
Re: Normalizing a PDF
http://www.mathworks.com/matlabcentral/newsreader/view_thread/247501#637670
jrenfree
On Mar 25, 9:59=A0am, "Roger Stafford"<br>
<ellieandrogerxy...@mindspring.com.invalid> wrote:<br>
> jrenfree <jrenf...@gmail.com> wrote in message <ab57211a90f64758b3341=<br>
b95f04ed...@y34g2000prb.googlegroups.com>...<br>
> > Hi all,<br>
><br>
> > What's the best way to normalize a PDF? =A0Here's an example of some<br>
> > code for a PDF that I'm trying to normalize:<br>
><br>
> > ****************************************<br>
> > function [Wr,x] =3D getPDF<br>
> > e =3D 0.01;<br>
> > b =3D linspace(.005,1,1e4);<br>
> > x =3D e./b; =A0 =A0% Values to evaluate for PDF<br>
><br>
> > % Define PDF function parameters<br>
> > gamma =3D 4;<br>
> > sigma =3D 1e4;<br>
><br>
> > Wr =3D riceanpdf(x,gamma,sigma); =A0 =A0 % get PDF<br>
><br>
> > trapz(x,Wr)<br>
><br>
> > function Wr =3D riceanpdf(e,gamma,sigma)<br>
> > %% Ricean pdf<br>
><br>
> > q =3D 2.*e.*sqrt(gamma.*(1+gamma))./sqrt(sigma);<br>
><br>
> > term1 =3D 2.*e.*(1+gamma)./sigma;<br>
> > term2 =3D exp(1.*((1+gamma).*e.^2+gamma.*sigma)./sigma);<br>
> > term3 =3D besseli(0,q);<br>
><br>
> > Wr =3D term1.*term2.*term3;<br>
><br>
> > Wr(isnan(Wr)) =3D 0;<br>
> > **********************************************<br>
><br>
> > When I do trapz(x,Wr), which should be the numerical integration, I'm<br>
> > getting a value of .4351 (which I'm not even sure is possible since<br>
> > Wr has no negative values). =A0How would I normalize this PDF so that<br>
> > the integration of it, or trapz(x,Wr), is 1?<br>
><br>
> > I've done it for other PDFs by doing something like:<br>
><br>
> > newWr =3D length(x).*Wr./(sum(Wr).*max(x));<br>
><br>
> > But that doesn't work in this case since the spacing in the xvector<br>
> > are not constant.<br>
><br>
> > Thanks!<br>
><br>
> =A0 Your variable of integration, x, is going backwards, which is why you=<br>
are getting negative answers. =A0When you write x =3D e/b with b increasin=<br>
g, x must decrease.<br>
><br>
> =A0 This also has the effect of making the spacing in x very nonuniform,=<br>
so 'trapz' could have accuracy difficulties at the beginning where the jum=<br>
ps in x are largest. =A0The first jump is 0.04 which is very much larger th=<br>
an the last one at 0.000001 . =A0Why are you using this kind of spacing and=<br>
why go backwards?<br>
><br>
> Roger Stafford<br>
<br>
I'm trying to follow some equations from a paper, and that's the way<br>
it is described. What I'm trying now is reordering my x vector so it<br>
is increasing, although I'm a little hesitant to change around<br>
variables when it's not mentioned in the paper.<br>
<br>
I don't know if you have time or access to this paper, but the one I'm<br>
talking about is by Trevorrow in 2005 called "The use of moored<br>
inverted echo sounders for monitoring mesozooplankton and fish near<br>
the ocean surface." I found it on Google Scholars, but you might need<br>
a subscription to view it. If you can get it, the equations in<br>
question are equations 4 through 6.<br>
<br>
Thanks.