You are on page 1of 50

Arrays

Recursion

Giovanni Zucchinetti
giovanni.zucchinetti@gitec.ch

Adapted from the course « Notions informatiques – Modèles de calcul »


with kind permission from Prof. Yves Pigneur and Dr. Gabor Maksay

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

• Arrays store series of data that can be


manipulated or referred to later.

• The set of values stored in an array must all be


of the same data type.

• You can refer to the array as a whole or you can


refer to its individual elements by an index
value.

CTA - G. Zucchinetti 4
Arrays - Definition
• To define an array, we can use the Dim, Private, or Public
variable declarations.

• There is not an array data type however.


- The array will be named and the data type will be assigned to all
of the elements in that array.

• VBA will recognize that your variable is an array and not a


scalar variable because you must specify the size of the array
in the variable declaration.

• Here is a quick example of defining an array of double data


type, which has 10 elements and is named data.
- Dim data(10) As Double

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

To enter multiple element values using indices, we use a For, Next


loop with a counter variable.

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.

To define multi-dimensional arrays, you just need to specify the size of


each dimension, separated by a comma.

For example, if we want to define a two-dimensional array of size 5 by


10 (number of rows by number of columns) we could type
Dim data(5, 10) As Double

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

Whichever initial index value is chosen should be coordinated with the


counter variable used in For, Next loops.
For i = 2 to 13
results(i) = value
Next i

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.

When declaring a dynamic array, the size is not specified; the


parentheses are left empty.
Dim input() As Double

CTA - G. Zucchinetti 13
Dynamic Arrays
However, to use this array or any of its elements, we will need to
eventually know its size.

To set the size of a dynamic array at some later point in the


code, we use the ReDim statement.

The ReDim statement can also be used to set or change the


number of dimensions and the indexing bounds.

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

LBound returns a Long containing the smallest available subscript for


the indicated dimension of an array.

CTA - G. Zucchinetti 16
Passing arrays as arguments
You can pass arrays to procedures (Subs, Functions) and functions can
return arrays as their result.

Arrays are always passed by reference (ByRef). You will receive a


compiler error if you attempt to pass an array ByVal. This means that
any modification that the called procedure does to the array parameter
is done on the actual array declared in the calling procedure.

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

- (w : last age of the table)

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

äx = 1 + v px äx+1 (recursive formula) Recursive


function call

npx = px px+1 px+2 … px+n

CTA - G. Zucchinetti 20
Declaring an array of px

Visibility : Module

Visibility : Module

Visibility : Sub procedure

CTA - G. Zucchinetti 21
Declaring an array of px

Dimensions of the array

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

tpx = px px+1 px+2 … px+t

CTA - G. Zucchinetti 28
First step in the loop

äx = 1 + v px + v2 p
2 x....... + vw-x p
w-x x

tpx = px px+1 px+2 … px+t

CTA - G. Zucchinetti 29
Next steps

äx = 1 + v px + v2 p
2 x....... + vw-x p
w-x x

tpx = px px+1 px+2 … px+t

CTA - G. Zucchinetti 30
Assigning the result

äx = 1 + v px + v2 p
2 x....... + vw-x p
w-x x

tpx = px px+1 px+2 … px+t

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

- (w : maximum age of the table)

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

… …

Function IRindex(int_rate As Double) As Integer


IRindex = CInt(int_rate * 400 + 1)
End Function

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

If argument passed by reference → misleading


side effect for the calling function !

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

• [Hainault 02], Jean-Luc Hainault, Bases de données et modèles de calcul (3ème


édition), Dunod, 2002

CTA - G. Zucchinetti 50

You might also like