1 | /*! |
---|
2 | \page userguide_sim BDM Use - System, Data, Simulation |
---|
3 | |
---|
4 | This section serves as introduction to the scenario of data simulation. Since it is the simplest of all scenarios defined in \ref userguide0 it also serves as introduction to configuration of an experiment (see \ref ui) and basic decision making objects (bdm::RV and bdm::DS). |
---|
5 | |
---|
6 | All experiments are demonstrated on mex file \c simulator, which is also available as a standalone application. |
---|
7 | |
---|
8 | Table of contents: |
---|
9 | \section ug_sim_config |
---|
10 | \section ug_sim |
---|
11 | \section ug_memds |
---|
12 | \section ug_rvs |
---|
13 | \subsection ug_rv_connect |
---|
14 | \section ug_pdfds |
---|
15 | \section ug_arx_sim |
---|
16 | \subsection ug_ini |
---|
17 | \section ug_store |
---|
18 | |
---|
19 | |
---|
20 | \section ug_sim_config Configuration of an experiment |
---|
21 | |
---|
22 | 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. |
---|
23 | |
---|
24 | 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 \c class. This is a string variable corresponding to the name of the class to be used. This information is stored in Matlab structures (or objects, see section on Matlab extensions). |
---|
25 | |
---|
26 | Advanced users can find more information in (\ref ui). |
---|
27 | |
---|
28 | \subsection ug_first First experiment |
---|
29 | |
---|
30 | The first experiment that can be performed is: |
---|
31 | \code |
---|
32 | DS.class='MemDS'; |
---|
33 | DS.Data =[1 2 3 4 5 6]; |
---|
34 | \endcode |
---|
35 | which can be found in file bdmtoolbox/tutorials/userguide/memds_example.m. |
---|
36 | |
---|
37 | The code above is the minimum necessary information to run scenario \c simulator in Matlab. |
---|
38 | To actually do so, make sure that Matlab paths are correctly set, as described in \ref install. |
---|
39 | |
---|
40 | The expected result for Matlab is: |
---|
41 | \code |
---|
42 | >> M=simulator(DS) |
---|
43 | |
---|
44 | M = |
---|
45 | |
---|
46 | ch0: [6x1 double] |
---|
47 | \endcode |
---|
48 | |
---|
49 | All that the simulator did was actually copying \c DS.Data to \c M.ch0. Explanation of the experiment and the logic used there follows. |
---|
50 | |
---|
51 | \section ug_sim Systems and DataSources |
---|
52 | |
---|
53 | In standard system theory, the system is typically illustrated graphically as: |
---|
54 | \dot |
---|
55 | digraph sys{ |
---|
56 | node [shape=box]; |
---|
57 | {"System"} |
---|
58 | node [shape=plaintext] |
---|
59 | {rank="same"; "u"; "System"; "y"} |
---|
60 | "u" -> "System" -> "y" [nodesep=2]; |
---|
61 | } |
---|
62 | \enddot |
---|
63 | Where \c u typically denotes input and \c y denotes output of the system. A causal dependence between input and output is typically presumed. |
---|
64 | |
---|
65 | We are predominantly concerned with discrete-time systems, hence, we will add indices \f$ _t \f$ to both input and output, \f$ u_t \f$ and \f$ y_t \f$. We presume that the causal dependence is \f$ u_t \f$ comes before \f$ y_t \f$. |
---|
66 | |
---|
67 | 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 \c DataSource is. |
---|
68 | |
---|
69 | DataSource is an object that is essentially able to: |
---|
70 | -# return data observed at time \f$ t \f$, |
---|
71 | -# perform one a time step. |
---|
72 | -# describe what these data are, via class RV, introduced in ref \ref ug_pdf_marg, |
---|
73 | -# store log of its activity into dedicated logger. |
---|
74 | |
---|
75 | No further specification, e.g. if the data are pre-recorded or computed on-the-fly, are given. |
---|
76 | For a list of available DataSources, see ... |
---|
77 | |
---|
78 | |
---|
79 | \section ug_memds DataSource of pre-recorded data -- MemDS |
---|
80 | |
---|
81 | The first experiment run in \ref ug_first was actually an instance of DataSource of pre-recorded data that were stored in memory, i.e. the bdm::MemDS class. |
---|
82 | |
---|
83 | Operation of such object is trivial, the data are stored as a matrix and the general operations defined above are specialized as follows: |
---|
84 | -# data observed at time \f$ t \f$ are columns of the matrix, getdata() returns current column, |
---|
85 | -# time step itself is performed by increasing the column index, |
---|
86 | -# each row is named as "ch0","ch1",... |
---|
87 | |
---|
88 | This is the default behavior. It can be customized using the UI mechanism. Full list of options is: |
---|
89 | \code |
---|
90 | DS.class = 'MemDS'; |
---|
91 | DS.Data = (...); // Data matrix or data vector |
---|
92 | --- optional --- |
---|
93 | DS.drv = RV({"ch0",...} ); // Identification how rows of the matrix Data will be known to others |
---|
94 | DS.time = 0; // Index of the first column to user_info, |
---|
95 | \endcode |
---|
96 | The compulsory fields are listed at the beginning; the optional fields are separated by string "--- optional ---". |
---|
97 | |
---|
98 | Fields \c time denotes the first record to start counting from. Field \c drv is a the one that specifies identification of the data elements, (point 3. of the general requirements of a DataSource). |
---|
99 | |
---|
100 | All optionals fields will be filled by default values, it this case: |
---|
101 | \code |
---|
102 | DS.drv = RV({'ch0'},1,0); |
---|
103 | DS.time = 0; |
---|
104 | \endcode |
---|
105 | Where the first line specifies a universal identification structure: random variable (bdm::RV). |
---|
106 | |
---|
107 | \section ug_rvs What is RV and how to use it |
---|
108 | |
---|
109 | RV stands for \c random \c variable which is a description of random variable or its realization. However, this object serves as general descriptor of fields in vectors of data. |
---|
110 | |
---|
111 | It is used for: |
---|
112 | - description of RV in pdfs, ways how to define marginalization and conditioning, |
---|
113 | - connection between source of data and computational objects that use them, |
---|
114 | - connection <b>time</b>, more exactly time shift from \f$ t \f$, defaults to 0. |
---|
115 | |
---|
116 | For example, the estimators will request the data from the above mentioned data source by asking for rv 'ch0'. If a more meaningful names are available, the fields drv can be added to read: |
---|
117 | \code |
---|
118 | DS.class='MemDS'; |
---|
119 | DS.Data =[1 2 3 4 5 6]; |
---|
120 | DS.drv = RV('y'); |
---|
121 | \endcode |
---|
122 | Data from this data source will be available when estimators ask for rv 'y'. |
---|
123 | |
---|
124 | \subsection ug_rv_connect Storing results |
---|
125 | |
---|
126 | results of an experiment can be stored in many ways. This functionality was abstracted into a class called logger. Exact form of the stored results is chosen by choosing appropriate class. |
---|
127 | For example, \c stdlog writes all output in the console, \c dirfilelog writes all data in the dirfilelog format for high-speed data processing, \c mexlog writes data into Matlab structure. |
---|
128 | The \c mexlog is the default option in bdmtoolbox. |
---|
129 | |
---|
130 | Connection between computational blocks and loggers is controlled by structure called \c log_level which governs the level of details to be logged. |
---|
131 | A standard Data source has two levels, \c logdt and \c logut which means "log all outputs, dt" and "log all inputs, ut". |
---|
132 | 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. |
---|
133 | |
---|
134 | \section ug_pdfds How to create a simulator from pdfs |
---|
135 | For example, we would like to simulate a random Uniform distributed noise on interval <-1,1>. |
---|
136 | This is achieved by plugging an object representing uniform pdf into general simulator of independent random samples, pdfDS. Uniform density is implemented as class bdm::euni, which is created from the following structure: |
---|
137 | \code |
---|
138 | U.class='euni'; |
---|
139 | U.rv = RV({'a'}); |
---|
140 | U.high = 1.0; |
---|
141 | U.low = -1.0; |
---|
142 | \endcode |
---|
143 | which encodes information:\f[ |
---|
144 | f(a) = \mathcal{U}(-1,1) |
---|
145 | \f] |
---|
146 | |
---|
147 | The datasource itself, i.e. the instance of \c EpdfDS can be then configured via: |
---|
148 | \code |
---|
149 | DS.class = 'pdfDS'; |
---|
150 | DS.pdf = U; |
---|
151 | \endcode |
---|
152 | where \c U is the structure defined above. |
---|
153 | |
---|
154 | Contrary to the previous example, we need to tell to algorithm \c simulator how many samples from the data source we need. This is configured by variable \c experiment.ndat. The configuration has to be finalized by: |
---|
155 | \code |
---|
156 | experiment.ndat = 10; |
---|
157 | M=simulator(DS,experiment); |
---|
158 | \endcode |
---|
159 | |
---|
160 | The result is as expected in field \c M.DS_dt_a the name of which corresponds to results form "datasouce" / "output_dt" / "name given in U.rv". |
---|
161 | |
---|
162 | If the task was only to generate random realizations, this would indeed be a very clumsy way of doing it. |
---|
163 | However, the power of the proposed approach will be revealed in more demanding examples, one of which follows next. |
---|
164 | |
---|
165 | By default, data from this datasouce will be named after the rvs in given by the pdfs. When pdf with no rv is used, drv of the data source is set again to 'ch0'. |
---|
166 | |
---|
167 | \section ug_arx_sim Simulating autoregressive model |
---|
168 | |
---|
169 | Consider the following autoregressive model: |
---|
170 | \f[ |
---|
171 | f(y_t|y_{t-3},u_{t-1}) = \mathcal{N}( a y_{t-3} + b u_{t-1}, r) |
---|
172 | \f] |
---|
173 | where \f$ a,b \f$ are known constants, and \f$ r \f$ is known variance. |
---|
174 | |
---|
175 | We need to handle two issues: |
---|
176 | -# extra unsimulated variable \f$ u \f$, |
---|
177 | -# time delays of the values. |
---|
178 | |
---|
179 | The first issue can be handled in two ways. First, \f$ u \f$ can be considered as input and as such it could be externally given to the datasource. This solution is used in scenario \c closedloop. |
---|
180 | However, for the \c simulator scenario we will apply the second option, that is we complement \f$ f(y_{t}|y_{t-3},u_{t-1})\f$ by extra pdf:\f[ |
---|
181 | f(u_t) = \mathcal{N}(0, r_u) |
---|
182 | \f] |
---|
183 | where \f$ r_u \f$ is another known constant. |
---|
184 | Thus, the joint density is now:\f[ |
---|
185 | f(y_{t},u_{t}|y_{t-3},u_{t-1}) = f(y_{t}|y_{t-3},u_{t-1})f(u_{t}) |
---|
186 | \f] |
---|
187 | 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. |
---|
188 | |
---|
189 | That is done in automatic way (via bdm::datalink_buffered). The only issue a user may need to take care about is the missing initial conditions for simulation. |
---|
190 | By default these are set to zeros. Using the default values, the full configuration of this system is: |
---|
191 | \code |
---|
192 | y = RV({'y'}); |
---|
193 | u = RV({'u'}); |
---|
194 | |
---|
195 | fy.class = 'mlnorm\<ldmat\>'; |
---|
196 | fy.rv = y; |
---|
197 | fy.rvc = RV({'y','u'}, [1 1], [-3, -1]); |
---|
198 | fy.A = [0.5, -0.9]; |
---|
199 | fy.const = 0; |
---|
200 | fy.R = 0.1; |
---|
201 | |
---|
202 | |
---|
203 | fu.class = 'enorm\<ldmat\>'; |
---|
204 | fu.rv = u; |
---|
205 | fu.mu = 0; |
---|
206 | fu.R = 0.2; |
---|
207 | |
---|
208 | DS.class = 'pdfDS'; |
---|
209 | DS.pdf.class = 'mprod'; |
---|
210 | DS.pdf.pdfs = {fy, fu}; |
---|
211 | \endcode |
---|
212 | |
---|
213 | Explanation of this example will require few remarks: |
---|
214 | - class of the \c fy object is 'mlnorm\<ldmat\>' which is Normal pdf with mean value given by linear function, and covariance matrix stored in LD decomposition, see bdm::mlnorm for details. |
---|
215 | - naming convention 'mlnorm\<ldmat\>' relates to the concept of templates in C++. For those unfamiliar with this concept, it is basically a way how to share code for different flavors of the same object. Note that mlnorm exist in three versions: mlnorm\<ldmat\>, mlnorm<chmat>, 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. |
---|
216 | - the same concept is used for enorm, where enorm<chmat> and enorm<fsqmat> are also possible. In this particular use, these objects are equivalent. In specific situation, e.g. Kalman filter implemented on Choleski decomposition (bdm::KalmanCh), only enorm<chmat> is approprate. |
---|
217 | - class 'mprod' represents the chain rule of probability, see \ref ug_pdf_cond. |
---|
218 | |
---|
219 | The code above can be immediately run, using the same execution sequence of \c estimator as above. |
---|
220 | |
---|
221 | \subsection ug_ini Initializing simulation |
---|
222 | |
---|
223 | When zeros are not appropriate initial conditions, the correct conditions can be set using additional commands: |
---|
224 | \code |
---|
225 | DS.init_rv = RV({'y','y','y'}, [1,1,1], [-1,-2,-3]); |
---|
226 | DS.init_values = [0.1, 0.2, 0.3]; |
---|
227 | \endcode |
---|
228 | |
---|
229 | The values of \c init_values will be copied to places in history identified by corresponding values of \c init_rv. |
---|
230 | Initial data is not checked for completeness, i.e. values of random variables missing from \c init_rv (in this case all occurrences of \f$ u \f$) are still initialized to 0. |
---|
231 | |
---|
232 | \section ug_store Storing results of simulation |
---|
233 | |
---|
234 | If the simulated data are to be analyzed off-line it may be advantageous to store them and use later on. |
---|
235 | This operation is straightforward if the class of logger used in the \c simulator is compatible with some datasource class. |
---|
236 | |
---|
237 | For example, the output of \c MemDS can be stored as an .it file (filename is specified in configuration structure) which can be later read by bdm::ITppFileDS. |
---|
238 | |
---|
239 | In Matlab, the output of mexlog is a structure of vectors or matrices. The results can be saved in a Matlab file using: |
---|
240 | \code |
---|
241 | Data=[M.y; M.u]; |
---|
242 | drv = RVjoin({y,u}); |
---|
243 | save pdfds_results Data drv |
---|
244 | \endcode |
---|
245 | Such data can be later provided e.g. by MemDS |
---|
246 | \code |
---|
247 | mxDS.class = 'MemDS'; |
---|
248 | mxDS.Data = 'Data'; |
---|
249 | mxDS.drv = drv; |
---|
250 | \endcode |
---|
251 | |
---|
252 | For list of all DataSources and loggers, see \ref app_base |
---|
253 | */ |
---|