probability density function programming problem
Author Message
probability density function programming problem

Hi,all,
I designed a subroutine to compute integral of one function with
one variable.i.e.
the integral routine is:
double integral(double x,double y);
in this routine,I will use
double func(double x) to compute the function value.
But for probability density function there are many parameters for
each distributiion.for example,it will be better write exponential
distribution as
double pdf_Exp(double mean,double x);
or gaussian distribution
double pdf_Gaussian(double mean,double variance,double x)
Then how can I use such in integral routine.Or can u please tell me
some other way to implement this?
Thanks
Z.Y.

Fri, 26 Nov 2004 16:39:42 GMT
probability density function programming problem

Quote:

>    I designed a subroutine to compute integral of one function with
> one variable.i.e.
> the integral routine is:
> double integral(double x,double y);
> in this routine,I will use
> double func(double x) to compute the function value.
> But for probability density function there are many parameters for
> each distributiion.for example,it will be better write exponential
> distribution as
> double pdf_Exp(double mean,double x);
> or gaussian distribution
> double pdf_Gaussian(double mean,double variance,double x)
> Then how can I use such in integral routine.Or can u please tell me
> some other way to implement this?

I suggest you take a close look at the definition of hshwalk() and
hshexecfn() in my hashlib system.  hshwalk applies hshexecfn to
every non-deleted item in the data base.  Two structures are
passed as void* pointers to allow the user to implement various
things.  Both markov.c and hashtest.c use the abilities.
Everything postpones actual type definitions to the user code.
Available at:

The library is allegedly usable under C++, but never tested.

--

Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>  USE worldnet address!

Fri, 26 Nov 2004 20:54:43 GMT
probability density function programming problem

Quote:
> Hi,all,
>    I designed a subroutine to compute integral of one function with
> one variable.i.e.
> the integral routine is:
> double integral(double x,double y);
> in this routine,I will use
> double func(double x) to compute the function value.
> But for probability density function there are many parameters for
> each distributiion.for example,it will be better write exponential
> distribution as
> double pdf_Exp(double mean,double x);
> or gaussian distribution
> double pdf_Gaussian(double mean,double variance,double x)
> Then how can I use such in integral routine.Or can u please tell me
> some other way to implement this?
> Thanks
> Z.Y.

Functions with variable numbers of parameters can be expressed in C++ as
"functors" -- classes with operator () defines so as to make them have the
syntax of a function. For instance:

class GaussianPDF
{
private:
double
m_mean;

double
m_sigma;

public:
GaussianPDF(double i_mean, double i_sigma) :
m_mean(i_mean), m_sigma(i_sigma) {}

double
operator(double x) const
{
const double k = 0.398942280401433; // 1/sqrt(2*pi)
double temp = (x - m_mu) / m_sigma;
return (k / m_sigma) * exp(-0.5 * temp * temp);
}

Quote:
};

and

class ExponentialPDF
{
private:
double
m_scale;

public:
ExponentialPDF(double i_scale) : m_scale(i_scale) {}

double
operator () (double x)
{
return exp(-x / m_scale) / m_scale;
}

Quote:
};

Now redefine your integrator as

template <typename FUNCTOR>
double integral(double x, double y, FUNCTOR f);

and call it as follows:

double i1 = integral(x, y, GaussianPDF(1.3, 2.6));
double i2 = integral(x, y, ExpontialPDF(3.2));

All code is untested.

Cy

Sat, 27 Nov 2004 00:45:17 GMT
probability density function programming problem
On Mon, 10 Jun 2002 16:45:17 GMT, "Cy Edmunds"

Quote:

>Functions with variable numbers of parameters can be expressed in C++ as
>"functors" -- classes with operator () defines so as to make them have the
>syntax of a function.

If you're going to post C++ code, then please snip Comp.lang.c from
the crossposts.

Quote:
>For instance:

<C++ example snipped>

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>

Sat, 27 Nov 2004 06:06:52 GMT
probability density function programming problem

Quote:
>   I designed a subroutine to compute integral of one function with one
>variable.i.e.
>the integral routine is:
>double integral(double x,double y);
>in this routine,I will use
>double func(double x) to compute the function value.
>But for probability density function there are many parameters for each
>distributiion.for example,it will be better write exponential
>distribution as
>double pdf_Exp(double mean,double x);
>or gaussian distribution
>double pdf_Gaussian(double mean,double variance,double x)
>Then how can I use such in integral routine.Or can u please tell me some
>other way to implement this?

A good way to solve this problem is to use objects.

