Building your first model
Installing MCHammer
Install the package as usual using Pkg.
using Pkg
Pkg.("MCHammer")
If you need to install direct, we recommend using ']' to go in the native Pkg manager.
(v1.1) pkg> add https://github.com/etorkia/MCHammer.jl
Loading MCHammer
To load the MCHammer package
using MCHammer
Getting your environment setup for modeling
In order to build your first model, you will need to get a few more packages installed:
Distributions.jl : To build a simulation, you need distributions as inputs. Julia offers univariate and multivariate distributions covering most needs.
StatsBase.jl and Statistics.jl : These packages provide all the functions to analyze results and build models.
To load the support packages:
julia> using Distributions, Statistics, StatsBase, DataFrames
Building a simple example
EVERY MONTE CARLO MODEL HAS 3 COMPONENTS
- Inputs: Ranges or Single Values
- A Model: Set of mathematical relationships f(x)
- Outputs: The variable(s) of interest you want to analyze
Main Distributions for most modeling situations
Though the most used distributions are cite below, Julia's Distributions package has an impressive array of options. Please check out the complete library of distributions at Distributions.jl
- Normal()
- LogNormal()
- Triangular()
- Uniform()
- Beta()
- Exponential()
- Gamma()
- Weibull()
- Poisson()
- Binomial()
- Bernoulli()
In order to define a simulated input you need to use the rand function. By assigning a variable name, you can generate any simulated vector you want.
using Distributions, Random
Random.seed!(1)
input_variable = rand(Normal(0,1),100)
100-element Vector{Float64}:
0.06193274031408013
0.2784058141640002
-0.5958244153640522
0.04665938957338174
1.0857940215432762
-1.5765649225859841
0.1759399913010747
0.8653808054093252
-2.790281005549307
-1.8920155582259128
⋮
0.747446113596017
-0.5200214906974596
-0.25155469243873996
1.219317449444866
1.0026816589160366
0.972024394360624
1.546409924955377
-0.5841980481085709
0.46774878539798725
Creating a simple model
A model is either a visual or mathematical representation of a situation or system. The easiest example of a model is
PROFIT = REVENUE - EXPENSES
Let's create a simple simulation model with 1000 trials with the following inputs:
Setup environment and inputs
n_trials = 1000
Revenue = rand(TriangularDist(2500000,4000000,3000000), n_trials)
Expenses = rand(TriangularDist(1400000,3000000,2000000), n_trials)
1000-element Vector{Float64}:
2.1276849954543347e6
1.8081873781452307e6
1.544879902357442e6
1.8880541225801776e6
2.5447218679510015e6
2.162878322589141e6
2.450585401245652e6
2.7665033929929947e6
2.0637354857680933e6
2.3840473601053483e6
⋮
2.1254683577667978e6
1.8042545395649583e6
1.8132617966068545e6
1.9613235513769118e6
1.8308271321740407e6
2.342718789527916e6
2.1114726078079096e6
2.636000060944828e6
2.0433610096553492e6
Define a Model and Outputs
# The Model
Profit = Revenue - Expenses
#Trial Results : the Profit vector (OUTPUT)
Profit
1000-element Vector{Float64}:
1.3059645011598323e6
1.54515492309168e6
1.7176910748612264e6
634634.3276538644
361826.2604960487
966992.6143540088
1.0469871383461733e6
99239.68692632765
914122.4699177747
616543.220599601
⋮
765170.8771777772
1.124483006433995e6
1.6927099550849595e6
925371.6289863433
1.2103149177766917e6
697792.9995724442
1.3143058833978418e6
726636.9191863816
1.1741165929685102e6
Analyzing the results in Julia
# `fractiles()` allows you to get the percentiles at various increments.
fractiles(Profit)
11×2 Matrix{Any}:
"P0.0" -3.30304e5
"P10.0" 4.72844e5
"P20.0" 6.4124e5
"P30.0" 7.73507e5
"P40.0" 9.03596e5
"P50.0" 1.02306e6
"P60.0" 1.14913e6
"P70.0" 1.28132e6
"P80.0" 1.41412e6
"P90.0" 1.63562e6
"P100.0" 2.29578e6
density_chrt(Profit)
Sensitivity Analysis
First we need to create a sensitivity table with hcat() using both the input and output vectors.
#Construct the sensitivity input table by consolidating all the relevant inputs and outputs.
s_table = DataFrame(Profit = Profit, Revenue = Revenue, Expenses = Expenses)
#To produce a sensitivity tornado chart, we need to select the output against
#which the inputs are measured for effect.
sensitivity_chrt(s_table, 1, 3)