Macro decoding
Hi,
I need to translate / understand the following macro in simple english step by step of what it does and how it works. Can you help please:
Function VaRCalculate(confidence, horizon, method, rf) Dim nrow As Integer, i As Integer Dim currentCell, nextCell Dim logreturn() As Double, vol As Double, simreturn(1 To 10000) As Double nrow = Range(Range("G26"), Range("G26").End(xlDown)).Rows.Count ReDim logreturn(1 To nrow - 1) As Double Set currentCell = Range("G26") For i = 1 To (nrow - 1) Set nextCell = currentCell.Offset(1, 0) logreturn(i) = Application.Ln(nextCell.Value) - Application.Ln(currentCell.Value) Set currentCell = nextCell Next i vol = StdDev(nrow - 1, logreturn) If method = 1 Then VaRCalculate = vol * Application.NormInv(confidence, 0, 1) * Sqr(horizon) ElseIf method = 2 Then For i = 1 To 10000 simreturn(i) = Exp((rf - 0.5 * vol * vol * 250) / 250 + vol * Sqr(250) * Sqr(1 / 250) * Application.NormInv(Rnd(), 0, 1)) - 1 Next i VaRCalculate = -Sqr(horizon) * Application.Percentile(simreturn, 1 - confidence) ElseIf method = 3 Then VaRCalculate = -Sqr(horizon) * Application.Percentile(logreturn, 1 - confidence) End If VaRCalculate = currentCell.Value * VaRCalculate End Function '*********************************************************************** '* Mean * '*********************************************************************** Function Mean(k As Long, Arr() As Double) Dim Sum As Double Dim i As Integer Sum = 0 For i = 1 To k Sum = Sum + Arr(i) Next i Mean = Sum / k End Function '************************************************************************ '* Standard Deviation * '************************************************************************ Function StdDev(k As Long, Arr() As Double) Dim i As Integer Dim avg As Double, SumSq As Double avg = Mean(k, Arr) For i = 1 To k SumSq = SumSq + (Arr(i) - avg) ^ 2 Next i StdDev = Sqr(SumSq / (k - 1)) End Function
Hi,This function seems to be
This function seems to be designed to compute financial or statistical data into a range of cells from the cell G26.
Between (), it is not conventional to put a range address into this type of function ; it should have been passed as a parameter but I suppose the author was near a tactical case...
I cannot give you details but I've commented the code as is :
Tell me if this was you was looking for...