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. |
| 9 | This tutorial assumes you are familiar with basic classes of BDM: pdfs, random variables (\ref userguide_pdf) and estimators (\ref userguide_estim). |
| 10 | |
| 11 | Previous 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). |
| 12 | These 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. |
34 | | The easiest example of a density is dirac Delta function: |
| 34 | The easiest example of a density is dirac Delta function |
| 35 | \f[ |
| 36 | f(x_t) = \delta( x_t - \bar{x}_t ) |
| 37 | \f] |
| 38 | with 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 | |
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. |
| 87 | An 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. |
| 88 | For example, in this case, the function checks if the property point is indeed a vector and not a matrix or other structure. |
| 89 | |
| 90 | Once you define the methdos above, this object can be plugged into (almost) an arbitrary algoritm, such as simulator, etc. |
| 91 | |
| 92 | For seamless use with other objects, you need to define its random variables - via attribute rv, \ref ug_pdf_create. |
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. |
| 125 | Any 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 | |
| 130 | Consider a systems, where data are generated by a Laplace model |
| 131 | \f[ |
| 132 | f(y_t|a,b) = \frac{1}{2b} \exp \left( -\frac{x-a}{b} \right) |
| 133 | \f] |
| 134 | with unknown time-varying parameters \f$ a \f$ and \f$ b \f$. |
| 135 | |
| 136 | We 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 | |
| 142 | Implementing 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 | |
| 165 | This estimator has been implemented as class mexLaplaceBM as follows: |
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 | |
| 200 | end |
| 201 | \endcode |
| 202 | |
| 203 | \section dev_mat_alt Alternative representations of this estimator |
| 204 | |
| 205 | Note, 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, |
| 206 | and thus it should be part of the posterior, which can thus evaluate more moments that the current Dirac density. |
| 207 | |
| 208 | Such 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 | |
| 212 | Note, 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 | |
| 214 | However, existence of this distribution is required by related tasks of data simulation and data prediction. |
| 215 | Therefore, a class mexLaplace was designed, which implements all operations of pdf in the same manner as the in \ref dev_mat_pdf. |
| 216 | |
| 217 | This pdf is then used in the mexLaplaceBM in function that creates predictors: |
| 218 | \code |
| 219 | function 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 |