+ All Categories
Home > Documents > Risk Analysis & Modelling Lecture 1: Introduction.

Risk Analysis & Modelling Lecture 1: Introduction.

Date post: 25-Dec-2015
Category:
Upload: amice-austin
View: 218 times
Download: 1 times
Share this document with a friend
Popular Tags:
76
Risk Analysis & Modelling Lecture 1: Introduction
Transcript
Page 1: Risk Analysis & Modelling Lecture 1: Introduction.

Risk Analysis & Modelling

Lecture 1: Introduction

Page 2: Risk Analysis & Modelling Lecture 1: Introduction.

Course Website:

ram.edu-sys.net

Course Email:[email protected]

Page 3: Risk Analysis & Modelling Lecture 1: Introduction.

What is Risk?The definition of risk is the “Possibility of Loss”

When we observe risk we observe uncertainty about a possible outcome in the future

Even if we are uncertain about the exact outcome, we can often estimate a range of possibilities that might occur

When some of these possible outcomes are less favourable than others (incur a loss) we have Risk

We will be focusing on Quantitative Techniques that can be used to describe and analyse the Risks face by Insurance Companies

Page 4: Risk Analysis & Modelling Lecture 1: Introduction.

Risk And Future Outcomes

Current Position

Future Position

?

Time

Worst Outcome

Best Outcome

Page 5: Risk Analysis & Modelling Lecture 1: Introduction.

Types of Financial Risk

Financial institutions, such as Insurance Companies, face many risksFor the purpose of study it is useful to categorise the types of risks faced by these institutionsSome examples of the types of risk we will study are: Market Risk, Underwriting Risk, Reserving Risk and Credit Risk.The meaning of these categories will become apparent when we study the techniques used to model themAs with most broad concepts their definition can a bit fuzzy!

Page 6: Risk Analysis & Modelling Lecture 1: Introduction.

Market RiskMarket Risk arises from owning an asset whose market value or price changes over time

Market Risk can be directly observed through movements in the market price of the asset

An example of Market Risk would be the possibility of the value of your portfolio decreasing over the next year if you had £100,000 invested in the FTSE-100

Page 7: Risk Analysis & Modelling Lecture 1: Introduction.

Market Risk Diagram

?

Initial Wealth

Future Wealth

Ma

rke

t A

sse

t P

rice

Ma

rke

t A

sse

t P

rice

Ma

rke

t A

sse

t P

rice

Page 8: Risk Analysis & Modelling Lecture 1: Introduction.

Underwriting Risk

When an Insurance Company sells a policy it Underwrites or Insures the policy holder against some specified lossThe number of losses and their size areunknown when the policy is soldThese two uncertainties lead to Underwriting Risk – the risk that the number and size of claims will be greater than expectedInsurance Companies model Underwriting Risk using statistical distributions to describe the number of losses (frequency) and their size (severity)

Page 9: Risk Analysis & Modelling Lecture 1: Introduction.

Underwriting Risk on an Insurance Policy

Insurance Policy……………………………………………………………………………………………

?

Large Claim

Small Claim

Average Claim

No Claim

Page 10: Risk Analysis & Modelling Lecture 1: Introduction.

Reserve RiskInsurance Companies are not just uncertain about the frequency and severity of claims - there is also uncertainty about the timing of the payments of the claimsFor certain classes of Insurance (such as Liability Insurance) there can be a delay of a number of years between an accident or loss occurring and a claim payment being madeThe insurance company needs to set aside capital to pay these future claims in a Loss ReserveThe Reserve Risk is the possibility that this capital set aside in the Loss Reserve will be insufficient to meet the actual claims (Ultimate Claims) eventually paid by the Insurer – this is known as Under ReservingUnder Reserving is often cited as the primary cause of Insolvency for Insurance Companies

Page 11: Risk Analysis & Modelling Lecture 1: Introduction.

Reserve Risk

Loss Reserve

LossReserve

Loss ReserveUltimate Claim

Loss Reserve Ultimate Claim

Loss ReserveUltimate Claim

Less Than

Equal To

Greater

?

Page 12: Risk Analysis & Modelling Lecture 1: Introduction.

Credit RiskCredit Risk arises from a counter-party in an agreement being unable or unwilling to meet their financial obligationsFor example, the Credit Risk on a bank loan is the possibility that the borrower can no longer pay the amount due. When the borrower cannot pay a Default is said to have occurred.Insurance Companies primarily experience Credit Risk through both their Reinsurance Contracts and the bonds they hold in their investment portfoliosCredit Risk is harder to measure and quantify than Market Risk since Credit Events are infrequent.

Page 13: Risk Analysis & Modelling Lecture 1: Introduction.

Credit Risk Diagram

Creditor

Debtor

Full Repayment

Partial Repayment

NoRepayment

?

Page 14: Risk Analysis & Modelling Lecture 1: Introduction.

Other Types of Risk

In the literature you will find definitions for 100s of types of riskEnterprise Risk, Operational Risk, Political Risk, Strategic Risk, Legal Risk and so onWe could spend the rest of the lecture going through all these types, however not all are of interest to us since we cannot always Model them using quantitative techniquesAnd this course is about Modelling Risk.

Page 15: Risk Analysis & Modelling Lecture 1: Introduction.

What is ModellingThe definition of Modelling is:

“To produce a representation or simulation of “

Risk Modelling is to Simulate Risk

The components or building blocks of our models will be numerical and statistical techniques

A crucial part of the modelling process is to build a simplified, abstraction of reality

Good models are generally simple models which capture important elements of the real world we wish to examine and learn about

Bad models are complex models which contain a lot of irrelevant detail – complexity leads to error

Page 16: Risk Analysis & Modelling Lecture 1: Introduction.

Modelling Involves Simplification

Complicated Real World Phenomena

Simplified Model of the Real World

Modelling Process:What is Important, What

Can We Simplify?

Interpreting Model:What Does It Mean?

Page 17: Risk Analysis & Modelling Lecture 1: Introduction.

The Use of Computers In Risk Modelling

Even though the models we will be building will be simplified for the purpose of teaching they will involve millions of calculationsFar too many calculations to make with a calculator, pen and paper!Risk modelling is a practical science and this course shows you how to build working risk models not just talk about them!Along with every concept we cover on the course we will also learn the numerical and computing techniques necessary to apply that concept in practice

Page 18: Risk Analysis & Modelling Lecture 1: Introduction.

Computers and ModellingWhen you buy a low end 2.8 Ghz computer with two Cores you are purchasing a machine that can make 5.6 billion calculations per second!Gigahertz (Ghz) stands for billions of cycles per second, and one cycle is roughly one calculationSo each Core can make approximately 2.8 billion calculations per secondIf you were to perform 5.6 billion calculations on a calculator at 1 calculation every 3 seconds without rest it would take you over 500 years!With this almost “unlimited” calculating power the problem is frequently not the complexity of the model but whether or not an individual has the technical skills to describe the model to the computer

Page 19: Risk Analysis & Modelling Lecture 1: Introduction.

Microsoft ExcelWe will use Microsoft Excel to build and explore the various Risk Models we will studyExcel is a spreadsheet software package and is the primary tool used in the financial industry to make calculations and build modelsWe will be using some of the advanced features of Excel combined with VBA (Visual Basic for Applications) programming techniques*Although Excel is one of the simplest and fastest tools with which to develop risk models it has some limitationsVBA provides an ideal introduction to the world of programming - which is extremely useful and a lot easier than many people realise!

Page 20: Risk Analysis & Modelling Lecture 1: Introduction.

Spreadsheets and Matrices

Page 21: Risk Analysis & Modelling Lecture 1: Introduction.

What is a spreadsheet?

A spreadsheet can be thought of as a giant table which can contain numbers and formula.The spreadsheet is made up of cells which are identified by their column (represented by a sequence of letters A,B,C,D…. ,AA,AB..) and row (represented by a number 1,2,3,4…)The best way to learn about spreadsheets is to play about with them….

Page 22: Risk Analysis & Modelling Lecture 1: Introduction.

Elements of a Spreadsheet

Column Identifiers

Row Identifiers

Formula; add A1 and A2

Numeric values