First, create a generic "probability density function" object.
This object has a routine to integrate its pdf.  Inside the integration
routine, you simply have calls to the function  double pdf(double x)
instead of func.  At the level of this generic object, the pdf function
is not yet defined but is only a degenerate
virtual function that is to be overridden differently by each
descendant of this object.

Second, create each specific probability distribution (Gaussian, exponential, ...)
as a descendent from the generic object.  Each of these instantiates its own
virtual pdf function.  The parameters for each distribution are
stored at the level of the specific object; there can be
as many as required, and the specific object's pdf function
can use them as required.  Thus, your integration routine
(in the generic object) does not need to know how many
parameters are required for each distribution's pdf function.

Sorry if I don't have the C++ terminology just right, but
I have done this in object-oriented Pascal, and it works perfectly.
Good luck.

Jeff

http://psy.otago.ac.nz/miller

Sat, 27 Nov 2004 08:19:03 GMT
probability density function programming problem

Quote:
>On Mon, 10 Jun 2002 16:45:17 GMT, "Cy Edmunds"

>>Functions with variable numbers of parameters can be expressed in C++ as
>>"functors" -- classes with operator () defines so as to make them have the
>>syntax of a function.

>If you're going to post C++ code, then please snip Comp.lang.c from
>the crossposts.

The unfortunate thing about crossposted threads is that they should
remain crossposted.

It's much more constructive to flame the posters who start inappropriately
crossposted threads.  And crossposting between comp.lang.c and
comp.lang.c++ is almost always inappropriate.

Dan
--
Dan Pop
DESY Zeuthen, RZ group

Sat, 27 Nov 2004 20:07:38 GMT
probability density function programming problem

Quote:

> >On Mon, 10 Jun 2002 16:45:17 GMT, "Cy Edmunds"

> >> Functions with variable numbers of parameters can be expressed in
> >> C++ as "functors" -- classes with operator () defines so as to
> >> make them have the syntax of a function.

> >If you're going to post C++ code, then please snip Comp.lang.c from
> >the crossposts.

> The unfortunate thing about crossposted threads is that they should
> remain crossposted.

> It's much more constructive to flame the posters who start
> inappropriately crossposted threads.  And crossposting between
> comp.lang.c and comp.lang.c++ is almost always inappropriate.

What is needed is a foolproof method of setting followups on the
article to which one is replying.  This, combined with an
anticipator module, makes for the best of all possible worlds.

--

Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>  USE worldnet address!

Sat, 27 Nov 2004 21:41:39 GMT
probability density function programming problem

Quote:

>> >On Mon, 10 Jun 2002 16:45:17 GMT, "Cy Edmunds"

>> >> Functions with variable numbers of parameters can be expressed in
>> >> C++ as "functors" -- classes with operator () defines so as to
>> >> make them have the syntax of a function.

>> >If you're going to post C++ code, then please snip Comp.lang.c from
>> >the crossposts.

>> The unfortunate thing about crossposted threads is that they should
>> remain crossposted.

>> It's much more constructive to flame the posters who start
>> inappropriately crossposted threads.  And crossposting between
>> comp.lang.c and comp.lang.c++ is almost always inappropriate.

>What is needed is a foolproof method of setting followups on the
>article to which one is replying.

Nope, even the Followup-To header is a misfeature: each poster has
the right to decide for himself where to post his article, but no
rights at all to decide where other people will post theirs.

If you want to restrict the scope of a subthread, you have to do it
by editing the Newsgroups header of your own post.  This way, most
people will respect your decision and follow up to the same subset
of newsgroups.

Dan
--
Dan Pop
DESY Zeuthen, RZ group

Sun, 28 Nov 2004 00:08:30 GMT
probability density function programming problem

Quote:

... snip ...

> >> It's much more constructive to flame the posters who start
> >> inappropriately crossposted threads.  And crossposting between
> >> comp.lang.c and comp.lang.c++ is almost always inappropriate.

> >What is needed is a foolproof method of setting followups on the
> >article to which one is replying.

> Nope, even the Followup-To header is a misfeature: each poster has
> the right to decide for himself where to post his article, but no
> rights at all to decide where other people will post theirs.

> If you want to restrict the scope of a subthread, you have to do it
> by editing the Newsgroups header of your own post.  This way, most
> people will respect your decision and follow up to the same subset
> of newsgroups.

Deadpan extreme exageration seems to be totally wasted on you.
The anticipator module is also needed.  Underneath it all I was
even agreeing with you. :-[

--

Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>  USE worldnet address!

Sun, 28 Nov 2004 05:09:51 GMT

 Page 1 of 1 [ 9 post ]

Relevant Pages