Chapter 5 Aggregate Loss Models

Chapter Description

This chapter introduces probability models for describing the aggregate (total) claims that arise from a portfolio of insurance contracts. We present two standard modeling approaches, the individual risk model and the collective risk model. Further, we discuss strategies for computing the distribution of the aggregate claims, including exact methods for special cases, recursion, and simulation. Finally, we examine the effects of individual policy modifications such as deductibles, coinsurance, and inflation, on the frequency and severity distributions, and thus on the aggregate loss distribution.

5.1 Introduction


In this section, we learn how to:

  • Record aggregate losses from an insurance system.
  • Identify actuarial applications of aggregate loss models.

Video: Introduction to Aggregate Loss Models

Overheads: Introduction to Aggregate Loss Models (Click Tab to View)

Hide
Hide
Hide
Hide
Hide

5.2 Individual Risk Model


In this section, we learn how to:

  • Build an individual risk model for a portfolio of insurance contracts.
  • Apply the individual risk model to life and nonlife insurance.
  • Compute the distribution of aggregate losses from an individual risk model.

Video: Individual Risk Model

Overheads: Individual Risk Model (Click Tab to View)

Hide
Hide
Hide
Hide
Hide
Hide

5.2.1 Exercise. Simulating Claim Frequency

Assignment Text

In this assignment, we revisit Example 5.2.1 of Loss Data Analytics using simulation in R. Even though we “officially” start to work with simulation beginning in Chapter 6 in Loss Data Analytics, this exercise provides you with an opportunity to taste a flavor of simulation.

Instructions

  • Use the function rbinom() to simulate the number of claims that follow binomial distribution. For reproducibility of results set the initial ‘seed’ as 1000 and then generate 10,000 observations of claim frequencies for each group of policies.
  • Specify the size and probability when you simulate the number of claims with binomial distribution in each group of policies.


eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiJzZXQuc2VlZCg/PykgICAgIFxuciAgPC0gPz8gICAgICAgICBcbnExIDwtIDAuMDUgICAgICAgICBcbnEyIDwtID8/ICAgICAgICAgXG5uMSA8LSA/PyAgICAgICAgIFxubjIgPC0gMjAwICAgICAgICAgIFxuXG5uMSA8LSByYmlub20ociwgc2l6ZT1uMSwgcHJvYj1xMSkgXG5uMiA8LSByYmlub20ociwgc2l6ZT0/PywgcHJvYj0/PykgIiwic29sdXRpb24iOiJzZXQuc2VlZCgxMD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QgcmVtb3ZlIGBuMmAhXCIpICU+JSBjaGVja19lcXVhbChpbmNvcnJlY3RfbXNnPW4ybXNnKVxuc3VjY2Vzc19tc2coXCJHb29kIGpvYiEgSW4gQ2hhcHRlciAyLCB3ZSByZXZpZXdlZCBmcmVxdWVuY3kgbW9kZWxpbmcuIFRoaXMgdHV0b3JpYWwgaW50cm9kdWNlcyBzaW11bGF0aW9uIHRlY2huaXF1ZXMgdGhhdCBjb25zaWRlcmFibHkgZW5oYWNlIHRoZSBicmVhZHRoIG9mIGFjdHVhcmlhbCBhcHBsaWNhdGlvbnMuIEFzIG5vdGVkIGluIHRoZSBhc3NpZ25tZW50IHRleHQgb2YgdGhpcyB0dXRvcmlhbCwgeW91IGNhbiBjaGVjayBvdXQgQ2hhcHRlciA2IG9mIExvc3MgRGF0YSBBbmFseXRpY3MgZm9yIG1vcmUgYmFja2dyb3VuZCBvbiBzaW11bGF0aW9uIHRlY2huaXF1ZXMuXCIpIiwiaGludCI6Ik5vdGUgdGhhdCBudW1iZXIgb2YgcG9saWNpZXMgYW5kIHByb2JhYmlsaXR5IG9mIGNsYWltIHBlciBwb2xpY3kgY29ycmVzcG9uZCB0byB0aGUgc2l6ZSBhbmQgaW5jaWRlbmNlIHByb2JhYmlsaXR5IGluIGEgYmlub21pYWwgZGlzdHJpYnV0aW9uIGZvciBhIGNsYWltIGZyZXF1ZW5jeSByYW5kb20gdmFyaWFibGUuIn0=

5.2.2 Exercise. Individual Risk Model

Assignment Text

