Beruflich Dokumente
Kultur Dokumente
7. Sort the portfolio deltas from lowest to highest using the Quicksort
algorithm
8. Calculate the percentile required (1 - confidence level)
9. VaR is given by portfolio delta(i) where all the portfolio deltas from
N simulations are in a vector, and i = percentile * N
10. VaRs can be extracted as an array for difference confidence
intervals if required
In the following simplified example, we assume a portfolio of assets
with prices that follow the Black Scholes model assumptions.
How To Code a Multivariate Value at Risk (VaR) VBA Monte Carlo Simulation
How to Code a Multivariate Value at Risk (VaR) VBA Monte Carlo Simulation
'------------ ASSUME ALL VECTORS HAVE SAME NUMBER OF
CELLS AS # OF ASSET PRICES, DEFINE LIMITS OF ALL
VECTORS THEN REDECLARE -----Vector_Limits = S_t_range.Rows.Count * S_t_range.Columns.Count
ReDim Preserve S(1 To Vector_Limits)
ReDim Preserve k(1 To Vector_Limits)
ReDim Preserve r(1 To Vector_Limits)
ReDim Preserve q(1 To Vector_Limits)
ReDim Preserve sigma(1 To Vector_Limits)
ReDim Preserve t(1 To Vector_Limits)
ReDim Preserve a(1 To Vector_Limits)
Dim N As Integer
Dim Sum_S0 As Double, Delta() As Double, Percentile As Double
RandVars = WorksheetFunction.Transpose(RandVars)
epsilon = Application.WorksheetFunction.MMult(L_Lower,
RandVars)
For i = 1 To Vector_Limits
S(i) = S_t_range(i)
SumS0 = SumS0 + S(i)
q(i) = q_range(i)
k(i) = K_range(i)
r(i) = r_scalar
t(i) = t_scalar
sigma(i) = sigma_range(i, j)
a(i) = r(i) - q(i) - 0.5 * sigma(i)
S(i) = S(i) * Exp(a(i) + sigma(i) * Sqr(t(i)) * epsilon(i, 1))
Sum_S = Sum_S + S(i)
Next i
Delta(j) = Sum_S - Sum_S0
L_Lower = cholesky(Matrix_range)
Next j
'Sort the Deltas and select the Delta corresponding to the percentile
Delta = quicksort(Delta)
Percentile = 1 - Confidence
Val_at_Risk = Delta(Round(Percentile * Runs, 0))
VaR_Sim = Val_at_Risk
Application.Calculation = xlCalculationAutomatic
How To Code a Multivariate Value at Risk (VaR) VBA Monte Carlo Simulation
How to Code a Multivariate Value at Risk (VaR) VBA Monte Carlo Simulation
End Function
Application.Volatile (True)
Application.ScreenUpdating = True
Next Steps
Dim i As Long
Dim epsilon() As Double
ReDim Preserve epsilon(1 To Vector_Limit)
For i = 1 To Vector_Limit
epsilon(i) = WorksheetFunction.NormSInv(Rnd)
Next i
Fill_epsilon = epsilon
End Function
How To Code a Multivariate Value at Risk (VaR) VBA Monte Carlo Simulation
How to Code a Multivariate Value at Risk (VaR) VBA Monte Carlo Simulation
Carlo Simulation
How to build a Black Scholes VBA Option Pricer for FX
Options
How to build a Black Scholes VBA Option Pricer for Equity
Options
How To Code a Multivariate Value at Risk (VaR) VBA Monte Carlo Simulation
How to Code a Multivariate Value at Risk (VaR) VBA Monte Carlo Simulation
Portfolio Reporting VBA Tool: Inserting Data into SQL/Access
Databases from Excel
Portfolio Reporting VBA Tool: Connecting Excel with SQL &
Access Databases
How To Design Classes For an Option Pricer in VBA: UML
Concepts
How To Design Classes for Object Orientated VBA
Programming
How To Code a Multivariate Value at Risk (VaR) VBA Monte Carlo Simulation