Page 23: Risk Analysis & Modelling Lecture 1: Introduction.

Copying and Pasting Numbers and Formula

To avoid having to retype numbers and formula we can copy and paste values

One important point to note is that when copying and pasting formula in Excel the row and column references for the input cells are shifted

This will turn out to be a very useful, time saving feature in many of the models we will build

We can instruct Excel not to adjust the formula by placing a ‘$’ sign in front of the elements of the formula

Page 24: Risk Analysis & Modelling Lecture 1: Introduction.

Copy & Pasting Values

Formula and values copied and pasted 2 rows to the right and 2 rows down, notice how the

formula adjusts

Page 25: Risk Analysis & Modelling Lecture 1: Introduction.

MatricesMatrices are ordered blocks or tables of numbersThe numbers are organised into Rows and Columns much like a spreadsheetMany of the operations that can be performed on numbers can be performed on matrices such as addition, subtraction and multiplicationThere are also some “special” matrix operations such as inverse and transposeMatrices are a very important practical tool for performing large scale calculationsMatrices are also an important conceptual tools that allows us to generalise calculations for problems of different size

Page 26: Risk Analysis & Modelling Lecture 1: Introduction.

A 2 by 2 Matrix

2

1 2

(2,2) matrix

8 4

9 1

1

• This is a 2 by 2 matrix because it has 2 rows and 2 columns. • The matrix contains a total of 4 elements.• It is a Square matrix because it has the same number of rows and columns• The element at row 2 and column 1 is 9

Row

s

Columns

Page 27: Risk Analysis & Modelling Lecture 1: Introduction.

Matrix NotationWhen we wish to denote a matrix we will use a bold capital letter:

AA is a matrixWe denote the size or dimensions of the matrix by giving the rows and columns of the matrix in brackets:

A is (Rows, Columns)When we wish to denote the element of a matrix we will use a capital letter with two subscripts:

Ar,c

Where r is the row of the element and c is the column of the element of matrix A

Page 28: Risk Analysis & Modelling Lecture 1: Introduction.

An Example of the Notation

R2

A1,1 A1,2 A1,3

A2,1 A2,2 A2,3

A3,1 A3,2 A3,3

R1

C1 C2 C3

A is (3,3)A is a square matrix

A =

R3

Page 29: Risk Analysis & Modelling Lecture 1: Introduction.

Matrix OperationsWe have seen that a Matrix is an ordered block of numbers

Like numbers the meaning of a Matrix is defined by the values they represent and the order of those values

Operations such as Addition, Subtraction and Multiplication that can be performed on numbers can also be performed on Matrices

Matrices and their operations are particularly useful because they allow us to describe large blocks of calculations simultaneously

The meaning of the operation depends on the matrices they are applied to

Page 30: Risk Analysis & Modelling Lecture 1: Introduction.

Matrix Addition and Subtraction

Two Matrices A and B of dimensions (rA,cA) and (rB,cB) can be added or subtracted if and only if rA = rB and cA = cB (columns of A equal columns B and rows A equal rows B)C = A + B or C = A - B then C is the same dimension as A and B: (rA ,cA) and (rB ,cB)

For addition, Ci,j= Ai,j + Bi,j the element at row i column j for C is equal to the sum of the elements at row i and column j in A and B.The meaning of the addition of 2 Matrices depends on the data that they store and the order of that data.

Page 31: Risk Analysis & Modelling Lecture 1: Introduction.

7 4 10

12 9 5

8 2 3

+9 9 1

5 10 22

4 8 21

=

7+9 4+9 10+1

12+5 9+10 5+22

8+4 2+8 3+21

=16 13 11

17 19 27

12 10 24

• We can add the 2 matrices because they are of the same dimension (3,3) • The resulting matrix is of dimension (3,3)

Matrix Addition an Example

Page 32: Risk Analysis & Modelling Lecture 1: Introduction.

17 12

12 9

8 12

-9 20

15 13

-12 8

=

17-9 12-20

12-15 9-13

8-(-12) 12-8

=8 -8

-3 -4

20 4

• We can subtract the 2 matrices because they are of the same dimension (3,2) • The resulting matrix is of dimension (3,2)

