Simulation Modeling Functions

Overview

Though most of your modeling can be done directly in raw Julia, some of the most important features in a Monte-Carlo simulation package involve analyzing and applying correlation in models. MCHammer's correlation approach is based on:

Ronald L. Iman & W. J. Conover (1982) A distribution-free approach to inducing rank correlation among input variables, Communications in Statistics - Simulation and Computation

The simulation and correlation functions are designed to quickly obtain risk and decision analysis metrics, such as moments, percentiles, and risk over time.

Risk Events and Conditional Distributions

Risk Events allow you to model a joint distribution accounting for the probability of an event occurring and the conditional impact when it does. The process simulates the Probability x Impact formula correctly.

MCHammer.RiskEventFunction
RiskEvent(Prob, Distribution, Trials; seed=0)

Risk Events are defined as conditional distributions that will inflate the 0.

The RiskEvent() allows you to conditionally sample any distribution for as many trials as defined. Prob is the conditional probability of sampling the impact Distribution Distribution is any univariate distribution from Distributions.jl Trials is the number of total iterations. seed allows you to set a seed for the RiskEvent. Left blank or set to 0, the seed is set to random.

source
# Simulate a conditional risk event with a 30% chance of occurring and an impact
# that is distributed according to a standard Normal. 10 trials are generated and
# about 3 should have non-zero outcomes.


RiskEvent(0.3, Normal(0,1), 10)
10-element Vector{Float64}:
  1.3000376425686369
  0.0
 -0.6127005369059596
 -0.0
 -0.0
 -0.0
 -0.680129214686657
 -0.7101094583211688
 -0.0
 -0.9198488895790397

Correlation and Covariance

Correlation and covariance functions help you understand relationships between different simulation inputs. In practical modeling:

  • Use Cases: Assess which variables move together (positive/negative correlation) to reduce or amplify overall risk.
  • Applications: Finance (portfolio risk), engineering (linked component failures), supply chain planning (demand and lead-time co-fluctuation).
MCHammer.cormatFunction
  cormat(ArrayName, RankOrder=1)

Cormat calculates a symetric correlation matrix using both PPMC and Rank Order. Rank Order is default because this is what it used in the Iman-Conover method for correlating of simulated variables.

RankOrder = 1 calculates the Spearman rank order correlation used in MCHammer (this argument is optional and defaults to Spearman)

RankOrder = 0 calculates the Pearson Product Moment Correlation

source
using Distributions, MCHammer, Random #hide
rng = MersenneTwister(1)
Random.seed!(1)
test = rand(Normal(), 1000, 5)
cormat(test)

# output

5×5 Matrix{Float64}:
  1.0         0.0262401  -0.0119314    0.0386272   -0.0755551
  0.0262401   1.0        -0.0118889    0.0137545   -0.0305986
 -0.0119314  -0.0118889   1.0         -0.00762943  -0.0264234
  0.0386272   0.0137545  -0.00762943   1.0         -0.00137442
 -0.0755551  -0.0305986  -0.0264234   -0.00137442   1.0
using Distributions, MCHammer, Random #hide
rng = MersenneTwister(1)
Random.seed!(1)
test = rand(Normal(), 1000, 5)
covmat(test)

# output
5×5 Matrix{Any}:
  1.01123      0.0293154    0.00167811   0.0401224   -0.0820211
  0.0293154    1.11648     -0.00357405   0.0142628   -0.032553
  0.00167811  -0.00357405   1.05398     -0.0033127   -0.0285162
  0.0401224    0.0142628   -0.0033127    0.872718    -0.00241684
 -0.0820211   -0.032553    -0.0285162   -0.00241684   1.01745

Correlating Simulation Variables

corvar adjusts your simulated datasets to match a desired correlation matrix. In modeling:

  • Use Cases: Forces scenario data to reflect real-world or hypothesized correlations, ensuring valid joint distributions.
  • Applications: Stress tests in finance, correlated risk analysis (e.g., hurricane + flood), and multivariate forecasting.
Missing docstring.

Missing docstring for corvar. Check Documenter's build log for details.