Continuing our work in Exercise 5.2.1, we now reproduce the theoretical results for calculation of moments in individual risk models using simulation. We use the simulated frequencies for each group of policies from Exercise 5.2.1 and continue to simulate compound losses in an individual risk model as elaborated in Section 5.4.2 on Loss Data Analytics.

Instructions

  • Use the function runif() to claim amounts that follow uniform distribution for each group of policies.
  • Specify the parameters of uniform random variables, minimum and maximum values of claim amounts from each type of policies.
  • Initialize an empty vector of length \(R\) to store \(R\) observations of simulated \(S\). In each simulation, calculate the \(j\)th simulated aggregate loss and store it in \(j\)th component of \(S\) for \(j=1, \ldots, R\).
  • Compute the mean and variance of \(S\) by simulation using mean() and var(). Then, compare these values to their corresponding theoretical values, 2,800 and 600,467, respectively.
  • Produce a visual summary of the distribution of \(S\) using the histogram function, hist() .


eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6InNldC5zZWVkKDEwMDApICAgICAjIEZvciByZXByb2R1Y2liaWxpdHkgb2YgcmVzdWx0c1xuciAgPC0gMTAwMDAgICAgICAgICMgTnVtYmVyIG9mIG9ic2VydmF0aW9ucyB0byBzaW11bGF0ZSBmb3IgZWFjaCBwb2xpY3lcbnExIDwtIDAuMDUgICAgICAgICAjIFByb2JhYmlsaXR5IG9mIGNsYWltIGZyb20gcG9saWN5IDFcbnEyIDwtIDAuMDYgICAgICAgICAjIFByb2JhYmlsaXR5IG9mIGNsYWltIGZyb20gcG9saWN5IDJcbm4xIDwtIDEwMCAgICAgICAgICAjIE51bWJlciBvZiB0eXBlIDEgcG9saWNpZXNcbm4yIDwtIDIwMCAgICAgICAgICAjIE51bWJlciBvZiB0eXBlIDIgcG9saWNpZXNcblxubjEgPC0gcmJpbm9tKHIsIHNpemU9bjEsIHByb2I9cTEpICMgR2VuZXJhdGUgcj0xMDAwMCBvYnNlcnZhdGlvbnMgb2YgTjEgZnJvbSBCaW5vbWlhbFxubjIgPC0gcmJpbm9tKHIsIHNpemU9bjIsIHByb2I9cTIpICMgR2VuZXJhdGUgcj0xMDAwMCBvYnNlcnZhdGlvbnMgb2YgTjIgZnJvbSBCaW5vbWlhbCIsInNhbXBsZSI6InNldC5zZWVkKDEwMDApICAgICBcbnIgIDwtIDEwMDAwICAgICAgICBcbm0xIDwtIDA7ICBNMSA8LSA/PyBcbm0yIDwtID8/OyBNMiA8LSAzMDAgXG4jIG4xOiBHZW5lcmF0ZWQgMTAwMDAgb2JzZXJ2YXRpb25zIG9mIGNsYWltIGZyZXF1ZW5jeSBmcm9tIHRoZSBmaXJzdCAgZ3Jv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GlzdHJpYnV0aW9ucyksIGNvbXB1dGluZyB0aGUgZGlzdHJpYnV0aW9uIG9mIGNsYWltcyBTIGlzIGF0IGJlc3QgdGVkaW91cyB3aGVuIHVzaW5nIGV4YWN0IG1ldGhvZHMuIFlvdSBjYW4gdXNlIHNpbXVsYXRpb24gdG8gcXVpY2tseSBnZXQgYSBzZW5zZSBvZiB0aGUgZGlzdHJpYnV0aW9uIGFuZCB1c2UgdGhpcyBhcyBhIGJhc2UgYXMgeW91IGFkanVzdCB0aGUgbW9kZWwgdG8gbWFrZSBpdCBiZXR0ZXIgc3VpdGVkIHRvIGFwcHJveGltYXRlIGFuIGFjdHVhcmlhbCBhcHBsaWNhdGlvbiBvZiBpbnRlcmVzdC5cIikiLCJoaW50IjoiTm90ZSB0aGF0IHdlIG5lZWQgdG8gc2ltdWxhdGUgY2xhaW0gYW1vdW50cyB0aGF0IGNvcnJlc3BvbmRzIHRvIHRoZSBudW1iZXIgb2YgY2xhaW1zIHNvIHRoYXQgdGhlIHBhcmFtZXRlcnMgdXNlZCBpbiA8Y29kZT5ydW5pZigpPC9jb2RlPiBmdW5jdGlvbiBtYXkgZGVwZW5kIG9uIHRoZSBudW1iZXIgb2Ygc2ltdWxhdGVkIGNsYWltcy4ifQ==

