Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Normalizing a PDF

Subject: Normalizing a PDF

From: jrenfree

Date: 25 Mar, 2009 16:25:42

Message: 1 of 3

Hi all,

What's the best way to normalize a PDF? Here's an example of some
code for a PDF that I'm trying to normalize:

****************************************
function [Wr,x] = getPDF
e = 0.01;
b = linspace(.005,1,1e4);
x = e./b; % Values to evaluate for PDF

% Define PDF function parameters
gamma = 4;
sigma = 1e-4;

Wr = riceanpdf(x,gamma,sigma); % get PDF

trapz(x,Wr)

function Wr = riceanpdf(e,gamma,sigma)
%% Ricean pdf

q = 2.*e.*sqrt(gamma.*(1+gamma))./sqrt(sigma);

term1 = 2.*e.*(1+gamma)./sigma;
term2 = exp(-1.*((1+gamma).*e.^2+gamma.*sigma)./sigma);
term3 = besseli(0,q);

Wr = term1.*term2.*term3;

Wr(isnan(Wr)) = 0;
**********************************************

When I do trapz(x,Wr), which should be the numerical integration, I'm
getting a value of -.4351 (which I'm not even sure is possible since
Wr has no negative values). How would I normalize this PDF so that
the integration of it, or trapz(x,Wr), is 1?

I've done it for other PDFs by doing something like:

newWr = length(x).*Wr./(sum(Wr).*max(x));

But that doesn't work in this case since the spacing in the x-vector
are not constant.

Thanks!

Subject: Normalizing a PDF

From: Roger Stafford

Date: 25 Mar, 2009 16:59:01

Message: 2 of 3

jrenfree <jrenfree@gmail.com> wrote in message <ab57211a-90f6-4758-b334-1b95f04ed3e1@y34g2000prb.googlegroups.com>...
> Hi all,
>
> What's the best way to normalize a PDF? Here's an example of some
> code for a PDF that I'm trying to normalize:
>
> ****************************************
> function [Wr,x] = getPDF
> e = 0.01;
> b = linspace(.005,1,1e4);
> x = e./b; % Values to evaluate for PDF
>
> % Define PDF function parameters
> gamma = 4;
> sigma = 1e-4;
>
> Wr = riceanpdf(x,gamma,sigma); % get PDF
>
> trapz(x,Wr)
>
> function Wr = riceanpdf(e,gamma,sigma)
> %% Ricean pdf
>
> q = 2.*e.*sqrt(gamma.*(1+gamma))./sqrt(sigma);
>
> term1 = 2.*e.*(1+gamma)./sigma;
> term2 = exp(-1.*((1+gamma).*e.^2+gamma.*sigma)./sigma);
> term3 = besseli(0,q);
>
> Wr = term1.*term2.*term3;
>
> Wr(isnan(Wr)) = 0;
> **********************************************
>
> When I do trapz(x,Wr), which should be the numerical integration, I'm
> getting a value of -.4351 (which I'm not even sure is possible since
> Wr has no negative values). How would I normalize this PDF so that
> the integration of it, or trapz(x,Wr), is 1?
>
> I've done it for other PDFs by doing something like:
>
> newWr = length(x).*Wr./(sum(Wr).*max(x));
>
> But that doesn't work in this case since the spacing in the x-vector
> are not constant.
>
> Thanks!

  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.

  This also has the effect of making the spacing in x very non-uniform, 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?

Roger Stafford

Subject: Normalizing a PDF

From: jrenfree

Date: 25 Mar, 2009 17:25:33

Message: 3 of 3

On Mar 25, 9:59=A0am, "Roger Stafford"
<ellieandrogerxy...@mindspring.com.invalid> wrote:
> jrenfree <jrenf...@gmail.com> wrote in message <ab57211a-90f6-4758-b334-1=
b95f04ed...@y34g2000prb.googlegroups.com>...
> > Hi all,
>
> > What's the best way to normalize a PDF? =A0Here's an example of some
> > code for a PDF that I'm trying to normalize:
>
> > ****************************************
> > function [Wr,x] =3D getPDF
> > e =3D 0.01;
> > b =3D linspace(.005,1,1e4);
> > x =3D e./b; =A0 =A0% Values to evaluate for PDF
>
> > % Define PDF function parameters
> > gamma =3D 4;
> > sigma =3D 1e-4;
>
> > Wr =3D riceanpdf(x,gamma,sigma); =A0 =A0 % get PDF
>
> > trapz(x,Wr)
>
> > function Wr =3D riceanpdf(e,gamma,sigma)
> > %% Ricean pdf
>
> > q =3D 2.*e.*sqrt(gamma.*(1+gamma))./sqrt(sigma);
>
> > term1 =3D 2.*e.*(1+gamma)./sigma;
> > term2 =3D exp(-1.*((1+gamma).*e.^2+gamma.*sigma)./sigma);
> > term3 =3D besseli(0,q);
>
> > Wr =3D term1.*term2.*term3;
>
> > Wr(isnan(Wr)) =3D 0;
> > **********************************************
>
> > When I do trapz(x,Wr), which should be the numerical integration, I'm
> > getting a value of -.4351 (which I'm not even sure is possible since
> > Wr has no negative values). =A0How would I normalize this PDF so that
> > the integration of it, or trapz(x,Wr), is 1?
>
> > I've done it for other PDFs by doing something like:
>
> > newWr =3D length(x).*Wr./(sum(Wr).*max(x));
>
> > But that doesn't work in this case since the spacing in the x-vector
> > are not constant.
>
> > Thanks!
>
> =A0 Your variable of integration, x, is going backwards, which is why you=
 are getting negative answers. =A0When you write x =3D e/b with b increasin=
g, x must decrease.
>
> =A0 This also has the effect of making the spacing in x very non-uniform,=
 so 'trapz' could have accuracy difficulties at the beginning where the jum=
ps in x are largest. =A0The first jump is 0.04 which is very much larger th=
an the last one at 0.000001 . =A0Why are you using this kind of spacing and=
 why go backwards?
>
> Roger Stafford

I'm trying to follow some equations from a paper, and that's the way
it is described. What I'm trying now is re-ordering my x vector so it
is increasing, although I'm a little hesitant to change around
variables when it's not mentioned in the paper.

I don't know if you have time or access to this paper, but the one I'm
talking about is by Trevorrow in 2005 called "The use of moored
inverted echo sounders for monitoring meso-zooplankton and fish near
the ocean surface." I found it on Google Scholars, but you might need
a subscription to view it. If you can get it, the equations in
question are equations 4 through 6.

Thanks.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us