| 1 | /*! |
|---|
| 2 | \page userguide_pdf BDM Use - Probability density functions |
|---|
| 3 | |
|---|
| 4 | This section serves as an introduction to basic elements of the BDM: probability density functions, pdfs. |
|---|
| 5 | |
|---|
| 6 | The tutorial is written for the BDM toolbox, if you are interested in use of C++ classes see class reference pages. |
|---|
| 7 | |
|---|
| 8 | \section ug_pdf_create Using built-in pdfs |
|---|
| 9 | |
|---|
| 10 | In BDM toolbox, a pdf is specified by matlab structure, e.g. |
|---|
| 11 | \code |
|---|
| 12 | Nab.class= 'enorm<ldmat>'; |
|---|
| 13 | Nab.mu = [3,2]; |
|---|
| 14 | Nab.R = eye(2); |
|---|
| 15 | Nab.rv = RV({'a','b'}); |
|---|
| 16 | \endcode |
|---|
| 17 | Which encodes information \f$ f(a,b) = \mathcal{N}(mu=[3;2],R=eye(2))\f$. |
|---|
| 18 | \li the keyword "enorm\<ldmat\>" means "Unconditional Normal distribution with covariance matrix in L'DL form", other possibilities are: |
|---|
| 19 | "enorm\<chmat\>" for Choleski decomposition, and "enorm\<fsqmat\>" for full (non-decomposed) matrices. |
|---|
| 20 | \li mu denotes mean value |
|---|
| 21 | \li R denotes variance (written in full matrix regardles of the used decomposition), |
|---|
| 22 | \li rv denotes names assigned to the variables. RV is more complicated structure, but here it is sufficient to use default values. |
|---|
| 23 | \li rv is an optional parameter, some operations do not need it, such as sampling or evaluation of moments |
|---|
| 24 | |
|---|
| 25 | For generating samples try: |
|---|
| 26 | \code |
|---|
| 27 | >> M=epdf_sample_mat(Nab,4); |
|---|
| 28 | \endcode |
|---|
| 29 | which should return 4 samples of the Nab distribution. |
|---|
| 30 | |
|---|
| 31 | For evaluation of mean and variance: |
|---|
| 32 | \code |
|---|
| 33 | >> Nab_m=epdf_mean(Nab); |
|---|
| 34 | >> Nab_v=epdf_variance(Nab); |
|---|
| 35 | \endcode |
|---|
| 36 | |
|---|
| 37 | Other distributions are created analogously, see ??? for their list and parameters??? |
|---|
| 38 | Sampling and evaluation of moments are done by exactly the same functions as for the normal density. |
|---|
| 39 | |
|---|
| 40 | \section ug_pdf_marg Marginalization and conditioning |
|---|
| 41 | |
|---|
| 42 | Basic operations on pdfs are marginalization and conditioning, which are provided by mex functions edpf_marginal and epdf_condition, respectively. |
|---|
| 43 | |
|---|
| 44 | This operation does require the rv parametetr to be fully specified. If it isn't, it will fail with the following message |
|---|
| 45 | \code |
|---|
| 46 | --- fill in the message ---- |
|---|
| 47 | \endcode |
|---|
| 48 | |
|---|
| 49 | If rv is correctly specified, marginal pdf of Nab on variable "a" is obtained by: |
|---|
| 50 | \code |
|---|
| 51 | Na = epdf_marginal(Nab,RV('a')); |
|---|
| 52 | \endcode |
|---|
| 53 | |
|---|
| 54 | Similarly for conditional: |
|---|
| 55 | \code |
|---|
| 56 | Na_b = epdf_condition(Nab,RV('a')); |
|---|
| 57 | Nb_a = epdf_condition(Nab,RV('b')); |
|---|
| 58 | \endcode |
|---|
| 59 | |
|---|
| 60 | \section ug_pdf_cond Conditioned densities |
|---|
| 61 | Note that the result of conditioning is of type "mlnorm\<ldmat\>" which is a special case of pdf with variables in condition, specifically |
|---|
| 62 | \f[ f(a|b) = \mathcal{N}(A*b+const, R)\f] |
|---|
| 63 | i.e. "Normal distributed pdf with mean value as linear function of variable b". |
|---|
| 64 | |
|---|
| 65 | This type of pdfs differ from previously used type is the way of use. For example, it is not possible to sample directly form such density, |
|---|
| 66 | it is necessary to specify what is the value of variable in condition. |
|---|
| 67 | |
|---|
| 68 | That is why a different function is used: |
|---|
| 69 | \code |
|---|
| 70 | Smp=pdf_samplecond_mat(Na_b, 10) |
|---|
| 71 | \endcode |
|---|
| 72 | |
|---|
| 73 | The conditioned and Unconditioned pdf may be combined together in the chain rule. The chain rule can be of two different types: conditioned or unconditioned, i.e.: |
|---|
| 74 | \f[ f(a,b)=f(a|b)f(b), OR, f(a,b|c)=f(a|b)f(b|c)\f] |
|---|
| 75 | Thus it is differently encoded as: |
|---|
| 76 | \code |
|---|
| 77 | fab.class = 'eprod'; % result is unconditioned pdf |
|---|
| 78 | fab.pdfs = {fa_b, fb}; |
|---|
| 79 | |
|---|
| 80 | fab_c.class = 'mprod'; % result is conditioned pdf |
|---|
| 81 | fab_c.pdfs = {fa_b, fb_c}; |
|---|
| 82 | \endcode |
|---|
| 83 | |
|---|
| 84 | \section ug_pdf_fnc Pdfs with functional transformation |
|---|
| 85 | |
|---|
| 86 | In more general type of pdfs, variables in condition may be transformed by a function. |
|---|
| 87 | For example Gaussian density with nonlinear transformation of mean value, \f$ f(x|y) = \mathcal{N}(g(y), R)\f$, is represented by class \c mgnorm |
|---|
| 88 | |
|---|
| 89 | \code |
|---|
| 90 | fx.class = 'mgnorm<ldmat>'; |
|---|
| 91 | fx.g = 'mexFunction'; % function is evaluated in matlab |
|---|
| 92 | fx.g.function = 'test_function'; % name of the matlab function to evaluate |
|---|
| 93 | fx.g.dim = 2; % expected dimension of output |
|---|
| 94 | fx.g.dimc = 2; % expected dimension of input |
|---|
| 95 | fx.R = eye(2); % variance R |
|---|
| 96 | \endcode |
|---|
| 97 | |
|---|
| 98 | This example is using generic function specified by name of Matlab .m file. |
|---|
| 99 | Compulsory fields \c g.dim and \c g.dimc are used to check correct dimension of inputs and outputs of the function. |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | \section ug_pdf_mex Creating user-defined pdfs in Matlab |
|---|
| 103 | |
|---|
| 104 | Definition of new pdf classes in matlab is done by extending (inheriting from) class mexPdf which is defined in file: bdmtoolbox/mex/mex_classes/mexEpdf.m |
|---|
| 105 | |
|---|
| 106 | The file lists all necessary functions that must be filled in order to plug the new class into other bdm algorithms. |
|---|
| 107 | |
|---|
| 108 | Please read Matlab manual for details on its implementation of object oriented programming. |
|---|
| 109 | |
|---|
| 110 | For easier start, an example class, mexLaplace, is defined in \<toolbox_dir\>/mex/mex_classes/mexLaplace.m |
|---|
| 111 | |
|---|
| 112 | Using matlab-extended classes is done via a structure with only two required fields: |
|---|
| 113 | \code |
|---|
| 114 | fL.class = 'mexEpdf'; % declaration of derivative from mexEpdf |
|---|
| 115 | fL.object = mexLaplace; % any particular instance of mexEpdf |
|---|
| 116 | |
|---|
| 117 | fL.object.mu = 1; % set values of attributes of the chosen class, in this case mexLaplace |
|---|
| 118 | fL.object.b = 1; |
|---|
| 119 | \endcode |
|---|
| 120 | |
|---|
| 121 | See example bdmtoolbox/tutorial/userguide/mexpdf_example.m |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | For list of all available pdf objects, see \ref app_base |
|---|
| 125 | */ |
|---|