5.3 Fitting a Collective Risk Model in Two Parts


In this section, we learn how to:

  • Build a collective risk model for a portfolio of insurance contracts.
  • Calculate the mean and variance of the aggregate loss.
  • Fit frequency and severity components in a collective risk model.

Video: Fitting a Two Part Collective Risk Model

Overheads: Fitting a Two Part Collective Risk Model (Click Tab to View)

Hide
Hide
Hide
Hide
Hide

5.3.1 Exercise. Estimating the Frequency Component of the Collective Risk Model

Assignment Text

In this exercise and the next, you will use data to calibrate the frequency and severity components of the collective risk model. The basis for this is the LGPIF data, introduced in Section 1.3 and used in many exercises.

In this assignment, we fit a negative binomial distribution to the frequency component. You may wish to review Exercise 2.3.5 where we fit a negative binomial distribution to a small synthetic data set.

Instructions

  • Restrict the sample to the subset of towns and identify the claims counts as a global variable \(N\).
  • Construct a function to determine the negative log-likelihood. Do this based on the probability mass function for the negative binomial distribution, dnbinom(). Note that we use the same parametrization as in the Section 2.2 of Loss Data Analytics so that \(\text{size}=r\) and \(\text{prob}=1/(1+\beta)\).
  • Determine initial, or starting, values for the algorithm using the method of moments.
  • Use the function optim() to find the maximum likelihood estimates of the frequency distribution.
  • Provide a table to compare the empirical distribution to the fitted negative binomial distribution.


eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6Ikluc2FtcGxlIDwtIHJlYWQuY3N2KFwiaHR0cHM6Ly9yYXcuZ2l0aHVidXNlcmNvbnRlbnQuY29tL09wZW5BY3RUZXh0cy9MREFDb3Vyc2UxL21haW4vRGF0YS9JbnNhbXBsZS5jc3ZcIiwgaGVhZGVyPVQsIG5hLnN0cmluZ3M9YyhcIi5cIiksIHN0cmluZ3NBc0ZhY3RvcnM9RkFMU0UpXG5saWJyYXJ5KGxtZTQpIiwic2FtcGxlIjoiSW5zYW1wbGVUb3duIDwtIHN1YnNldChJbnNhbXBsZSwgVHlwZVRvd249PTEpXG5OIDwtIEluc2FtcGxlVG93biRGcmVxXG4jIFRoZSBuZWdhdGl2ZSBMb2cgLSBsaWtlbGlob29kXG5mcmVxX2xpayA8LSBmdW5jdGlvbihwYXJtKSB7XG4gIHIgICAgPC0gcGFybVsxXVxuICBiZXRhIDwtIHBhcm1bMl1cbiAgbGlrICA8LSAtc3VtKGRuYmlub20oTiwgc2l6ZT0gPz8sIHByb2I9MS8oMSs/PyksIGxvZz1UUlVFKSlcbiAgcmV0dXJuKGxpaykgfVxuIyBpbml0aWFsIGVzdGltYXRlcyBieSBtZXRob2Qgb2YgbW9tZW50c1xuaW5pdC5wYXJtLmZyZXEgPC0gYyhtZWFuKE4pLyh2YXIoTikvbWVhbihOKS0xKSwgdmFyKE4pL21lYW4oTiktMSkgXG4jIE1heGltdW0gbGlrZWxpaG9vZCBlc3RpbWF0aW9uIGZvciB0aGUgZn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XMuXCIpIiwiaGludCI6IlNpbmNlIDxlbT5vcHRpbTwvZW0+IG1pbmltaXplcyBnaXZlbiBmdW5jdGlvbiBieSBkZWZhdWx0LCB3ZSBwdXQgdGhlIG5lZ2F0aXZlIGxpa2VsaWhvb2QgYXMgYW4gb2JqZWN0aXZlIGZ1bmN0aW9uIHRvIGJlIG1pbmltaXplZC4ifQ==

5.3.2 Exercise. Estimating the Severity Component of the Collective Risk Model

Assignment Text

This is a continuation of Exercise 5.3.1. In this assignment, we fit a Pareto distribution to the severity component. You may wish to review Exercise 3.5.1 where we fit a Pareto distribution to a different subset of the LGPIF data.

