R Programming for Actuarial Science [First Edition] 9781119754992, 9781119754978, 9781119754985, 9781119755005

R Programming for Actuarial Science Professional resource providing an introduction to R coding for actuarial and finan

125 89

English Pages 640 Year 2023

Report DMCA / Copyright

DOWNLOAD FILE

R Programming for Actuarial Science [First Edition]
 9781119754992, 9781119754978, 9781119754985, 9781119755005

Table of contents :
Cover
Title Page
Copyright Page
Dedication
Table of Contents
About the Companion Website
Introduction
1 Main Objectives of This Book
2 Who Is This Book For?
3 How to Use This Book
4 Book Structure
5 Chapter Style
6 Examples and Exercises
7 Verification of Code and Calculations – Best Practice
8 Website: www.wiley.com/go/rprogramming.com
9 R or Microsoft Excel?
10 Caveats
11 Acknowledgements
1 R : What You Need to Know to Get Started
1.1 Introduction
1.2 Getting Started: Installation of R and RStudio
1.2.1 Installing R
1.2.2 What Is RStudio?
1.2.3 Inputting R Commands
1.3 Assigning Values
1.4 Help in R
1.5 Data Objects in R
1.6 Vectors
1.6.1 Numeric Vectors
1.6.2 Logical Vectors
1.6.3 Character Vectors
1.6.4 Factor Vectors
1.7 Matrices
1.8 Dataframes
1.9 Lists
1.10 Simple Plots and Histograms
1.11 Packages
1.12 Script Files
1.13 Workspace, Saving Objects, and Miscellany
1.14 Setting Your Working Directory
1.15 Importing and Exporting Data
1.15.1 Importing Data
1.15.2 Exporting Data
1.16 Common Errors Made in Coding
1.17 Next Steps
1.18 Recommended Reading
1.19 Appendix: Coercion
2 Functions in R
2.1 Introduction
2.1.1 Objectives
2.1.2 Core and Package Functions
2.1.3 User-Defined Functions
2.2 An Introduction to Applying Core and Package Functions
2.2.1 Examples of Simple, Common Functions
2.3 User-Defined Functions
2.3.1 What does a “udf” consist of?
2.3.2 Naming Conventions
2.3.3 Examples and Exercises
2.4 Using Loops in R – the “for” Function
2.5 Integral Calculus in R
2.5.1 The “Integrate” Function
2.5.2 Numerical Integration
2.6 Recommended Reading
3 Financial Mathematics (1): Interest Rates and Valuing Cashflows
3.1 Introduction
3.2 The Force of Interest
3.3 Present Value of Future Cashflows
3.4 Instantaneous Forward Rates and Spot Rates
3.5 Non-Constant Force of Interest
3.5.1 Discrete Cashflows
3.5.2 Cashflows Which Are Continuous
3.6 Effective and Nominal Rates of Interest
3.6.1 Effective Rates of Interest
3.6.2 Why Do We Use Effective Rates?
3.6.3 Nominal Interest Rates
3.7 Appendix: Force of Interest – An Analogy with Mortality Rates
3.8 Recommended Reading
4 Financial Mathematics (2): Miscellaneous Examples
4.1 Introduction
4.2 Writing Annuity Functions
4.2.1 Writing a function for an annuity certain
4.3 The ‘presentValue’ Function
4.4 Annuity Function
4.5 Bonds – Pricing and Yield Calculations
4.6 Bond Pricing: Non-Constant Interest Rates
4.7 The Effect of Future Yield Changes on Bond Prices Throughout the Term of the Bond
4.8 Loan Schedules
4.8.1 Introduction
4.8.2 Method 1
4.8.3 Method 2
4.9 Recommended Reading
5 Fundamental Statistics: A Selection of Key Topics . Dr A Kume
5.1 Introduction
5.2 Basic Distributions in Statistics
5.3 Some Useful Functions for Descriptive Statistics
5.3.1 Introduction
5.3.2 Bivariate or Higher Order Data Structure
5.4 Statistical Tests
5.4.1 Exploring for Normality or Any Other Distribution in the Data
5.4.2 Goodness-of-fit Testing for Fitted Distributions to Data
5.4.2.1 Continuous distributions
5.4.2.2 Discrete distributions
5.4.3 T-tests
5.4.3.1 One sample test for the mean
5.4.3.2 Two sample tests for the mean
5.4.4 F-test for Equal Variances
5.5 Main Principles of Maximum Likelihood Estimation
5.5.1 Introduction
5.5.2 MLE of the Exponential Distribution
5.5.2.1 Obtaining the MLE numerically using R
5.5.2.2 Obtaining the MLE analytically
5.5.3 Large Sample (Asymptotic) Properties of MLE
5.5.4 Fitting Distributions to Data in R Using MLE
5.5.5 Likelihood Ratio Test, LRT
5.6 Regression: Basic Principles
5.6.1 Simple Linear Regression
5.6.2 Quantifying Uncertainty on
5.6.3 Analysis of Variance in Regression
5.6.3.1 R2 and adjusted R2 Coefficient of Determination
5.6.4 Some Visual Diagnostics for the Proposed Simple Regression Model
5.7 Multiple Regression
5.7.1 Introduction
5.7.2 Regression and MLE
5.7.2.1 Multivariate Regression
5.7.3 Tests
5.7.3.1 Likelihood Ratio Test in Regression
5.7.3.2 Akaike Information Criterion: AIC
5.7.3.3 AIC and Regression model selection
5.7.3.4 Bayesian Information Criterion: BIC
5.7.4 Variable Selection, Finding the Most Appropriate Sub-Model
5.7.5 Backward Elimination
5.7.6 Forward Selection
5.7.7 Using AIC/BIC Criteria
5.7.8 LRT in Model Selection
5.7.9 Automatic Search Using R-squared Criteria
5.7.10 Concluding Remarks on Test Data
5.7.11 Modelling Beyond Linearity
5.8 Dummy/Indicator Variable Regression
5.8.1 Introducing Categorical Variables
5.8.2 Continuous and Indicator Variable Predictors – Including Load in the Model
5.9 Recommended Reading
6 Multivariate Distributions, and Sums of Random Variables
6.1 Multivariate Distributions – Examples in Finance
6.2 Simulating Multivariate Normal Variables
6.3 The Summation of a Number of Random Variables
6.4 Conclusion
6.5 Recommended Reading
7 Benefits of Diversification
7.1 Introduction
7.2 Background
7.3 Key Mathematical Ideas
7.4 Running Simulations
7.5 Recommended Reading
8 Modern Portfolio Theory
8.1 Introduction
8.2 2-Asset Portfolio
8.3 3-Asset Portfolio
8.4 Introduction of a Risk-free Asset to the Portfolio
8.4.1 Adding a Risk-free Asset
8.4.2 Capital Market Line and the Sharpe Ratio
8.4.3 Borrowing to Obtain Higher Returns
8.5 Appendix: Lagrange Multiplier Method
8.6 Recommended Reading
9 Duration – A Measure of Interest Rate Sensitivity
9.1 Introduction
9.2 Duration – Definitions and Interpretation
9.3 Duration Function in R
9.4 Practical Applications of Duration
9.5 Recommended Reading
10 Asset-Liability Matching: An Introduction
10.1 Introduction
10.2 What Interest Rates Do Institutions Use To Measure Their Liabilities?
10.3 Variance of the Solvency Position
10.4 Characteristics of Various Asset Classes and Liabilities
10.5 Our Scenarios
10.6 Results
10.7 Simulations
10.8 Exercise and Discussion – an Insurer With Predominately Short-Term Liabilities
10.9 Potential Exercise
10.10 Conclusions
10.11 Recommended Reading
11 Hedging: Protecting Against a Fall in Equity Markets
11.1 Introduction
11.2 Our Example
11.2.1 Futures Contracts – A Brief Explanation
11.2.2 Our Task
11.3 Adopting a Better Hedge
11.4 Allowance for Contract and Portfolio Sizes
11.5 Negative Hedge Ratio
11.6 Parameter and Model Risk
11.7 A Final Reminder on Hedging
11.8 Recommended Reading
12 Immunisation – Redington and Beyond
12.1 Introduction
12.2 Outline of Redington Theory and Alternatives
12.3 Redington’s Theory of Immunisation
12.4 Changes in the Shape of the Yield Curve
12.5 A More Realistic Example
12.5.1 Determining a Suitable Bond Allocation
12.5.2 Change in Yield Curve Shape
12.5.3 Liquidity Risk
12.6 Conclusion
12.7 Recommended Reading
13 Copulas
13.1 Introduction
13.2 Copula Theory – The Basics
13.3 Commonly Used Copulas
13.3.1 The Independent Copula
13.3.2 The Gaussian Copula
13.3.3 Archimedian Copulas
13.3.4 Clayton Copula
13.3.5 Gumbel Copula
13.4 Copula Density Functions
13.5 Mapping from Copula Space to Data Space
13.6 Multi-dimensional Data and Copulas
13.7 Further Insight into the Gaussian Copula: A Non-rigorous View
13.8 The Real Power of Copulas
13.9 General Method of Fitting Distributions and Simulations – A Copula Approach
13.9.1 Fitting the Model
13.9.2 Simulating Data Using the mvdc and rMvdc Functions
13.10 How Non-Gaussian Copulas Can Improve Modelling
13.11 Tail Correlations
13.12 Exercise (Challenging)
13.13 Appendix 1 – Copula Properties
13.14 Appendix 2 – Rank Correlation and Kendall’s Tau, τ
13.15 Recommended Reading
14 Copulas – A Modelling Exercise
14.1 Introduction
14.2 Modelling Future Claims
14.2.1 Data
14.2.2 Fitting Appropriate Marginal Distributions
14.2.3 Fitting The Copula
14.2.4 Assessing Risk From the Analysis of Simulated Values
14.2.5 Comparison with the Gaussian Copula Model
14.2.6 Comparison of the Models with the Data
14.3 Another Example: Banking Regulator
14.4 Conclusion
15 Bond Portfolio Valuation: A Simple Credit Risk Model
15.1 Introduction
15.2 Our Example Bond Portfolio
15.2.1 Description
15.2.2 The Transition Matrix
15.2.3 Correlation Matrix
15.2.4 Simulations and Results
15.2.5 Incorporating Interest Rate Risk – A Simple Adjustment
15.2.6 Portfolio Consisting of Highly Correlated Bonds
15.3 Further Development of this Model
15.4 Recommended Reading
16 The Markov 2-State Mortality Model
16.1 Introduction
16.2 Markov 2-State Model
16.3 Simple Applications of the 2-State Model
16.4 Estimating Mortality Rates from Data
16.5 An Example: Calculating Mortality Rates for One Age Band
16.6 Uncertainty in Our Estimates
16.7 Next Steps?
16.8 Appendices
16.8.1 Informal Discussion of μ
16.8.2 Intuitive meaning of fx(t)
16.9 Recommended Reading
17 Approaches to Fitting Mortality Models: The Markov 2-state Model and an Introduction to Splines
17.1 Introduction
17.2 Graduation of Mortality Rates
17.3 Fitting Our Data
17.3.1 Objective
17.3.2 Summarised Data
17.4 Model Fitting with Least Squares
17.5 Individual Member Data
17.6 Comparing Life Tables with a Parametric Formula
17.7 Splines: An Introduction
17.7.1 Overview
17.7.2 Data
17.7.3 Fitting the Model: Spline regression
17.7.4 Adjusted Dataset
17.8 Summary
17.9 Recommended Reading
18 Assessing the Suitability of Mortality Models: Statistical Tests
18.1 Introduction
18.2 Theory
18.3 Our Mortality Data and Various Proposed Mortality Rates
18.4 Testing the Standard Table Rates – Table 1,
18.4.1 Data and initial plot
18.4.2 x2 test
18.4.3 Signs Test – for Overall Bias
18.4.4 Serial Correlations Test; Testing for Bias Over Age Ranges
18.4.5 Analysing the Distribution of Deviances
18.4.6 logL, AIC Calculations
18.4.7 Conclusions on Conclusions on
18.5 Graduation of Mortality Rates by Adjusting a Standard Table
18.5.1 Testing Table 2,
18.5.2 Adjusting Table 2
18.6 Testing Graduated Rates Obtained from a Parametric Formula,
18.7 Comparing Our Candidate Rates
18.8 Over-fitting
18.9 Other Thoughts
18.10 Appendix – Alternative Calculations of LogL’s
18.11 Recommended Reading
19 The Lee-Carter Model
19.1 Introduction
19.2 Using the L-C Model to Create Data and Fit the Model
19.2.1 Introducing the Lee-Carter Model
19.2.2 Calculating the Parameter Values
19.2.3 Interpretation of ax, bx, and kt
19.3 Using L-C to Model Actual Mortality Data from HMD
19.4 Using the lca Function in the Demography Package
19.5 Constructing Your Own Demogdata Object
19.6 Forecasting Mortality Rates
19.7 Case Study: The Impact of the HIV Virus on Mortality Rates
19.8 Recommended Reading
20 The Kaplan-Meier Estimator
20.1 Introduction
20.2 What Is Censoring?
20.2.1 Non-Informative Censoring
20.3 Defining the Relevant Event
20.4 K-M Theory
20.5 Introductory Example: Monitoring Delays in Making Claim Payments
20.6 Lung Cancer Example
20.6.1 Basic Results
20.6.2 Comparison of Male and Female Rates
20.6.3 Doctor Assessment Scores – ph.ecog
20.7 Issues with the Kaplan-Meier Model
20.8 Recommended reading
21 Cox Proportionate Hazards Regression Model
21.1 Introduction
21.2 Cox Model Equation
21.3 Applications
21.3.1 Smokers’ Mortality: Small Data Set
21.3.2 Smokers’ Mortality: Larger Data Set
21.3.3 Multiple covariates and interactions
21.4 Comparison of Cox and Kaplan Meier Analyses of Lung Cancer Data
21.5 Recommended Reading
22 Markov Multiple State Models: Applications to Life Contingencies
22.1 Introduction
22.2 The Markov Property
22.3 Markov Chains and Jump Models
22.3.1 Examples
22.3.2 Differences between Markov Chain and Markov Jump Models
22.4 Markov Chains (Discrete Time)
22.4.1 Applying Markov Chains to Estimate Future Probabilities
22.4.2 Markov Chain Model – NCD
22.4.3 Coding Exercise for Markov Chains
22.5 Markov Jump Models
22.5.1 Example – Simple 3-State Model (All Transitions Possible)
22.5.2 Example – H-S-D Model
22.6 Non-Constant Rates
22.7 Premium Calculations
22.8 Transition Rate Estimation
22.9 Multiple Decrement Models
22.9.1 Introduction
22.9.2 Using a Numerical Approach for the above Fixed Rate Problems
22.9.3 An Exact Approach
22.9.4 Age-Dependent Rates
22.10 Recommended Reading
23 Contingencies I
23.1 Introduction
23.2 What is Meant by “Contingencies” in an Actuarial Context?
23.3 The Life Table
23.4 Expected Present Values of the Key Contingency Functions
23.5 Writing Our Own Code – Some Introductory Exercises
23.6 The Lifecontingencies Package
23.6.1 The Lifetable and Actuarialtable Objects
23.6.2 Application to Actual Mortality Tables: AM92 and AF92
23.6.3 Annuities
23.6.4 Annuities Paid more Frequently than Annually
23.6.5 Increasing Annuities
23.6.6 Reversionary Annuities
23.6.7 Example: Annuity Company Valuation
23.6.8 Life Assurance functions
23.6.9 Assurance Policies with immediate Payment on Death: Ax
23.7 Simulation of Future Lifetimes
23.8 Recommended Reading
24 Contingencies II
24.1 Introduction
24.2 Mortality Tables: AM92
24.3 Uncertainty in Present Values: Variance
24.4 Simulations
24.4.1 Single Policy
24.4.2 Portfolios with 100 Policies – Portfolio Claim Distribution from Simulations
24.5 Simulation of Annuities
24.6 Premium Calculations
24.7 Profits – Probability Distributions of Single Policies and Portfolios
24.8 Progression of expected profits throughout the lifetime of a policy: no reserves held
24.9 Policy Values
24.9.1 Calculating Policy Values
24.9.2 Recursive Formulae – Discrete and Continuous (Thiele)
24.9.3 Recursive Equation with 3 States – HSD Model
24.10 Profits from Policies where Reserves Are Held
24.10.1 Calculating the Profit Vector
24.10.2 Measures of Profit and Profit Testing
24.11 Profit Uncertainty: Interest Rate and Mortality Risk
24.12 Risk Capital and Risk-adjusted Return Measures
24.13 Unit-linked Policies
24.13.1 Introduction
24.13.2 Example with Deterministic and Stochastic Projections
24.14 Additional Exercises
24.15 Appendix: Dependent and Independent Rates
24.16 Recommended Reading
25 Actuarial Risk Theory – An Introduction: Collective and Individual Risk Models
25.1 Introduction
25.2 Collective Risk Model
25.3 Poisson Compound Collective Risk Model
25.4 Applications of the Model
25.4.1 Setting Appropriate Reserves and Premium Pricing
25.4.2 Increasing the Number of Independent Policies
25.4.3 Adopting a Normal Distribution Approximation
25.4.4 Return on Capital
25.4.5 Skewness of the Compound Poisson Model
25.4.6 Sum of Compound Poisson Distributions
25.5 Compound Binomial Collective Risk Model
25.6 Compound Negative Binomial Distribution
25.7 Panjer’s Recursion Formula
25.8 Closing Thoughts on Collective Risks Models
25.9 Individual Risk Model
25.9.1 Standard Individual Risk Model
25.9.2 Alternative Model – ‘The Poisson Individual Risk Model’
25.10 Issues with Heterogeneity
25.11 Policies Which Are Not Independent
25.12 Incorporating Parameter Uncertainty in the Models
25.13 Claim Amount Distributions: Alternatives to the Gamma Distribution
25.14 Conclusions
25.15 Recommended Reading
26 Collective Risk Models: Exercise
26.1 Introduction
26.2 Analysis of Claims Data
26.3 Running Simulations
26.4 Tails of the Distribution
26.5 Allowing for Parameter Uncertainty
26.6 Conclusions
26.7 Recommended Reading
27 Generalised Linear Models: Poisson Regression
27.1 Introduction
27.2 Examples/Exercises/Data
27.3 Brief Recap on Multiple Linear Regression
27.4 Generalised Linear Models (“GLMs”)
27.5 Goodness of Fit of GLMs
27.6 Poisson Regression
27.6.1 Introduction
27.6.2 Using Poisson Regression to Model Claim Numbers
27.7 Data with Varying Exposure Periods
27.7.1 Claim Rates and the Offset
27.7.2 Application to Aggregated Data in Section 27.1
27.8 Categorical and Continuous Variables
27.8.1 Problem with Continuous Variables
27.8.2 Categorical Variables
27.9 Interaction between Variables
27.10 Over-dispersion
27.11 Miscellaneous Exercises
27.12 Further Study / Next Steps
27.13 Recommended Reading
28 Extreme Value Theory
28.1 Introduction
28.2 Why Use EVT?
28.3 Generalised Pareto Distribution – “GPD”
28.4 EVT Analysis of Historic Daily Equity Market Returns (S&P 500)
28.4.1 Basic EVT Analysis
28.4.2 Will a Normal Distribution (and Other Alternatives) Do Just as Well?
28.5 Data for Further EVT Analysis
28.6 Recommended Reading
29 Introduction to Machine Learning: k-Nearest Neighbours (kNN)
29.1 Introduction
29.2 Example 1 – Identifying a Fruit Type
29.2.1 Data
29.2.2 Overview of the Process
29.2.3 How does the kNN Algorithm Work?
29.2.4 Normalising Our Data
29.2.5 Varying k
29.2.6 Using Our Model
29.3 Analysis of Our Model – the Confusion Matrix
29.4 Example 2 – Cancer Diagnoses
29.5 Conclusion
29.6 Recommended Reading
30 Time Series Modelling in R – Dr A Kume
30.1 Introduction
30.2 Linear Regression Versus Autoregressive Model
30.3 Three Components for Time Series Modelling
30.4 Stationarity
30.5 Main Tools in R for ARIMA Modelling
30.5.1 PACF as a Derivation of ACF and Their General Behaviour for ARMA(p,q) Models
30.5.2 How to Simulate and Obtain the Theoretical Values of ACF and PACF for ARMA Models
30.6 Identifying a Set Possible Models to the Data Including the Order of Differencing
30.6.1 Model Fitting to Time Series Data
30.6.2 Parameter Estimation for Pure Auto-Regressive Models
30.6.3 Diagnostic Plots
30.6.4 Forecasting
30.7 Dealing with Real Data far from Stationary
30.7.1 Non Parametric Approaches
30.7.2 Airline Data Modelling Using Multiplicative Seasonal Models
30.8 Recommended Reading
31 Volatility Models – GARCH
31.1 Introduction
31.2 Why Use GARCH Models?
31.3 Outline of the Chapter
31.4 Key Theoretical Concepts with GARCH
31.5 Simulation of Data Using a GARCH Model
31.6 Fitting a GARCH Model to Data, and Analysis
31.6.1 Fitting a GARCH Model
31.6.2 Further Analysis of the Data; Comparison with the Normal Distribution
31.6.3 Further Analysis of the Data; Volatility Clustering
31.7 A Note on Correlation and Dependency
31.8 GARCH Long-Term Variance
31.9 Exercise: Shocks to Global Equity Markets – The Global Financial Crisis 2008, and COVID-19
31.10 Extensions to the GARCH Model
31.11 Appendix – A Mixture of Normal Distributions
31.12 Recommended Reading
32 Modelling Future Stock Prices Using Geometric Brownian Motion: An Introduction
32.1 Introduction
32.1.1 Discrete Gaussian Random Walk
32.2 Geometric Brownian Motion
32.3 Applications of GBM, and Simulating Prices
32.4 Recommended Reading
33 Financial Options: Pricing, Characteristics, and Strategies
33.1 Introduction
33.2 What is a Financial Option?
33.3 What are Financial Options Used for?
33.4 Black, Scholes and Merton Differential Equation
33.4.1 Assumptions Underlying B-S-M Formulation
33.4.2 Solution to B-S-M Equation for European Call Options
33.4.3 Call Option Price Function
33.5 Calculating the Option Price Using Simulations
33.6 Factors Which Affect the Price of a Call Option
33.6.1 Share Price
33.6.2 Time to Expiry
33.6.3 Combined Effect of Share Price and Time to Expiry
33.6.4 Other Factors
33.7 Greeks
33.8 Volatility of Call Option Positions
33.9 Put Options
33.10 Delta Hedging
33.11 Sketch of the B-S-M Derivation
33.12 Further Tasks
33.13 Appendix
33.14 Recommended Reading
Index
End User License Agreement

Polecaj historie