montecarlo

montecarlo, keyword

montecarlo opens a block repeated a fixed number of iterations and collects sample aggregations over those iterations, either as scalars or vectors.

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

The ranvar() accumulator also supports vector targets:

table T = extend.range(3)

montecarlo 100 with
  sample T.R = ranvar(random.normal(T.N, 0.1))

show table "Empirical ranvars" with
  T.N
  mean(T.R) as "Mean"
  dispersion(T.R) as "Dispersion"

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.

When sampling a ranvar, out-of-bounds draws are discarded and the resulting distribution is normalized over accepted samples only.

If a sampled value is invalid (for example, non-finite or out of bounds), the simulation fails at runtime with an error that names the sampler.

User Contributed Notes
0 notes + add a note