# Correlating Variables in Your Model

To get started, we are going to recap what we did in your first model and build a correlated version of the same model and compare.

## Building a Simple Uncorrelated Model

```
using Distributions, StatsBase, DataFrames, MCHammer
n_trials = 10000
Revenue = rand(TriangularDist(2500000,4000000,3000000), n_trials)
Expenses = rand(TriangularDist(1400000,3000000,2000000), n_trials)
# The Model
Profit = Revenue - Expenses
#Trial Results : the Profit vector (OUTPUT)
Profit
# Trials or Results Table (OUTPUT)
Trials = hcat(Profit, Revenue, Expenses)
Trials = DataFrame(Trials)
names!(Trials, [:Profit, :Revenue, :Expenses])
cormat(Trials)
```

3×3 Array{Float64,2}: 1.0 0.669217 -0.716073 0.669217 1.0 0.000726586 -0.716073 0.000726586 1.0

## Applying correlation to your simulation

Using the `corvar()`

function, we are going to correlate the Revenue and Expenses at -0.8 and generate the results tables for both the correlated and uncorrelated versions.

```
#Apply correlation to random samples
Rev_Exp_Cor = 0.8
cor_matrix = [1 Rev_Exp_Cor; Rev_Exp_Cor 1]
#Validate input correlation. You can also use cormat() to define the correlation
#matrix from historical data.
cor_matrix
```

2×2 Array{Float64,2}: 1.0 0.8 0.8 1.0

It is very important to join Trial into an array before applying correlation. Furthermore, this step is necessary in order to produce a `sensitivity_chrt()`

```
c_table = [Revenue Expenses]
C_Trials = corvar(c_table, n_trials, cor_matrix)
#Correlated Model(2) - Create Correlated Results Array
C_Profit = C_Trials[1] - C_Trials[2]
C_Trials = [C_Profit, C_Trials[1], C_Trials[2]]
C_Trials = DataFrame(C_Trials)
names!(C_Trials, [:C_Profit, :C_Revenue, :C_Expenses])
cormat(C_Trials)
```

3×3 Array{Float64,2}: 1.0 0.225132 -0.400685 0.225132 1.0 0.778227 -0.400685 0.778227 1.0

## Analyze the impact of correlation on your output

### Input Correlation between Revenue and Expenses

Input Correlation:

`cor(Revenue,Expenses)`

-0.0010405497680156886

Input Correlation for the Correlated Model:

`cor(C_Trials[2],C_Trials[3])`

0.7889819301131576

Make sure to put a line in your project that lists all the outputs you can query with the charting and stats functions.

`println("Model Outputs: Trials, C_Trials, Profit, C_Profit")`

## Correlated vs. Uncorrelated results in Julia

Let us compare the percentiles of an uncorrelated model vs. a correlated one.

### Uncorrelated Results

`density_chrt(Profit)`

Probability of Making 1m or less (uncorrelated) :

`GetCertainty(Profit, 1000000, 0)`

0.4747

`fractiles()`

allows you to get the percentiles at various increments.

`fractiles(Profit)`

11×2 Array{Any,2}: "P0.0" -337427.0 "P10.0" 433308.0 "P20.0" 637083.0 "P30.0" 790785.0 "P40.0" 916016.0 "P50.0" 1.03172e6 "P60.0" 1.14253e6 "P70.0" 1.26634e6 "P80.0" 1.41062e6 "P90.0" 1.6273e6 "P100.0" 2.43465e6

`sensitivity_chrt(Trials,1)`

### Correlated Results

`density_chrt(C_Profit)`

Probability of Making 1m or less (correlated) :

`GetCertainty(C_Profit, 1000000, 0)`

0.4324

`fractiles(C_Profit)`

11×2 Array{Any,2}: "P0.0" 217536.0 "P10.0" 761763.0 "P20.0" 857252.0 "P30.0" 926631.0 "P40.0" 983526.0 "P50.0" 1.03499e6 "P60.0" 1.08516e6 "P70.0" 1.13697e6 "P80.0" 1.20116e6 "P90.0" 1.2912e6 "P100.0" 1.867e6

`sensitivity_chrt(C_Trials,1)`

## A quick analysis of the results

- Accounting for correlation meant a 5% (42.5% vs. 47.7%) reduction in probability of not making our goals.

- The Worse Case goes from -290k to 230k, a 225% difference

- The critical driver in both cases is expenses.