Professional Documents
Culture Documents
Reported as the partial fulfilment of the requirements for the course MAST726/4
1
1 Introduction
We have a portfolio of dwelling. In the year 1981 we received 793 claims where each claim amount
and the floor space(in square meters) are given. The underlying policies are without deductible.
Given the data set our task is to obtain a feasible model for the portfolio. While searching for
models we managed to get some good fit which are not feasible from different point of views. And
here we decided to include only the PP-plots for those “good fit but not feasible” model. We
will describe only one of those non- feasible models. And finally we will talk about two feasible
but not best fit models.
At first we used regression analysis to study the independence of two components of the data
set, namely space data and claim data.
When estimating the parameters for different models we used the utilities included in sophis-
ticated packages of R. Here we should mention that though we tried our best to figure out best
model still we could not try some model because of the failure in optimization while estimating
the parameters.
Our efforts were consistent in getting sensitivity curve, plotting likelihood region for some
parameters, and finally getting box-plot for comparing two feasible models. For each model we
produced graphs of probability density function(PDF) over Histogram, cumulative density func-
tion(CDF), limited expected value(LEV), mean residual life (MRL). We put both the empirical
and theoretical graphs together to get the idea of goodness of fit. The PP-plots we produced
supports, specially, our comments that to get a feasible model we need to sacrifice the best model
we managed to get with infinite mean for the original claims.
In conducting the goodness of fit test for different models , we should mention that, we
run our own routine in R for that job. We carried out Kolmogorov-Smirnov test for individual
data, Chi-square goodness of fit test for grouped data, Anderson-Darling test for heavy tailed
model and Akaike selection criterion for model selection. We also programmed codes for com-
puting Cramer-Von-Moises distances for both individual and grouped data with weights. For
weights we use the mean of each class. All the codes are included in the Appendix.
Talking about the plots of likelihood region we should mention that we fix one parameter to
get the plot in two dimensions. However, at the same time, we produce 3D plots of the likelihood
region to get the idea of optimal region containing the parameters.
Our last strenuous effort is devoted to the calculation of “premium”. We devote a complete
section for this purpose.
2
for x > 0 and I(x) is an indicator function. By definition it is a step function that jumps by
1
n at each observation. Since its is a discrete function the corresponding empirical probability
function is defined as:
n
1X
fn (x) = I[Xi =x] (2)
n
i=1
Ê(X) − Ê(X ∧ d)
ê(d) = (5)
1 − Fn (d)
1 Pn 1 P P
n i=1 Xi − n [ Xi <d Xi + d Xi ≥d 1]
= 1 Pn
1 − n i=1 I[Xi ≤d]
where all the functions on the right hand side are defined earlier.
be the frequency in the interval (cj−1 , cj ] for j = 1, · · · , r. At boundary point cj the empirical
CDF is defined as (possibly with cr = ∞):
j
1X
Fn (cj ) = ni , Fn (cj ) = 0 (7)
n
i=1
where n = rj=1 nj .
P
A smooth version of Fn is obtained, (in order to see whether the derivative exists), by simply
connecting the jump points by straight lines:
0
ifx ≤ co
(c −x)F (c )+(x−c )F (c )
F˜n (x) = j n j−1
(cj −cj−1 )
j−1 n j
ifcj−1 < x ≤ cj (8)
1 ifx > cr
3
Clearly Fn˜(x) is not defined for x > cr−1 if cr = ∞ and nr 6= 0.
The derivative of the above function , if it exists, is known as histogram and is defined as:
0
ifx ≤ co
˜ Fn (cj )−Fn (cj−1 ) nj
fn (x) = (cj −cj−1 ) = n(cj −cj−1 ) ifcj−1 < x ≤ cj (9)
0 ifx > cr
Hence
n
X 1[cj−1 <Xi <cj ]
f˜n (x) = if cj−1 < x < cj .
n(cj − cj−1 )
i=1
The k th sample moment for grouped data can be obtained from empirical CDF as:
Z cr r
0 k
X nj
µˆk = x dFn (x) = ckj (10)
c0 n
j=1
r
X nj (ck+1
j − ck+1
j−1 )
=
n(k + 1)(cj − cj−1 )
j=1
The limited expected value , for cj−1 < d ≤ cj , can be expressed as:
Z d
Ê(X ∧ d) = xdFn (x) + d[1 − Fn (d)] (12)
0
j−1 r
X ci ni X ni
= +d
n n
i=1 i=j
j−1 Z
X ci Z u
= xf˜n (x)dx + xf˜n (x)dx
i=1 ci−1 cj−1
Z cj r
X Z ci
+ uf˜n (x)dx + uf˜n (x)dx
u i=j+1 ci−1
j−1
X ni (ci + ci−1 ) nj (2ucj − c2j−1 − u2 )
= +
2n 2n(cj − cj−1 )
i=1
r
X ni u
+ .
n
i=j+1
4
And then the empirical MRL is defined as:
Ê(x) − Ê(X ∧ d)
ê(d) = (14)
1 − Fn (x)
and
Ẽ(x) − Ẽ(X ∧ d)
ẽ(d) = (15)
1 − F̃n (x)
depending on discontinuous and continuous versions of CDF’s, where all other terms on the
right hand side are defined earlier.
SCn (x; x1 , · · · , xn−1 , Tn ) = n[Tn (x1 , · · · , xn−1 , x) − Tn−1 (x1 , · · · , xn−1 )].
5
Figure 1: Dispersion with outlier
Theorem 3.1 Let X be a continuous random variable with PDF f (x). Let Y = g(X) is a
transformation of X such that Y is a continuous random variable. Then the PDF of Y is given
by:
fX (x)
fY (y) = 0 .
g (x)
Thus if Y = log (X) is the transformation then the new density is given by:
f (log (x))
fY (y) =
x
6
Figure 2: Dispersion without outlier
7
Figure 3: Sensitivity curve with the estimator mean
empirical and theoretical mean we declared inverse gaussian our best model. Because a relevant
and consistent theoretical means is vital for “premium ” calculation.
log y−µ dz 1
To get the closed form we use the transformation σ = z = N (0, 1). So dy = σy . Thus
Z log y−µ
σ 1 1 2
F (x) = √ e− 2 z dz (18)
−∞ 2π
log y − µ
= Φ( )
σ
= Φ(z)
The theoretical LEV for the log-normal model is given by:
Z d
1 1 log x−µ 2 log y − µ
E(X ∧ d) = x. √ e− 2 ( σ ) dx + d[1 − Φ( )] (19)
0 xσ 2π σ
8
Figure 4: Sensitivity curve with the estimator median
To see how it gets into a closed form let us use the transformation
log y−µ
σ = z = N (0, 1). Then x = eµ+zσ which implies dx = σeµ+zσ dz. Then (19) becomes:
log d−µ
log d − µ
Z
µ+ 21 σ 2
σ 1 1 2
E(X ∧ d) = e √ e− 2 (z−σ) dz + d[1 − Φ( )]
−∞ 2π σ
1 2 log d − µ log d − µ
= eµ+ 2 σ P {N (σ, 1) < } + d[1 − Φ( )]
σ σ
1 2 log d − µ log d − µ
= eµ+ 2 σ P {N (0, 1) < − σ} + d[1 − Φ( )]
σ σ
1 log d − µ − σ 2
2 log d − µ
= eµ+ 2 σ Φ( ) + d[1 − Φ( )] (20)
σ σ
With the form of LEV given by (20) the MRL function could be expressed in closed form as
follows:
E(X) − E(X ∧ d)
e(d) = (21)
1 − F (d)
1 2 1 2 2
eµ+ 2 σ − {eµ+ 2 σ Φ( log d−µ−σ
σ ) + d[1 − Φ( log σd−µ )]}
=
1 − Φ( log σd−µ )
Here we mention that E(X) can be easily obtained, from (20) by simply letting d −→ ∞ , as
1 2
E(X) = eµ+ 2 σ
9
Figure 5: Summary statistics with and without outlier
n
∂l(µ, σ 2 ) n 1 X
S2 (µ, σ 2 ) = =− + 3 (ln xi − µ)2
∂σ σ σ
i=1
n
1X
σ̂ 2 = (ln xi − µ̂)2 (23)
n
i=1
We used a package included in R to estimate theses parameters (see the source code in Ap-
pendix). Since the package needs initial values we used Method of Moments(mom) as initial
10
Figure 6: Empirical model tools for log(data)
11
Figure 7: Empirical model tools for the original data
from 793 claims lets say arbitrary large number of times. For each sample we need to estimate
the parameters separately and then if the mean of those estimated parameters are very close to
the real estimate of the parameters and the standard deviation of the estimates coming from
samples is small enough so that the original estimate falls in 95% confidence interval we can say
the parameters are robust.
Also we should mention that we incorporate “Huber estimates”(see Appendix for the codes)
in method of moments estimates which are supplied as the initial value of our MLE estima-
tion, which we believe has a robustifying effect.
There is another way to check the robustness of our parameters.
Now we talk about Variance-Covariance Matrix of log-normal mle’s . To estimate the
Variance-Covariances matrix, we used the Loglikelihood function and got the derivatives.
∂ 2 l(µ, σ 2 ) n
2
=− 2
∂µ σ
n
∂ 2 l(µ, σ 2 ) n 3 X
= − (ln xi − µ)2
∂σ 2 σ2 σ4
i=1
n
∂ 2 l(µ, σ 2 ) 2 X
=− 3 (ln xi − µ)2
∂σ∂µ σ
i=1
12
So the Covariance matrix can be computed as follows:
∂ 2 l(µ,σ 2 )
−1
∂ 2 l(µ,σ 2 ) ∂σ∂µ
∂µ2
∂ 2 l(µ,σ 2 ) −1
∂σ∂µ ∂ 2 l(µ,σ 2 )
∂σ 2
−1 −2 Pn 2
−n σ3 i=1 (ln xi − µ)
= Pn σ2
−2 −1
σ3 i=1 (ln xi − µ)2 n 3 Pn 2
σ2− 4 σ i=1 (ln xi −µ)
0.0000443293 0
=
0 0.00002216616
Hence we can compute a 95% confidence interval around parameters, that is, approximately
1.96 standard deviations on both sides of each estimate:
Evaluating the log-likelihood function using our estimates for µ and σ 2 we obtain:
n n
n X 1 X ln xi − µ
r(µ, σ 2 ) = − (ln σ 2 + ln 2π) − ln xi − ( ) − (−1451.867)
2 2 σ2
i=1 i=1
The set of values of µ and σ 2 such that r(µ, σ 2 ) > ln(p) is called a 100×p Likelihood Region(LR)
for each parameter. In order to show the LR for different values of p (let say 10% and 50%),
we plot LLF in 2-dimensions (fixing one parameter) and in 3-dimensions without fixing any
parameter.
We generated possible values of µ for fixed σ to find the feasible region of µ. In Figure(8)
we can see the plausible region from where we can choose the most likely parameter value for µ
when σ is fixed.
We also generated possible values for µ and σ to find the feasible region around the optimum
values of the parameters. In Figure(9) we can see the plausible surface from where we can choose
the most likely values for µ and σ.
13
Figure 8: Likelihood region of the feasible models
The theoretical LEV for this model , with the density given by (24), can be obtained as:
1 log(log(x))−µ 2
d
1 e− 2 ( )
Z
σ
o
E (X ∧ d) = x √ dx + d[1 − F o (d)] (26)
1 x log(x)σ 2π
14
Figure 9: 3D Likelihood region of log normal model
E o (X) − E o (X ∧ d)
e(d) = (27)
1 − F o (d)
where E o (X) = limd→∞ E o (X ∧ d) and the functions on the right hand side are given above.
15
Figure 10: Log normal model graphs
E(X) − E(X ∧ d)
e(d) = (31)
1 − F (d)
where the functions on the right hand side are defined above.
16
Figure 11: PP-plot of the feasible models
nµ̂2
θ̂ = Pn (xi −µ̂)2
(33)
i=1 xi
17
Figure 12: Log(lognormal) model graphs
We used a package inclueded in R to estimate these parameters (see the source code in Ap-
pendix). Because R needs initial values we used method of moments estimates as initial values
which are given by: Pn
xi
µ̂mom = i=1
n
µ̂3mom
θ̂mom = Pn
x2i
i=1
n − µ̂2mom
18
the Variance-Covariances matrix, we used the Loglikelihood function and got the derivatives.
∂ 2 l(µ, θ) n
=− 2
∂2θ 2θ
Pn
∂ 2 l(µ, θ) xi − nµ
= i=1 3
∂θ∂µ µ
0.003028186 −0.0001363386
=
−0.0001363386 132.7429
Hence we can compute a 95% confidence interval around parameters, that is, approximately
1.96 standard deviations on both sides of each estimate:
µ : 8.197649 ± 1.96(0.003028186)1/2 ⇒ µ ∈ (8.089792, 8.305506)
θ : 229.417857 ± 1.96(132.7429)1/2 ⇒ θ ∈ (206.8359, 251.9998)
We can see that our estimates fall in 95% confidence interval.
We know the Log-relative likelihood function is defined as:
Evatuating the log-likelihood function using the estimates of the parameters µ and θ we obtain:
n n
n 3X θ X (xi − µ)2
r(µ, θ) = (ln θ − ln 2π) − ln xi − 2 − (−1451.670)
2 2 2µ xi
i=1 i=1
The set of values of µ and θ such that r(µ, θ) > ln(p) is called a 100×p Likelihood Region(LR)
for each parameter. In order to show the LR for different values of p (let say 10% and 50%),
we plot LLF in 2-dimensions (fixing one parameter) and in 3-dimensions without fixing any
parameter.
We generated possible values for µ and we fixed θ to find the feasible region for µ. In Figure(8)
we can see the plausible region from where we can choose the most likely parameter value for µ
when θ is fixed.
We also generated possible values for µ and θ to find the feasible region around the optimum
parameter values. In Figure(13) we can see the plausible surface from where we can choose the
most likely parameter values for µ and θ.
19
Figure 13: 3D likelihood region for Inverse gaussian model
20
Figure 14: Inverse gaussian model graphs
E o (X) − E o (X ∧ d)
e(d) = (37)
1 − F o (d)
21
Figure 15: log(inverse gaussian) model graphs
22
6.1.2 Parameter estimation
As usual we try to estimate the parameters of Gumble distribution by LogLikelihood Method.
The Loglikelihood function is given , for α R and β > 0, by:
n n
1X X α − xi
l(α, β) = −n ln β + (α − xi ) − exp ( )
β β
i=1 i=1
We have to solve the following equations to get the maximum likelihood estimators (mle’s):
n
X α − xi
exp ( )−n=0
β
i=1
n n
X X α − xi
−nβ + xi − xi exp ( )=0
β
i=1 i=1
Since we are not able to find an explicit form for the estimators, we had to use the package in-
built in R to estimate them numerically. As an initial value we supply the method of moments
estimates. From our experience we learned that supplying method of moments estimates as
initial value sometimes leads to some absurd estimates in R. But fortunately this time the mles
given by R with method of moments estimates as initial value are consistent. We know that
theoretical first and second moments are:
E[X] = α + γβ
1
V [X] = E[X 2 ] − E 2 [X] = (πβ)2
6
So we matched the empirical moments with the theoretical one to get the initial values needed
to get the mle’s:
α̂ = 7.464233 and β̂ = 1.294363
23
Figure 16: Gumble fit for log(data)
24
Figure 17: PP plots of non-feasible models for log(data)
25
Figure 18: PP plots of non-feasible models for original data
k
X
+n (Fn (yj )2 {log (F (yj+1 )) − log (F (yj ))} (39)
1
where yk ’s are class boundaries with t = y0 < y1 < y2 < · · · < yk < yk+1 = u.
The Cramer-Von -Misses distance for the individual data is defined as :
n
1 X 2
Cn (θ̂) = Fn (xi ) − F (xi , θ̂) (40)
n
1
All the above test statistics and the selection criteria are applied on some of our non-feasible
and two feasible models. The results are produced in tabular form in Figure(20). Since we test
the goodness of fit for two feasible models we have to choose better one from these two. Both
the model passed the tests. Now it might have been better to compare the models with some
empirical statistics. But because of computational difficulties most of the theoretical statistics are
26
Figure 19: log(Gumble) fit for the original data
not available to compare. However comparing theoretical and empirical means(See Figure(21))
we can clearly opt for Inverse Gaussian model. And we are more interested in doing so because a
consistent mean is the vital concern of premium calculation. The boxplot(appears in Figure(22))
we produce also strongly supports the choice of inverse Gaussian model. Because, first of all, we
can see from box plot that the claims treated as outliers(the dots outside the whiskers in box
plot )are very few in inverse Gaussian model compare to log-normal or even Gumble. That
means that inverse Gaussian model can accommodate more and more large claims with less
premium(because of smaller mean) than log-normal. Also we know the length of the box, which
is just IQR(inter quartile range), is a good source of comparison for different models. From
this point too we see that the inverse Gaussian quartiles matched better, than the log-normal
quartiles, with the empirical quartiles (two extremes of the box being 1st and 3rd quartiles
and the line inside the box being median). So we believe that our moled selection is strongly
justified.
P = (1 + θ)λ̂E(X) (42)
27
Figure 20: Comparison of different models
where θ is the security loading. In most cases it varies among 1%,2% and 5%. The calculated
premiums with different security loadings appear in Figure(23).
Though we didn’t include in our premium calculation we tried in another way to simulate
the claims arrival time. Since in our data we are missing recording or arrival times of claims we
simulate the arrival times. With those simulated arrival times one can apply the Poisson model
to estimate the parameter λ of the associated Poisson process.
Talking about simulating claim arrival times we should mention the idea we applied. We
simulate 1000 times. For each simulation we generate exponential inter arrival times with fixed
rate, so the arrival times can be treated as Poisson with same rate. The rates, which varies from
one simulation to other, are generated randomly from U (1.17, 3.17). We chose this interval as
we have , originally, 793 claims observed in one year , which means the rate is 793
365 = 2.17. And
we deviated one unit on both sides. Finally we take the mean , of those 1000 simulated arrival
times for each of 793 claims , as the arrival time of each claims.
We come up with some , seemingly, nice codes. We were able to write a code which will
give us , for a sorted vector of claims, the number of repetitions of each claims. We also write
a code to compute ”n.claimsday”, where n = 1, 2 · · · , which gives us , how many days, out of
365 days, we received n (number of) claims. See the source codes in Appendix. A typical output
appears in Figure(24).
28
Figure 21: Final model selection
29
Figure 22: Comparison by box plots
10 Appendix
This section is completely devoted to the source codes . Here basically we put all the codes , in
R, we used for different parameter estimation, various numerical computations and graphical
manipulation etc.
30
Figure 23: Premiums with different security loadings
kurt(func)
quantile(func, c(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9))
func.mu=mean(func)
func.std=sd(func)
func.mu
func.std
31
{(if(x[i] < max(x)) (a[i] <- sum(x[x-x[i] > 0] - x[i])
/(length(x))/(1-func.fn[i])) else a[i] <- 0)}
return(a)}
func.en<-emrl(func.s) plot(func.s, func.en, type="l",
xlab="ln(Losses)", ylab="Limited Expected Value", main="Empirical
MRL of the Individual Ln(Losses)",col="red")
Empirical LEV:
func.lev<-mean(func.s)-func.en*(1-func.fn)
plot(func.s,
func.lev,type="l", xlab="ln(Losses)", ylab="Limited Expected Value",
main="Empirical LEV of the Individual Ln(Losses)",col="red")
Histogram:
n.sim<-10000
t.period<-365
rate<-rep(0,n.sim)
lambda<-rep(0,n.sim)
claimnumbers<-rep(0,t.period)
for (i in 1:n.sim){
lambda[i]<-runif(1,2,2.34)
claimnumbers<-rpois(t.period,lambda[i])
rate[i]<-sum(claimnumbers)/t.period }
rate.estimate<-mean(rate)
rate.estimate
num.claims=rpois(365,rate.estimate)
sum(num.claims)
func<-lnloss
library(stats4)
library (MASS)
library(lattice)
To obtain MLE:
32
#MLE
ll<-function(alpha,beta){n*log(beta)-sum((alpha-y)/beta)+sum(exp((alpha-y)/beta))}
est<-mle(minuslog=ll,start=list(alpha=alpha.mom,beta=beta.mom))
summary(est)
alpha_gumbel=7.464233
beta_gumbel=1.294363
alpha=7.464233
beta=1.294363
#PDF
pdfgumbel=function(x,alpha,beta){(beta^(-1))*exp((alpha-x)/beta)*exp(-exp((alpha-x)/beta))}
pdf7=pdfgumbel(func,alpha,beta) hist(func, prob=T, breaks=hbreak,
col="gray",xlim=c(4,14.5),xlab="ln(losses)", main="Histogram
ln(losses) vs Gumbel Distribution")
lines(func,pdf7,type="o",xpd=T,col="red")
#CDF
cdfgumbel=function(x,alpha,beta) {exp(-exp((alpha-x)/beta))}
alpha=alpha_gumbel
beta=beta_gumbel
cumgumbel=cdfgumbel(func,alpha,beta)
plot(func.s, func.fn, type="s",
xlab="ln(losses)", ylab="F_n", main="Empirical vs Theoretical Gumbel
CDF",col="blue")
lines(func.s, cumgumbel, type="o",col="red")
#LEV
n=length(func)
alpha=alpha_gumbel
beta=beta_gumbel
int=rep(0,793)
levgumbel=rep(0,n)
for(i in 1:n)
{
integrand=
function(z)
{z*((1/beta)*exp((alpha-z)/beta))*exp(-exp((alpha-z)/beta))}
int=integrate(integrand, lower = 0, upper = func[i])
levgumbel[i]=int$va+(func.s[i]*(1-cumgumbel[i]))
}
plot(func.s,
func.lev, type="l", xlab="ln(Losses)", ylab="Limited Expected
Value", main="Empirical-Gumbel LEV of the Ln(Losses)",col="blue")
lines(func.s,levgumbel,type="o",xpd=T,col="red")
#MRL
euler=0.577215665
33
mean.gumbel=alpha+(euler*beta)
mrl3=(mean.gumbel-levgumbel)/(1-cumgumbel)
plot(func.s, func.en,
type="l", xlab="ln(Losses)", ylab="Limited Expected Value",
main="Empirical-Gumbel MRL of the Ln(Losses)",col="blue")
lines(func.s,mrl3,type="o",xpd=T,col="red")
# P-P Plot
qqplot(cumgumbel,func.fn,xlab="CDF Theoretical",
ylab="CDF Empirical",main="P-P Plot Gumbel vs
ln(Losses)",col="blue")
abline(0,1)
# Q-Q Plot and simulating
‘‘Gumble’’
u=runif(793)
simgumbel=alpha-(beta*log(-log(u)))
qgumbel=quantile(simgumbel, probs = func.fn, na.rm = FALSE,names =
TRUE, type = 7) qfunc=quantile(func, probs = func.fn, na.rm =
FALSE,names = TRUE, type = 7)
plot(qgumbel,qfunc,xlab="Theoretical
Quantiles",ylab="Sample
Quantiles",xlim=c(min(func),max(func)),col="red",main="Q-Q Plot
Gumbel vs ln(Losses)")
abline(0,1)
34
# Not Robust MEAN
#mu=2.0859586
#sigma=0.1874896
#Robust MEAN
mu=2.0859586
sigma= 0.1874917
# PDF
pdflognorm=function(x,mu,sigma){(1/(x*sigma*sqrt(2*pi)))*exp(-(((log(x)-mu)/sigma)^2)/2)}
denlognorm=pdflognorm(func,mu,sigma) hist(func,prob=T,breaks=hbreak,
col="gray",xlim=c(4,15),xlab="ln(Losses)", main="Histogram
ln(Losses) vs LogNormal Distribution")
lines(func,denlognorm,type="o",xpd=T,col="red")
# CDF
cdflognorm=function(x,mu,sigma) {pnorm((log(x)-mu)/sigma,0,1)}
cumlognorm=cdflognorm(func,mu,sigma) plot(func.s, func.fn, type="s",
xlab="ln(Losses)", ylab="F_n", main="Empirical vs Theoretical
LogNormal CDF",col="blue")
lines(func.s,cumlognorm,type="o",col="red")
# LEV
int=rep(0,793)
levlognorm=rep(0,n)
for(i in 1:n)
{
integrand=function(z){z*((1/(z*sigma*sqrt(2*pi)))*exp(-(((log(z)-mu)/sigma)^2)/2))}
int=integrate(integrand, lower = 0, upper = y[i])
levlognorm[i]=int$va+(y[i]*(1-cumlognorm[i]))
}
plot(func.s,func.lev, type="l", xlab="ln(Losses)", ylab="Limited
Expected Value", main="Empirical-LogNormal LEV of the
ln(Losses)",col="blue")
lines(func.s, levlognorm,type="o",col="red")
# MRL
mean.lognorm=exp(mu+((sigma^2)/2))
mrllognorm=(mean.lognorm-levlognorm)/(1-cumlognorm)
plot(func.s,func.en, type="l", xlab="ln(Losses)", ylab="Limited
Expected Value", main="Empirical-Lognormal MRL of the
Ln(Losses)",col="blue")
lines(func.s,mrllognorm,type="o",xpd=T,col="red")
35
Quantiles",xlim=c(min(func),max(func)),col="red",main="Q-Q Plot
LogNormal vs ln(Losses)") abline(0,1)
36
# Covariance Matrix
var.mu=-1/(-n/sigma^2)
var.sigma=-1/((n/sigma^2)-((3/sigma^4)*sum((log(y)-mu)^2)))
cov.mu.sigma=(-2/sigma^3)*(sum(log(y)-mu))
sqrt(var.mu)
sqrt(var.sigma)
cov.mu.sigma
rho.mu.sigma=cov.mu.sigma/(sqrt(var.mu)*sqrt(var.sigma))
rho.mu.sigma
# Confidence Interval (95%)
lim.inf.mu=mu-(1.96*sqrt(var.mu))
lim.sup.mu=mu+(1.96*sqrt(var.mu))
lim.inf.sigma=sigma-(1.96*sqrt(var.sigma))
lim.sup.sigma=sigma+(1.96*sqrt(var.sigma))
lim.inf.mu
lim.sup.mu
lim.inf.sigma
lim.sup.sigma
#mu.mom=mean(y)
#theta.mom=(n*(mean(y)^3))/(sum(y^2)-(n*(mean(y)^2)))
robmean=huber(y,k=1.5,tol = 1e-06)$mu
robstd=huber(y,k=1.5,tol=1e-06)$s
mu.mom=robmean
theta.mom=robmean^3/robstd^2
# MLE library(stats4)
ll<-function(mu,theta){-((n*log(theta)/2)-(n*log(2*pi)/2)-(1.5*sum(log(y)))
-(theta*sum((((y-mu)/mu)^2)/(2*y))))}
est<-mle(minuslog=ll,start=list(mu=mu.mom,theta=theta.mom))
summary(est)
mu=8.197649
theta=229.417857
# PDF
pdfinvnorm=function(x,mu,theta)
{((theta/(2*pi*x^3))^(1/2))*exp(-theta*((x-mu)^2)/(2*x*mu^2))}
deninvnorm=pdfinvnorm(y,mu,theta)
hist(func,prob=T,breaks=hbreak,col="gray",xlim=c(4,15),xlab="ln(Losses)",
main="Histogram ln(Losses) vs Inverse Gaussian Distribution")
lines(func,deninvnorm,type="o",xpd=T,col="red")
#CDF
int=rep(0,n)
37
cdfinvnorm=rep(0,n)
for(i in 1:n)
{
integrand=function(x) {pdfinvnorm(x,mu,theta)}
int=integrate(integrand, lower = 0, upper = y[i])
cdfinvnorm[i]=int$va
}
plot(func.s,func.fn,type="l",xlab="ln(Losses)", ylab="F_n",
main="Empirical vs Theoretical Inverse Gaussian CDF",col="blue")
lines(func.s,cdfinvnorm,type="o",col="red")
# LEV
int=rep(0,793)
levinvnorm=rep(0,n)
for(i in 1:n)
{
integrand=function(z) {z*pdfinvnorm(z,mu,theta)}
int=integrate(integrand,lower= 0, upper = y[i])
levinvnorm[i]=int$va+(y[i]*(1-cdfinvnorm[i]))
}
plot(func.s,func.lev, type="l", xlab="ln(Losses)", ylab="Limited
Expected Value", main="Empirical-Inverse Gaussian LEV of the
ln(Losses)",col="blue")
lines(func.s, levinvnorm,type="o",col="red")
# MRL
mean.invnorm=mu
mrlinvnorm=(mean.invnorm-levinvnorm)/(1-cdfinvnorm)
plot(func.s,func.en, type="l", xlab="ln(Losses)", ylab="Limited
Expected Value", main="Empirical-Inverse Gaussian MRL of the
Ln(Losses)",col="blue")
lines(func.s,mrlinvnorm,type="o",xpd=T,col="red")
# P-P Plot
qqplot(cdfinvnorm,func.fn,xlab="CDF Theoretical",
ylab="CDF Empirical",main="P-P Plot Inverse Gaussian vs
ln(Losses)",col="blue")
abline(0,1)
38
if(length(lambda)>1 && length(lambda)!=n) lambda <-
rep(lambda,length=n)
y2 <- rchisq(n,1)
u <- runif(n)
r1 <- mu/(2*lambda) * (2*lambda + mu*y2 - sqrt(4*lambda*mu*y2 +
mu^2*y2^2))
r2 <- mu^2/r1
ifelse(u < mu/(mu+r1), r1, r2)
}
siminvgaussian=rinvgauss(n, mu, lambda)
39
list(z=40, x=-70),
light.source =
c(10,0,10),xlab="Mu",ylab="Theta",zlab="Loglikelihood
F.",main="Loglikelihood Function InvGaussian") #mu.1=rep(0,nrep)
plane=matrix(rep(0,nrep*nrep),ncol=nrep) p.l=log(0.5)
k=((-1/theta)*(p.l-n*log(theta)+n*log(2*pi)+3*sum(log(y))))
a.eq=k-sum(1/y) b.eq=2*n c.eq=n*mean(y)
root1=(-b.eq-(sqrt(b.eq^2-(4*a.eq*c.eq))))/(2*a.eq)
root2=(-b.eq+(sqrt(b.eq^2-(4*a.eq*c.eq))))/(2*a.eq)
for(i in 1:nrep)
{
for(j in 1:nrep)
{
plane[i,j]=rpar(root1[i],theta[j])
}
}
win.graph()
wireframe(llr_m, shade = TRUE,list(arrows =
TRUE),more=TRUE,
aspect = c(60/97, 0.4),drape=TRUE,colorkey = FALSE,screen =
list(z=40, x=-70),
light.source =
c(10,0,10),xlab="Mu",ylab="Theta",zlab="Loglikelihood
F.",main="Loglikelihood Function InvGaussian")
10.3 Selective codes to get the model tools for original data
In this section we provide mostly the similar codes as the previous section but for original data.
losses=fires.x[,2]
# Transformation:
origfunc=losses
y=origfunc
#Classes used in this analysis classes=c(0, 229, 337, 727, 1068,
1569, 2304, 3383, 4968, 7295, 10712, 15730, 23098, 33917, 49805,
73134, 107391, 157693, 231559, 340023, 688498, 3470555)
hbreak=classes
40
hist(origfunc, prob=T, breaks=classes ,
xlim=c(0,40000), xlab="Losses",main="Histogram - Losses")
We dont like to report the codes for empirical PDF, CDF, LEV, MRL for the original data as
those are exactly same as log(data) with the respective changes being made in input.
# PDF
pdflnlognormal=function(x,mu,sigma){(1/x)*pdflognorm(log(x),mu,sigma)}
denlnlognormal=pdflnlognormal(origfunc,mu,sigma)
hist(origfunc,prob=T, breaks=classes , xlim=c(0,40000),
xlab="Losses",main="Histogram - Empirical vs Theoretical
LnLogNormal",col="gray")
lines(origfunc,denlnlognormal,type="p",xpd=T,col="red")
#CDF
int=rep(0,793)
cdflnlognormal=rep(0,n)
41
for(i in 1:n)
{
integrand= function(x){ pdflnlognormal(x,mu,sigma)}
int=integrate(integrand, lower = 1, upper = origfunc.s[i])
cdflnlognormal[i]=int$va
}
plot(origfunc.s, origfunc.fn, type="s",
xlab="Losses", ylab="F_n",main="Empirical vs Theoretical lnlognormal
CDF",col="blue")
lines(origfunc.s,cdflnlognormal,type="p",col="red")
# LEV
int=rep(0,n)
levlnlognormal=rep(0,n)
for(i in 1:n)
{
integrand= function(x) {x*pdflnlognormal(x,mu,sigma)}
int=integrate(integrand, lower = 1, upper = origfunc[i])
levlnlognormal[i]=int$va+(origfunc.s[i]*(1-cdflnlognormal[i]))
}
plot(origfunc.s, origfunc.lev, type="l", xlab="Losses",
ylab="Limited Expected Value",
ylim=c(0,30000),xlim=c(0,600000),main="Empirical-lnlognormal LEV of
Losses",col="blue")
lines(origfunc.s,levlnlognormal,type="p",xpd=T,col="red")
#Computingthe mean :
integrand= function(x)
{x*pdflnlognormal(x,mu,sigma)}
int=integrate(integrand,lower=1,upper= Inf)
# MRL
mean_lnlognormal=int$va
mrllnlognormal=(mean_lnlognormal-levlnlognormal)/(1-cdflnlognormal)
plot(origfunc.s, origfunc.en, type="l", xlab="Losses",
xlim=c(0,origfunc[n-1]),ylim=c(0,2500000),ylab="Limited Expected
Value",main="Empirical-lnlognormal MRL of Losses",col="blue")
lines(origfunc.s,mrllnlognormal,type="p",xpd=T,col="red")
# P-P Plot
qqplot(cdflnlognormal,origfunc.fn,xlab="CDF Theoretical",
ylab="CDF Empirical",main="P-P Plot Log-LogNormal vs
Losses",col="blue")
abline(0,1)
42
simstdnormal=sqrt(-2*log(u))*cos(2*pi*v)
simnormal=mu+sigma*simstdnormal simlognorm=exp(simnormal)
simloglognorm=exp(exp(simnormal))
qloglognorm=quantile(simloglognorm, probs = origfunc.fn, na.rm =
FALSE,names = TRUE, type = 7)
qfunc=quantile(origfunc,probs=origfunc.fn, na.rm = FALSE,names =
TRUE, type = 7)
plot(qloglognorm,origfunc,xlab="TheoreticalQuantiles",ylab="Sample
Quantiles",xlim=c(min(origfunc),100000),ylim=c(min(origfunc),100000),col="red",main="Q-Q
Plot Log-LogNormal vs Losses")
abline(0,1)
# CDF
int=rep(0,n)
cdflninvnorm=rep(0,n)
for(i in 1:n)
{
integrand=function(x) {pdflninvnorm(x,mu,theta)}
int=integrate(integrand,lower = 1, upper = y[i])
cdflninvnorm[i]=int$va
}
plot(origfunc,origfunc.fn, type="l",
xlab="Losses", ylab="F_n",main="Empirical vs Theoretical Ln(Inverse
Gaussian) CDF",col="blue")
lines(origfunc.s,cdflninvnorm,type="p",col="red")
# LEV
int=rep(0,n)
levlninvnorm=rep(0,n)
for(i in 1:n)
{
integrand=function(x) {x*pdflninvnorm(x,mu,theta)}
int=integrate(integrand,lower = 1, upper = origfunc[i])
43
levlninvnorm[i]=int$va+(origfunc.s[i]*(1-cdflninvnorm[i]))
}
plot(origfunc.s, origfunc.lev, type="l", xlab="Losses",
ylab="Limited Expected Value",
ylim=c(0,30000),xlim=c(0,y[n-1]),main="Empirical-Ln(Inverse
Gaussian) LEV of Losses",col="blue")
lines(origfunc.s,levlninvnorm,type="p",xpd=T,col="red")
#Computingthe mean :
integrand= function(x) {x*pdflninvnorm(x,mu,theta)}
int=integrate(integrand, lower = 1, upper = Inf)
# MRL
mean_lninvnorm=int$va
mrllninvnorm=(mean_lninvnorm-levlninvnorm)/(1-cdflninvnorm)
plot(origfunc.s, origfunc.en, type="l", xlab="Losses",
xlim=c(0,y[n-2]),ylim=c(0,origfunc.en[n-1]),ylab="Limited Expected
Value",main="Empirical-Ln(Inverse Gaussian) MRL of
Losses",col="blue")
lines(origfunc.s,mrllninvnorm,type="p",xpd=T,col="red")
# P-P Plot
qqplot(cdflninvnorm,origfunc.fn,xlab="CDF Theoretical", ylab="CDF
Empirical",main="P-P Plot Log-InvGaussian vs Losses",col="blue")
abline(0,1)
mu=2.08
sigma= 0.18
mcdf=function(x) {cdflognorm(x,mu,sigma)}
#Kolmogorov smirnov test:
ks.n<-rep(0,n)
ks.n[1]<-abs(ecdf(y)[1]-mcdf(y[1]))
for(i in 2:n)
{ks.n[i]<-max(abs(ecdf(y)[i-1]-mcdf(y[i])),abs(ecdf(y)[i]-mcdf(y[i])))}
ks.statistic<-max(ks.n)
# Critical value at 10% significance level
ks.cri.val<-(1.36/sqrt(n))
ks.statistic
ks.cri.val
In “Inverse Gaussian” model the code is same just parameter values and “mcdf”are different.
Anderson Darling test:
Here we report the code for inverse gaussian and for other models its same just need to change
the respective inputs.
44
mu=8.197649
theta=229.417857
z=vector()
integrand= function(x)
{pdfinvnorm(x,mu,theta)}
mcdf=function(z) {integrate(integrand,
lower = 0, upper = z)}
fsum=0
for(i in 1:(n-1)) {
fsum=(((1-ecdf(y)[i])^(2))*(log(1-mcdf(y[i])$va)-log(1-mcdf(y[i+1])$va)))+fsum
}
fsum
ssum=0
for(i in 2:(n-1)) {
ssum=(((ecdf(y)[i])^(2))*(log(mcdf(y[i+1])$va)-log(mcdf(y[i])$va)))+ssum
}
ssum
adtest=(-n*mcdf(y[n])$va)+n*fsum+n*ssum
adcrit=1.933
adtest
adcrit
The code for “log-normal” is same just need to change the parameters and “mcdf”.
Cramer Von Misses(individiual data) test:
mu=8.197649
theta=229.417857
z=vector()
integrand= function(x)
{pdfinvnorm(x,mu,theta)} mcdf=function(z) {integrate(integrand,
lower = 0, upper = z)}
crv.test=0
for(i in 1:n) {
crv.test=(1/n)*sum((ecdf(y)[i]-mcdf(y[i])$va)^2)+crv.test }
crv.test
par=2
alpha=7.464233
beta=1.294363
mcdf=function(x)
{cdfgumbel(x,alpha,beta)} freq<-rep(0,k) total<-rep(0,k) for(i in
2:k) {
for(j in 1:n)
45
{
(if(y[j]<=p[i])(total[i]<-total[i]+y[j]))
}
}
classsum<-rep(0,k-1)
for(mx in 1:(k-1)) {
classsum[mx]<-total[mx+1]-total[mx] }
classfreq<-rep(0,k-1)
for(nx
in 1:(k-1)) { classfreq[nx]<-table(ybreaks)[nx] }
classmean=classsum/classfreq
cvm=0
for(i in 2:k) {
cvm=(classmean[i-1]*((((cumsum(classfreq)[i-1])/n)-mcdf(p[i]))^2))+cvm
}
cvm
Chi square test:
Here is another code which we didn’t use but may be helpful to many people. We set it for
Gumble but can be easily modified for other models by changing the “inputs”.
par=2
alpha=7.464233
beta=1.294363
mcdf=function(x)
{cdfgumbel(x,alpha,beta)}
# Computing expected frequencies:
n<-length(y)
ybreaks=cut(y,breaks=classes)
p=classes
k=length(p)
f.ex<-rep(0,k-1)
for(i in 1:k-1)
{f.ex[i]<-(mcdf(p[i+1])-mcdf(p[i]))*n}
# Getting observed frequecies
& computing chisquare:
f.ob=rep(0,k-1)
for(i in 1:(k-1))
{f.ob[i]=table(ybreaks)[[i]]}
# Empirical Statistis Chi-Square
chi.sq<-sum((f.ob-f.ex)^2/f.ex)
chi.sq
# Getting degrees of freedom
df=(k-1)-par-1
# Getting p-value
p.value=1-pchisq(chi.sq,df)
p.value
t_value=qchisq(0.95,df)
46
e_value=chi.sq
t_value
e_value
n.sim<-10000
t.period<-365
rate<-rep(0,n.sim)
lambda<-rep(0,n.sim)
claimnumbers<-rep(0,t.period)
for (i in 1:n.sim){
lambda[i]<-runif(1,2,2.34)
claimnumbers<-rpois(t.period,lambda[i])
rate[i]<-sum(claimnumbers)/t.period }
rate.estimate<-mean(rate)
rate.estimate
num.claims=rpois(365,rate.estimate)
sum(num.claims)
n<-793
n.sim<-50
mat<-matrix(c(rep(0,n.sim*n)),nrow=n.sim,ncol=n,byrow=TRUE)
lambda<-rep(0,n.sim)
for(j in 1:n.sim){ i.ar.time<-rep(0,n)
ar.time2<-rep(0,n)
ar.time<-rep(0,n)
lambda[j]<-runif(1,1.17,3.17)
i.ar.time<-rexp(n,lambda[j])
ar.time2[1]<-i.ar.time[1]
for(i in
1:(n-1)){ar.time2[i+1]<-ar.time2[i]+i.ar.time[i+1]}
c<-365/max(ar.time2)
ar.time<-c*ar.time2
mat[j,]<-ar.time}
47
f.ar.time<-rep(0,n)
for(k in
1:n){f.ar.time[k]<-ceiling(sum(mat[,k])/n.sim)}
##Frequency
counter(My best efforts succeeded):
y<-f.ar.time
f<-rep(0,length(y))
for(i in 1:length(y)){
t1<-sum(y<=y[i])
t2<-sum(y>=y[i])
if(t1+t2!=length(y))(f[i]<-(t1+t2-length(y)))}
distinct.total=1
for(i in 1:(length(y)-1)){
temp1<-y[i]
temp2<-y[i+1]
if(temp1!=temp2
)(distinct.total<-distinct.total+1) }
max(f)
cardinality.y<-distinct.total
cardinality.y
cl.count<-rep(0,max(f))
j<-1
repeat{
##repeat can help nicely
in place of repeated use of of "for loop".
if(j>max(f)){break}
i<-1
repeat{
if(i>n){break}
if(f[i]==j){cl.count[j]<-cl.count[j]+1}
i<-(i+1) } j<-(j+1) }
n.claimsday<-rep(0,max(f))
for(i in
1:max(f)){n.claimsday[i]<-((cl.count[i])/i)}
no.claimsday<-(365-cardinality.y)
cardinality.y
cl.count
sum(cl.count)
n.claimsday
no.claimsday
sum(n.claimsday)+no.claimsday
References
[1] Garrido, Jose (2007). Loss distribution lecture notes Unpublished.
48
[2] Klugman, Stuart A., Panjer, Harry H. ,Willmot, Gordon E(2004). Loss Models: From Data
to Decision. Wiley serier in probability and statistics.
[4] The R Develoment Core Team(2003)R: A Language and Environment for Statistical Com-
puting.
[6] Prof. Dr. Antonio Jos Sez Castillo Funciones de una variable aleatoria
[7] Chhikara and Folks(1989)The Inverse Gaussian Distribution Marcel Dekker, page53. GKS
15 Jan 98
49