Matrix Subtraction an Example

Page 33: Risk Analysis & Modelling Lecture 1: Introduction.

Matrix Subtraction Review Question

Subtract Matrix B from Matrix A ie (A-B)

4

7

3

B =

12

1

3

A =

Note that both A and B have a single column and are therefore a special type of Matrix called a Vector

Page 34: Risk Analysis & Modelling Lecture 1: Introduction.

Matrix Subtraction Review Question

Subtract Matrix B from Matrix A

12-4

1-7

3-3

4

7

3

12

1

3

- = =

8

-6

0

Page 35: Risk Analysis & Modelling Lecture 1: Introduction.

An Invalid Matrix Addition

7 4 10

12 9 5

8 2 3

+9 9

5 10 =

7+9 4+9 12+?

12+5 9+10 5+?

8+? 2+? 3+?

=16 13 ?

17 19 ?

? ? ?

• We cannot add the 2 matrices because they are of different dimensions (3,3) and (2,2)• The resulting matrix is invalid and has no meaning

Page 36: Risk Analysis & Modelling Lecture 1: Introduction.

Array Formula In ExcelAll of the matrix operations we will be performing in Excel will be a special class of formula called “Array Formula”Array Formula are different from normal Excel formula in that they work on ranges or arrays rather than individual cellsWhen entering an Array Formula an output range is selected before the formula is typed. The formula is entered by pressing Ctrl-Shift-EnterCurly Brackets appear about the formula once Ctrl-Shift-Enter is pressedThe Array Formula represents a “block” made up of more than one cell.

Page 37: Risk Analysis & Modelling Lecture 1: Introduction.

Matrix Addition In Excel

Input Ranges Output Range Selected

Formula adds the 2 matrices “A3:C5” and “E3:G5” :“A3:C5+E3:G5” notice the curly brackets that appear after the formula is entered with Ctrl-Shift-Enter

Page 38: Risk Analysis & Modelling Lecture 1: Introduction.

Input Ranges/Matrices Output Range Selected

Formula subtracts the 2 matrices “A3:C5” and “E3:G5” :“A3:C5-E3:G5” notice the curly brackets indicating an array formula

Matrix Subtraction In Excel

Page 39: Risk Analysis & Modelling Lecture 1: Introduction.

Matrix MultiplicationMatrix Multiplication is a more complex operation and can involve many calculations.Matrix Multiplication is one of the key calculations we will use in our Risk Models

Two Matrices A and B of dimensions (rA,cA) and (rB,cB) can multiplied if and only if cA = rB

C = A * B then C is of dimension (rA ,cB)

Ci,j= Ai,k * Bk,j for k = 1 to N where N = cA = rB

Matrix multiplication is not commutative A*B doesn’t equal B*A

Page 40: Risk Analysis & Modelling Lecture 1: Introduction.

* =

=

5 3 2

4

5*2 + 3*4 22

Matrix Multiplication an Example 1

• We can multiply these 2 matrices because the columns of A (2) is equal to the rows of B (2)• The resulting matrix C has a number of rows equal to 1 (since A has 1 rows) and columns equal to 1 (since B has 1 columns)

A B

CC

(1,2) (2,1)

(1,1)(1,1)

Page 41: Risk Analysis & Modelling Lecture 1: Introduction.

Matrix Multiplication an Example 1a

* =

=

5 3 2

4

7

5*2 + 3*4 + ?*7 ??

CC

(1,2)

(3,1)

(1,1) (1,1)

Page 42: Risk Analysis & Modelling Lecture 1: Introduction.

Matrix Multiplication an Example 2

* =5 3 2 4

4 8

A B

(1,2) (2,2)

5*2 + 3*4 5*4 + 3*8

C

(1,2)

= 22 44

Page 43: Risk Analysis & Modelling Lecture 1: Introduction.

Matrix Multiplication an Example 2

* =

=

2 4

4 8

5 3

3 7

5*2 + 3*4 5*4 + 3*8

3*2 + 7*4 3*4 + 7*8

22 44

20 68

A B

C C(2,2) (2,2)

(2,2) (2,2)

