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
input_variable = rand(Normal(0,1),100)
100-element Array{Float64,1}:
0.7486097437558422
0.3553588695406532
0.09177607042068095
-0.9880222647627901
0.1323935614468582
-0.1866528314762285
0.3165799400611648
-1.044385240934001
-0.536780380907565
0.7016225625210913
⋮
-0.09965994463749257
-1.4159797658810227
0.6517148359669904
-2.897634203082797
-1.6044319897589026
-1.3349042702791458
0.4913398106510818
1.2546007974382325
-1.394671134509341
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
using Distributions, StatsBase, DataFrames, MCHammer
n_trials = 1000
Revenue = rand(TriangularDist(2500000,4000000,3000000), n_trials)
Expenses = rand(TriangularDist(1400000,3000000,2000000), n_trials)
1000-element Array{Float64,1}:
2.5924848459853358e6
1.6345740989024893e6
2.183127811496687e6
1.6924626820727405e6
1.5544189752713381e6
2.196939073858935e6
2.3407355021325457e6
2.2473124427373107e6
2.378187164370492e6
2.15136362693495e6
⋮
2.0095656626148196e6
1.887693044844761e6
2.209005549554387e6
2.6735879312010715e6
2.0002887015474397e6
1.5682691466134065e6
1.586827893243728e6
1.9631723931472646e6
2.2486246670551766e6
Define a Model and Outputs
# The Model
Profit = Revenue - Expenses
#Trial Results : the Profit vector (OUTPUT)
Profit
1000-element Array{Float64,1}:
1.008821500934768e6
1.4692795073693616e6
1.2879233584915022e6
1.599409134142953e6
2.1777070068928422e6
912480.969472941
703322.2545680455
747338.2508386448
1.0970101990636662e6
1.140561600803073e6
⋮
1.2142737928832169e6
871780.9522913583
872443.1833644402
319899.49430688703
973650.8365729577
1.5431230160808973e6
1.7084449345202905e6
1.8525632886503306e6
1.1170191594372606e6
Analyzing the results in Julia
# `fractiles()` allows you to get the percentiles at various increments.
fractiles(Profit)
11×2 Array{Any,2}:
"P0.0" -3.30483e5
"P10.0" 4.2265e5
"P20.0" 6.24649e5
"P30.0" 7.8756e5
"P40.0" 8.97666e5
"P50.0" 1.0224e6
"P60.0" 1.14013e6
"P70.0" 1.25247e6
"P80.0" 1.39248e6
"P90.0" 1.60689e6
"P100.0" 2.20975e6
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 = hcat(Profit, Revenue, Expenses)
#We then need to convert to a DataFrame and add names
s_table = DataFrame(s_table)
names!(s_table, [:Profit, :Revenue, :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)