simulation::montecarlo(3tcl)
π simulation::montecarlo β bestie's guide to Monte Carlo simulations π
okay so this is the manual for simulation::montecarlo, a page from the 3tcl section of the Tcl Simulation Tools docs. yes it's math, yes it's iconic, no you don't need to be scared. let's go π
β¨ NAME β¨
simulation::montecarlo β Monte Carlo simulations. that's it, that's the vibe. literally just doing a thing a bunch of times with random numbers until the truth falls out. π¦
πΈ 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 π
so the whole Monte Carlo technique is, lowkey, super simple. like it's giving "why didn't I think of that" energy:
- 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 those parameters.
- after a suitable number of such trials, deduce an overall characteristic of the model. β¨
and bestie, the "model" can be literally anything. a network of computers? an ecosystem? anything that can be quantitatively described and has some stochastic element in it. if it's got randomness in it, it's fair game. periodt.
the simulation::montecarlo package gives you a basic framework for exactly that kind of 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}
so what's this example even doing? it's trying to figure out how well the median value and the mean value of a random set of numbers correlate. and honestly? sometimes a median value is just a more robust characteristic than a mean value β especially when you're dealing with a statistical distribution that has "fat" tails. the median said "I got this" and the mean said "β¦ok". π«Ά
π₯ PROCEDURES π₯
the package defines the following auxiliary procedures (auxiliary = the little helpers that do the real work, iykyk):
::simulation::montecarlo::getOption keywordβ grab the value of an option that was given as part of thesingleExperimentcommand. it's giving "what did I set this to again".- string
keywordβ the given keyword (without a leading minus, bestie, no dashes here)
- string
::simulation::montecarlo::hasOption keywordβ returns1if the option is available,0if not. simple yes/no, no notes. β- string
keywordβ the given keyword (without leading minus)
- string
::simulation::montecarlo::setOption keyword valueβ set the value of the given option.- string
keywordβ the given keyword (without leading minus) - string
valueβ the (new) value for the option
- string
::simulation::montecarlo::setTrialResult valuesβ store the results of the trial for later analysis. hoard those numbers, queen. πΎ- 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 the 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. it's giving "flip it, reverse it" β¨- list
valuesβ list of lists of values
- list
then there are two main procedures: integral2D and singleExperiment. the headliners. the main characters. π
::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
and 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: this is a Required keyword. not optional. do not skip it. π
-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
and literally any other options? you can use them via the getOption procedure in the body. the framework said "bring your own". π
π§ TIPS π§
the procedure singleExperiment works by constructing a temporary procedure that does the actual work. it loops for the given number of trials. slay.
and because it constructs a temporary procedure, local variables defined at the start continue to exist in the loop. so yes bestie, your setup variables survive. π«Ά
β KEYWORDS β
math, montecarlo simulation, stochastic modelling
π CATEGORY π
Mathematics
π COPYRIGHT π
Copyright (c) 2008 Arjen Markus <arjenmarkus@users.sourceforge.net>
and that's the whole page, bestie!! π this one's from tcllib, version 0.2, and it's the simulation::montecarlo(3tcl) manual page. go forth and run a hundred trials, you absolute icon. β¨ππ¦
π¦ where this page comes from
- debian trixie: tcllib 2.0+dfsg-4 Β· Tcl
- ubuntu questing: tcllib 2.0+dfsg-4
- ubuntu resolute: tcllib 2.0+dfsg-5 Β· Tcl
- ubuntu stonking: tcllib 2.0+dfsg-6 Β· Tcl
- 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