Page 44: Risk Analysis & Modelling Lecture 1: Introduction.

Matrix Multiplication an Example 3

1 0

0 1* =9 6

AB

9*1 + 6*0 9*0 + 6*1 = 9 6

(1,2)

(2,2)

(1,2) (1,2)

• We can multiply these 2 matrices because the columns of A (2) is equal to the rows of B (2)• The resulting matrix C has a number of rows equal to 1 (since A has 1 rows) and columns equal to 2 (since B has 2 rows)

C C

Page 45: Risk Analysis & Modelling Lecture 1: Introduction.

Matrix Multiplication Review Question

Multiply these 2 matrices together:

9 6

(1,2)

(2,2)

* =

•What is the size/dimension of the resulting matrix?•What are its element(s)

2 1

1 2

Page 46: Risk Analysis & Modelling Lecture 1: Introduction.

Matrix Multiplication Review Question

2 1

1 29 6

(1,2)

(2,2)

* = 9*2+6*1 9*1+6*2

24 21=

Page 47: Risk Analysis & Modelling Lecture 1: Introduction.

Matrix Multiplication In ExcelFormula multiplies matrices “A3:C5” by “E3:G5”: “MMULT(A3:C5, E3:G5)” and outputs to the result to the selected range I3:K5. MMULT is a special built in Excel Function.

Input Ranges/Matrices Output Range Selected

Page 48: Risk Analysis & Modelling Lecture 1: Introduction.

The Transpose of a matrix

When a Matrix is Transposed its rows and columns are interchanged

If A is of dimension (rb ,cb) then AT is of dimension (cb ,rb) where AT

j,i= Ai,j

Sometimes matrices need to be transposed before they are multiplied or added

Page 49: Risk Analysis & Modelling Lecture 1: Introduction.

A = 1 7

44 32

75 12

1 44 75

7 32 12AT =

Matrix Transposition an Example

•The columns of matrix A become the rows of AT

•The matrix is rotated when it is transposed

Page 50: Risk Analysis & Modelling Lecture 1: Introduction.

Matrix Transpose Review Question

-8 4 10

12 19 53

8 21 3

• Transpose the following matrix:

•What is the size/dimenion of the resulting matrix?•What are its element(s)

Page 51: Risk Analysis & Modelling Lecture 1: Introduction.

Matrix Transpose Review Question

-8 12 8

4 19 21

10 53 3

•What is the size/dimenion of the resulting matrix?•What are its element(s)

Page 52: Risk Analysis & Modelling Lecture 1: Introduction.

Matrix Transpose In ExcelFormula transposes range “A3:B5” onto the selected output range “D3:F4”: “Transpose(A3:B5)”. When selecting the output range the rows/columns must be the correct shape.

Transposed Output Matrix/RangeInput Matrix/Range

Page 53: Risk Analysis & Modelling Lecture 1: Introduction.

Random Numbers in Excel

Page 54: Risk Analysis & Modelling Lecture 1: Introduction.

What are Random Numbers?

Randomness is a lack of order, purpose, cause, or predictability Random numbers are simply numbers that do not have any order or pattern - they are unpredictableComputers are particularly good at generating equally likely random, decimal numbers between 0 and 1This type of random number is called a Uniform Random Number

Page 55: Risk Analysis & Modelling Lecture 1: Introduction.

Rand functionIf we want Excel to give us a uniform random number we simply enter the formula “=rand()”Every time you enter this formula (or press F9) you will get a different random number between 0 and 1 - every number has an equal and very small chance of occurringYou would have to keep F9 pressed for about 3 million years before you would see the same number twice!Rand is the basis of one of the most important quantitative tools we will learn on this course – the Monte Carlo Simulation

Page 56: Risk Analysis & Modelling Lecture 1: Introduction.

Rand

Rand()

0.015345 0.247535 0.31243 0.517841 0.696173 0.89792 0.97532

Rand() randomly selects a decimal number between 0 and 1, all decimal numbers in the range have an equal but very small probability of selection

Page 57: Risk Analysis & Modelling Lecture 1: Introduction.

Rand Function in Excel

Random Number Between 0 and 1, this number will change every time you enter the formula or press F9

To instruct Excel to generate a random number simply type =RAND() in the cell you wish to place the random number

Page 58: Risk Analysis & Modelling Lecture 1: Introduction.

Using Rand to Simulate Uncertainty

Rand allows us to Simulate real world uncertainties or risks in the computer and then examine their behaviour

Today, we will use it to simulate one of the simplest random phenomenon – the flipping of a coin

How do we turn the output of rand() into a heads or tails outcome?

Page 59: Risk Analysis & Modelling Lecture 1: Introduction.

Cutting up the Output from Rand

=Rand()

Tails Bucket Heads Bucket

0.123342

0.323342

0.423342

0.223342

0.0233420.723342

0.923342

0.823342

0.623342

50% of Numbers 50% of Numbers

X?

Less Than X Greater Than X

What is X?

Page 60: Risk Analysis & Modelling Lecture 1: Introduction.

The IF Statement

The IF statement is one of the most important constructs in computer programming

IF is how we teach the computer to make decisions

It comes down to a simple statement:

IF X is true then do A otherwise do B

An example of this is IF the number is greater than 0.5 display a 1 otherwise display a 0

Page 61: Risk Analysis & Modelling Lecture 1: Introduction.

The IF Function in Excel

IF is one of the more complicated functions we will be using, it has the form:

=IF(Condition, Value if True, Value if False)

An example of this would be

=IF(A1 > 0.5,1,0)

This would test if the value in A1 is greater than 0.5, if this is then display a 1, otherwise display 0

Page 62: Risk Analysis & Modelling Lecture 1: Introduction.

IF Function in ExcelThe IF function in Excel has three parameters separated by commas, this tests if A1 is greater than 0.5, if it is then display a 1 otherwise display 0

Either a 1 or a 0 is displayed on the spreadsheet depending on the value in cell A1

Page 63: Risk Analysis & Modelling Lecture 1: Introduction.

Coin Flipping Game

Imagine you have the opportunity to play a game of chance in which a coin is flipped 10 timesIf the coin is heads then you win £1.5, if it is tails you lose £1You want to know how likely it is that you would lose more than £5 if you play the game 10 times in a rowFirstly lets simulate the game in Excel….

Page 64: Risk Analysis & Modelling Lecture 1: Introduction.

Estimating the Risk by Simulation

Rather than using statistics we could use our coin flipping simulation to estimate the chance or probability of losing more than £5We could do this by running the simulation 100 times and counting the number of times we lose £5 or moreThis would give us an estimate of the risk or probability of losing this amountAlthough the answer we will get for the probability or risk will vary each time we run the simulation – why and how can we make our answer more accurate?

Page 65: Risk Analysis & Modelling Lecture 1: Introduction.

Doing it the Hard Way…

We could have also calculated this probability using mathematicsFirstly we observe that we lose £5 or more when we have 2 or less heads out of the 10 flips (2 heads and 8 tails would give -£5, 1 head and 9 tails would give -£7.5, and 0 heads and 10 tails would give -£10)To calculate the probability of these 3 outcomes we would use the Binomial Distribution

Page 66: Risk Analysis & Modelling Lecture 1: Introduction.

The formula for the Binomial Distribution is:

In this case n is the number of flips (10), k is the number of heads (0,1,2) and p is the probability of a head (0.5)Applying this formula we find the probability of getting 0 heads is 0.00097, the probability of getting 1 head is 0.009766 and the probability of getting 2 heads is 0.043945Summing these 3 probabilities we calculate the probability of losing £5 or more is 0.054688 or 5.4688%Our answer we derived from the Monte Carlo simulation was a lot simpler to calculate but had a little bit of noise in it – but a lot of the time a little bit of error doesn’t matter!

knk ppknk

