Boost logo

Boost :

Subject: Re: [boost] [math distributions] Laplace distribution
From: Paul A. Bristow (pbristow_at_[hidden])
Date: 2008-11-28 11:57:40


> -----Original Message-----
> From: boost-bounces_at_[hidden] [mailto:boost-bounces_at_[hidden]]
On
> Behalf Of Thijs van den Berg
> Sent: 28 November 2008 13:58
> To: boost_at_[hidden]
> Subject: Re: [boost] [math distributions] Laplace distribution

> I've done a first commit to the sandbox!
>
> 1) sandbox\math_toolkit\boost\math\distributions\laplace.cpp
> 2) sandbox\math_toolkit\libs\math\test\test_laplace.cpp
>
> ..more later! (doc's, equations, charts, discussion)
>
>
> The unit test file was (is) quite a lot of work, ..there are *so many*
things to consider/check.

;-))
 
> How about this:
> We could write some generic test based on the properties of
distributions...
>
> General relations:
> 1) quantile(cdf(x)) == x
> 2) hazard(x) = pdf(x)/(1-cdf(x))
> 3) pdf(x,location,scale) = pdf( (x-location)/scale, 0, 1)/scale
> 4) cdf(x,location,scale) = cdf( (x-location)/scale, 0, 1)
> 5) cdf(complement(N,x)) = cdf(N(-x))
> 6) quantile(complement(N,p)) = quantile(N(-x,1-p))
>
> perhaps some automatic checking (for all distribution) of error throwing
> 7) support <-> cdf, pdf
> 8) quantile <-> p=0, p=1
>
> And some generic test for distributions with specific properties Symmetric
> distributions:
> pdf(x) = pdf(-x)
> cdf(x) = 1-cdf(-x)
>
> etc.
> we could write template functions for that, that get passed a set of 'x'
values etc

That would indeed be neat - but our tests just grew like Topsy ;-)

And it would be quite a lot of work to change now.

I also worry that the acceptable tolerances vary widely, so you would have
keep passing these as parameters anyway.

We also (still are) a bit schizophrenic about whether to allow infinity as
parameter, so a uniform test to check throw would be difficult.

I note you haven't tried to deal with the long double case. Even if your
system like my MSVC only does double == long double 64 bit reals, we should
have the hooks in place for systems that do proper long doubles.

This rather messy code does this, if appropriate.

#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  test_spots(0.0L); // Test long double.
#if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0582))
  test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
#endif
#else
   std::cout << "<note>The long double tests have been disabled on this
platform "
      "either because the long double overloads of the usual math functions
are "
      "not available at all, or because they are too inaccurate for these
tests "
      "to pass.</note>" << std::cout;
#endif

At least a few really accurate values could be calculated using the
published formula using a 100 decimal digit calculator?
But round tripping should test well and I don't expect any trouble for
higher precision types (in the unlikely event that someone want them - no
accounting for some peoples taste).

(I wonder if we can get rid of the Borland test yet?)

You might also check the convenience typedef works like this?

   // Check that can generate lognormal distribution using the two
convenience methods:
   boost::math::lognormal myf1(1., 2); // Using typedef
   lognormal_distribution<> myf2(1., 2); // Using default RealType double.

http://mathworld.wolfram.com/LaplaceDistribution.html is a good link for the
doc?
Weisstein, Eric W. "Laplace Distribution." From MathWorld--A Wolfram Web
Resource. http://mathworld.wolfram.com/LaplaceDistribution.html

<aside>
(This looks rather amusing
http://demonstrations.wolfram.com/SampleVersusTheoreticalDistribution/).
</aside>

Looking good to me.

Paul

PS You can write

2. 1., , 0.5, 0.25... without the L because they can be exactly represented
as float, double, long double (unlike 0.1, 0.01... )

static_cast<RealType>(0.5)

which saves some typing and clutter.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk