# Julia version of Montecarlo notebook¶

The following is a Julia version of the core parts of our MC calculations, for those of you interested in comparing Python with Julia. Julia is still lagging Python in terms of use but is catching up fast, especially for quantitative applications, in part because it is built to be faster.

In [74]:
# Parameters

Avalue=[0,0.9,1,1.1] # possible growth rates/factors
probabilities=[0.02,0.1,0.78,0.1] # associated probabilities

cost=500000 #investment cost at date 0
cf1=50000 # first cash flow
life=20 # maximum number of cash flows
rate=10/100 # discount rate
n=100000 # number of paths

# run import Pkg; Pkg.add("Distributions") if package not installed yet

using Random, Distributions

Ashocks=[rand(Categorical([0.02,0.1,0.78,0.1]),life-1) for i in 1:n]

cashflows=cf1*ones(n,life+1)
cashflows[1:n,1]=-cost*ones(1,n) # Julia arrays start at 1!!!!!!

for i in 1:n
for j in 3:life+1
cashflows[i,j]=cashflows[i,j-1]*Avalue[Ashocks[i][j-2]]
end
end

Ecf= mean(cashflows,dims=1)

using NPFinancial # same as Python's Numpy_Financial
using Printf
using StatsPlots

@printf("The project's IRR is %.2f%%. ", 100*irr(vec(Ecf)))

irrpath=[irr(vec(cashflows[i,1:life+1])) for i in 1:n]

histogram(100*irrpath,bins=100,legend=:topleft,labels=["IRR by history, in percent"])

The project's IRR is 5.85%.
Out[74]: