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)
Sim. Values -6×10⁶ -5×10⁶ -4×10⁶ -3×10⁶ -2×10⁶ -1×10⁶ 0 1×10⁶ 2×10⁶ 3×10⁶ 4×10⁶ 5×10⁶ 6×10⁶ 7×10⁶ 8×10⁶ -5.0×10⁶ -4.8×10⁶ -4.6×10⁶ -4.4×10⁶ -4.2×10⁶ -4.0×10⁶ -3.8×10⁶ -3.6×10⁶ -3.4×10⁶ -3.2×10⁶ -3.0×10⁶ -2.8×10⁶ -2.6×10⁶ -2.4×10⁶ -2.2×10⁶ -2.0×10⁶ -1.8×10⁶ -1.6×10⁶ -1.4×10⁶ -1.2×10⁶ -1.0×10⁶ -8.0×10⁵ -6.0×10⁵ -4.0×10⁵ -2.0×10⁵ 0 2.0×10⁵ 4.0×10⁵ 6.0×10⁵ 8.0×10⁵ 1.0×10⁶ 1.2×10⁶ 1.4×10⁶ 1.6×10⁶ 1.8×10⁶ 2.0×10⁶ 2.2×10⁶ 2.4×10⁶ 2.6×10⁶ 2.8×10⁶ 3.0×10⁶ 3.2×10⁶ 3.4×10⁶ 3.6×10⁶ 3.8×10⁶ 4.0×10⁶ 4.2×10⁶ 4.4×10⁶ 4.6×10⁶ 4.8×10⁶ 5.0×10⁶ 5.2×10⁶ 5.4×10⁶ 5.6×10⁶ 5.8×10⁶ 6.0×10⁶ 6.2×10⁶ 6.4×10⁶ 6.6×10⁶ 6.8×10⁶ 7.0×10⁶ -5×10⁶ 0 5×10⁶ 1×10⁷ -5.0×10⁶ -4.5×10⁶ -4.0×10⁶ -3.5×10⁶ -3.0×10⁶ -2.5×10⁶ -2.0×10⁶ -1.5×10⁶ -1.0×10⁶ -5.0×10⁵ 0 5.0×10⁵ 1.0×10⁶ 1.5×10⁶ 2.0×10⁶ 2.5×10⁶ 3.0×10⁶ 3.5×10⁶ 4.0×10⁶ 4.5×10⁶ 5.0×10⁶ 5.5×10⁶ 6.0×10⁶ 6.5×10⁶ 7.0×10⁶ h,j,k,l,arrows,drag to pan i,o,+,-,scroll,shift-drag to zoom r,dbl-click to reset c for coordinates ? for help ? -1.25×10⁻⁶ -1.00×10⁻⁶ -7.50×10⁻⁷ -5.00×10⁻⁷ -2.50×10⁻⁷ 0 2.50×10⁻⁷ 5.00×10⁻⁷ 7.50×10⁻⁷ 1.00×10⁻⁶ 1.25×10⁻⁶ 1.50×10⁻⁶ 1.75×10⁻⁶ 2.00×10⁻⁶ 2.25×10⁻⁶ -1.00×10⁻⁶ -9.50×10⁻⁷ -9.00×10⁻⁷ -8.50×10⁻⁷ -8.00×10⁻⁷ -7.50×10⁻⁷ -7.00×10⁻⁷ -6.50×10⁻⁷ -6.00×10⁻⁷ -5.50×10⁻⁷ -5.00×10⁻⁷ -4.50×10⁻⁷ -4.00×10⁻⁷ -3.50×10⁻⁷ -3.00×10⁻⁷ -2.50×10⁻⁷ -2.00×10⁻⁷ -1.50×10⁻⁷ -1.00×10⁻⁷ -5.00×10⁻⁸ 0 5.00×10⁻⁸ 1.00×10⁻⁷ 1.50×10⁻⁷ 2.00×10⁻⁷ 2.50×10⁻⁷ 3.00×10⁻⁷ 3.50×10⁻⁷ 4.00×10⁻⁷ 4.50×10⁻⁷ 5.00×10⁻⁷ 5.50×10⁻⁷ 6.00×10⁻⁷ 6.50×10⁻⁷ 7.00×10⁻⁷ 7.50×10⁻⁷ 8.00×10⁻⁷ 8.50×10⁻⁷ 9.00×10⁻⁷ 9.50×10⁻⁷ 1.00×10⁻⁶ 1.05×10⁻⁶ 1.10×10⁻⁶ 1.15×10⁻⁶ 1.20×10⁻⁶ 1.25×10⁻⁶ 1.30×10⁻⁶ 1.35×10⁻⁶ 1.40×10⁻⁶ 1.45×10⁻⁶ 1.50×10⁻⁶ 1.55×10⁻⁶ 1.60×10⁻⁶ 1.65×10⁻⁶ 1.70×10⁻⁶ 1.75×10⁻⁶ 1.80×10⁻⁶ 1.85×10⁻⁶ 1.90×10⁻⁶ 1.95×10⁻⁶ 2.00×10⁻⁶ -1×10⁻⁶ 0 1×10⁻⁶ 2×10⁻⁶ -1.0×10⁻⁶ -9.0×10⁻⁷ -8.0×10⁻⁷ -7.0×10⁻⁷ -6.0×10⁻⁷ -5.0×10⁻⁷ -4.0×10⁻⁷ -3.0×10⁻⁷ -2.0×10⁻⁷ -1.0×10⁻⁷ 0 1.0×10⁻⁷ 2.0×10⁻⁷ 3.0×10⁻⁷ 4.0×10⁻⁷ 5.0×10⁻⁷ 6.0×10⁻⁷ 7.0×10⁻⁷ 8.0×10⁻⁷ 9.0×10⁻⁷ 1.0×10⁻⁶ 1.1×10⁻⁶ 1.2×10⁻⁶ 1.3×10⁻⁶ 1.4×10⁻⁶ 1.5×10⁻⁶ 1.6×10⁻⁶ 1.7×10⁻⁶ 1.8×10⁻⁶ 1.9×10⁻⁶ 2.0×10⁻⁶ Frequency

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)
% Contribution to Variance -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 -3.0 -2.9 -2.8 -2.7 -2.6 -2.5 -2.4 -2.3 -2.2 -2.1 -2.0 -1.9 -1.8 -1.7 -1.6 -1.5 -1.4 -1.3 -1.2 -1.1 -1.0 -0.9 -0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.0 -4 -2 0 2 4 -3.0 -2.8 -2.6 -2.4 -2.2 -2.0 -1.8 -1.6 -1.4 -1.2 -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 Positive Negative impact h,j,k,l,arrows,drag to pan i,o,+,-,scroll,shift-drag to zoom r,dbl-click to reset c for coordinates ? for help ? Revenue Expenses Input Variables with Biggest Impact