Professional Documents
Culture Documents
Recursion
Giovanni Zucchinetti
giovanni.zucchinetti@gitec.ch
HEC Lausanne
Computanional Tools for Actuaries (CTA)
1
Agenda
1. Arrays
2. Recursive function call
2
Arrays in VBA
HEC Lausanne
Computanional Tools for Actuaries (CTA)
Using Arrays : when and why
CTA - G. Zucchinetti 4
Arrays - Definition
• To define an array, we can use the Dim, Private, or Public
variable declarations.
CTA - G. Zucchinetti 5
Array - Values
To insert values into this array, we can use the entire array variable and
the Array function.
The Array function allows you to list values for each array element.
For example to insert 10 values into the data array, we could type the
following.
data = Array(12.3, 13.4, 16.5, 13.8, 7, 2.9, 24.2, 5.5, 8, 9.1)
CTA - G. Zucchinetti 6
Array - Values
To set the value of a specific element of the array, we use an index.
For example, if we want to change the third value in the above data
array from 16.5 to 10.5, we type.
- data(3) = 10.5
For example, if we want to set each element in the data array as its
index number we type.
For i = 1 to 10
data(i) = I
Next i
CTA - G. Zucchinetti 7
Size : Multi-dimensional Arrays
For a one-dimensional array, a single number is all that is necessary to
specify the size.
CTA - G. Zucchinetti 8
Multi-dimensional (cont’d)
To insert values into a multi-dimensional array or to search for a value,
use nested For, Next loops with different counter variables.
For example, to set the value of each element in the above two-
dimensional array equal to the product of its index numbers.
For i = 1 to 5
For j = 1 to 10
data(i, j) = i*j
Next j
Next i
CTA - G. Zucchinetti 9
Indexing
The default initial index value of arrays in VBA is 0.
However, to change the initial index value of all arrays in your module
to 1, simply type Option Base 1 at the top of the module.
If you want to keep the default initial index as 0 but have a specific
array which you want to index starting at 1, you can specify the starting
index value in the array declaration as.
- (1 to arraysize)
CTA - G. Zucchinetti 10
Indexing
To compare, in the example below, Option Base 1 is used.
Option Base 1
Dim data(10) As Double, results(12) As Double
Here both the data and results arrays will have an initial index of 1
(as will any other array which is later defined in this module).
Below we do not specify the Option Base setting but instead change
the index of one of the arrays.
Dim data(1 to 10) As Double, results(12) As Double
Now the data array will begin with index of 1, but the results array will
begin with an index of 0.
CTA - G. Zucchinetti 11
Indexing
There may be situations in which specific arrays need different indexing
bounds.
In this case, you can start the index at any value, just be aware of the
size of your array.
- size = upper index bound – lower index bound + 1
- Dim results(2 to 13) As Double
CTA - G. Zucchinetti 12
Dynamic Arrays
If you are not sure what size an array should be, or will be
depending on the user or other dynamic programming reasons,
you can define a dynamic array.
CTA - G. Zucchinetti 13
Dynamic Arrays
However, to use this array or any of its elements, we will need to
eventually know its size.
CTA - G. Zucchinetti 14
Dynamic Arrays
• Suppose we want to ask the user to insert some input values, which
we will store in our input array, but we are unsure how many values
they want to insert.
• We can first assign a variable to an Input Box which asks for the
number of values, and then we can insert the value of each element
using a For, Next loop.
size = InputBox(“How many values will you enter as input?”)
ReDim input(1 to size) As Double
For i = 1 to size
input(i) = InputBox(“Please enter value “ & i )
Next i
CTA - G. Zucchinetti 15
Dynamic Arrays
Function UBound returns a Long containing the largest available
subscript for the indicated dimension of an array.
Ubound (arrayname[, dimension])
Example :
Dim A(1 To 100, 0 To 3, -3 To 4)
UBound(A, 1) : 100
UBound(A, 2) : 3
UBound(A, 3) : 4
CTA - G. Zucchinetti 16
Passing arrays as arguments
You can pass arrays to procedures (Subs, Functions) and functions can
return arrays as their result.
The data type of the array declared in the calling procedure must
match the data type declared in the called procedure's parameter list.
CTA - G. Zucchinetti 17
Annuity example
HEC Lausanne
Computanional Tools for Actuaries (CTA)
Present value of an annuity
• äx = 1 + v p
x + v2 p
2 x....... + vw-x p
w-x x
• Recursive formula :
- äx = 1 + v px äx+1 if x < w
- äx = 1 if x = w
CTA - G. Zucchinetti 19
Present value of an annuity
äx = 1 + v p + v2 p + vw-x p
x 2 x....... w-x x Loop
function
CTA - G. Zucchinetti 20
Declaring an array of px
Visibility : Module
Visibility : Module
CTA - G. Zucchinetti 21
Declaring an array of px
CTA - G. Zucchinetti 22
Fetching the probabilities
CTA - G. Zucchinetti 23
Fetching the probabilities
CTA - G. Zucchinetti 24
Progressing
CTA - G. Zucchinetti 25
Accessing the array
CTA - G. Zucchinetti 26
Annuity : Loop solution
HEC Lausanne
Computanional Tools for Actuaries (CTA)
Initializing the loop
äx = 1 + v px + v2 p
2 x....... + vw-x p
w-x x
CTA - G. Zucchinetti 28
First step in the loop
äx = 1 + v px + v2 p
2 x....... + vw-x p
w-x x
CTA - G. Zucchinetti 29
Next steps
äx = 1 + v px + v2 p
2 x....... + vw-x p
w-x x
CTA - G. Zucchinetti 30
Assigning the result
äx = 1 + v px + v2 p
2 x....... + vw-x p
w-x x
CTA - G. Zucchinetti 31
Annuity : Recursive function
call
HEC Lausanne
Computanional Tools for Actuaries (CTA)
Present value of an annuity
• äx = 1 + v p
x + v2 p
2 x....... + vw-x p
w-x x
• Recursive formula :
- äx = 1 + v px äx+1 if x < w
- äx = 1 if x = w
CTA - G. Zucchinetti 33
Recursive function call
- äx = 1 if x = w
- äx = 1 + v px äx+1 if x < w
CTA - G. Zucchinetti 34
Recursive function call
- äx = 1 if x = w
- äx = 1 + v px äx+1 if x < w
CTA - G. Zucchinetti 35
Recursive function call
- äx = 1 if x = w
- äx = 1 + v px äx+1 if x < w
CTA - G. Zucchinetti 36
Recursive function call (stacking the
calls)
105 1
104
103
102
101
100
CTA - G. Zucchinetti 37
Annuity : Array
HEC Lausanne
Computanional Tools for Actuaries (CTA)
Which implementation for ax ?
get_ax
- ax_loop
- ax_recursive
- Array of ax values ?
Array implementation :
- Interest rate index (valuation for each 0.25% between 0 and
5%)
- Smart initialization
CTA - G. Zucchinetti 39
Which implementation for ax ?
Interest rate Index
0% 1
0.25% 2
0.50% 3
0.75% 4
… …
CTA - G. Zucchinetti 40
Array initialization
CTA - G. Zucchinetti 41
Writing the array’s contents on a
worksheet
CTA - G. Zucchinetti 42
Annuity : what if interest rates
are dependent on maturity ?
HEC Lausanne
Computanional Tools for Actuaries (CTA)
Yield curve annuity
äx = 1 + v1 px + v22 2px....... + vw-xw-x w-xpx
Interest curve
3.50
3.00
2.50
2.00
Interest rate
1.50
1.00
0.50
-
1
4
7
10
13
16
19
22
25
28
31
34
37
40
43
46
49
Maturity (years)
CTA - G. Zucchinetti 44
Interest rates
CTA - G. Zucchinetti 45
Interest rates
CTA - G. Zucchinetti 46
Interest rates
CTA - G. Zucchinetti 47
Yield curve annuity
CTA - G. Zucchinetti 48
Yield curve annuity
CTA - G. Zucchinetti 49
References
• [Maksay 08] Maksay Gabor, Pigneur Yves, Modéliser par l'exemple, Presses
polytechniques et universitaires romande, 2008,
http://www.ppur.org/produit/290/9782880748951/Modeliser%20par%20lexemple
%20
• [Seref 07] Michelle M.H. Şeref, Ravindra K. Ahuja, and Wayne L. Winston ;
Developing Spreadsheet-Based Decision Support Systems Using Excel and VBA
for Excel ; Dynamic Ideas, Belmont, Massachusetts 2007
CTA - G. Zucchinetti 50