root/doc/latex/manual.tex @ 275

Revision 275, 22.3 kB (checked in by smidl, 16 years ago)

doc

RevLine 
[275]1\index{User Manual@{User Manual}}
2
3This page contains some tutorial examples that will help you getting started using BDM.
4
5Basic linear algebra in BDM (using IT++):\begin{itemize}
6\item \hyperlink{vector_and_matrix}{A very simple tutorial about vectors and matrixes}\item \hyperlink{itfile}{Writing and reading data from files}\item \hyperlink{timer}{Using timers to measure execution time}\end{itemize}
7
8
9Basic concepts and philosophy of BDM:\begin{itemize}
10\item \hyperlink{intro}{Introduction to Bayesian Decision Making Toolbox BDM}\item \hyperlink{ui}{User Infos and their use}\item \hyperlink{mexfiles}{How to write and use mex files for Matlab}\end{itemize}
11
12
13Library of predefined tasks:
14
15Examples for Various mathematical models (BDM):\begin{itemize}
16\item \hyperlink{arx_ui}{Running experiment {\tt estimator} with ARX data fields}\item \hyperlink{kalman}{Examples of (extended) Kalman filtering}\end{itemize}
17
18
19Examples of control (BDM):\begin{itemize}
20\item To be done... \end{itemize}
21\hypertarget{vector_and_matrix}{}\section{A very simple tutorial about vectors and matrixes}\label{vector_and_matrix}
22Let's start with a really simple example. Try to complile the following program:
23
24
25
26\begin{DocInclude}\begin{verbatim}#include <itpp/itbase.h>
27
28using namespace itpp;
29
30//These lines are needed for use of cout and endl
31using std::cout;
32using std::endl;
33
34int main()
35{
36  //Declare vectors and matricies:
37  vec a, b, c;
38  mat A, B;
39
40  //Use the function linspace to define a vector:
41  a = linspace(1.0, 2.0, 10);
42
43  //Use a string of values to define a vector:
44  b = "0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0";
45
46  //Add two vectors:
47  c = a + b;
48
49  //Print results:
50  cout << "a = " << a << endl;
51  cout << "b = " << b << endl;
52  cout << "c = " << c << endl;
53
54  //Use a string to define a matrix:
55  A = "1.0 2.0;3.0 4.0";
56
57  //Calculate the inverse of matrix A:
58  B = inv(A);
59
60  //Print results:
61  cout << "A = " << A << endl;
62  cout << "B = " << B << endl;
63
64  //Exit program:
65  return 0;
66
67}
68\end{verbatim}
69\end{DocInclude}
70
71
72When you run this program, the output shall look like this
73
74
75
76\begin{DocInclude}\begin{verbatim}a = [1 1.11111 1.22222 1.33333 1.44444 1.55556 1.66667 1.77778 1.88889 2]
77b = [0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1]
78c = [1.1 1.31111 1.52222 1.73333 1.94444 2.15556 2.36667 2.57778 2.78889 3]
79A = [[1 2]
80 [3 4]]
81B = [[-2 1]
82 [1.5 -0.5]]
83\end{verbatim}
84\end{DocInclude}
85
86
87If this is what you see, then congratulations! You have managed to compile your first it++ program! \hypertarget{itfile}{}\section{Writing and reading data from files}\label{itfile}
88Here we will use the {\tt it\_\-file} class to store some data. The program {\tt write\_\-it\_\-file.cpp} looks as follows:
89
90
91
92\begin{DocInclude}\begin{verbatim}#include <itpp/itcomm.h>
93
94using namespace itpp;
95
96int main()
97{
98  // Declare the it_file class
99  it_file ff;
100
101  // Open a file with the name "it_file_test.it"
102  ff.open("it_file_test.it");
103
104  // Create some data to put into the file
105  vec a = linspace(1, 20, 20);
106
107  // Put the variable a into the file. The Name("a") tells the file class
108  // that the next variable shall be named "a".
109  ff << Name("a") << a;
110
111  // Force the file to be written to disc. This is useful when performing
112  // iterations and ensures that the information is not stored in any cache
113  // memory. In this simple example it is not necessary to flush the file.
114  ff.flush();
115
116  // Close the file
117  ff.close();
118
119  // Exit program
120  return 0;
121}
122\end{verbatim}
123\end{DocInclude}
124
125
126When you run this program you will obtain a file called {\tt it\_\-file\_\-test.it} in your current directory. You can read the file into Matlab/Octave to view the data by using the following commands:
127
128
129
130\begin{Code}\begin{verbatim}itload('it_file_test.it')
131figure(1); clf;
132plot(a)
133\end{verbatim}
134\end{Code}
135
136
137
138Note: Make sure that {\tt \$PREFIX/share/itpp} is in your Matlab/Octave path and that you run the code above from the directory where {\tt it\_\-file\_\-test.it} is located ({\tt \$PREFIX} is the IT++ installation prefix; {\tt /usr/local} by default).
139
140The IT++ program {\tt read\_\-it\_\-file.cpp} that reads the file and prints its content can look like this:
141
142
143
144\begin{DocInclude}\begin{verbatim}\end{verbatim}
145\end{DocInclude}
146
147
148Here is the output of the program:
149
150
151
152\footnotesize\begin{verbatim}
153a = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]
154\end{verbatim}
155\normalsize
156 \hypertarget{timer}{}\section{Using timers to measure execution time}\label{timer}
157In this example we are using the Real\_\-Timer class to measure the execution time of a simple program. The Real\_\-Timer class is included in the itmisc library.
158
159
160
161\begin{DocInclude}\begin{verbatim}#include <itpp/itbase.h>
162
163using namespace itpp;
164
165//These lines are needed for use of cout and endl
166using std::cout;
167using std::endl;
168
169int main()
170{
171  //Declare the scalars used:
172  long i, sum, N;
173
174  //Declare tt as an instance of the timer class:
175  Real_Timer tt;
176
177  //Initiate the variables:
178  N = 1000000;
179  sum = 0;
180
181  //Start and reset the timer:
182  tt.tic();
183
184  //Do some processing
185  for (i = 0; i < N; i++) {
186    sum += i;
187  }
188
189  // Print the elapsed time
190  tt.toc_print();
191
192  //Print the result of the processing:
193  cout << "The sum of all integers from 0 to " << N - 1 << " equals " << sum << endl;
194
195  //Exit program:
196  return 0;
197
198}
199\end{verbatim}
200\end{DocInclude}
201
202
203When you run this program, the output will look something like this:
204
205
206
207\begin{DocInclude}\begin{verbatim}Elapsed time = 0.000797055 seconds
208The sum of all integers from 0 to 999999 equals 1783293664
209\end{verbatim}
210\end{DocInclude}
211 \hypertarget{intro}{}\section{Introduction to Bayesian Decision Making Toolbox BDM}\label{intro}
212This is a brief introduction into elements used in the BDM. The toolbox was designed for two principle tasks:
213
214\begin{itemize}
215\item Design of Bayesian decisions-making startegies,  \item Bayesian system identification for on-line and off-line scenarios.  \end{itemize}
216Theoretically, the latter is a special case of the former, however we list it separately to highlight its importance in practical applications.
217
218Here, we describe basic objects that are required for implementation of the Bayesian parameter estimation.
219
220Key objects are: \begin{description}
221\item[Bayesian Model: class {\tt BM}  ]which is an encapsulation of the likelihood function, the prior and methodology of evaluation of the Bayes rule. This methodology may be either exact or approximate. \item[Posterior density of the parameter: class {\tt epdf}  ]representing posterior density of the parameter. Methods defined on this class allow any manipulation of the posterior, such as moment evaluation, marginalization and conditioning.  \end{description}
222\hypertarget{intro_bm}{}\section{Class BM}\label{intro_bm}
223The class BM is designed for both on-line and off-line estimation. We make the following assumptions about data: \begin{itemize}
224\item an individual data record is stored in a vector, {\tt vec} {\tt dt}\item a set of data records is stored in a matrix,{\tt mat} {\tt D}, where each column represent one individual data record  \end{itemize}
225
226
227On-line estimation is implemented by method
228
229\begin{Code}\begin{verbatim} void bayes(vec dt)
230\end{verbatim}
231\end{Code}
232
233 Off-line estimation is implemented by method
234
235\begin{Code}\begin{verbatim} void bayesB(mat D)
236\end{verbatim}
237\end{Code}
238
239
240
241As an intermediate product, the bayes rule computes marginal likelihood of the data records $ f(D) $. Numerical value of this quantity which is important e.g. for model selection can be obtained by calling method {\tt \_\-ll()}.\hypertarget{intro_epdf}{}\section{Getting results from BM}\label{intro_epdf}
242Class {\tt BM} offers several ways how to obtain results: \begin{itemize}
243\item generation of posterior or predictive pdfs, methods {\tt \_\-epdf()} and {\tt predictor()}  \item direct evaluation of predictive likelihood, method {\tt logpred()}  \end{itemize}
244Underscore in the name of method {\tt \_\-epdf()} indicate that the method returns a pointer to the internal posterior density of the model. On the other hand, {\tt predictor} creates a new structure of type {\tt epdf()}.
245
246Direct evaluation of predictive pdfs via logpred offers a shortcut for more efficient implementation.\hypertarget{intro_epdf}{}\section{Getting results from BM}\label{intro_epdf}
247As introduced above, the results of parameter estimation are in the form of probability density function conditioned on numerical values. This type of information is represented by class {\tt epdf}.
248
249This class allows such as moment evaluation via methods {\tt mean()} and {\tt variance()}, marginalization via method {\tt marginal()}, and conditioning via method {\tt condition()}.
250
251Also, it allows generation of a sample via {\tt sample()} and evaluation of one value of the posterior parameter likelihood via {\tt evallog()}. Multivariate versions of these operations are also available by adding suffix {\tt \_\-m}, i.e. {\tt sample\_\-m()} and {\tt evallog\_\-m()}. These methods providen multiple samples and evaluation of likelihood in multiple points respectively.\hypertarget{intro_pc}{}\section{Classes for probability calculus}\label{intro_pc}
252When a more demanding task then generation of point estimate of the parameter is required, the power of general probability claculus can be used. The following classes (together with {\tt epdf} introduced above) form the basis of the calculus: \begin{itemize}
253\item {\tt mpdf} a pdf conditioned on another symbolic variable, \item {\tt RV} a symbolic variable on which pdfs are defined. \end{itemize}
254The former class is an extension of mpdf that allows conditioning on a symbolic variable. Hence, when numerical results - such as samples - are required, numericla values of the condition must be provided. The names of methods of the {\tt epdf} are used extended by suffix {\tt cond}, i.e. {\tt samplecond()}, {\tt evallogcond()}, where {\tt cond} precedes matrix estension, i.e. {\tt samplecond\_\-m()} and {\tt evallogcond\_\-m()}.
255
256The latter class is used to identify how symbolic variables are to be combined together. For example, consider the task of composition of pdfs via the chain rule: \[ f(a,b,c) = f(a|b,c) f(b) f(c) \] In our setup, $ f(a|b,c) $ is represented by an {\tt mpdf} while $ f(b) $ and $ f(c) $ by two {\tt epdfs}. We need to distinguish the latter two from each other and to deside in which order they should be added to the mpdf. This distinction is facilitated by the class {\tt RV} which uniquely identify a random varibale.
257
258Therefore, each pdf keeps record on which RVs it represents; {\tt epdf} needs to know only one {\tt RV} stored in the attribute {\tt rv}; {\tt mpdf} needs to keep two {\tt RVs}, one for variable on which it is defined ({\tt rv}) and one for variable incondition which is stored in attribute {\tt rvc}. \hypertarget{ui}{}\section{User Infos and their use}\label{ui}
259For easier interaction with users, experiments can be configured via structures called User Info. These structures contain information about details of the experiment that is to be performed. Since experiments involve the use of basic BDM classes and their compositions, the experiment description is also hierarchical with specific user info for each object or class.
260
261The User Infos are designed using customized version of the libconfig library (link!). It is specialized for BDM so as to recognize basic mathematical objects, such as vectors and matrices, see ... for details.
262
263(Technically it can be made compatible with matlab structures!)
264
265For example a simple experiment can be configures in a following way:
266
267\begin{Code}\begin{verbatim}ndat = 100;                //number of data points
268prior = {type="enorm";
269        mu = [1, 2, 3];
270        R = [1, 0, 0,
271             0, 1, 0,
272             0, 0, 1];
273};
274\end{verbatim}
275\end{Code}
276
277 Exact meaning of root fields in this structure (i.e. ndat and prior) is defined by the application (or mex file) that is using this configuration file. It will look for expected fields and it will ignore any other structures. When it does not find what it is looking for, it terminates with an appropriate error message.
278
279A structure with field {\tt type=\char`\"{}identifier\char`\"{}} is special. Such a structure will be parsed by an appropriate class \hyperlink{classbdm_1_1UIbuilder}{bdm::UIbuilder} which will construct the desired object, in this instance of an object of the class \hyperlink{classbdm_1_1enorm}{bdm::enorm}. For a detailed example how this mechanism works in practice see \hyperlink{arx_ui}{Running experiment {\tt estimator} with ARX data fields}. \hypertarget{mexfiles}{}\section{How to write and use mex files for Matlab}\label{mexfiles}
280\hypertarget{mexfiles_use}{}\section{Howto use predefined mexfiles}\label{mexfiles_use}
281A range of mexfiles is predefined in directory {\tt library/mex}. Many of these mexfile process ui files (see \hyperlink{ui}{User Infos and their use}) examples of these files are in directory {\tt library/tutorial}. Note that in order to run these files you need to let matlab know where to find them:
282
283\begin{Code}\begin{verbatim}>> addpath path_to_bdm/library/mex
284\end{verbatim}
285\end{Code}
286
287
288
289Then, you can go to {\tt library/tutorial} and run e.g. {\tt arx\_\-test\_\-mex}.\hypertarget{mexfiles_write}{}\section{Howto write custom mex file}\label{mexfiles_write}
290Due to special nature of the mex files, the mex file can be split in three parts: \begin{itemize}
291\item input conversion, where input arguments are converted to IT++ structures, \item main body of algorithm, where any C++ bdm constructions can be used, \item output conversion, where resulting IT++ structures are converted to mxArrays.\end{itemize}
292The first and the third part is achieved using prepared IT++ routines, see IT++ documentation.
293
294Script {\tt }./buildmex is prepared to compile and link the mexfile with bdm.
295
296\begin{Code}\begin{verbatim}$ ./buildmex.sh my_mex_file.cpp
297\end{verbatim}
298\end{Code}
299
300 on Linux, or
301
302\begin{Code}\begin{verbatim}$ ./buildmex.bat my_mex_file.cpp
303\end{verbatim}
304\end{Code}
305
306 on Windows.
307
308Example of a mexfile:
309
310\begin{DocInclude}\begin{verbatim}#include <itpp/itmex.h>
311#include <estim/arx.h>
312
313using namespace bdm;
314
315void mexFunction(int n_output, mxArray *output[], int n_input, const mxArray *input[])
316{
317    // Check the number of inputs and output arguments
318        if(n_output!=1) mexErrMsgTxt("Wrong number of output variables!");
319        if(n_input!=2) mexErrMsgTxt("Usage: arx1d(ysize, Data)!");
320
321    // Convert input variables to IT++ format
322        int ysize = mxArray2int(input[0]);
323        mat Data = mxArray2mat(input[1]);
324
325    // ------------------ Start of routine ---------------------------
326        ARX Ar;
327        Ar.set_statistics(ysize, 1e-5*eye(Data.rows()) );
328        Ar.bayesB(Data);
329        // ------------------ End of routine -----------------------------
330
331    // Create output vectors
332        output[0] = mxCreateDoubleMatrix(1,Data.rows(), mxREAL);
333
334    // Convert the IT++ format to Matlab format for output
335        vec2mxArray(Ar.posterior().mean(), output[0]);
336}
337\end{verbatim}
338\end{DocInclude}
339 \hypertarget{arx_ui}{}\section{Running experiment $\backslash$c estimator with ARX data fields}\label{arx_ui}
340The experiment estimator::cpp can be run either on command line, or as a mex file in Matlab.\hypertarget{arx_ui_cmd}{}\section{Command-line usage}\label{arx_ui_cmd}
341In order to use it for estimation of an ARX model, we can define the following \hyperlink{ui}{User Infos and their use} structure:
342
343\begin{DocInclude}\begin{verbatim}//Data generating system
344system = {
345        type = "ArxDS";
346        y = {type="rv"; names=["y", "u"];};
347        u = {type="rv"; names=[]; };
348        rgr = {type="rv";
349                names = ["y","y","y","u"];
350                times = [-1, -2, -3, -1];
351        };
352        //AR parameters
353        theta = [0.8, -0.3, 0.4, 1.0,
354                 0.0, 0.0, 0.0, 0.0];
355        // offset
356        offset = [0.0, 0.0];
357        //variance
358        r = [0.1, 0.0,
359             0.0, 1.0];
360        // log also theta
361        opt="L_theta";
362};
363
364//store results
365logger = {
366        type= "dirfilelog";
367        dirname = "exp/arx_ui";
368        maxlen = 1000; //
369};
370
371//estimation
372estimator = {
373        type = "ARXest";
374        y = {type="rv"; names=["y"]; };
375        rgr = {type="rv";
376                names = ["y","y","y","u"];
377                times = [-1, -2, -3, -1];
378        };
379
380        //optional fields
381        dV0 = [1e-3, 1e-5, 1e-5, 1e-5, 1e-5]; //default: 1e-3 for y, 1e-5 for rgr
382        nu0 = 8.;      //default: rgrlen + 2
383        frg = 1.0;    // forgetting, default frg=1.0
384};
385
386//experiment description
387experiment:{
388        ndat = 9000;
389};
390\end{verbatim}
391\end{DocInclude}
392
393
394The structure is interpreted by application {\tt estimator}, which looks for fields: \begin{description}
395\item[system]description of a Data Source generating Data. The structure must by UI with {\tt type=\char`\"{}DS\_\-offspring\char`\"{}}. In our example, it is of type \char`\"{}ArxDS\char`\"{} which is parsed by bdm::UIArxDS UIbuilder generating a Data Source simulating ARX process. \item[estimator ]description of a Baysian model used to estimate parameters of the data model. In this case, it is of type \char`\"{}ARXest\char`\"{} which is parsed by bdm::UIARX UIbuilder generating Bayesian estimator of autoregressive processess. \item[logger]description of a way how to store results. UI is of {\tt type=\char`\"{}logger\_\-offspring\char`\"{}}. In this case, it is of class \char`\"{}dirfilelog\char`\"{} which is parsed by bdm::UIdirfilelog which generates object storing data in directory specified by dirname=\char`\"{}\char`\"{} field in fileformat understood by program kst. \end{description}
396When the application estimator is run with the above code, it produces a directory of data files, which can be displayed by program kst. Expected results are:  \begin{Image}
397\begin{center}
398\includegraphics[width=\linewidth]{arx_ui_kst.png}\caption{Typical run of estimator arx\_\-test.cfg}
399\end{center}
400\end{Image}
401\hypertarget{arx_ui_mex}{}\section{Matlab mex file}\label{arx_ui_mex}
402The matlab mex file can be run with exactly the same configuration as above. However, when we wish to see the results in Matlab, we may wish to change the logger object to {\tt type=\char`\"{}mexlog\char`\"{}} which will store the results in a matlab structure.
403
404The exact configuration file may look as follows:
405
406\begin{DocInclude}\begin{verbatim}//Data generating system
407system = { type="external"; filename="arx_test.cfg"; path="system";};
408
409//store results
410logger = {
411        type= "mexlog";
412        maxlen = 90; //
413    //dirname = "exp/ax";
414};
415
416//estimation
417estimator = {
418   type = "ARXest";
419        y = {type="rv"; names=["y"]; };
420        rgr = {type="rv";
421                names = ["y","y","y","u"];
422                times = [-1, -2, -3, -1];
423        };
424
425        //optional fields
426        dV0 = [1e-3, 1e-5, 1e-5, 1e-5, 1e-5]; //default: 1e-3 for y, 1e-5 for rgr
427        //nu0 = 8.;      //default: rgrlen + 2
428        frg = .9991;    // forgetting, default frg=1.0
429};
430
431//experiment description
432experiment:{
433        ndat = 90;
434};
435\end{verbatim}
436\end{DocInclude}
437
438
439The resulting structure can be displayed using matlab script arx\_\-test\_\-disp.m, typically producing the following results:  \begin{Image}
440\begin{center}
441\includegraphics[width=\linewidth]{arx_ui_mex.png}\caption{Typical run of matlab file arx\_\-test\_\-disp}
442\end{center}
443\end{Image}
444 \hypertarget{kalman}{}\section{Examples of (extended) Kalman filtering}\label{kalman}
445Kalman filtering and Extended Kalman filtering are special cases of Bayesian filtering. The Kalman filter is optimal for linear state space model with Gaussian disturbances, the extended Kalman filter is derived as linearization of non-linear state space models with Gaussian noises. Hence it is only sub-optimal filter.
446
447More advanced filtering algorithms for non-linear non-Gaussian models can be derived, see ...\hypertarget{kalman_klm}{}\section{Kalman Filtering}\label{kalman_klm}
448Kalman filtering is optimal estimation procedure for linear state space model: \begin{eqnarray} x_t &= &A x_{t-1} + B u_{t} + v_t,\\ y_t &= &C x_{t} + D u_{t} + w_t, \end{eqnarray} where $ x_t $ is the state, $ y_t $ is the system output, $ A, B, C, D$ are state matrices of appropriate dimensions, $v_t, w_t$ are zero mean Gaussian noises with covariance matrices $Q, R$, respectively.
449
450Both prior and posterior densities on the state are Gaussian, i.e. of the class enorm.
451
452There is a range of classes that implements this functionality, namely:\begin{itemize}
453\item \hyperlink{classbdm_1_1KalmanFull}{bdm::KalmanFull} which implements the estimation algorithm on full matrices,\item \hyperlink{classbdm_1_1KalmanCh}{bdm::KalmanCh} which implements the estimation algorithm using choleski decompositions and QR algorithm.\end{itemize}
454\hypertarget{kalman_ekf}{}\section{Extended Kalman Filtering}\label{kalman_ekf}
455Extended Kalman filtering arise by linearization of non-linear state space model: \begin{eqnarray} x_t &= &g( x_{t-1}, u_{t}) + v_t,\\ y_t &= &h( x_{t} , u_{t}) + w_t, \end{eqnarray} where $ g(), h() $ are general non-linear functions which have finite derivatives. Remaining variables have the same meaning as in the Kalman Filter.
456
457In order to use this class, the non-linear functions and their derivatives must be defined as an instance of class {\tt diffbifn}.
458
459Two classes are defined:\begin{itemize}
460\item \hyperlink{classbdm_1_1EKFfull}{bdm::EKFfull} on full size matrices,\item \hyperlink{classbdm_1_1EKFCh}{bdm::EKFCh} on Choleski decompositions and using QR algorithm.\end{itemize}
461\hypertarget{kalman_exa}{}\section{Examples of Use}\label{kalman_exa}
462The classes can be used directly in C++ or via User Info. The latter example is illustrated in file estimator. A very short example of the former follows:
463
464
465
466\begin{DocInclude}\begin{verbatim}#include <estim/libKF.h>
467using namespace bdm;
468       
469// estimation of AR(0) model
470int main() {
471        //dimensions
472        int dx=3, dy=3, du=1;
473        // matrices
474        mat A = eye(dx);
475        mat B = zeros(dx,du);
476        mat C = eye(dx);
477        mat D = zeros(dy,du);
478        mat Q = eye(dx);
479        mat R = 0.1*eye(dy);
480        //prior
481        mat P0 = 100*eye(dx);
482        vec mu0 = zeros(dx);
483        // Estimator
484        KalmanCh KF;
485        KF.set_parameters(A,B,C,D,/*covariances*/ Q,R);
486        KF.set_statistics(mu0,P0);
487        // Estimation loop
488        for (int i=0;i<100;i++){
489                KF.bayes(randn(dx+du));
490        }
491        //print results
492        cout << "Posterior estimate of x is: "  << endl;
493        cout << "mean: "<< KF.posterior().mean()<< endl;
494        cout << "variance: "<< KF.posterior().variance()<< endl;
495}
496\end{verbatim}
497\end{DocInclude}
498 
Note: See TracBrowser for help on using the browser.