simulation::montecarlo(n)
π NAME π
okay so, this page is simulation::montecarlo(n) β yes bestie, the (n) is the Tcl manual-page flex, we love that for her β and it's literally all about Monte Carlo simulations. β¨
β¨ SYNOPSIS β¨
package require Tcl ?8.5 9?
package require simulation::montecarlo 0.2
package require simulation::random
package require math::statistics
::simulation::montecarlo::getOption keyword
::simulation::montecarlo::hasOption keyword
::simulation::montecarlo::setOption keyword value
::simulation::montecarlo::setTrialResult values
::simulation::montecarlo::setExpResult values
::simulation::montecarlo::getTrialResults
::simulation::montecarlo::getExpResult
::simulation::montecarlo::transposeData values
::simulation::montecarlo::integral2D ...
::simulation::montecarlo::singleExperiment args
π DESCRIPTION π
The whole Monte Carlo simulation thing is genuinely so simple, like it's giving "why is this not obvious":
- generate random values for one or more parameters. π²
- evaluate the model of some system you're interested in and record the interesting results for each realisation of these parameters.
- after a suitable number of such trials, deduce an overall characteristic of the model.
You can think of a model of a network of computers, an ecosystem of some kind, or in fact anything that can be quantitatively described and has some stochastic element in it. no cap, that's the whole range. π
The package simulation::montecarlo offers a basic framework for such a modelling technique:
#
# MC experiments:
# Determine the mean and median of a set of points and compare them
#
::simulation::montecarlo::singleExperiment -init {
package require math::statistics
set prng [::simulation::random::prng_Normal 0.0 1.0]
} -loop {
set numbers {}
for { set i 0 } { $i < [getOption samples] } { incr i } {
lappend numbers [$prng]
}
set mean [::math::statistics::mean $numbers]
set median [::math::statistics::median $numbers] ;# ? Exists?
setTrialResult [list $mean $median]
} -final {
set result [getTrialResults]
set means {}
set medians {}
foreach r $result {
foreach {m M} $r break
lappend means $m
lappend medians $M
}
puts [getOption reportfile] "Correlation: [::math::statistics::corr $means $medians]"
} -trials 100 -samples 10 -verbose 1 -columns {Mean Median}
This example attempts to find out how well the median value and the mean value of a random set of numbers correlate. Sometimes a median value is a more robust characteristic than a mean value β especially if you have a statistical distribution with "fat" tails. π
π¦ PROCEDURES π¦
The package defines the following auxiliary procedures:
::simulation::montecarlo::getOption keywordβ Get the value of an option given as part of thesingleExperimentcommand.- string
keywordβ Given keyword (without leading minus)
- string
::simulation::montecarlo::hasOption keywordβ Returns1if the option is available,0if not.- string
keywordβ Given keyword (without leading minus)
- string
::simulation::montecarlo::setOption keyword valueβ Set the value of the given option.- string
keywordβ Given keyword (without leading minus) - string
valueβ (New) value for the option
- string
::simulation::montecarlo::setTrialResult valuesβ Store the results of the trial for later analysis.- list
valuesβ List of values to be stored
- list
::simulation::montecarlo::setExpResult valuesβ Set the results of the entire experiment (typically used in the final phase).- list
valuesβ List of values to be stored
- list
::simulation::montecarlo::getTrialResultsβ Get the results of all individual trials for analysis (typically used in the final phase or after completion of the command).::simulation::montecarlo::getExpResultβ Get the results of the entire experiment (typically used in the final phase or even after completion of thesingleExperimentcommand).::simulation::montecarlo::transposeData valuesβ Interchange columns and rows of a list of lists and return the result.- list
valuesβ List of lists of values
- list
There are two main procedures: integral2D and singleExperiment. π«Ά
::simulation::montecarlo::integral2D ...β Integrate a function over a two-dimensional region using a Monte Carlo approach.- Arguments
PM
- Arguments
::simulation::montecarlo::singleExperiment argsβ Iterate code over a number of trials and store the results. The iteration is gouverned by parameters given via a list of keyword-value pairs.- int
nβ List of keyword-value pairs, all of which are available during the execution via thegetOptioncommand.
- int
The singleExperiment command predefines the following options:
-init code: code to be run at start up-loop body: body of code that defines the computation to be run time and again. The code should usesetTrialResultto store the results of each trial (typically a list of numbers, but the interpretation is up to the implementation). Note: Required keyword. β οΈ-final code: code to be run at the end-trials n: number of trials in the experiment (required)-reportfile file: opened file to send the output to (default:stdout)-verbose: write the intermediate results (1) or not (0) (default:0)-analysis proc: either"none"(no automatic analysis),standard(basic statistics of the trial results and a correlation matrix) or the name of a procedure that will take care of the analysis.-columns list: list of column names, useful for verbose output and the analysis
Any other options can be used via the getOption procedure in the body. π
πΈ TIPS πΈ
The procedure singleExperiment works by constructing a temporary procedure that does the actual work. It loops for the given number of trials. π
As it constructs a temporary procedure, local variables defined at the start continue to exist in the loop. (iconic behaviour, honestly)
π KEYWORDS π
math, montecarlo simulation, stochastic modelling
π CATEGORY π
Mathematics
π COPYRIGHT π
Copyright (c) 2008 Arjen Markus <arjenmarkus@users.sourceforge.net>
okay that's the whole page bestie, you ate that π«Ά this was simulation::montecarlo(n) from tcllib, version 0.2, and the copyright vibes go all the way back to 2008 β periodt. π
β¨
π¦ where this page comes from
- fedora 43: tcllib 2.0-3.fc43 Β· TCL
- fedora 44: tcllib 2.0-4.fc44 Β· TCL
- nixos 26.05: tcllib 2.0
- nixpkgs unstable: tcllib 2.0
- Copyright (c) 2008 Arjen Markus <arjenmarkus@users\&.sourceforge\&.net>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms