Program 10 shows the construction of the kernel map and Figure 3.12 depicts is representation.
1 /*============================================================================= 2 * File : kernelMap.cpp 3 *============================================================================= 4 * 5 *------------------------- Description --------------------------------------- 6 * This program shows the main functionalities of the kernel dictionary: 7 * kernelMap. 8 *----------------------------------------------------------------------------- 9 */ 10 11 12 #include <pl.h> 13 14 main() 15 { 16 /********************************************************************** 17 Defining the variable type, set and values 18 ***********************************************************************/ 19 plRealType Tx(1,100); 20 plRealType Ty(0,200); 21 22 plSymbol x("x",Tx); 23 plSymbol y("y",Ty); 24 25 plValues values(x^y); 26 27 /********************************************************************** 28 Constructing kernel dictionary P( y | x) 29 ***********************************************************************/ 30 plCUniform Py0(y); 31 plNormal Py1(y,100,10); 32 plNormal Py2(y,50,12); 33 plNormal Py3(y,41,5); 34 plGamma Py4(y,5.0,1.0); 35 plGamma Py5(y,8.0,1.0); 36 37 plKernelMap Py(y,x); 38 39 // Pushing the default kernel 40 Py.push_default(Py0); 41 42 // Pushing the pairs (key, kernel) 43 values [x] = 1.78l; 44 Py.push(values,Py1); 45 cout<<"inserting kernel for x= "<<values[x]<<endl; 46 47 values [x] = 60.8l; 48 Py.push(values,Py2); 49 cout<<"inserting kernel for x= "<<values[x]<<endl; 50 51 values [x] = 56.9l; 52 Py.push(values,Py3); 53 cout<<"inserting kernel for x= "<<values[x]<<endl; 54 55 values [x] = 30.0l; 56 Py.push(values,Py4); 57 cout<<"inserting kernel for x= "<<values[x]<<endl; 58 59 values [x] = 40.0l; 60 Py.push(values,Py5); 61 cout<<"inserting kernel for x= "<<values[x]<<"\n\n"; 62 63 cout<<Py<<"\n\n"; 64 65 /********************************************************************** 66 Reading some key values and instantiating the corresponding kernel 67 ***********************************************************************/ 68 69 plFloat vx,vy; 70 plKernel new_kernel; 71 72 for(int i=0;i<3;i++) { 73 cout<<"Value for x: "; 74 cin>>vx; 75 cout<<"Value for y: "; 76 cin>>vy; 77 values[x] = vx; 78 values[y] = vy; 79 if (Py.find(values)) 80 cout<<"Kernel FOUND for x="<<vx<<"\nthe kernel is: "; 81 else 82 cout<<"Kernel NOT FOUND for x="<<vx<<"\nussing default kernel: "; 83 Py.instantiate(new_kernel,values); 84 cout<<new_kernel<<endl; 85 cout<<"compute = "<<new_kernel.compute(values)<<"\n\n"; 86 } 87 }
At its creation the kernel map is empty, line 37, then the default
kernel is specified by means of the push_default
method, line
40. A plValues
is used to indicate values of then, by using
the
push
method, we associate to each of the values a specific
kernel, lines 42 to 61. Finally, we print and test out kernel map,
lines 63 to 86. By giving the and
values a sequence of
computes are executed. For those values of
inserted into the
kernel map the associated kernel is used, for the others values, the
default kernel is employed. An example of the program output is given
below.
inserting kernel for x= 1.78 inserting kernel for x= 60.8 inserting kernel for x= 56.9 inserting kernel for x= 30 inserting kernel for x= 40 P(y | x) = plKernelMap = { (1.78 )= P(y | x) = plNormal(y,100,10) (30 )= P(y | x) = plGamma(y, 5, 1, 0) (40 )= P(y | x) = plGamma(y, 8, 1, 0) (56.9 )= P(y | x) = plNormal(y,41,5) (60.8 )= P(y | x) = plNormal(y,50,12) Default = 0.005 } Value for x: 20.7 Value for y: 75.4 Kernel NOT FOUND for x=20.7 ussing default kernel: P(y) = 0.005 compute = 0.005 Value for x: 1.78 Value for y: 45 Kernel FOUND for x=1.78 the kernel is: P(y) = plNormal(y,100,10) compute = 1.07698e-08 Value for x: 56.9 Value for y: 45.7 Kernel FOUND for x=56.9 the kernel is: P(y) = plNormal(y,41,5) compute = 0.0512943