montecarlo
montecarlo, keyword
montecarlo opens a block repeated a fixed number of iterations and collects
sample aggregations over those iterations.
Examples
iterations = 10
montecarlo iterations with
x = random.uniform(0, 1)
sample avgX = avg(x)
show table "Monte Carlo" with
avgX as "Avg"
This produces the following table:
| Avg |
|---|
| 0.4521479 |
Full-table lookups are supported inside montecarlo blocks, and can be sampled per line.
table enum Key = "a", "b"
table U[Key] = with
[| as Key, as A |]
[| enum<<Key>>("a"), 10 |]
[| enum<<Key>>("b"), 20 |]
table T = with
[| as K |]
[| enum<<Key>>("a") |]
[| enum<<Key>>("b") |]
[| enum<<Key>>("a") |]
montecarlo 1000 with
r = random.uniform(0.8, 1.2)
T.A = U.A[T.K] * r
sample T.AvgA = avg(T.A)
show table "Average A" with
T.K
T.AvgA
This produces a table similar to:
| K | AvgA |
|---|---|
| a | 9.945009 |
| b | 19.89002 |
| a | 9.945009 |
Remarks
The iteration count must be a positive integer. It can be any scalar number
value, not necessarily a const.
Lookups inside montecarlo follow the usual rules: the lookup key must be an
ordinal dimension or an enum, not a raw text value.