For instance, suppose you want to construct a kernel corresponding to a Laplace distribution (see Figure 4.3):
where is the mean and
is the
variance. A kernel representing this distribution can be constructed by
means of an anonymous kernel. The basic idea is very simple and is
depicted by the following example.
1 /*============================================================================= 2 * File : laplace.cpp 3 *============================================================================= 4 * 5 *------------------------- Description --------------------------------------- 6 * This program implements an anonymous kernel corresponding to a Laplace 7 * distribution with mu = 3.0 an b = 0.6. It generates a gnuplot file 8 * that depicts the histogram resulting from drawing 100000 values from 9 * the Laplace distribution. To see the histogram make "gnuplot test_laplace". 10 *----------------------------------------------------------------------------- 11 */ 12 13 #include <pl.h> 14 15 #define B 0.6 16 #define MU 3.0 17 18 19 plProbValue laplace(const plValues &x) 20 { 21 double xx = x[0]; 22 23 return (1.0/(2.0*B)*exp(-fabs(xx-MU)/B)); 24 } 25 26 27 main() 28 { 29 /********************************************************************** 30 Defining the variable type, set and values 31 ***********************************************************************/ 32 33 plRealType distance(0,10,1000); 34 plSymbol x("x",distance); 35 plValues v(x); 36 37 /********************************************************************** 38 Creating the external function and the anonymous kernel 39 ***********************************************************************/ 40 41 plExternalProbFunction laplace_dist(x,laplace); 42 plAnonymousKernel Px(x,laplace_dist); 43 44 /********************************************************************** 45 Drawing 10 000 values an writing the resulting histogram in the 46 gnuplot file "test_laplace" 47 ***********************************************************************/ 48 49 Px.test_draw(100000,"test_laplace"); 50 51 /********************************************************************** 52 Getting and showing the best value of the distribution 53 ***********************************************************************/ 54 55 Px.best(v); 56 cout<<"The best is "<<v<<endl; 57 }
Program 15 shows the solution. We begin by defining
an external probability function, line 41; an intermediary object
that allows to construct the anonymous kernels at line 42. The
function of
Px
is then the Laplace distribution
function coded by lines 19 to 24. At line 49 a test of 100 000 random
values is generated with Px
, the result is a gnuplot file that
plots the resulting histogram4.2, see
Figure 4.4. Finally, lines 55 and 56 compute
and show the following result:
The best is { x=3.00004 }
To get the previous value, ProBT runs an optimization algorithm with the
external probability function, for more details see section
.
A new version of Program 15 (using an object class) is obtainin by substituting lines 19 to 24 by
class laplace_pdf { double mu; // mean double b; // variance is 2*b^2 public: // Constructor from the mean end b parameters laplace_pdf(const double &mean, const double &the_b); // This method returns the evaluation of f(x) where f(x) is a // Laplace pdf with mean mu and variance 2*b^2 plProbValue laplace(const plValues &x); }; // Constructor laplace_pdf::laplace_pdf(const double &mean, const double &the_b) { mu = mean; b = the_b; } // Returns Laplace(x,mean,b) plProbValue laplace_pdf::laplace(const plValues &x) { double xx = x[0]; return (1.0/(2.0*b)*exp(-fabs(xx-mu)/b)); }
and lines 41 and 42 by
laplace_pdf my_pdf_obj(MU,B); plExternalProbFunction laplace_dist(x,&my_pdf_obj,&laplace_pdf::laplace); plAnonymousKernel Px(x,laplace_dist);