Changeset 988

Show
Ignore:
Timestamp:
05/26/10 18:42:06 (14 years ago)
Author:
smidl
Message:

doc

Files:
2 modified

Legend:

Unmodified
Added
Removed
  • applications/bdmtoolbox/mex/mex_classes/mexLaplaceBM.m

    r983 r988  
    1111            obj.apost_pdf.point = [0;0]; 
    1212            obj.log_evidence = 0;                             % evidence is not computed! 
    13             disp('val'); 
    1413        end 
    1514 
     
    3433            p.mu = obj.apost_pdf.point(1); 
    3534            p.b  = obj.apost_pdf.point(2); 
     35                                % do not forget to validate 
    3636            p=p.validate; 
     37                                % assign descriptions 
     38                                p.rv = yrv; 
     39                                % rvc is empty be default 
    3740        end 
    3841    end 
  • library/doc/tutorial/040devguide_matlab.dox

    r983 r988  
    77See \ref ug_pdf_fnc example. 
    88 
    9 This tutorial assumes you are familiar with basic classes of BDM: random variables \ref userguide_pdf and estimators \ref userguide_estim. 
    10  
    11 If no premade class is suitable for your mneeds, you can create your own class in Matlab via predefined interfaces to class epdf (mexEpdf) and BM (mexBM). 
    12 These classes are treated as regular C++ classes in prepared mex files. Creation of a new estimator (offspring of mexBM) can be achieved by inheriting from class mexBM. 
     9This tutorial assumes you are familiar with basic classes of BDM: pdfs, random variables (\ref userguide_pdf) and estimators (\ref userguide_estim). 
     10 
     11Previous part of the tutorial introduced ways how to use existing classes. If no premade class is suitable for your mneeds, you can create your own class in Matlab via predefined interfaces to class epdf (mexEpdf) and BM (mexBM). 
     12These classes are treated as regular C++ classes in prepared mex files. Creation of a new estimator (offspring of class BM) can be achieved by inheriting from class mexBM. 
    1313 
    1414In practice this means creation of a new file, e.g. my_bm.m, with header: 
     
    3232Epdf is a class representing probability density function of a multivariate (vector) variable. 
    3333 
    34 The easiest example of a density is dirac Delta function: 
     34The easiest example of a density is dirac Delta function  
     35\f[ 
     36f(x_t) = \delta( x_t - \bar{x}_t ) 
     37\f] 
     38with properties: 
     39 - \f$ \bar{x}_t \f$ is the statistics of this density. 
     40 - dimension of the density is dimension of the vector \f$ \bar{x}_t \f$ 
     41 - the mean value of the density is \f$ E(x_t) = \bar{x}_t \f$ 
     42 - the variance value of the density is \f$ var(x_t) = zeros() \f$ 
     43 - all samples from this density are equal to \f$ \bar{x}_t \f$ 
     44  
     45 These properties are encoded in software as follows: 
     46  
    3547\code 
    3648classdef mexDirac < mexEpdf 
    3749    % Dirac delta probability distribution 
    3850    properties                                                 % DATA structures 
    39         point                                                  % point of the support 
     51        point                                                  % point of the support, \bar{x}_t 
    4052    end 
    4153    methods 
     
    7385\endcode 
    7486 
    75 Once you define these functions, this object can be plugged into (almost) an arbitrary algoritm, such as simulator, etc. 
    76  
    77 For seamless use with other objects, you nedd to define its random variables - via attribute rv, \ref ug_pdf_create. 
     87An additional compulsory function is called \c validate which is called always before use of the class in an algorithm. The task of this function is to detect any inconsistency in the object and give the user an appropriate warning.  
     88For example, in this case, the function checks if the property point is indeed a vector and not a matrix or other structure. 
     89 
     90Once you define the methdos above, this object can be plugged into (almost) an arbitrary algoritm, such as simulator, etc. 
     91 
     92For seamless use with other objects, you need to define its random variables - via attribute rv, \ref ug_pdf_create. 
    7893 
    7994For inspiration see example class mexLaplace.m and its use in simulation in tutorial/userguide/epdfds_example.m 
    8095 
    81 \section dev_mat_bm Creating your own Bayesian Model (BM) 
    82  
     96\section dev_mat_bm Defining your own Bayesian Estimator (BM) 
    8397If you look into the mexBM.m file you see the basic attributes and default functions. mexBM as such will not work as a valid estimator. 
    8498In order to make it work, you need to re-define at least functions <b>dimensions</b> and <b>bayes</b>, and attribute <b>apost_pdf</b>: 
    85   \code 
    86   function dims=dimensions(p) 
    87       %please fill 
    88       %dims = [size_of_posterior size_of_data size_of_condition] 
    89       dims = [0,0,0] % 
    90   end 
    91   \endcode 
     99   
    92100This function announces sizes of random variables to other objects.  
    93101 
     
    115123Note, that statistics of the posterior are stored in attribute apost_pdf. So, the task of this function is to update fileds in apost_pdf.  
    116124 
    117 Any additional sttributes needed for this task shoul be added to properties of the object. 
    118  
    119 An example calss of BM for Laplace pdf is implemented in class mexLaplaceBM.m. This class represents maximum likelihood estimator of two parameters of the density.  
    120 One parameter is a median of data on a sliding window.   
    121 This can be interpreted as a coarse approximation of Bayesian filtering with posterior reduced to Dirac delta. 
    122  
    123 Hence, we need: 
    124  - apost_pdf to be of type mexDirac, 
    125  - store information about window length in the properties 
    126  - store the windown of data in properties 
    127  - the method bayes adds new data to the window and copies median of the window to the point in apost_pdf. 
     125Any additional attributes needed for this task shoul be added to properties of the object. 
     126 
     127 
     128\section dev_exa Example task: Laplace estimation 
     129 
     130Consider a systems, where data are generated by a Laplace model  
     131\f[ 
     132f(y_t|a,b) = \frac{1}{2b} \exp \left( -\frac{x-a}{b} \right) 
     133\f] 
     134with unknown time-varying parameters \f$ a \f$ and \f$ b \f$.  
     135 
     136We decided to estimate parameters of the system using ML estimates of parameters on a sliding window. The estimates are then: 
     137\f{eqnarray} 
     138\hat{a}& =& \mathrm{median}([y_{t},y_{t-1},\ldots , y_{t-d}),\\ 
     139\hat{b}& = & \frac{1}{d} \sum_{\tau=t-d}^{t} | y_\tau - \hat{a} | 
     140\f} 
     141 
     142Implementing this system in BDM requires the following steps: 
     143 - realize that ML estimation is an extreme approximation of Bayes rule with posterior in the form of Dirac delat pdf, this pdf is ready from the previous step. 
     144 - under this choice, the posterior is a 2-dimensional dirac, which is encoded as: 
     145 \code 
     146 apost_pdf = mexDirac; 
     147 apost_pdf.point = [0;0]; 
     148 \endcode  
     149 - the class mexDirac as introduced has only one attribute: point, which is a vector of the support. If we wish to distinguish the meaning of these values, we use attribute rv. For example,  
     150 \code 
     151 apost_pdf.rv = RV({'a','b'}); 
     152 \endcode 
     153 - the class representing an estimator of this type, needs to remember internal statistics in the form window of data, which will be stored in properties of the class 
     154 
     155 - dimensionality of the estimator is then: 2-dimensional parameters, 1-dimensional observation, 0-dimensional conditioning variables. It is encoded as: 
     156 \code 
     157  function dims=dimensions(p) 
     158      %please fill 
     159      %dims = [size_of_posterior size_of_data size_of_condition] 
     160      dims = [2,1,0] % 
     161  end 
     162  \endcode 
     163 - the method \c bayes then copies the obtained data into the window and updates estimates \f$ \hat{a}, \hat{b}\f$ 
     164 
     165This estimator has been implemented as class mexLaplaceBM as follows: 
    128166  
    129167This is implemented as follows: 
     
    141179            obj.apost_pdf.point = [0;0]; 
    142180            obj.log_evidence = 0;                             % evidence is not computed! 
    143             disp('val'); 
    144181        end 
    145182 
     
    159196            obj.apost_pdf.point = [m_hat; b_hat];             % store result in psoterior pdf 
    160197        end 
    161         function p=epredictor(obj,cond)                       % when predictive density is needed approximate it by Laplace with point estimates 
    162             % return predictive density (max likelihood) 
    163             p = mexLaplace; 
    164             p.mu = obj.apost_pdf.point(1); 
    165             p.b  = obj.apost_pdf.point(2); 
    166             p=p.validate; 
    167         end 
    168     end 
    169  
     198    end 
     199 
     200end 
     201\endcode 
     202 
     203\section dev_mat_alt Alternative representations of this estimator 
     204 
     205Note, that the estimator above is rather coarse approximation of the Bayes rule. Indeed, in this case, the sliding window represnts a statistics on the posterior parameters,  
     206and thus it should be part of the posterior, which can thus evaluate more moments that the current Dirac density. 
     207 
     208Such a design is certainly possible, however, it would result in much more complex calculations and more complex coding. If that is the aim, a new class can be designed. 
     209 
     210\section dev_mat_pred Predictors and simulation 
     211 
     212Note, that the Laplacian likelihood model of the example is not needed for its estimation. The very basic estimator introduced above does not need to know about existence of the likelihood function. 
     213 
     214However, existence of this distribution is required by related tasks of data simulation and data prediction. 
     215Therefore, a class mexLaplace was designed, which implements all operations of pdf in the same manner as the in \ref dev_mat_pdf. 
     216 
     217This pdf is then used in the mexLaplaceBM in function that creates predictors: 
     218\code  
     219function p=epredictor(obj,cond)                       % when predictive density is needed approximate it by Laplace with point estimates 
     220   % return predictive density (max likelihood) 
     221   p = mexLaplace; 
     222   p.mu = obj.apost_pdf.point(1); 
     223   p.b  = obj.apost_pdf.point(2); 
     224        % do not forget to validate 
     225   p=p.validate; 
     226        % assign descriptions 
     227        p.rv = yrv; 
     228        % rvc is empty be default 
    170229end 
    171230\endcode 
    172231   
     232Also, the class mexLaplace can be used to generate data, see bdmtoolbox/mex/epdfds_example.m 
     233 
     234Since it provides all required methods, it can be combined with other pdfs in chain rules, as in pdfds_example.m 
     235 
    173236Similar extensions to mexEpdf and mexBM can be made to other classes. See library/mex/mex_Epdf.h and library/mex/mex_BM.h how it is done. 
    174237As always, feel free to contact the maintainer for help.