n

)1.(.)!(!

!

Page 67: Risk Analysis & Modelling Lecture 1: Introduction.

Bernoulli Random Numbers & Life Insurance

The random outcome for the flip of a coin is an example of a Bernoulli Random Number (a random number which can only take one of two possible values)This type of random number is widely used in the modelling of the Underwriting Risks faced by Life Insurance CompaniesA Term Life Policy is a Life Insurance policy which usually pays a fixed amount known as a Death Benefit if the insured dies over the period coveredJust like the flip of a coin the insured either survives to the end of the period covered by a Term Life Policy or they do not surviveWe will now model the level of claims a Life Insurer might incur on a portfolio (cohort) of 100 life policies each of which has a death benefit of £10,000 and a death probability (probability of the insured dying over the period) of 5%

Page 68: Risk Analysis & Modelling Lecture 1: Introduction.

Cutting up the Output from Rand

=Rand()

Tails Bucket Heads Bucket

0.123342

0.323342

0.423342

0.223342

0.0233420.723342

0.923342

0.823342

0.623342

95% of Numbers 5% of Numbers

X?

Less Than X Greater Than X

What is X?

Page 69: Risk Analysis & Modelling Lecture 1: Introduction.

Simulating the Claim Payment on One Life Policy

Rand() IF(B1>0.95,10000,0)

B1 ?YES

10000

NO

0

Page 70: Risk Analysis & Modelling Lecture 1: Introduction.

Appendix: Custom Functions in Excel

Excel allows the user to create custom functions using VBAThese functions must be added to a Module – which is just a special page where code for an Excel workbook is writtenTo add a module to a workbook select the Visual Basic option under the Developer Tab (if you do not have the Developer Tab then check the Excel Options -> Popular -> Show Developer Tab in Ribbon) then select the Insert -> Module menu option in the Visual Basic editorLets start with a simple function that adds two numbers together

Page 71: Risk Analysis & Modelling Lecture 1: Introduction.

MyAdd Function

Public Function MyAdd(NumberA, NumberB) MyAdd = NumberA + NumberB End Function

Special VBA words are displayed in blue (keywords).MyAdd, NumberA and NumberB are just words selected at random. MyAdd is the name of the function that is used to call the function on the spreadsheetNumberA and NumberB are used to reference the two parameters passed into the functionTo call this function from the spreadsheet we would type = MyAdd(4,7)In this case NumberA would be 4 and NumberB would be 7

Page 72: Risk Analysis & Modelling Lecture 1: Introduction.

If we wanted to add three numbers A, B and C:

Public Function Add3(A, B, C)Add3= A + B + CEnd Function

VBA also has an IF statement the following function will display 1 the input number is greater than 2 else 0:

Public Function IsGreaterThanTwo(A)If A > 2 ThenIsGreaterThanTwo = 1ElseIsGreaterThanTwo = 0End IfEnd Function

Page 73: Risk Analysis & Modelling Lecture 1: Introduction.

Appendix: Further Matrix Operations Determinant

The determinant of a matrix is a number which is associated with square matricesIt is useful in determining if a square matrix is singular or notThe formula for the calculation of the determinant in Excel is MDETEMWe will come across determinants when we look at the Eigen Values of the covariance matrix in a later lecture

Page 74: Risk Analysis & Modelling Lecture 1: Introduction.

Appendix: Further Matrix Operations Identity Matrix

The identity matrix, often denoted by I, is a special square matrix which when multiplied by another square matrix A of the same dimensions results in the same matrix A:

A.I = AThe identity matrix is a diagonal matrix with 1’s along the principal diagonal and 0’s everywhere elseThere is no function to generate the identity matrix in Excel (although you should be able to make your own by the end of the course!)Instead you can just type it in

Page 75: Risk Analysis & Modelling Lecture 1: Introduction.

Identity Matrix Example

* =

=

1 0

0 1

5 4

3 7

5*1 + 3*0 5*0 + 4*1

3*1 + 7*0 3*0 + 7*1

5 4

3 7

IA

(2,2)A A

(2,2)

(2,2) (2,2)

Page 76: Risk Analysis & Modelling Lecture 1: Introduction.

Appendix: Further Matrix Operations Inverse

If A is a square matrix with a non-zero determinant then there exists another matrix A-1 known as the inverse such that:

A. A-1 = I

Where I is the identity matrix

The inverse matrix can be calculated in Excel using the MINVERSE function

We will not be using the MINVERSE on this course but we will be using other matrix operations such as the Cholesky and Singular Value Decompositions


Recommended