1simulation::montecarlo(n)    Tcl Simulation Tools    simulation::montecarlo(n)
2
3
4
5______________________________________________________________________________
6

NAME

8       simulation::montecarlo - Monte Carlo simulations
9

SYNOPSIS

11       package require Tcl  ?8.4?
12
13       package require simulation::montecarlo  0.1
14
15       package require simulation::random
16
17       package require math::statistics
18
19       ::simulation::montecarlo::getOption keyword
20
21       ::simulation::montecarlo::hasOption keyword
22
23       ::simulation::montecarlo::setOption keyword value
24
25       ::simulation::montecarlo::setTrialResult values
26
27       ::simulation::montecarlo::setExpResult values
28
29       ::simulation::montecarlo::getTrialResults
30
31       ::simulation::montecarlo::getExpResult
32
33       ::simulation::montecarlo::transposeData values
34
35       ::simulation::montecarlo::integral2D ...
36
37       ::simulation::montecarlo::singleExperiment args
38
39_________________________________________________________________
40

DESCRIPTION

42       The technique of Monte Carlo simulations is basically simple:
43
44       ·      generate random values for one or more parameters.
45
46       ·      evaluate  the  model  of  some  system you are interested in and
47              record the interesting results for  each  realisation  of  these
48              parameters.
49
50       ·      after  a suitable number of such trials, deduce an overall char‐
51              acteristic of the model.
52
53       You can think of a model of a network of  computers,  an  ecosystem  of
54       some  kind or in fact anything that can be quantitatively described and
55       has some stochastic element in it.
56
57       The package simulation::montecarlo offers a basic framework for such  a
58       modelling technique:
59
60       #
61       # MC experiments:
62       # Determine the mean and median of a set of points and compare them
63       #
64       ::simulation::montecarlo::singleExperiment -init {
65           package require math::statistics
66
67           set prng [::simulation::random::prng_Normal 0.0 1.0]
68       } -loop {
69           set numbers {}
70           for { set i 0 } { $i < [getOption samples] } { incr i } {
71               lappend numbers [$prng]
72           }
73           set mean   [::math::statistics::mean $numbers]
74           set median [::math::statistics::median $numbers] ;# ? Exists?
75           setTrialResult [list $mean $median]
76       } -final {
77           set result [getTrialResults]
78           set means   {}
79           set medians {}
80           foreach r $result {
81               foreach {m M} $r break
82               lappend means   $m
83               lappend medians $M
84           }
85           puts [getOption reportfile] "Correlation: [::math::statistics::corr $means $medians]"
86
87       } -trials 100 -samples 10 -verbose 1 -columns {Mean Median}
88
89       This example attemps to find out how well the median value and the mean
90       value of a random set of numbers correlate. Sometimes a median value is
91       a more robust characteristic than a mean value - especially if you have
92       a statistical distribution with "fat" tails.
93

PROCEDURES

95       The package defines the following auxiliary procedures:
96
97       ::simulation::montecarlo::getOption keyword
98              Get the value of an option given as part of the  singeExperiment
99              command.
100
101              string keyword
102                     Given keyword (without leading minus)
103
104
105       ::simulation::montecarlo::hasOption keyword
106              Returns 1 if the option is available, 0 if not.
107
108              string keyword
109                     Given keyword (without leading minus)
110
111
112       ::simulation::montecarlo::setOption keyword value
113              Set the value of the given option.
114
115              string keyword
116                     Given keyword (without leading minus)
117
118              string value
119                     (New) value for the option
120
121
122       ::simulation::montecarlo::setTrialResult values
123              Store the results of the trial for later analysis
124
125              list values
126                     List of values to be stored
127
128
129       ::simulation::montecarlo::setExpResult values
130              Set  the results of the entire experiment (typically used in the
131              final phase).
132
133              list values
134                     List of values to be stored
135
136
137       ::simulation::montecarlo::getTrialResults
138              Get the results of all individual trials for analysis (typically
139              used in the final phase or after completion of the command).
140
141
142       ::simulation::montecarlo::getExpResult
143              Get  the results of the entire experiment (typically used in the
144              final phase or even after  completion  of  the  singleExperiment
145              command).
146
147
148       ::simulation::montecarlo::transposeData values
149              Interchange  columns  and rows of a list of lists and return the
150              result.
151
152              list values
153                     List of lists of values
154
155       There are two main procedures: integral2D and singleExperiment.
156
157       ::simulation::montecarlo::integral2D ...
158              Integrate a function over a two-dimensional region using a Monte
159              Carlo approach.
160
161              Arguments PM
162
163
164       ::simulation::montecarlo::singleExperiment args
165              Iterate  code over a number of trials and store the results. The
166              iteration is gouverned by parameters given via a  list  of  key‐
167              word-value pairs.
168
169              int n  List  of  keyword-value pairs, all of which are available
170                     during the execution via the getOption command.
171
172       The singleExperiment command predefines the following options:
173
174       ·      -init code: code to be run at start up
175
176       ·      -loop body: body of code that defines the computation to be  run
177              time  and again. The code should use setTrialResult to store the
178              results of each trial (typically a  list  of  numbers,  but  the
179              interpretation is up to the implementation). Note: Required key‐
180              word.
181
182       ·      -final code: code to be run at the end
183
184       ·      -trials n: number of trials in the experiment (required)
185
186       ·      -reportfile file: opened file to send the  output  to  (default:
187              stdout)
188
189       ·      -verbose:   write  the  intermediate  results  (1)  or  not  (0)
190              (default: 0)
191
192       ·      -analysis proc: either "none" (no automatic analysis),  standard
193              (basic statistics of the trial results and a correlation matrix)
194              or the name of a procedure that will take care of the analysis.
195
196       ·      -columns list: list of column names, useful for  verbose  output
197              and the analysis
198
199       Any other options can be used via the getOption procedure in the body.
200

TIPS

202       The procedure singleExperiment works by constructing a temporary proce‐
203       dure that does the actual work. It loops for the given number  of  tri‐
204       als.
205
206       As  it constructs a temporary procedure, local variables defined at the
207       start continue to exist in the loop.
208

KEYWORDS

210       math, montecarlo simulation, stochastic modelling
211
213       Copyright (c) 2008 Arjen Markus <arjenmarkus@users.sourceforge.net>
214
215
216
217
218simulation                            0.1            simulation::montecarlo(n)
Impressum