Instructions

  • Restrict the sample to the subset of towns and identify the average claim severity as a global variable \(Xbar\).
  • Construct a function to determine the negative log-likelihood. Do this based on the probability density function dlomax(). Note that we use the same parametrization as in the Section 3.2 of Loss Data Analytics so that \(\text{shape}=\alpha\) and \(\text{scale}=\theta\).
  • Determine initial, or starting, values for the algorithm using the method of moments.
  • Use the function optim() to find the maximum likelihood estimates of the severity distribution.
  • Provide a \(qq\) plots of the empirical versus fitted quantiles to assess the adequacy of the model fit.


eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6Ikluc2FtcGxlIDwtIHJlYWQuY3N2KFwiaHR0cHM6Ly9yYXcuZ2l0aHVidXNlcmNvbnRlbnQuY29tL09wZW5BY3RUZXh0cy9MREFDb3Vyc2UxL21haW4vRGF0YS9JbnNhbXBsZS5jc3ZcIiwgaGVhZGVyPVQsIG5hLnN0cmluZ3M9YyhcIi5cIiksIHN0cmluZ3NBc0ZhY3RvcnM9RkFMU0UpXG5saWJyYXJ5KGxtZTQpXG5cbmRsb21heCA8LSBmdW5jdGlvbih4LCBzaGFwZT0xLCBzY2FsZT0xLCBsb2c9RkFMU0UpIHtcbiAgaWYgKGxvZz09RkFMU0UpIHJlc3VsdCA8LSBzaGFwZSpzY2FsZV5zaGFwZSAvIChzY2FsZSArIHgpXihzaGFwZSsxKVxuICBpZiAobG9nPT1U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GNoZWNrX2VxdWFsKGluY29ycmVjdF9tc2c9c2V2X21vZG1zZylcbnN1Y2Nlc3NfbXNnKFwiQ2hlZXJzISBUaGlzIGV4ZXJjaXNlIHByb3ZpZGVzIGEgcXVpY2sgb3ZlcnZpZXcgb2YgdGhlIGltcG9ydGFudCBzdGVwcyBuZWVkZWQgZm9yIHNldmVyaXR5IG1vZGVsaW5nLiBOb3RlIHRoYXQgaXQgaXMgdXN1YWwgdG8gb2JzZXJ2ZSB0aGF0IGFuIGFjdHVhbCBjbGFpbXMgZGF0YXNldCBtYXkgY29tZSB3aXRoIHZlcnkgZmV3IHBvc2l0aXZlIGNsYWltcyBidXQgYWxzbyBoZWF2eSB0YWlsIGJlaGF2aW9yLlwiKSIsImhpbnQiOiJTaW5jZSA8ZW0+b3B0aW08L2VtPiBtaW5pbWl6ZXMgZ2l2ZW4gZnVuY3Rpb24gYnkgZGVmYXVsdCwgd2UgcHV0IHRoZSBuZWdhdGl2ZSBsaWtlbGlob29kIGFzIGFuIG9iamVjdGl2ZSBmdW5jdGlvbiB0byBiZSBtaW5pbWl6ZWQuIn0=

5.4 Computation Strategies for a Collective Risk Model


In this section, we learn how to:

  • Compute the aggregate loss distribution.
  • Implement numerical strategies in R.

Video: Computing a Collective Risk Model Distribution

Overheads: Computing a Collective Risk Model Distribution (Click Tab to View)

Hide
Hide
Hide
Hide
Hide
Hide

xx ### Exercise. Collective Risk Model II

Assignment Text

In this assignment, we analyze the aggregate loss distribution by combining fitted frequency and severity components. Because it is usually very hard to obtain a closed form expression for the distribution function of the compound loss in a collective risk model, we rely on simulations to compute the aggregate loss distribution. We use the fitted parameters from the exercises in Section 5.3. Specifically, fitted shape and scale negative binomial parameters have already been recorded as the global variables r.est and beta.est, respectively. In the same way, fitted shape and scale Pareto parameters have already been recorded as the global variables alpha.est and theta.est, respectively.

Instructions

  • As an input to calculating the aggregate distribution, we need the expression function. This function allows to call functions without actually evaluating them.
  • Also, to generate random samples from the fitted frequency and severity models, we use rnbinom() and rlomax(). Note that these generated random samples are also used as inputs to compute the aggregate claim amount distribution.
  • We use aggregateDist() to simulate compound losses that match the observed data upon the fitted frequency and severity models.
  • With the aggregate distribution, plot its distribution function. For comparison, superimpose on this plot the empiricial (cumulative) distribution function, created using the ecdf() function.


eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6Ikluc2FtcGxlIDwtIHJlYWQuY3N2KFwiaHR0cHM6Ly9yYXcuZ2l0aHVidXNlcmNvbnRlbnQuY29tL09wZW5BY3RUZXh0cy9MREFDb3Vyc2UxL21haW4vRGF0YS9JbnNhbXBsZS5jc3ZcIiwgaGVhZGVyPVQsIG5hLnN0cmluZ3M9YyhcIi5cIiksIHN0cmluZ3NBc0ZhY3RvcnM9RkFMU0UpXG5saWJyYXJ5KGxtZTQpXG5cbmFnZ3JlZ2F0ZS5wb3J0Zm9saW8gPC0gZnVuY3Rpb24oeCwgYnkgPSBuYW1lcyh4JG5vZGVzKSwgRlVOID0gc3VtLFxuICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBjbGFzc2lmaWNhdGlvbiA9IFRSVUUsIHByZWZpeCA9IE5VTEwsIC4uLilcbntcbiAgICBsZXZlbC5uYW1lcyA8LSBuYW1lcyh4JG5vZGVzKSAgICAgICAjIGxldmVsIG5hbWVzXG4gICAgbmxldmVscyA8LSBsZW5ndGgobGV2ZWwubmFtZXMpICAgICAgIyBudW1iZXIgb2YgbGV2ZWxzXG4gICAgeWVhcnMgPC0gbGV2ZWwubmFtZXNbbmxldmVsc10gICAgICAgIyBuYW1lIG9mIGxhc3QgbGV2ZWxcblxuICAgICMjIE1hdGNoIGxldmVsIG5hbWVzIGluICdieScgdG8gdGhvc2UgaW4gdGhlIG1vZGVsXG4gICAgYnkgPC0gbWF0Y2guYXJnKGJ5LCBsZXZlbC5uYW1lcywgc2V2ZXJhbC5vayA9IFRSVUUpXG5cbiAgICAjIyBWZXJzaW9uIG9mIEZVTiBhYmxlIHRvIHdvcm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InBvaXNzb25cIixcbiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgXCJnZW9tZXRyaWNcIixcbiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgXCJuZWdhdGl2ZSBiaW5vbWlhbFwiLFxuICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBcImJpbm9taWFsXCIsXG4gICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIFwibG9nYXJpdGhtaWNcIixcbiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgXCJ6ZXJvLXRydW5jYXRlZCBwb2lzc29uXCIsXG4gICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIFwiemVyby10cnVuY2F0ZWQgZ2VvbWV0cmljXCIsXG4gICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIFwiemVyby10cnVuY2F0ZWQgbmVnYXRpdmUgYmlub21pYWxcIixcbiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgXCJ6ZXJvLXRydW5jYXRlZCBiaW5vbWlhbFwiLFxuICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBcInplcm8tbW9kaWZpZWQgbG9nYXJpdGhtaWNcIixcbiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgXCJ6ZXJvLW1vZGlmaWVkIHBvaXNzb25cIixcbiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgXCJ6ZXJvLW1vZGlmaWVkIGdlb21ldHJpY1wiLFxuICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBcInplcm8tbW9kaWZpZWQgbmVnYXRpdmUgYmlub21pYWxcIixcbiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgXCJ6ZXJvLW1vZGlmaWVkIGJpbm9taWFsXCIpKVxuICAgICAgICAgICAgRlVOIDwtIHBhbmplcihmeCA9IG1vZGVsLnNldiwgZGlzdCA9IGRpc3QsIHAwID0gcDAsXG4gICAgICAgICAgICAgICAgICAgICAgICAgIHguc2NhbGUgPSB4LnNjY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lmZmVyZW50IGluICdub2RlcycsICdtb2RlbC5mcmVxJyBhbmQgJ21vZGVsLnNldidcIilcbiAgICAgICAgfVxuICAgIH1cbiAgICBlbHNlXG4gICAge1xuICAgICAgICBpZiAoaGFzLnNldilcbiAgICAgICAge1xuICAgICAgICAgICAgaWYgKCFpZGVudGljYWwobGV2ZWwubmFtZXMsIHNldi5uYW1lcykpXG4gICAgICAgICAgICAgICAgc3RvcChcImxldmVsIG5hbWVzIGRpZmZlcmVudCBpbiAnbm9kZXMnLCAnbW9kZWwuZnJlcScgYW5kICdtb2RlbC5zZXYnXCIpXG4gICAgICAgIH1cbiAgICAgICAgZWxzZVxuICAgICAgICAgICAgc3RvcChcIm9uZSBvZiAnbW9kZWwuZnJlcScgb3IgJ21vZGVsLnNldicgbXVzdCBiZSBub24tTlVMTFwiKVxuICAgIH1cblxuICAgICMjIFRoZSBmdW5jdGlvbiBpcyB3cml0dGVuIGZvciBtb2RlbHMgd2l0aCBhdCBsZWFzdCB0d28gbGV2ZWxzXG4gICAgIyMgKGVudGl0eSBhbmQgeWVhcikuIElmIHRoZXJlIGlzIG9ubHkgb25lLCBhZGQgYSBkdW1teSBsZXZlbCB0b1xuICAgICMjIGF2b2lkIHNjYXR0ZXJpbmcgdGhlIGNvZGUgd2l0aCBjb25kaXRpb25zLlxuICAgIGlmIChsZW5ndGgobm9kZXMpIDwgMkwpXG4gICAge1xuICAgICAgICBub2RlcyA8LSBjKG5vZGUgPSAxLCBub2RlcylcbiAgICAgICAgbW9kZWwuZnJlcSA8LVxuICAgICAgICAgICAgaWYgKGhhcy5mcmVxKSBjKGV4cHJlc3Npb24obm9kZSA9IE5VTEwpLCBtb2RlbC5mcmVxKSBlbHNlIE5VTExcbiAgICAgICAgbW9kZWwuc2V2IDwtXG4gICAgICAgICAgICBpZiAoaGFzLnNldikgYyhleHByZXNzaW9uKG5vZGUgPSBOVUxMKSwgbW9kZWwuc2V2KSBlbHNlIE5VTExcbiAgICB9XG5cbiAgICAjIyB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aV1dXG5cbiAgICAgICAgICAgICMjIFJlcGVhdCB0aGUgbWl4aW5nIHBhcmFtZXRlcnMgb2YgYWxsIGxldmVscyBhYm92ZSB0aGVcbiAgICAgICAgICAgICMjIGN1cnJlbnQgb25lIHRoYXQgd2VyZSBzaW11bGF0ZWQgaW4gdGhlIHBhc3QuXG4gICAgICAgICAgICBmb3IgKGogaW4gc2VxX2Fsb25nKHBhcmFtcykpXG4gICAgICAgICAgICAgICAgZXZhbChzdWJzdGl0dXRlKHggPC0gcmVwLmludCh4LCBuLmN1cnJlbnQpLFxuICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBsaXN0KHggPSBhcy5uYW1lKHBhcmFtc1tbal1dKSkpKVxuXG4gICAgICAgICAgICAjIyBTaW11bGF0ZSBkYXRhIG9ubHkgaWYgdGhlcmUgaXMgYSBtb2RlbCBhdCB0aGUgY3VycmVudFxuICAgICAgICAgICAgIyMgbGV2ZWwuXG4gICAgICAgICAgICBpZiAoIWlzLm51bGwoQ2FsbCkpXG4gICAgICAgICAgICB7XG4gICAgICAgICAgICAgICAgIyMgQWRkIHRoZSBudW1iZXIgb2YgdmFyaWF0ZXMgdG8gdGhlIGNhbGwuXG4gICAgICAgICAgICAgICAgQ2FsbCRuIDwtIHN1bShuLmN1cnJlbnQpXG5cbiAgICAgICAgICAgICAgICAjIyBTaW11bGF0aW9uIG9mIHRoZSBtaXhpbmcgcGFyYW1ldGVycyBvciB0aGUgZGF0YS4gSW5cbiAgICAgICAgICAgICAgICAjIyB0aGUgbGF0dGVyIGNhc2UsIHN0b3JlIHRoZSByZXN1bHRzIGluIGEgZml4ZWRcbiAgICAgICAgICAgICAgICAjIyB2YXJpYWJsZSBuYW1lLlxuICAgICAgICAgICAgICAgIGlmIChpIDwgbmxldmVscylcbiAgICAgICAgICAgICAgICB7XG4gICAgICAgICAgICAgICAgICAgIGFzc2lnbihsZXZlbC5uYW1lc1tbaV1dLCBldmFsKENhbGwpKVxuICAgICAgICAgICAgICAgICA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xuICAgICAgICAgICAgQ2FsbCA8LSBtb2RlbC5zZXZbW2ldXVxuXG4gICAgICAgICAgICBmb3IgKGogaW4gc2VxX2Fsb25nKHBhcmFtcykpXG4gICAgICAgICAgICAgICAgZXZhbChzdWJzdGl0dXRlKHggPC0gcmVwLmludCh4LCBuLmN1cnJlbnQpLFxuICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBsaXN0KHggPSBhcy5uYW1lKHBhcmFtc1tbal1dKSkpKVxuXG4gICAgICAgICAgICBpZiAoIWlzLm51bGwoQ2FsbCkpXG4gICAgICAgICAgICB7XG4gICAgICAgICAgICAgICAgIyMgVGhlIHJlc3Qgb2YgdGhlIHByb2NlZHVyZSBkaWZmZXJzIGRlcGVuZGluZyBpZiB3ZVxuICAgICAgICAgICAgICAgICMjIGFyZSBzdGlsbCBzaW11bGF0aW5nIG1peGluZyBwYXJhbWV0ZXJzIG9yIGNsYWltXG4gICAgICAgICAgICAgICAgIyMgYW1vdW50cy5cbiAgICAgICAgICAgICAgICBpZiAoaSA8IG5sZXZlbHMpXG4gICAgICAgICAgICAgICAge1xuICAgICAgICAgICAgICAgICAgICAjIyBTaW11bGF0aW9uIG9mIG1peGluZyBwYXJhbWV0ZXJzIGlzIGlkZW50aWNhbCB0byB0aGVcbiAgICAgICAgICAgICAgICAgICAgIyMgc2ltdWxhdGlvbiBvZiBmcmVxdWVuY2llcy5cbiAgICAgICAgICAgICAgICAgICAgQ2FsbCRuIDwtIHN1bShuLmN1cnJlbnQpXG4gICAgICAgICAgICAgICAgICAgIGFzc2lnbihsZXZlbC5uYW1lc1tbaV1dLCBldmFsKENhbGwpKVxuICAgICAgICAgICAgICAgICAgICBwYXJhbXNbaV0gPC0gbGV2ZWwubmFtZXNbW2ldXVxuICAgICAgICAgICAgICAgIH1cbiAgICAgICAgICAgICAgICBlbHNlXG4gICAgICAgICAgICAgICAge1xuICAgICAgICAgICAgICAgICAgICAjIyBGb3IgdGhlIHNpbXVsYXRpb24gb2YgY2xhaW0gYW1vdW50cywgdGhlIG51bWJlclxuICAgICAgICAgICAgICAgICAgICAjIyBvZiB2YXJpYXRlcyBpcyByYXRoZXIgZ2l2ZW4gYnkgdGhlXG4gICAgICAgICAgICAgICAgICAgICMjICdmcmVxdWVuY2llcycgb2JqZWN0LiBGdXJ0aGVybW9yZSwgdGhlIG1peGluZ1xuICAgICAgICAgICAgICAgICAgICAjIyBwYXJhbWV0ZXJzIG11c3QgYmUgcmVjeWNsZWQgb25jZSBtb3JlIHRvIG1hdGNoXG4gICAgICAgICAgICAgICAgICAgICMjIHRoZSB2ZWN0b3Igb2YgZnJlcXVlbmNpZXMuXG4gICAgICAgICAgICAgICAgICAgIGZvciAocCBpbiBpbnRlcnNlY3QoYWxsLnZhcnMoQ2FsbCksIHBhcmFtcykpXG4gICAgICAgICAgICAgICAgICAgICAgICBldmFsKHN1YnN0aXR1dGUoeCA8LSByZXAuaW50KHgsIGZyZXF1ZW5jaWVzKSxcbiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBsaXN0KHggPSBhcy5uYW1lKHApKSkpXG4gICAgICAgICAgICAgICAgICAgIENhbGwkbiA8LSBzdW0oZnJlcXVlbmNpZXMpXG4gICAgICAgICAgICAgICAgICAgIHNldmVyaXRpZXMgPC1ldmFsKENhbGwpXG4gICAgICAgICAgICAgICAgfVxuICAgICAgICAgICAgfVxuICAgICAgICB9XG4gICAgfVxuICAgIGVsc2VcbiAgICAgICAgc2V2ZXJpdGllcyA8LSByZXAuaW50KDEsIHN1bShmcmVxdWVuY2llcykpXG5cbiAgICAjIyBXZSBtdXN0IG5vdyBkaXN0cmlidXRlIHRoZSBjbGFpbSBhbW91bnRzIGluIHZlY3RvciAnc2V2ZXJpdGllcydcbiAgICAjIyB0byB0aGUgYXBwcm9wcmlhdGUgbm9kZXMuIFRoaXMgaXMgY29tcGxpY2F0ZWQgYnkgdGhlXG4gICAgIyMgcG9zc2liaWxpdHkgdG8gaGF2ZSBkaWZmZXJlbn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ZiBjb21wb3VuZCBsb3NzZXMgYW5kIG1ha2Ugc3Vic2VxdWVudCBkZWNpc2lvbnMuXCIpIiwiaGludCI6IldlIHNob3VsZCB1c2UgdGhlIGVzdGltYXRlZCBwYXJhbWV0ZXJzIGZyb20gdGhlIGZyZXF1ZW5jeSBhbmQgc2V2ZXJpdHkgZGlzdHJpYnV0aW9ucyBpbiBjYWxjdWxhdGlvbiBvZiB0aGUgYWdncmVnYXRlIGNsYWltIGFtb3VudCBkaXN0cmlidXRpb24uIn0=