using Distributions, MCHammer, Random #hide
rng = MersenneTwister(1)
Random.seed!(1)
n_trials = 1000
sample_data = [rand(rng, LogNormal(0, 0.5), n_trials) rand(rng, Normal(3,2), n_trials) rand(rng, Gamma(1, 0.5), n_trials) rand(rng, LogNormal(0, 0.5), n_trials) rand(rng, Normal(3,2), n_trials) rand(rng, Gamma(1, 0.5), n_trials)]

test_cmatrix = [1 0 0 0 0 0; 0 1 0 0 0 0; 0 0 1 0 0 0;0 0 0 1 0.75 -0.7; 0 0 0 0.75 1 -0.95; 0 0 0 -0.7 -0.95 1 ]

cormat(corvar(sample_data, n_trials, test_cmatrix))

# output

6×6 Matrix{Float64}:
  1.0         0.0262401  -0.0119314    0.0386272   -0.0295832   0.034389
  0.0262401   1.0        -0.0118889    0.0137545   -0.0160956   0.0147439
 -0.0119314  -0.0118889   1.0         -0.00762943  -0.0242712   0.0420867
  0.0386272   0.0137545  -0.00762943   1.0          0.713931   -0.659495
 -0.0295832  -0.0160956  -0.0242712    0.713931     1.0        -0.939242
  0.034389    0.0147439   0.0420867   -0.659495    -0.939242    1.0

Analyzing Simulation Results

Result analysis functions like GetCertainty and fractiles help interpret simulation outputs:

  • Use Cases: Identify probability of crossing thresholds (e.g., "chance of negative profit"), or measure percentile-based risk.
  • Applications: Cost-risk analysis, reliability engineering, or performance metrics (min, max, median) in uncertain environments.
MCHammer.GetCertaintyFunction
  GetCertainty(ArrayName, x, AboveBelow=0)

This function returns the percentage of trials Above (1) or Below(0) a target value of x.

source
using Random, MCHammer, Distributions #hide
rng = MersenneTwister(1)
Random.seed!(1)
test = rand(Normal(), 1000)

GetCertainty(test, 0, 1)

# output

0.528
MCHammer.fractilesFunction
  fractiles(ArrayName, Increment=0.1)

The fractiles function calculates percentiles at equal increments. The default optional argument for Increments is 0.1 for deciles but can be set to anything such as 0.05 for quintiles or 0.01 for percentiles.

source
using Random, MCHammer, Distributions #hide
rng = MersenneTwister(1)
Random.seed!(1)
test = rand(Normal(), 1000)

fractiles(test)

# output

11×2 Matrix{Any}:
 "P0.0"    -2.95049
 "P10.0"   -1.30285
 "P20.0"   -0.834923
 "P30.0"   -0.472307
 "P40.0"   -0.227086
 "P50.0"    0.0882202
 "P60.0"    0.319301
 "P70.0"    0.579717
 "P80.0"    0.873708
 "P90.0"    1.29457
 "P100.0"   2.97612

Misc Functions

These utility functions provide convenience in data cleanup and quick calculation:

  • Use Cases: Scripting and reporting automation where formatting or command line interactions are needed.
  • Applications: Rounding values for dashboards, truncating decimal places in sensitivity outputs, or running shell commands mid-simulation.
MCHammer.cmdFunction
  cmd(x)

Shell /Dos Command wrapper to run batch and shell commands in script. This is used to process SQL from the command line or perform system level operation in a script using a command prompt.

source
MCHammer.truncate_digitFunction
function  truncate_digit(num, digits=2)

Truncation algorithim to remove decimals (ported by anonymous author from Maple) e.g.

  0.066 = 0.06
  0.063 = 0.06
source
using Random, MCHammer #hide
Result_1 = truncate_digit(0.667)
Result_2 = truncate_digit(0.661)
Result_1 == Result_2

# output

true

Sources & References

  • Eric Torkia, Decision Superhero Vol. 2, chapter 5 : Predicting 1000 futures, Technics Publishing 2025
  • Available on Amazon : https://a.co/d/4YlJFzY . Volumes 2 and 3 to be released in Spring and Fall 2025.