root/library/doc/html/user_guide.html @ 617

Revision 617, 43.3 kB (checked in by smidl, 15 years ago)

simulator + doc

Line 
1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html xmlns="http://www.w3.org/1999/xhtml">
3<head>
4<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
5<title>mixpp: Howto Use BDM - System, Data, Simulation</title>
6<link href="tabs.css" rel="stylesheet" type="text/css"/>
7<link href="doxygen.css" rel="stylesheet" type="text/css"/>
8</head>
9<body>
10<!-- Generated by Doxygen 1.6.1 -->
11<script type="text/javascript">
12<!--
13function changeDisplayState (e){
14  var num=this.id.replace(/[^[0-9]/g,'');
15  var button=this.firstChild;
16  var sectionDiv=document.getElementById('dynsection'+num);
17  if (sectionDiv.style.display=='none'||sectionDiv.style.display==''){
18    sectionDiv.style.display='block';
19    button.src='open.gif';
20  }else{
21    sectionDiv.style.display='none';
22    button.src='closed.gif';
23  }
24}
25function initDynSections(){
26  var divs=document.getElementsByTagName('div');
27  var sectionCounter=1;
28  for(var i=0;i<divs.length-1;i++){
29    if(divs[i].className=='dynheader'&&divs[i+1].className=='dynsection'){
30      var header=divs[i];
31      var section=divs[i+1];
32      var button=header.firstChild;
33      if (button!='IMG'){
34        divs[i].insertBefore(document.createTextNode(' '),divs[i].firstChild);
35        button=document.createElement('img');
36        divs[i].insertBefore(button,divs[i].firstChild);
37      }
38      header.style.cursor='pointer';
39      header.onclick=changeDisplayState;
40      header.id='dynheader'+sectionCounter;
41      button.src='closed.gif';
42      section.id='dynsection'+sectionCounter;
43      section.style.display='none';
44      section.style.marginLeft='14px';
45      sectionCounter++;
46    }
47  }
48}
49window.onload = initDynSections;
50-->
51</script>
52<div class="navigation" id="top">
53  <div class="tabs">
54    <ul>
55      <li><a href="main.html"><span>Main&nbsp;Page</span></a></li>
56      <li class="current"><a href="pages.html"><span>Related&nbsp;Pages</span></a></li>
57      <li><a href="annotated.html"><span>Classes</span></a></li>
58      <li><a href="files.html"><span>Files</span></a></li>
59    </ul>
60  </div>
61</div>
62<div class="contents">
63
64
65<h1><a class="anchor" id="user_guide">Howto Use BDM - System, Data, Simulation </a></h1><p>This section serves as introdustion to the scenario of data simulation. Since it is the simpliest of all scenarios defined in <a class="el" href="user_guide0.html">BDM Use - Introduction</a> it also serves as introduction to configuration of an experiment (see ui) and basic decision making objects (<a class="el" href="classbdm_1_1RV.html" title="Class representing variables, most often random variables.">bdm::RV</a> and <a class="el" href="classbdm_1_1DS.html" title="Abstract class for discrete-time sources of data.">bdm::DS</a>).</p>
66<p>All experiments are demonstarted on scenario simulator which can be either standalone application of mex file (simulator.mex**).</p>
67<h2><a class="anchor" id="config">
68Configuration of an experiment</a></h2>
69<p>Configuration file (or config structure) is organized as a tree of information. High levels represent complex structures, leafs of the tree are basic data elements such as strings, numbers or vectors.</p>
70<p>Specific treatment was developed for objects. Since BDM is designed as object oriented library, the configuration was designed to honor the rule of inheritance. That is, offspring of a class can be used in place of its predecessor. Hence, objects (instances of classes) are configured by a structure with compulsory field <code>class</code>. This is a string variable corresponding to the name of the class to be used.</p>
71<p>The configuration has two possible options:</p>
72<ul>
73<li>configuration file using syntax of libconfig (see ui),</li>
74<li>matlab structure. For the purpose of tutorial, we will use the matlab notation. These two options can be mutually converted from one to another using prepared mex files: config2mxstruct.mex and mxstruct2config.mex. Naturally, these scripts require matlab to run. If it is not available, manual conversion is relatively trivial, the major difference is in using different types of brackets (ui)</li>
75</ul>
76<h3><a class="anchor" id="first">
77First experiment</a></h3>
78<p>The first experiment that can be performed is: </p>
79<div class="fragment"><pre class="fragment">DS.class=<span class="stringliteral">&apos;MemDS&apos;</span>;
80DS.Data =[1 2 3 4 5 6];
81</pre></div><p> which can be found in file bdmtoolbox/tutorials/userguide/memds_example.m.</p>
82<p>The code above is the minimum necessary information to run scenario <code>simulator</code> in matlab. To actually do so, make sure that matlab can find the simulator.mex file, e.g. by running: </p>
83<div class="fragment"><pre class="fragment">&gt;&gt; addpath _path_to_/bmtoolbox/mex/
84</pre></div><p>The expected result for Matlab is: </p>
85<div class="fragment"><pre class="fragment">&gt;&gt; M=simulator(DS)
86
87M =
88
89    ch0: [6x1 <span class="keywordtype">double</span>]
90</pre></div><p>If you see this result, you have configured BDM correctly and you have sucessfully run you first experiment. In other cases, please check your installation, installation. All that the simulator did was actually copying <code>DS.Data</code> to <code>M.ch0</code>. Explanation of the experiment and the logic used there follows.</p>
91<h2><a class="anchor" id="sim">
92Systems and DataSources</a></h2>
93<p>In standard system theory, the system is typically illustrated graphically as: </p>
94<div align="center">
95<img src="inline_dotgraph_1.dot.png" alt="inline_dotgraph_1.dot" border="0" usemap="#inline_dotgraph_1.dot.map">
96<map name="inline_dotgraph_1.dot.map" id="inline_dotgraph_1.dot.map"></map>
97</div>
98<p> Where <code>u</code> typically denotes input and <code>y</code> denotes output of the system. A causal dependence between input and output is typically presumed.</p>
99<p>We are predominantly concerned with discrete-time systems, hence, we will add indeces <img class="formulaInl" alt="$ _t $" src="form_159.png"/> to both input and output, <img class="formulaInl" alt="$ u_t $" src="form_5.png"/> and <img class="formulaInl" alt="$ y_t $" src="form_6.png"/>. We presume that the causal dependence is <img class="formulaInl" alt="$ u_t $" src="form_5.png"/> comes before <img class="formulaInl" alt="$ y_t $" src="form_6.png"/>.</p>
100<p>One of the definition of a system is that system is a "set of variables observed on a part of the world". Under this definition system is understood as generator of data. This definition may be a considered too simplistic, but it serves well as a description of what software object <code>DataSource</code> is.</p>
101<p>DataSource is an object that is essentially:</p>
102<ol type="1">
103<li>able to return data observed at time <img class="formulaInl" alt="$ t $" src="form_18.png"/>, (<a class="el" href="classbdm_1_1DS.html#ac1ec862dec7164fee3284c6f9a51b0ba" title="Returns full vector of observed data=[output, input].">bdm::DS::getdata()</a>),</li>
104<li>able to perform one a time step, (<a class="el" href="classbdm_1_1DS.html#a9a14c64d881e838ddc3a5617ee8a973b" title="Moves from  to , i.e. perfroms the actions and reads response of the system.">bdm::DS::step()</a>).</li>
105<li>able to describe what these data are, (<a class="el" href="classbdm_1_1DS.html#a069497b610296be053d862a2493be0ce" title="access function">bdm::DS::_drv()</a>),</li>
106</ol>
107<p>No fruther specification, e.g. if the data are pre-recorded or computed on-the-fly, are given. Specific behaviour of various DataSources is implemented as specialization of the root class <a class="el" href="classbdm_1_1DS.html" title="Abstract class for discrete-time sources of data.">bdm::DS</a>.</p>
108<h2><a class="anchor" id="memds">
109DataSource of pre-recorded data -- MemDS</a></h2>
110<p>The first experiment run in <a class="el" href="user_guide.html#first">First experiment</a> was actually an instance of DataSource of pre-recorded data that were stored in memory, i.e. the <a class="el" href="classbdm_1_1MemDS.html" title="Memory storage of off-line data column-wise.">bdm::MemDS</a> class.</p>
111<p>Operation of such object is trivial, the data are stored as a matrix and the general operations defined above are specialized as follows:</p>
112<ol type="1">
113<li>data observed at time <img class="formulaInl" alt="$ t $" src="form_18.png"/> are columns of the matrix, getdata() ruturns current column,</li>
114<li>time step itself is performed by increasing the column index,</li>
115<li>each row is named as "ch0","ch1",...</li>
116</ol>
117<p>This is the default bahavior. It can be customized using the UI mechanism. When the object of class MemDS is created it calls method <a class="el" href="classbdm_1_1MemDS.html#afaebfe6d7a2a43421be05d8cf7d7ae45">bdm::MemDS::from_setting()</a> and the input structure is parsed for settings. All available settings are documented in the method, see <a class="el" href="classbdm_1_1MemDS.html#afaebfe6d7a2a43421be05d8cf7d7ae45">bdm::MemDS::from_setting()</a>. The options are: </p>
118<div class="fragment"><pre class="fragment">DS.class = <span class="stringliteral">&apos;MemDS&apos;</span>;
119DS.Data = (...);            <span class="comment">// Data matrix or data vector</span>
120        --- optional ---
121DS.drv = RV({<span class="stringliteral">&quot;ch0&quot;</span>,...} ); <span class="comment">// Identification how rows of the matrix Data will be known to others</span>
122DS.time = 0;               <span class="comment">// Index of the first column to user_info,</span>
123DS.rowid = [1,2,3...];     <span class="comment">// ids of rows to be used</span>
124</pre></div><p> The compulsory fields are listed at the beginning; the optional fields are separated by string "--- optional ---".</p>
125<p>Fields <code>time</code> and <code>rowid</code> are self-explanatory. Field <code>drv</code> is a the one that specifies identification of the data elements, (point 3. of the general requirements of a DataSource).</p>
126<p>All optionals fields will be filled by default values, it this case: </p>
127<div class="fragment"><pre class="fragment">DS.drv  = RV({<span class="stringliteral">&apos;ch0&apos;</span>},1,0);
128DS.time = 0;
129DS.rowid = [1];
130</pre></div><p> Where the first line specifies a universal identification structure: random variable (<a class="el" href="classbdm_1_1RV.html" title="Class representing variables, most often random variables.">bdm::RV</a>).</p>
131<h2><a class="anchor" id="rvs">
132What is RV and how to use it</a></h2>
133<p>RV stands for <code>random</code> <code>variable</code> which is a description of random variable or its realization. This object playes role of identifier of elements of vectors of data (in datasources), expected inputs to functions (in pdfs), or required results (operations conditioning).</p>
134<p>Mathematical interpretation of RV is straightforward. Consider pdf <img class="formulaInl" alt="$ f(a)$" src="form_142.png"/>, then <img class="formulaInl" alt="$ a $" src="form_143.png"/> is the part represented by RV. Explicit naming of random variables may seem unnecessary for many operations with pdf, e.g. for generation of a uniform sample from &lt;0,1&gt; it is not necessary to specify any random variable. For this reason, RV are often optional information to specify. However, the considered scenanrio <code>simulator</code> is build in a way that requires RV to be given.</p>
135<p>The <code>simulator</code> scenario connects the DataSource to second basic class of BDM, bdm:logger. The logger is a class that take care of storing results -- in this case, results of simulation. The connection between these blocks is done automatically. The logger stores results of simulations under the names specified in drv. Readers familiar with Simulink environment may look at the RV as being unique identifiers of inputs and outputs of simulation blocks. The inputs are connected automatically with the outputs with matching RV. This view is however, very incomplete, RV have more roles than this.</p>
136<h2><a class="anchor" id="loggers">
137Loggers for flexible handling of results</a></h2>
138<p>Loggers are universal objects for storing and manipulating the results of an experiment. Similar to DataSource, every logger has to provide basic functionality:</p>
139<ol type="1">
140<li>initialize its storage (<a class="el" href="classbdm_1_1logger.html#acd6291a73491f755dc7bc88d022eb2fc" title="Initialize the storage.">bdm::logger.init()</a>),</li>
141<li>assign a connection point to each interested object (bdm::logger.logadd()),</li>
142<li>accept data to be logged to given connection (<a class="el" href="classbdm_1_1logger.html#ae30f695619931b3c372ab280e452d309" title="log this vector">bdm::logger.logit()</a>),</li>
143<li>finalize the storage when experiment is finished.</li>
144</ol>
145<p>These abstarct operations can be specialized in many ways. For example, storing all results in memory and writing them to disc when finished (<a class="el" href="classbdm_1_1memlog.html">bdm::memlog</a>), storing data in a matlab structure (<a class="el" href="classbdm_1_1mexlog.html" title="Logger storing results into an mxArray.">bdm::mexlog</a>), writing them out in ascii (<a class="el" href="classbdm_1_1stdlog.html" title="Simple logger used for debugging All data records are written out to std from where...">bdm::stdlog</a>) or more sophisticated buffered output to harddrive (<a class="el" href="classbdm_1_1dirfilelog.html" title="Logging into dirfile with buffer in memory.">bdm::dirfilelog</a>).</p>
146<p>Since all experiments are performed in matlab, the default mexlog class will be used. However, the way how the results are to be stored can be configured using configuration structure filled by fields from <code>from_setting</code> of the chosen logger, and passing it as third argument to <code>simulator</code>.</p>
147<h2><a class="anchor" id="datasource">
148Class inheritance and DataSources</a></h2>
149<p>As mentioned above, the scenario <code>simulator</code> is written to accept any datasource (i.e. any offspring of <a class="el" href="classbdm_1_1DS.html" title="Abstract class for discrete-time sources of data.">bdm::DS</a>). For full list of offsprings, click see Classes &gt; Class Hierarchy.</p>
150<p>At the time of writing this tutorial, available datasources are <a class="el" href="classbdm_1_1DS.html" title="Abstract class for discrete-time sources of data.">bdm::DS</a></p>
151<ul>
152<li><a class="el" href="classbdm_1_1EpdfDS.html" title="Simulate data from a static pdf (epdf).">bdm::EpdfDS</a></li>
153<li><a class="el" href="classbdm_1_1MemDS.html" title="Memory storage of off-line data column-wise.">bdm::MemDS</a><ul>
154<li><a class="el" href="classbdm_1_1FileDS.html">bdm::FileDS</a><ul>
155<li><a class="el" href="classbdm_1_1CsvFileDS.html" title="CSV file data storage The constructor creates Data matrix from the records in a CSV...">bdm::CsvFileDS</a></li>
156<li><a class="el" href="classbdm_1_1ITppFileDS.html" title="Read Data Matrix from an IT file.">bdm::ITppFileDS</a></li>
157</ul>
158</li>
159</ul>
160</li>
161<li><a class="el" href="classbdm_1_1MpdfDS.html" title="Simulate data from conditional density Still having only one density but allowing...">bdm::MpdfDS</a></li>
162<li><a class="el" href="classbdm_1_1stateDS.html">bdm::stateDS</a></li>
163</ul>
164<p>The MemDS has already been introduced in the example in <a class="el" href="user_guide.html#memds">DataSource of pre-recorded data -- MemDS</a>. However, any of the classes listed above can be used to replace it in the example. This will be demonstrated on the <code>EpdfDS</code> class.</p>
165<p>Brief decription of the class states that EpdfDS "Simulate data from a static pdf (epdf)". The static pdf means unconditional pdf in the sense that the random variable is conditioned by numerical values only. In mathematical notation it could be both <img class="formulaInl" alt="$ f(a) $" src="form_144.png"/> and <img class="formulaInl" alt="$ f(x_t |d_1 \ldots d_t)$" src="form_145.png"/>. The latter case is true only when all <img class="formulaInl" alt="$ d $" src="form_146.png"/> denotes observed values.</p>
166<p>For example, we wish to simulate realizations of a Uniform pdf on interval &lt;-1,1&gt;. This is achieved by plugging an object representing uniform pdf into general simulator of independent random samples, EpdfDS. Uniform density is implemented as class <a class="el" href="classbdm_1_1euni.html" title="Uniform distributed density on a rectangular support.">bdm::euni</a>. An instance of <code>euni</code> can be again created method <code>from_setting</code>, in this case <a class="el" href="classbdm_1_1euni.html#a77f5fef1f006fe056066da23b9e5f042">bdm::euni.from_setting()</a>. Using documentation we define it with the following code: </p>
167<div class="fragment"><pre class="fragment">U.class=<span class="stringliteral">&apos;euni&apos;</span>;
168U.rv   = RV({<span class="charliteral">&apos;a&apos;</span>});
169U.high = 1.0;
170U.low  = -1.0;
171</pre></div><p> which encodes information:</p>
172<p class="formulaDsp">
173<img class="formulaDsp" alt="\[ f(a) = \mathcal{U}(-1,1) \]" src="form_160.png"/>
174</p>
175<p>The datasource itself, i.e. the instanc of <code>EpdfDS</code> can be then configured via: </p>
176<div class="fragment"><pre class="fragment">DS.class = <span class="stringliteral">&apos;EpdfDS&apos;</span>;
177DS.epdf  = U;
178</pre></div><p> where <code>U</code> is the structure defined above.</p>
179<p>Contrary to the previous example, we need to tell to algorithm <code>simulator</code> how many samples from the data source we need. This is configured by variable <code>experiment.ndat</code>. The configuration has to be finalized by: </p>
180<div class="fragment"><pre class="fragment">experiment.ndat = 10;
181M=simulator(DS,experiment);
182</pre></div><p>The result is as expected in field <code>M.a</code> the name of which corresponds to name of <code>U.rv</code> .</p>
183<p>If the task was only to generate random realizations, this would indeed be a very clumsy way of doing it. However, the power of the proposed approach will be revelead in more demanding examples, one of which follows next.</p>
184<h2><a class="anchor" id="arx">
185Simulating autoregressive model</a></h2>
186<p>Consider the following autoregressive model: </p>
187<p class="formulaDsp">
188<img class="formulaDsp" alt="\[ f(y_t|y_{t-3},u_{t-1}) = \mathcal{N}( a y_{t-3} + b u_{t-1}, r) \]" src="form_161.png"/>
189</p>
190<p> where <img class="formulaInl" alt="$ a,b $" src="form_148.png"/> are known constants, and <img class="formulaInl" alt="$ r $" src="form_149.png"/> is known variance.</p>
191<p>Direct application of <code>EpdfDS</code> is not possible, since the pdf above is conditioned on values of <img class="formulaInl" alt="$ y_{t-3}$" src="form_150.png"/> and <img class="formulaInl" alt="$ u_{t-1}$" src="form_151.png"/>. We need to handle two issues:</p>
192<ol type="1">
193<li>extra unsimulated variable <img class="formulaInl" alt="$ u $" src="form_152.png"/>,</li>
194<li>time delayes of the values.</li>
195</ol>
196<p>The first issue can be handled in two ways. First, <img class="formulaInl" alt="$ u $" src="form_152.png"/> can be considered as input and as such it could be externally given to the datasource. This solution is used in scenario <code>closedloop</code>. However, for the <code>simulator</code> scenario we will apply the second option, that is we complement <img class="formulaInl" alt="$ f(y_{t}|y_{t-3},u_{t-1})$" src="form_153.png"/> by extra pdf:</p>
197<p class="formulaDsp">
198<img class="formulaDsp" alt="\[ f(u_t) = \mathcal{N}(0, r_u) \]" src="form_162.png"/>
199</p>
200<p> where <img class="formulaInl" alt="$ r_u $" src="form_163.png"/> is another known constant. Thus, the joint density is now:</p>
201<p class="formulaDsp">
202<img class="formulaDsp" alt="\[ f(y_{t},u_{t}|y_{t-3},u_{t-1}) = f(y_{t}|y_{t-3},u_{t-1})f(u_{t}) \]" src="form_155.png"/>
203</p>
204<p> and we have no need for input since the datasource have all necessary information inside. All that is required is to store them and copy their values to appropriate places.</p>
205<p>That is done in automatic way using dedicated class <a class="el" href="classbdm_1_1datalink__buffered.html" title="Datalink that buffers delayed values - do not forget to call step().">bdm::datalink_buffered</a>. The only issue a user may need to take care about is the missing initial conditions for simulation. By default these are set to zeros. Using the default values, the full configuration of this system is: </p>
206<div class="fragment"><pre class="fragment">y = RV({<span class="charliteral">&apos;y&apos;</span>});
207u = RV({<span class="charliteral">&apos;u&apos;</span>});
208
209fy.class = <span class="stringliteral">&apos;mlnorm&lt;ldmat&gt;&apos;</span>;
210fy.rv    = y;
211fy.rvc   = RV({<span class="charliteral">&apos;y&apos;</span>,<span class="charliteral">&apos;u&apos;</span>}, [1 1], [-3, -1]);
212fy.A     = [0.5, -0.9];
213fy.const = 0;
214fy.R     = 0.1;
215
216
217fu.class = <span class="stringliteral">&apos;enorm&lt;ldmat&gt;&apos;</span>;
218fu.rv    = u;
219fu.mu    = 0;
220fu.R     = 0.2;
221
222DS.class = <span class="stringliteral">&apos;MpdfDS&apos;</span>;
223DS.mpdf.class  = <span class="stringliteral">&apos;mprod&apos;</span>;
224DS.mpdf.mpdfs  = {fy, epdf2mpdf(fu)};
225</pre></div><p>Explanation of this example will require few remarks:</p>
226<ul>
227<li>class of the <code>fy</code> object is 'mlnorm&lt;ldmat&gt;' which is Normal pdf with mean value given by linear function, and covariance matrix stored in LD decomposition, see <a class="el" href="classbdm_1_1mlnorm.html" title="Normal distributed linear function with linear function of mean value;.">bdm::mlnorm</a> for details.</li>
228<li>naming convention 'mlnorm&lt;ldmat&gt;' relates to the concept of templates in C++. For those unfamiliar with this concept, it is basicaly a way how to share code for different flavours of the same object. Note that mlnorm exist in three versions: mlnorm&lt;ldmat&gt;, <a class="el" href="classbdm_1_1mlnorm.html">mlnorm&lt;chmat&gt;</a>, mlnorm&lt;fsqmat&gt;. Those classes act identically the only difference is that the internal data are stored either in LD decomposition, choleski decomposition or full matrices, respectively.</li>
229<li>the same concept is used for enorm, where <a class="el" href="classbdm_1_1enorm.html">enorm&lt;chmat&gt;</a> and <a class="el" href="classbdm_1_1enorm.html">enorm&lt;fsqmat&gt;</a> are also possible. In this particular use, these objects are equivalent. In specific situation, e.g. Kalman filter implemented on Choleski decomposition (<a class="el" href="classbdm_1_1KalmanCh.html" title="Kalman filter in square root form.">bdm::KalmanCh</a>), only <a class="el" href="classbdm_1_1enorm.html">enorm&lt;chmat&gt;</a> is approprate.</li>
230<li>class 'mprod' represents the chain rule of probability. Attribute <code>mpdfs</code> of its configuration structure is a list of conditional densities. Conditional density <img class="formulaInl" alt="$ f(a|b)$" src="form_156.png"/> is represented by class <code>mpdf</code> and its offsprings. Class <code>RV</code> is used to describe both variables before conditioning (field <code>rv</code> ) and after conditioning sign (field <code>rvc</code>).</li>
231<li>due to simplicity of implementation, mprod accept only conditional densities in the field <code>mpdfs</code>. Hence, the pdf <img class="formulaInl" alt="$ f(u_t)$" src="form_157.png"/> must be converted to conditional density with empty conditioning, <img class="formulaInl" alt="$ f(u_t| \{\})$" src="form_158.png"/>. This is achieved by calling function epdf2mpdf which is only a trivial wrapper creating class <a class="el" href="classbdm_1_1mepdf.html" title="Unconditional mpdf, allows using epdf in the role of mpdf.">bdm::mepdf</a>.</li>
232</ul>
233<p>The code above can be immediatelly run, usin the same execution sequence of <code>estimator</code> as above.</p>
234<h3><a class="anchor" id="ini">
235Initializing simulation</a></h3>
236<p>When zeros are not appropriate initial conditions, the correct conditions can be set using additional commands (see <a class="el" href="classbdm_1_1MpdfDS.html#a39ef55fb36da354c93c544129e03f558">bdm::MpdfDS.from_setting()</a> ): </p>
237<div class="fragment"><pre class="fragment">DS.init_rv = RV({<span class="charliteral">&apos;y&apos;</span>,<span class="charliteral">&apos;y&apos;</span>,<span class="charliteral">&apos;y&apos;</span>}, [1,1,1], [-1,-2,-3]);
238DS.init_values = [0.1, 0.2, 0.3];
239</pre></div><p>The values of <code>init_values</code> will be copied to places in history identified by corresponding values of <code>init_rv</code>. Initial data is not checked for completeness, i.e. values of random variables missing from <code>init_rv</code> (in this case all occurences of <img class="formulaInl" alt="$ u $" src="form_152.png"/>) are still initialized to 0.</p>
240<p></p>
241<p>BDM is a library of basic components for Bayesian decision making, hence its direct use is not possible. In order to use BDM the components must be pulled together in order to achieve desired functionality. We expect two kinds of users:</p>
242<ul>
243<li><b> Basic users </b> who run prepared scripts with different parameterizations and analyze their results,</li>
244<li><b> Advanced users </b> who are able to understand the logic of BDM and extend its functionality to new applications.</li>
245</ul>
246<p>The primary design aim of BDM was to ease development of complex algorithms, hence the target user is the advanced one. However, running experiments is the first task to learn for both types of users.</p>
247<h2><a class="anchor" id="param">
248Experiment is fully parameterized before execution</a></h2>
249<p>Experiments in BDM can be performed using either standalone applications or function bindings in high-level environment. A typical example of the latter being mex file in Matlab environment.</p>
250<p>The main logic behind the experiment is that all necessary information about it are gathered in advance in a configuration file (for standalone applications) or in configuration structure (Matlab). This approach was designed especially for time consuming experiments and Monte-Carlo studies for which it suits the most.</p>
251<p>For smaller decision making tasks, interactive use of the experiment can be achieved by showing the full configuration structure (or its selected parts), running the experiment on demand and showing the results.</p>
252<p>Semi-interactive experiments can be designed by sequential run of different algorithms. This topic will be covered in advanced documentation.</p>
253<h2><a class="anchor" id="config">
254Configuration of an experiment</a></h2>
255<p>Configuration file (or config structure) is organized as a tree of information. High levels represent bigger structures, leafs of the structures are basic data elements such as strings, numbers or vectors.</p>
256<p>Specific treatment was developed for objects. Since BDM is designed as object oriented library, the configuration was designed to honor the rule of inheritance. That is, offspring of a class can be used in place of its predecessor. Hence, objects (instances of classes) are configured by a structure with compulsory field <code>class</code>. This is a string variable corresponding to the name of the class to be used.</p>
257<p>Consider the following example: </p>
258<div class="fragment"><pre class="fragment">DS = {<span class="keyword">class</span>=<span class="stringliteral">&quot;MemDS&quot;</span>;
259   data = [1, 2, 3, 4, 5, 6, 7];
260}
261</pre></div><p> or written equivalently in Matlab as </p>
262<div class="fragment"><pre class="fragment">DS.class=<span class="stringliteral">&apos;MemDS&apos;</span>;
263DS.Data =[1 2 3 4 5 6];
264</pre></div><p>The code above is the minimum necessary information to run a pre-made algorithm implemented as executable <code>estimator</code> or Matlab mex file <code>estimator</code>. The expected result for Matlab is: </p>
265<div class="fragment"><pre class="fragment">&gt;&gt; M=estimator(DS,{})
266
267M =
268
269    ch0: [6x1 <span class="keywordtype">double</span>]
270</pre></div><p>The structure <code>M</code> has one field called <code>ch0</code> to which the data from <code>DS.Data</code> were copied. This was configured to be the default behavior which can be easily changed by adding more information to the configuration structure.</p>
271<p>First, we will have a look at all options of MemDS.</p>
272<h2><a class="anchor" id="memds">
273DataSource of pre-recorded data -- MemDS</a></h2>
274<p>As a first step, the estimator algorithm has created an object of class MemDS and called its method <a class="el" href="classbdm_1_1MemDS.html#afaebfe6d7a2a43421be05d8cf7d7ae45">bdm::MemDS::from_setting()</a>. This is a universal method called when creating an instance of class from configuration. Object that does not implement this method can not be created automatically from configuration.</p>
275<p>The documentation contains the full structure which can be loaded. e.g.: </p>
276<div class="fragment"><pre class="fragment">{ <span class="keyword">class </span>= &apos;MemDS&apos;;
277        Data = (...);            <span class="comment">// Data matrix or data vector</span>
278        --- optional ---
279        drv = {<span class="keyword">class</span>=<span class="stringliteral">&apos;RV&apos;</span>; ...} <span class="comment">// Identification how rows of the matrix Data will be known to others</span>
280        time = 0;               <span class="comment">// Index of the first column to user_info,</span>
281        rowid = [1,2,3...];     <span class="comment">// ids of rows to be used</span>
282}
283</pre></div><p> for MemDS. The compulsory fields are listed at the beginning; the optional fields are separated by string "--- optional ---".</p>
284<p>For the example given above, the missing fields were filled as follows: </p>
285<div class="fragment"><pre class="fragment">  drv  = {<span class="keyword">class</span>=<span class="stringliteral">&quot;RV&quot;</span>; names=<span class="stringliteral">&quot;{ch0 }&quot;</span>; sizes=[1];};
286  time = 0;
287  rowid = [1];
288</pre></div><p> Meaning that the data will be read from the first column (time=0), all rows of data are to be read (rowid=[1]), and this row will be called "ch0".</p>
289<dl class="note"><dt><b>Note:</b></dt><dd><b>Mixtools reference</b> This object replaces global variables DATA and TIME. In BDM, data can be read and written to a range of <code>datasources</code>, objects derived from <a class="el" href="classbdm_1_1DS.html" title="Abstract class for discrete-time sources of data.">bdm::DS</a>.</dd></dl>
290<h2><a class="anchor" id="rvs">
291What is RV and how to use it</a></h2>
292<p>RV stands for <code>random</code> <code>variable</code> which is a description of random variable or its realization. This object playes role of identifier of elements of vectors of data (in datasources), expected inputs to functions (in pdfs), or required results (operations conditioning).</p>
293<dl class="note"><dt><b>Note:</b></dt><dd><b>Mixtools reference </b> RV is generalization of "structures" <code>str</code> in Mixtools. It replaces channel numbers by string names, and adds extra field size for each record.</dd></dl>
294<p>Mathematical interpretation of RV is straightforward. Consider pdf <img class="formulaInl" alt="$ f(a)$" src="form_142.png"/>, then <img class="formulaInl" alt="$ a $" src="form_143.png"/> is the part represented by RV. Explicit naming of random variables may seem unnecessary for many operations with pdf, e.g. for generation of a uniform sample from &lt;0,1&gt; it is not necessary to specify any random variable. For this reason, RV are often optional information to specify. However, the considered algorithm <code>estimator</code> is build in a way that requires RV to be given.</p>
295<p>The <code>estimator</code> use-case expects to join the data source with an array of estimators, each of which declaring its input vector of data. The connection will be made automatically using the mechanism of datalinks (<a class="el" href="classbdm_1_1datalink.html" title="DataLink is a connection between two data vectors Up and Down.">bdm::datalink</a>). Readers familiar with Simulink environment may look at the RV as being unique identifiers of inputs and outputs of simulation blocks. The inputs are connected automatically with the outputs with matching RV. This view is however, very incomplete, RV are much more powerful than this.</p>
296<h2><a class="anchor" id="datasource">
297Class inheritance and DataSources</a></h2>
298<p>As mentioned above, the algorithm <code>estimator</code> is written to accept any datasource (i.e. any offspring of <a class="el" href="classbdm_1_1DS.html" title="Abstract class for discrete-time sources of data.">bdm::DS</a>). For full list of offsprings, click Classes &gt; Class Hierarchy.</p>
299<p>At the time of writing this tutorial, available datasources are <a class="el" href="classbdm_1_1DS.html" title="Abstract class for discrete-time sources of data.">bdm::DS</a></p>
300<ul>
301<li><a class="el" href="classbdm_1_1EpdfDS.html" title="Simulate data from a static pdf (epdf).">bdm::EpdfDS</a></li>
302<li><a class="el" href="classbdm_1_1MemDS.html" title="Memory storage of off-line data column-wise.">bdm::MemDS</a><ul>
303<li><a class="el" href="classbdm_1_1FileDS.html">bdm::FileDS</a><ul>
304<li><a class="el" href="classbdm_1_1CsvFileDS.html" title="CSV file data storage The constructor creates Data matrix from the records in a CSV...">bdm::CsvFileDS</a></li>
305<li><a class="el" href="classbdm_1_1ITppFileDS.html" title="Read Data Matrix from an IT file.">bdm::ITppFileDS</a></li>
306</ul>
307</li>
308</ul>
309</li>
310<li><a class="el" href="classbdm_1_1MpdfDS.html" title="Simulate data from conditional density Still having only one density but allowing...">bdm::MpdfDS</a></li>
311<li><a class="el" href="classbdm_1_1stateDS.html">bdm::stateDS</a></li>
312</ul>
313<p>The MemDS has already been introduced in the example in <a class="el" href="user_guide.html#memds">DataSource of pre-recorded data -- MemDS</a>. However, any of the classes listed above can be used to replace it in the example. This will be demonstrated on the <code>EpdfDS</code> class.</p>
314<p>Brief decription of the class states that EpdfDS "Simulate data from a static pdf (epdf)". The static pdf means unconditional pdf in the sense that the random variable is conditioned by numerical values only. In mathematical notation it could be both <img class="formulaInl" alt="$ f(a) $" src="form_144.png"/> and <img class="formulaInl" alt="$ f(x_t |d_1 \ldots d_t)$" src="form_145.png"/>. The latter case is true only when all <img class="formulaInl" alt="$ d $" src="form_146.png"/> denotes observed values.</p>
315<p>For example, we wish to simulate realizations of a Uniform density on interval &lt;-1,1&gt;. Uniform density is represented by class <a class="el" href="classbdm_1_1euni.html" title="Uniform distributed density on a rectangular support.">bdm::euni</a>. From <a class="el" href="classbdm_1_1euni.html#a77f5fef1f006fe056066da23b9e5f042">bdm::euni.from_setting()</a> we can find that the code is: </p>
316<div class="fragment"><pre class="fragment">U={<span class="keyword">class</span>=<span class="stringliteral">&quot;euni&quot;</span>; high=1.0; low = -1.0;}
317</pre></div><p> for configuration file, and </p>
318<div class="fragment"><pre class="fragment">U.class=<span class="stringliteral">&apos;euni&apos;</span>;
319U.high = 1.0;
320U.low  = -1.0;
321U.rv.class = <span class="stringliteral">&apos;RV&apos;</span>;
322U.rv.names = {<span class="charliteral">&apos;a&apos;</span>};
323</pre></div><p> for Matlab.</p>
324<p>The datasource itself, can be then configured via </p>
325<div class="fragment"><pre class="fragment">DS = {<span class="keyword">class</span>=<span class="stringliteral">&apos;EpdfDS&apos;</span>; epdf=@U;};
326</pre></div><p> in config file, or </p>
327<div class="fragment"><pre class="fragment">DS.class = <span class="stringliteral">&apos;EpdfDS&apos;</span>;
328DS.epdf  = U;
329</pre></div><p> in Matlab.</p>
330<p>Contrary to the previous example, we need to tell to algorithm <code>estimator</code> how many samples from the data source we need. This is configured by variable <code>experiment.ndat</code>. The configuration has to be finalized by: </p>
331<div class="fragment"><pre class="fragment">experiment.ndat = 10;
332M=estimator(DS,{},experiment);
333</pre></div><p>The result is as expected in field <code>M.a</code> the name of which corresponds to name of <code>U.rv</code> .</p>
334<p>If the task was only to generate random realizations, this would indeed be a very clumsy way of doing it. However, the power of the proposed approach will be revelead in more demanding examples, one of which follows next.</p>
335<h2><a class="anchor" id="arx">
336Simulating autoregressive model</a></h2>
337<p>Consider the following autoregressive model: </p>
338<p class="formulaDsp">
339<img class="formulaDsp" alt="\[ y_t \sim \mathcal{N}( a y_{t-3} + b u_{t-1}, r) \]" src="form_147.png"/>
340</p>
341<p> where <img class="formulaInl" alt="$ a,b $" src="form_148.png"/> are known constants, and <img class="formulaInl" alt="$ r $" src="form_149.png"/> is known variance.</p>
342<p>Direct application of <code>EpdfDS</code> is not possible, since the pdf above is conditioned on values of <img class="formulaInl" alt="$ y_{t-3}$" src="form_150.png"/> and <img class="formulaInl" alt="$ u_{t-1}$" src="form_151.png"/>. We need to handle two issues:</p>
343<ol type="1">
344<li>extra unsimulated variable <img class="formulaInl" alt="$ u $" src="form_152.png"/>,</li>
345<li>time delayes of the values.</li>
346</ol>
347<p>The first issue can be handled in two ways. First, <img class="formulaInl" alt="$ u $" src="form_152.png"/> can be considered as input and as such it could be externally given to the datasource. This solution is used in algorithm use-case <code>closedloop</code>. However, for the <code>estimator</code> scenario we will apply the second option, that is we complement <img class="formulaInl" alt="$ f(y_{t}|y_{t-3},u_{t-1})$" src="form_153.png"/> by extra pdf:</p>
348<p class="formulaDsp">
349<img class="formulaDsp" alt="\[ u_t \sim \mathcal{N}(0, r_u) \]" src="form_154.png"/>
350</p>
351<p> Thus, the joint density is now:</p>
352<p class="formulaDsp">
353<img class="formulaDsp" alt="\[ f(y_{t},u_{t}|y_{t-3},u_{t-1}) = f(y_{t}|y_{t-3},u_{t-1})f(u_{t}) \]" src="form_155.png"/>
354</p>
355<p> and we have no need for input since the datasource have all necessary information inside. All that is required is to store them and copy their values to appropriate places.</p>
356<p>That is done in automatic way using dedicated class <a class="el" href="classbdm_1_1datalink__buffered.html" title="Datalink that buffers delayed values - do not forget to call step().">bdm::datalink_buffered</a>. The only issue a user may need to take care about is the missing initial conditions for simulation. By default these are set to zeros. Using the default values, the full configuration of this system is: </p>
357<div class="fragment"><pre class="fragment">y = RV({<span class="charliteral">&apos;y&apos;</span>});
358u = RV({<span class="charliteral">&apos;u&apos;</span>});
359
360fy.class = <span class="stringliteral">&apos;mlnorm&lt;ldmat&gt;&apos;</span>;
361fy.rv    = y;
362fy.rvc   = RV({<span class="charliteral">&apos;y&apos;</span>,<span class="charliteral">&apos;u&apos;</span>}, [1 1], [-3, -1]);
363fy.A     = [0.5, -0.9];
364fy.const = 0;
365fy.R     = 0.1;
366
367
368fu.class = <span class="stringliteral">&apos;enorm&lt;ldmat&gt;&apos;</span>;
369fu.rv    = u;
370fu.mu    = 0;
371fu.R     = 0.2;
372
373DS.class = <span class="stringliteral">&apos;MpdfDS&apos;</span>;
374DS.mpdf.class  = <span class="stringliteral">&apos;mprod&apos;</span>;
375DS.mpdf.mpdfs  = {fy, epdf2mpdf(fu)};
376</pre></div><p>Explanation of this example will require few remarks:</p>
377<ul>
378<li>class of the <code>fy</code> object is 'mlnorm&lt;ldmat&gt;' which is Normal pdf with mean value given by linear function, and covariance matrix stored in LD decomposition, see <a class="el" href="classbdm_1_1mlnorm.html" title="Normal distributed linear function with linear function of mean value;.">bdm::mlnorm</a> for details.</li>
379<li>naming convention 'mlnorm&lt;ldmat&gt;' relates to the concept of templates in C++. For those unfamiliar with this concept, it is basicaly a way how to share code for different flavours of the same object. Note that mlnorm exist in three versions: mlnorm&lt;ldmat&gt;, <a class="el" href="classbdm_1_1mlnorm.html">mlnorm&lt;chmat&gt;</a>, mlnorm&lt;fsqmat&gt;. Those classes act identically the only difference is that the internal data are stored either in LD decomposition, choleski decomposition or full matrices, respectively.</li>
380<li>the same concept is used for enorm, where <a class="el" href="classbdm_1_1enorm.html">enorm&lt;chmat&gt;</a> and <a class="el" href="classbdm_1_1enorm.html">enorm&lt;fsqmat&gt;</a> are also possible. In this particular use, these objects are equivalent. In specific situation, e.g. Kalman filter implemented on Choleski decomposition (<a class="el" href="classbdm_1_1KalmanCh.html" title="Kalman filter in square root form.">bdm::KalmanCh</a>), only <a class="el" href="classbdm_1_1enorm.html">enorm&lt;chmat&gt;</a> is approprate.</li>
381<li>class 'mprod' represents the chain rule of probability. Attribute <code>mpdfs</code> of its configuration structure is a list of conditional densities. Conditional density <img class="formulaInl" alt="$ f(a|b)$" src="form_156.png"/> is represented by class <code>mpdf</code> and its offsprings. Class <code>RV</code> is used to describe both variables before conditioning (field <code>rv</code> ) and after conditioning sign (field <code>rvc</code>).</li>
382<li>due to simplicity of implementation, mprod accept only conditional densities in the field <code>mpdfs</code>. Hence, the pdf <img class="formulaInl" alt="$ f(u_t)$" src="form_157.png"/> must be converted to conditional density with empty conditioning, <img class="formulaInl" alt="$ f(u_t| \{\})$" src="form_158.png"/>. This is achieved by calling function epdf2mpdf which is only a trivial wrapper creating class <a class="el" href="classbdm_1_1mepdf.html" title="Unconditional mpdf, allows using epdf in the role of mpdf.">bdm::mepdf</a>.</li>
383</ul>
384<p>The code above can be immediatelly run, usin the same execution sequence of <code>estimator</code> as above.</p>
385<h3><a class="anchor" id="ini">
386Initializing simulation</a></h3>
387<p>When zeros are not appropriate initial conditions, the correct conditions can be set using additional commands: </p>
388<div class="fragment"><pre class="fragment">DS.init_rv = RV({<span class="charliteral">&apos;y&apos;</span>,<span class="charliteral">&apos;y&apos;</span>,<span class="charliteral">&apos;y&apos;</span>}, [1,1,1], [-1,-2,-3]);
389DS.init_values = [0.1, 0.2, 0.3];
390</pre></div><p>The values of <code>init_values</code> will be copied to places in history identified by corresponding values of <code>init_rv</code>. Initial data is not checked for completeness, i.e. values of random variables missing from <code>init_rv</code> (in this case all occurences of <img class="formulaInl" alt="$ u $" src="form_152.png"/>) are still initialized to 0.</p>
391<h2><a class="anchor" id="conc">
392What was demonstrated in this tutorial</a></h2>
393<p>The purpose of this page was to introduce software image of basic elements of decision making as implemented in BDM.</p>
394<ul>
395<li>random values as identification mechanism (<a class="el" href="classbdm_1_1RV.html" title="Class representing variables, most often random variables.">bdm::RV</a>)</li>
396<li>unconditional pdfs (<a class="el" href="classbdm_1_1epdf.html" title="Probability density function with numerical statistics, e.g. posterior density.">bdm::epdf</a>),</li>
397<li>conditional pdfs (<a class="el" href="classbdm_1_1mpdf.html" title="Conditional probability density, e.g. modeling , where  is random variable, rv, and...">bdm::mpdf</a>),</li>
398</ul>
399<p>And the use of these in simulation of data and function of datasources. In the next tutorial, Bayesian models (<a class="el" href="classbdm_1_1BM.html" title="Bayesian Model of a system, i.e. all uncertainty is modeled by probabilities.">bdm::BM</a>) and loggers (<a class="el" href="classbdm_1_1logger.html" title="Class for storing results (and semi-results) of an experiment.">bdm::logger</a>) will be introduced. </p>
400</div>
401<hr size="1"/><address style="text-align: right;"><small>Generated on Tue Sep 15 23:42:47 2009 for mixpp by&nbsp;
402<a href="http://www.doxygen.org/index.html">
403<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.1 </small></address>
404</body>
405</html>
Note: See TracBrowser for help on using the browser.