5.5 Tweedie Distribution

In this section, we learn how to:

  • Construct the Tweedie distribution from a collective risk model.
  • Establish the Tweedie distribution as a member of the exponential family of distributions.
  • Fit Tweedie distribution as a generalized linear model.

Video: Tweedie Distribution

Overheads: Tweedie Distribution (Click Tab to View)

Hide
Hide
Hide
Hide
Hide

5.5.1 Exercise. Fitting a Tweedie Distribution

Assignment Text

In this assignment, we fit a Tweedie model for a collective risk model using observed compound losses in LGPIF data with R. Unlike the previous tutorials that required separate model fitting for the frequency and severity components, use of the Tweedie distribution enables us to fit a collective risk model in a single step.

Instructions

  • Restrict the sample to the subset of towns and identify the total claim severity as a global variable \(S\).
  • Construct a function to determine the negative log-likelihood. Do this based on the hybrid probability mass/density function dtweedie(). This is a built-in function from R library tweedie .
  • From the fitted Tweedie model via optim(), extract the estimated mu, phi, and p.
  • After estimating the parameters of the Tweedie model, plot the empirical quantiles against the quantiles from the fitted model. Note that we generate the fitted quantiles via qtweedie() and the estimated Tweedie parameters.


eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6Ikluc2FtcGxlIDwtIHJlYWQuY3N2KFwiaHR0cHM6Ly9yYXcuZ2l0aHVidXNlcmNvbnRlbnQuY29tL09wZW5BY3RUZXh0cy9MREFDb3Vyc2UxL21haW4vRGF0YS9JbnNhbXBsZS5jc3ZcIiwgaGVhZGVyPVQsIG5hLnN0cmluZ3M9YyhcIi5cIiksIHN0cmluZ3NBc0ZhY3RvcnM9RkF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IGZldyBwYXJhbWV0ZXJzLiBUaGlzIG1lYW5zIGl0IG1heSBub3QgZml0IGFzIHdlbGwgZm9yIHNvbWUgZGF0YXNldHMsIGVzcGVjaWFsbHkgdGhvc2VzIHdpdGggbG9uZ2VyIHRhaWxzLiBIb3dldmVyLCB0aGUgZmV3IHBhcmFtZXRlcnMgbWVhbnMgdGhhdCBpdCBpcyBlYXNpZXIgdG8gaW50cmVwcmV0LiBGdXJ0aGVyLCB0aGlzIHNpbXBsaWNpdHkgb2YgcGFyYW1ldGVycyBjYW4gYmVjb21lIHZlcnkgaW1wb3J0YW50IHdoZW4gaW50cm9kdWNpbmcgY292YXJpYXRlcy9yYXRpbmcgZmFjdG9ycyBpbnRvIHRoZSBtb2RlbGluZyBwcm9jZXNzLlwiKSIsImhpbnQiOiJGb3IgVHdlZWRpZSBkaXN0cmlidXRpb24sIHdlIGVzdGltYXRlIHRocmVlIHBhcmFtZXRlcnMgKG11LCBwaGksIHApIHRoYXQgZXhwbGFpbiB0aGUgb3ZlcmFsIG1lYW4sIGRpc3BlcnNpb24sIGFuZCBzaGFwZSBvZiB0aGUgZGlzdHJpYnV0aW9uLiJ9

5.6 Effects of Coverage Modifications


In this section, we learn how to:

  • Examine the impact of aggregate deductible on the aggregate loss.
  • Examine the effect of per-occurrence deductible on frequency and severity components in the aggregate loss.

Video: Effects of Coverage Modifications

Overheads: Effects of Coverage Modifications (Click Tab to View)

Hide
Hide
Hide
Hide
Hide

Chapter Contributors

  • Authors. Himchan Jeong, Simon Fraser University, and Peng Shi, University of Wisconsin-Madison, are the principal authors of the initial version of this chapter.
  • Chapter Maintainers. Please contact Himchan and/or Jed at for chapter comments and suggested improvements.