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 <a class="el" href="ui_page.html">User Infos and their use</a>),</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 (<a class="el" href="ui_page.html">User Infos and their use</a>)</li> |
75 | | </ul> |
76 | | <h3><a class="anchor" id="ug_first"> |
| 63 | 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> |
| 64 | 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> |
| 65 | The configuration has two possible options:<ul> |
| 66 | <li>configuration file using syntax of libconfig (see <a class="el" href="ui_page.html">User Infos and their use</a>),</li><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 (<a class="el" href="ui_page.html">User Infos and their use</a>)</li></ul> |
| 67 | <h3><a class="anchor" name="ug_first"> |
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">>> addpath _path_to_/bmtoolbox/mex/ |
84 | | </pre></div><p>The expected result for Matlab is: </p> |
85 | | <div class="fragment"><pre class="fragment">>> M=simulator(DS) |
| 71 | </pre></div> which can be found in file bdmtoolbox/tutorials/userguide/memds_example.m.<p> |
| 72 | 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: <div class="fragment"><pre class="fragment">>> addpath _path_to_/bmtoolbox/mex/ |
| 73 | </pre></div><p> |
| 74 | The expected result for Matlab is: <div class="fragment"><pre class="fragment">>> M=simulator(DS) |
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#a4df78b21d352013d8804a8dda860b7ea" 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="ug_memds"> |
| 86 | 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> |
| 87 | 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> |
| 88 | 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> |
| 89 | DataSource is an object that is essentially:<ol type=1> |
| 90 | <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#c1ec862dec7164fee3284c6f9a51b0ba" title="Returns full vector of observed data=[output, input].">bdm::DS::getdata()</a>),</li><li>able to perform one a time step, (<a class="el" href="classbdm_1_1DS.html#4df78b21d352013d8804a8dda860b7ea" title="Moves from to , i.e. perfroms the actions and reads response of the system.">bdm::DS::step()</a>).</li><li>able to describe what these data are, (<a class="el" href="classbdm_1_1DS.html#069497b610296be053d862a2493be0ce" title="access function">bdm::DS::_drv()</a>),</li></ol> |
| 91 | <p> |
| 92 | 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>.<h2><a class="anchor" name="ug_memds"> |
110 | | <p>The first experiment run in first 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">'MemDS'</span>; |
| 94 | The first experiment run in first 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> |
| 95 | Operation of such object is trivial, the data are stored as a matrix and the general operations defined above are specialized as follows:<ol type=1> |
| 96 | <li>data observed at time <img class="formulaInl" alt="$ t $" src="form_18.png"> are columns of the matrix, getdata() ruturns current column,</li><li>time step itself is performed by increasing the column index,</li><li>each row is named as "ch0","ch1",...</li></ol> |
| 97 | <p> |
| 98 | 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#faebfe6d7a2a43421be05d8cf7d7ae45">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#faebfe6d7a2a43421be05d8cf7d7ae45">bdm::MemDS::from_setting()</a>. The options are: <div class="fragment"><pre class="fragment">DS.class = <span class="stringliteral">'MemDS'</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">'ch0'</span>},1,0); |
| 104 | </pre></div> The compulsory fields are listed at the beginning; the optional fields are separated by string "--- optional ---".<p> |
| 105 | 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> |
| 106 | All optionals fields will be filled by default values, it this case: <div class="fragment"><pre class="fragment">DS.drv = RV({<span class="stringliteral">'ch0'</span>},1,0); |
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 <0,1> 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>In software, <code>RV</code> has three compulsory properties:</p> |
136 | | <ul> |
137 | | <li><b>name</b>, unique identifier, two RV with the same name are considered to be identical</li> |
138 | | <li><b>size</b>, size of the random variable, if not given it is assumed to be 1,</li> |
139 | | <li><b>time</b>, more exactly time shift from <img class="formulaInl" alt="$ t $" src="form_18.png"/>, defaults to 0. For example, scalar <img class="formulaInl" alt="$ x_{t-2} $" src="form_178.png"/> is encoded as (name='x',sizes=1,time=-2). Each RV stores array of these elements, hence RV with: <div class="fragment"><pre class="fragment"> names={<span class="charliteral">'a'</span>, <span class="charliteral">'b'</span>}; |
| 111 | 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> |
| 112 | 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 <0,1> 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> |
| 113 | In software, <code>RV</code> has three compulsory properties:<ul> |
| 114 | <li><b>name</b>, unique identifier, two RV with the same name are considered to be identical</li><li><b>size</b>, size of the random variable, if not given it is assumed to be 1,</li><li><b>time</b>, more exactly time shift from <img class="formulaInl" alt="$ t $" src="form_18.png">, defaults to 0. For example, scalar <img class="formulaInl" alt="$ x_{t-2} $" src="form_178.png"> is encoded as (name='x',sizes=1,time=-2). Each RV stores array of these elements, hence RV with: <div class="fragment"><pre class="fragment"> names={<span class="charliteral">'a'</span>, <span class="charliteral">'b'</span>}; |
146 | | <p>Algebra on RVs (adding, searching in, subtraction, intersection, etc.) is implemented, see <a class="el" href="classbdm_1_1RV.html" title="Class representing variables, most often random variables.">bdm::RV</a>.</p> |
147 | | <p>For convenience in Matlab, the following operations are defined:</p> |
148 | | <ul> |
149 | | <li>RV(names,sizes,times) creates configuration structure for RV,</li> |
150 | | <li>RVjoin(rvs) joins configuration structures for array of RVs rvs=[rv1,rv2,...],</li> |
151 | | <li>RVtimes(rvs,times) assign times to corresponding rvs.</li> |
152 | | </ul> |
153 | | <p>See examples in bdmtoolbox/tutorial/userguide</p> |
154 | | <h3><a class="anchor" id="ug_rv_connect"> |
| 120 | Algebra on RVs (adding, searching in, subtraction, intersection, etc.) is implemented, see <a class="el" href="classbdm_1_1RV.html" title="Class representing variables, most often random variables.">bdm::RV</a>.<p> |
| 121 | For convenience in Matlab, the following operations are defined:<ul> |
| 122 | <li>RV(names,sizes,times) creates configuration structure for RV,</li><li>RVjoin(rvs) joins configuration structures for array of RVs rvs=[rv1,rv2,...],</li><li>RVtimes(rvs,times) assign times to corresponding rvs.</li></ul> |
| 123 | <p> |
| 124 | See examples in bdmtoolbox/tutorial/userguide<h3><a class="anchor" name="ug_rv_connect"> |
159 | | <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> |
160 | | <ol type="1"> |
161 | | <li>initialize its storage (<a class="el" href="classbdm_1_1logger.html#acd6291a73491f755dc7bc88d022eb2fc" title="Initialize the storage.">bdm::logger.init()</a>),</li> |
162 | | <li>assign a connection point to each interested object (bdm::logger.logadd()),</li> |
163 | | <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> |
164 | | <li>finalize the storage when experiment is finished.</li> |
165 | | </ol> |
166 | | <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> |
167 | | <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> |
168 | | <h2><a class="anchor" id="ug_datasource"> |
| 128 | Loggers are universal objects for storing and manipulating the results of an experiment. Similar to DataSource, every logger has to provide basic functionality:<ol type=1> |
| 129 | <li>initialize its storage (<a class="el" href="classbdm_1_1logger.html#cd6291a73491f755dc7bc88d022eb2fc" title="Initialize the storage.">bdm::logger.init()</a>),</li><li>assign a connection point to each interested object (bdm::logger.logadd()),</li><li>accept data to be logged to given connection (<a class="el" href="classbdm_1_1logger.html#e30f695619931b3c372ab280e452d309" title="log this vector">bdm::logger.logit()</a>),</li><li>finalize the storage when experiment is finished.</li></ol> |
| 130 | <p> |
| 131 | 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> |
| 132 | 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>.<h2><a class="anchor" name="ug_datasource"> |
170 | | <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 > Class Hierarchy.</p> |
171 | | <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> |
172 | | <ul> |
173 | | <li><a class="el" href="classbdm_1_1EpdfDS.html" title="Simulate data from a static pdf (epdf).">bdm::EpdfDS</a></li> |
174 | | <li><a class="el" href="classbdm_1_1MemDS.html" title="Memory storage of off-line data column-wise.">bdm::MemDS</a><ul> |
| 134 | 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 > Class Hierarchy.<p> |
| 135 | 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><ul> |
| 136 | <li><a class="el" href="classbdm_1_1EpdfDS.html" title="Simulate data from a static pdf (epdf).">bdm::EpdfDS</a></li><li><a class="el" href="classbdm_1_1MemDS.html" title="Memory storage of off-line data column-wise.">bdm::MemDS</a><ul> |
176 | | <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> |
177 | | <li><a class="el" href="classbdm_1_1ITppFileDS.html" title="Read Data Matrix from an IT file.">bdm::ITppFileDS</a></li> |
178 | | </ul> |
179 | | </li> |
180 | | </ul> |
181 | | </li> |
182 | | <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> |
183 | | <li><a class="el" href="classbdm_1_1stateDS.html">bdm::stateDS</a></li> |
184 | | </ul> |
185 | | <p>The MemDS has already been introduced in the example in memds. 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> |
186 | | <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> |
187 | | <p>For example, we wish to simulate realizations of a Uniform pdf on interval <-1,1>. 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> |
188 | | <div class="fragment"><pre class="fragment">U.class=<span class="stringliteral">'euni'</span>; |
189 | | U.rv = RV({<span class="charliteral">'a'</span>}); |
| 138 | <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><li><a class="el" href="classbdm_1_1ITppFileDS.html" title="Read Data Matrix from an IT file.">bdm::ITppFileDS</a></li></ul> |
| 139 | </li></ul> |
| 140 | </li><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><li>bdm::stateDS</li></ul> |
| 141 | <p> |
| 142 | The MemDS has already been introduced in the example in memds. 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> |
| 143 | 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> |
| 144 | For example, we wish to simulate realizations of a Uniform pdf on interval <-1,1>. 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#77f5fef1f006fe056066da23b9e5f042">bdm::euni.from_setting()</a>. Using documentation we define it with the following code: <div class="fragment"><pre class="fragment">U.class=<span class="stringliteral">'euni'</span>; |
| 145 | U.rv = RV({<span class="charliteral">'a'</span>}); |
207 | | <p>Consider the following autoregressive model: </p> |
208 | | <p class="formulaDsp"> |
209 | | <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"/> |
210 | | </p> |
211 | | <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> |
212 | | <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> |
213 | | <ol type="1"> |
214 | | <li>extra unsimulated variable <img class="formulaInl" alt="$ u $" src="form_152.png"/>,</li> |
215 | | <li>time delayes of the values.</li> |
216 | | </ol> |
217 | | <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> |
218 | | <p class="formulaDsp"> |
219 | | <img class="formulaDsp" alt="\[ f(u_t) = \mathcal{N}(0, r_u) \]" src="form_162.png"/> |
220 | | </p> |
221 | | <p> where <img class="formulaInl" alt="$ r_u $" src="form_163.png"/> is another known constant. Thus, the joint density is now:</p> |
222 | | <p class="formulaDsp"> |
223 | | <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"/> |
224 | | </p> |
225 | | <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> |
226 | | <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> |
227 | | <div class="fragment"><pre class="fragment">y = RV({<span class="charliteral">'y'</span>}); |
228 | | u = RV({<span class="charliteral">'u'</span>}); |
229 | | |
230 | | fy.class = <span class="stringliteral">'mlnorm<ldmat>'</span>; |
| 161 | Consider the following autoregressive model: <p class="formulaDsp"> |
| 162 | <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"> |
| 163 | <p> |
| 164 | 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> |
| 165 | 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:<ol type=1> |
| 166 | <li>extra unsimulated variable <img class="formulaInl" alt="$ u $" src="form_152.png">,</li><li>time delayes of the values.</li></ol> |
| 167 | <p> |
| 168 | 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 class="formulaDsp"> |
| 169 | <img class="formulaDsp" alt="\[ f(u_t) = \mathcal{N}(0, r_u) \]" src="form_162.png"> |
| 170 | <p> |
| 171 | where <img class="formulaInl" alt="$ r_u $" src="form_163.png"> is another known constant. Thus, the joint density is now:<p class="formulaDsp"> |
| 172 | <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"> |
| 173 | <p> |
| 174 | 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> |
| 175 | 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: <div class="fragment"><pre class="fragment">y = RV({<span class="charliteral">'y'</span>}); |
| 176 | u = RV({<span class="charliteral">'u'</span>}); |
| 177 | |
| 178 | fy.class = <span class="stringliteral">'mlnorm<ldmat>'</span>; |
246 | | </pre></div><p>Explanation of this example will require few remarks:</p> |
247 | | <ul> |
248 | | <li>class of the <code>fy</code> object is 'mlnorm<ldmat>' 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> |
249 | | <li>naming convention 'mlnorm<ldmat>' 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<ldmat>, <a class="el" href="classbdm_1_1mlnorm.html">mlnorm<chmat></a>, mlnorm<fsqmat>. 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> |
250 | | <li>the same concept is used for enorm, where <a class="el" href="classbdm_1_1enorm.html">enorm<chmat></a> and <a class="el" href="classbdm_1_1enorm.html">enorm<fsqmat></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<chmat></a> is approprate.</li> |
251 | | <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> |
252 | | <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> |
253 | | </ul> |
254 | | <p>The code above can be immediatelly run, usin the same execution sequence of <code>estimator</code> as above.</p> |
255 | | <h3><a class="anchor" id="ug_ini"> |
| 194 | </pre></div><p> |
| 195 | Explanation of this example will require few remarks:<ul> |
| 196 | <li>class of the <code>fy</code> object is 'mlnorm<ldmat>' 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><li>naming convention 'mlnorm<ldmat>' 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<ldmat>, <a class="el" href="classbdm_1_1mlnorm.html">mlnorm<chmat></a>, mlnorm<fsqmat>. 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><li>the same concept is used for enorm, where <a class="el" href="classbdm_1_1enorm.html">enorm<chmat></a> and <a class="el" href="classbdm_1_1enorm.html">enorm<fsqmat></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<chmat></a> is approprate.</li><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><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></ul> |
| 197 | <p> |
| 198 | The code above can be immediatelly run, usin the same execution sequence of <code>estimator</code> as above.<h3><a class="anchor" name="ug_ini"> |
257 | | <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> |
258 | | <div class="fragment"><pre class="fragment">DS.init_rv = RV({<span class="charliteral">'y'</span>,<span class="charliteral">'y'</span>,<span class="charliteral">'y'</span>}, [1,1,1], [-1,-2,-3]); |
| 200 | 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#39ef55fb36da354c93c544129e03f558">bdm::MpdfDS.from_setting()</a> ): <div class="fragment"><pre class="fragment">DS.init_rv = RV({<span class="charliteral">'y'</span>,<span class="charliteral">'y'</span>,<span class="charliteral">'y'</span>}, [1,1,1], [-1,-2,-3]); |
263 | | <p>If the simulated data are to be analyzed off-line it may be advantageous to store them and use for later use. This operation is straightforward if the class of logger used in the <code>simulator</code> is compatible with some datasource class.</p> |
264 | | <p>For example, the output of <code>MemDS</code> can be stored as an .it file (filename is specified in configuration structure) which can be later read by <a class="el" href="classbdm_1_1ITppFileDS.html" title="Read Data Matrix from an IT file.">bdm::ITppFileDS</a>.</p> |
265 | | <p>In matlab, the output of mexlog is a structure of vectors or matrices. The results can be saved in a matlab file using: </p> |
266 | | <div class="fragment"><pre class="fragment">Data=[M.y; M.u]; |
| 205 | If the simulated data are to be analyzed off-line it may be advantageous to store them and use for later use. This operation is straightforward if the class of logger used in the <code>simulator</code> is compatible with some datasource class.<p> |
| 206 | For example, the output of <code>MemDS</code> can be stored as an .it file (filename is specified in configuration structure) which can be later read by <a class="el" href="classbdm_1_1ITppFileDS.html" title="Read Data Matrix from an IT file.">bdm::ITppFileDS</a>.<p> |
| 207 | In matlab, the output of mexlog is a structure of vectors or matrices. The results can be saved in a matlab file using: <div class="fragment"><pre class="fragment">Data=[M.y; M.u]; |