File: MixedCopulae.R

package info (click to toggle)
fcopulae 260.72-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 712 kB
  • ctags: 5
  • sloc: makefile: 13
file content (578 lines) | stat: -rwxr-xr-x 17,194 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU Library General 
# Public License along with this library; if not, write to the 
# Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
# MA  02111-1307  USA

# Copyrights (C)
# for this R-port: 
#   1999 - 2007, Diethelm Wuertz, GPL
#   Diethelm Wuertz <wuertz@itp.phys.ethz.ch>
#   info@rmetrics.org
#   www.rmetrics.org
# for the code accessed (or partly included) from other R-ports:
#   see R's copyright and license files
# for the code accessed (or partly included) from contributed R-ports
# and other sources
#   see Rmetrics's copyright file


################################################################################
# TAIL DEPENDENCE ESTIMATOR 
#   derived from the "Mixed Gumbel-SurvivalGumbel-Normal Copula" approach
#   supported Marginals: Normal, NIG and Generalized Hyperbolic Student-t


################################################################################
# FUNCTION:                    GUMBEL COPULA:
#  .rgumbelCopula               Generates fast Gumbel copula random variates
#  .dgumbelCopula               Computes Gumbel copula probability
#  .pgumbelCopula               Computes Gumbel copula probability
# FUNCTION:                    MIXED GUMBEL-SURVIVALGUMBEL-NORMAL COPULA:
#  .rgsgnormCopula              Generates G-SG-NORM copula random variates
#  .dgsgnormCopula              Computes G-SG-NORM copula probability
#  .gsgnormCopulaFit            Computes G-SG-NORM copula probability
# FUNCTION:                    NON-PARAMETRIC TAIL DEPENDECY ESTIMATOR:
#  .cfgTDE                      Estimates non-parametrically tail dependence
# FUNCTION:                    COPULA FIT WITH NIG MARGINALS:
#  .normDependencyFit           Estimates tail dependence with normal marginals
#  .nigDependencyFit            Estimates tail dependence with NIG marginals  
#  .ghtDependencyFit            Estimates tail dependence with GHT marginals   
################################################################################


################################################################################
# Gumbel Copula


.rgumbelCopula =
function(n, alpha = 2)
{   # A function implemented by Diethelm Wuertz

    # Description:
    #   Generates fast Gumbel copula random variates
    
    # Arguments:
    
    # FUNCTION:
    
    # RVs:
    dim = 2
    theta <- runif(n, 0, pi)
    w <- rexp(n)
    b = 1/alpha
    a <- sin((1-b)*theta)*(sin(b*theta))^(b/(1-b))/(sin(theta))^(1/(1-b))
    fr = (a/w)^((1-b)/b)
    fr <- matrix(fr, nrow = n, ncol = dim)
    val <- matrix(runif(dim * n), nrow = n)
    s = -log(val)/fr
    ans = exp(-s^(1/alpha))
    
    control = list(alpha = alpha, copula = "archm", type = "gumbel")
    attr(ans, "control") <- unlist(control)
    
    # Return Value:
    ans
}


# ------------------------------------------------------------------------------


.dgumbelCopula = 
function(u = 0.5, v = u, alpha = 2, output = c("vector", "list"))
{   # A function implemented by Diethelm Wuertz

    # Description:
    #   Computes Bivariate Gumbel Copula Density
    
    # FUNCTION:
    
    # Conveniance Wrapper:
    ans = darchmCopula(u, v, alpha, type = "4", output = output)
    
    # Return Value:
    ans
}


# ------------------------------------------------------------------------------


.pgumbelCopula = 
function(u = 0.5, v = u, alpha = 2, output = c("vector", "list"))
{   # A function implemented by Diethelm Wuertz

    # Description:
    #   Computes Bivariate Gumbel Copula Probability
    
    # FUNCTION:
    
    # Conveniance Wrapper:
    ans = parchmCopula(u, v, alpha, type = "4", output = output)
    
    # Return Value:
    ans
}
 
    
################################################################################
# Mixed Gumbel-SurvivalGumbel-Normal Copula


.rgsgnormCopula = 
function(n = 1000, alpha = c(2, 2), rho = 0, gamma = c(0.5, 0.5))
{   # A function implemented by Diethelm Wuertz

    # Description:
    #   Computes RVs from a mixed GSG copula
    
    # Example:
    #   .rgsgnormCopula(20)
    
    # FUNCTION:
        
    # Upper Gumbel = 1 , Lower Gumbel = 2, t = 3:
    n1 = round(gamma[1]*n)
    n2 = n - n1
    n1 = round(gamma[2]*n1)
    n2 = round(gamma[2]*n2)
    n3 = n - n1 - n2
    
    # Random Variates:
    r = rbind(
        if (n1 > 0) .rgumbelCopula(n1, alpha[1]),
        if (n2 > 0) 1-.rgumbelCopula(n2, alpha[2]),
        if (n3 > 0) rellipticalCopula(n3, rho, type = "norm") )
    index = sample(1:n)
    ans = r[index, ]
    N = c(n, n1, n2, n3)
    names(N) = c("n", "n1", "n2", "n3")
    attr(ans, "control")<-N
    
    # Return Value:
    ans
} 


# ------------------------------------------------------------------------------


.dgsgnormCopula = 
function(u = 0.5, v = u, alpha = c(2, 2), rho = 0, gamma = c(0.5, 0.5))
{   # A function implemented by Diethelm Wuertz

    # Description:
    #   Computes mixed GSG copula density
    
    # Example:
    #   .dgsgnormCopula(grid2d()$x, grid2d()$y)
   
    # FUNCTION:
    
    # Settings:
    if (is.list(u)) {
        v = u[[2]]
        u = u[[1]]
    }
    if (is.matrix(u)) {
        v = u[, 1]
        u = u[, 2]
    }
    
    # Mix Gumbel + Survival Gumbel:
    dCopula1 = .dgumbelCopula(u, v, alpha[1], output = "list")
    dCopula2 = .dgumbelCopula(1-u, 1-v, alpha[2], output = "list")
    dCopula12 = dCopula1
    dCopula12$z = gamma[1]*dCopula1$z + (1-gamma[1])*dCopula2$z
    
    # Mix Gumbel/SurvivalGumbel + Student-t:
    dCopula3 = dellipticalCopula(u, v, rho, type = "norm", output = "list")
    dCopula123 = dCopula12
    dCopula123$z = gamma[2]*dCopula12$z + (1-gamma[2])*dCopula3$z
    ans = dCopula123
    
    # Return Value:
    ans
}


# ------------------------------------------------------------------------------


.gsgnormCopulaFit =
function(u, v =  NULL, trace = FALSE)
{   # A function implemented by Diethelm Wuertz

    # Description:
    #   Fits parameters for a mixed GSG copula
    
    # FUNCTION:
    
    # Settings:
    U <- u
    V <- v
    if (is.list(u)) {
        U = u[[1]]
        V = u[[2]]
    }
    if (is.matrix(u)) {
        U = u[, 1]
        V = u[, 2]
    }
     
    # Estimate Copula:
    start = c(1.5, 1.5, 0, 1/3, 1/3)
    fun = function(x, U, V, trace) 
    {
        density = .dgsgnormCopula(u = U, v = V, 
            alpha = x[1:2], rho = x[3], gamma = x[4:5])$z
        density = density[!is.na(density)]
        f = -mean( log(density) )
        if (trace) {
            cat("\n Objective Function Value:  ", -f)
            cat("\n Parameter Estimates:       ",   
                round(c(x[1:3], x[5]*x[4], x[5]*(1-x[4]), 1-x[5]), 4), "\n")
        }
        f
    }

    # Fit:
    fit = nlminb(start = start, objective = fun, 
        lower = c(  1,   1, -0.999, 0, 0), 
        upper = c(Inf, Inf,  0.999, 1, 1), U = U, V = V, trace = trace)
        
    param = fit$par
    alpha1 = param[1]
    alpha2 = param[2]
    gamma1 = param[4]
    gamma2 = param[5]
    Upper = (gamma2*gamma1*(2 - 2^(1/alpha1)))[[1]]
    Lower = (gamma2*(1-gamma1)*(2 - 2^(1/alpha2)))[[1]]
    Param = c(param[1:3], param[5]*param[4], param[5]*(1-param[4]), 1-param[5])
    names(Param) = c("alpha1", "alpha2", "rho", "gumbel", "survival", "norm")
    Lambda = c(lower = Lower, upper = Upper)
      
    # Return Value:
    list(param = Param, lambda = Lambda, fitted = fit$par)
}


################################################################################
# Non-Parametric Tail Dependence Estimator


.cfgTDE = 
function(x, y)
{   # A function implemented by Diethelm Wuertz

    # Description:
    #   Estimates non-parametrically tail dependency coefficient
    
    # FUNCTION:
    
    # Upper Tail:
    lambda = NULL
    n = length(x)
    for(i in 1:n){
        lambda = c(lambda,
            log(sqrt(log(1/x[i])*log(1/y[i]))/log(1/max(x[i],y[i])^2)))
    }
    upper = 2-2*exp(sum(lambda/n))
    
    # Lower Tail:
    x = 1-x
    y = 1-y
    lambda = NULL
    n = length(x)
    for(i in 1:n){
        lambda = c(lambda,
            log(sqrt(log(1/x[i])*log(1/y[i]))/log(1/max(x[i],y[i])^2)))
    }
    lower = 2-2*exp(sum(lambda/n))
    
    # Return Value:
    c(lower = lower, upper = upper)
}


################################################################################
# GSGNORM Parametric Tail Dependence Estimator


.normDependencyFit = 
function(x, doplot = TRUE, trace = TRUE)
{   # A function implemented by Diethelm Wuertz

    # Description:
    #   Estimates tail dependency coefficients with Normal marginals
    
    # Arguments:
    #   x - a multivariate 'timeSeries' object
    
    # FUNCTION:
    
    # Settings: 
    N = ncol(x)
    lowerLambda = upperLambda = 0*diag(N)
    assetsNames = colnames(x)
    P = NULL
    
    for (i in 1:(N-1)) {
        # First asset:
        r1 = as.vector(x[, i])
        fit1 = nFit(r1)
        estim1 = fit1$estimate
        p1 = pnorm(r1, estim1[1], estim1[2]) 
        Main1 = assetsNames[i]
        P = cbind(P, p1)
        for (j in (i+1):N) 
        {  
            # Second asset:
            r2 = as.vector(x[, j])
            fit2 = nFit(r2) 
            estim2 = fit2$estimate      
            p2 = pnorm(r2, estim2[1], estim2[2]) 
            Main2 = assetsNames[j]
            
            # Optional Plot:
            if (doplot) 
            {
                # Plot Distribution:
                MainR = paste("Distribution:", Main1, "-", Main2)
                plot(r1, r2, pch = 19, main = MainR)
                grid()
                
                # Plot Copula:
                MainP = paste("Copula:", Main1, "-", Main2)
                plot(p1, p2, pch = 19, main = MainP)
                grid()
            }
            
            # Fit GSG copula parameters:
            fit = .gsgnormCopulaFit(u = p1, v = p2, trace = FALSE)
            if (trace)
                cat(assetsNames[c(i,j)], round(fit$lambda, 3), "\n")  
            
                # Compose lambda Matrix:
            lowerLambda[i, j] = lowerLambda[j, i] = fit$lambda[1]
            upperLambda[i, j] = upperLambda[j, i] = fit$lambda[2]
        }
    }
    
    # Result:
    colnames(lowerLambda) = rownames(lowerLambda) = assetsNames
    colnames(upperLambda) = rownames(upperLambda) = assetsNames
    ans = list(lower = lowerLambda, upper = upperLambda)
     
    # Return Value:
    ans 
}


# ------------------------------------------------------------------------------


.nigDependencyFit = 
function(x, doplot = TRUE, trace = TRUE)
{   # A function implemented by Diethelm Wuertz

    # Description:
    #   Estimates tail dependency coefficients with NIG marginals
    
    # Arguments:
    #   x - a multivariate 'timeSeries' object
    
    # FUNCTION:
    
    # Settings:
    N = ncol(x)
    lowerLambda = upperLambda = 0*diag(N)
    assetsNames = colnames(x)
    P = NULL
    
    for (i in 1:(N-1)) {
        # First asset:
        r1 = as.vector(x[, i])
        fit1 = nigFit(r1, doplot = FALSE)
        estim1 = fit1@fit$estimate
        p1 = pnig(r1, estim1[1], estim1[2], estim1[3], estim1[4]) 
        Main1 = assetsNames[i]
        P = cbind(P, p1)
        for (j in (i+1):N) {  
            # Second asset:
            r2 = as.vector(x[, j])
            fit2 = nigFit(r2, doplot = FALSE) 
            estim2 = fit2@fit$estimate      
            p2 = pnig(r2, estim2[1], estim2[2], estim2[3], estim2[4]) 
            Main2 = assetsNames[j]
            # Optional Plot:
            if (doplot) {
                MainR = paste("Distribution:", Main1, "-", Main2)
                plot(r1, r2, pch = 19, main = MainR)
                grid()
                MainP = paste("Copula:", Main1, "-", Main2)
                plot(p1, p2, pch = 19, main = MainP)
                grid()
            }
            # Fit GSG copula parameters:
            fit = .gsgnormCopulaFit(u = p1, v = p2, trace = FALSE)
            if (trace)
                cat(assetsNames[c(i,j)], round(fit$lambda, 3), "\n")  
            # Compose lambda Matrix:
            lowerLambda[i, j] = lowerLambda[j, i] = fit$lambda[1]
            upperLambda[i, j] = upperLambda[j, i] = fit$lambda[2]
        }
    }
    
    # Result:
    colnames(lowerLambda) = rownames(lowerLambda) = assetsNames
    colnames(upperLambda) = rownames(upperLambda) = assetsNames
    ans = list(lower = lowerLambda, upper = upperLambda)
     
    # Return Value:
    ans 
}


# ------------------------------------------------------------------------------


.ghtDependencyFit = 
function(x, doplot = TRUE, trace = TRUE)
{   # A function implemented by Diethelm Wuertz

    # Description:
    #   Estimates tail dependency coefficients with GH Student-t marginals
    
    # Arguments:
    #   x - a multivariate 'timeSeries' object
    
    # FUNCTION:
    
    # Settings: 
    N = ncol(x)
    lowerLambda = upperLambda = 0*diag(N)
    assetsNames = colnames(x)
    P = NULL
    
    for (i in 1:(N-1)) {
        # First asset:
        r1 = as.vector(x[, i])
        fit1 = nigFit(r1, doplot = FALSE)
        estim1 = fit1@fit$estimate
        p1 = pnig(r1, estim1[1], estim1[2], estim1[3], estim1[4]) 
        Main1 = assetsNames[i]
        P = cbind(P, p1)
        for (j in (i+1):N) {  
            # Second asset:
            r2 = as.vector(x[, j])
            fit2 = nigFit(r2, doplot = FALSE) 
            estim2 = fit2@fit$estimate      
            p2 = pnig(r2, estim2[1], estim2[2], estim2[3], estim2[4]) 
            Main2 = assetsNames[j]
            # Optional Plot:
            if (doplot) {
                MainR = paste("Distribution:", Main1, "-", Main2)
                plot(r1, r2, pch = 19, main = MainR)
                grid()
                MainP = paste("Copula:", Main1, "-", Main2)
                plot(p1, p2, pch = 19, main = MainP)
                grid()
            }
            # Fit GSG copula parameters:
            fit = .gsgnormCopulaFit(u = p1, v = p2, trace = FALSE)
            if (trace)
                cat(assetsNames[c(i,j)], round(fit$lambda, 3), "\n")  
            # Compose lambda Matrix:
            lowerLambda[i, j] = lowerLambda[j, i] = fit$lambda[1]
            upperLambda[i, j] = upperLambda[j, i] = fit$lambda[2]
        }
    }
    
    # Result:
    colnames(lowerLambda) = rownames(lowerLambda) = assetsNames
    colnames(upperLambda) = rownames(upperLambda) = assetsNames
    ans = list(lower = lowerLambda, upper = upperLambda)
     
    # Return Value:
    ans 
}
  

################################################################################
# Examples


if (FALSE) 
{
    require(fCopulae)
    
    # Simulated data:
    x = .rnormCopula(1000, rho = 0.7)
    .gsgnormCopulaFit(x, trace = TRUE)
    
    # LPP Portfolio:
    require(fPortfolio)
    data(LPP2005REC)
    x = 100 * as.timeSeries(LPP2005REC)[, 1:6]
    head(x)
    
    # Tail Dependency Estimation:
    par(mfrow = c(2,2), cex = 0.7)   
    ans = .nigDependencyFit(x) 
    ans 
    
    #         Lower Upper
    # SBI SPI 0     0 
    # SBI SII 0.055 0 
    # SBI LMI 0.064 0.069 
    # SBI MPI 0     0 
    # SBI ALT 0     0 
    # SPI SII 0     0.064 
    # SPI LMI 0     0.072 
    # SPI MPI 0.352 0.214 
    # SPI ALT 0.273 0.048 
    # SII LMI 0.075 0 
    # SII MPI 0     0.164 
    # SII ALT 0     0.152 
    # LMI MPI 0     0 
    # LMI ALT 0     0 
    # MPI ALT 0.124 0.012 
    # 
    # $lower
    #            SBI       SPI        SII        LMI       MPI       ALT
    # SBI 0.00000000 0.0000000 0.05524575 0.06369211 0.0000000 0.0000000
    # SPI 0.00000000 0.0000000 0.00000000 0.00000000 0.3517273 0.2728653
    # SII 0.05524575 0.0000000 0.00000000 0.07541669 0.0000000 0.0000000
    # LMI 0.06369211 0.0000000 0.07541669 0.00000000 0.0000000 0.0000000
    # MPI 0.00000000 0.3517273 0.00000000 0.00000000 0.0000000 0.1236074
    # ALT 0.00000000 0.2728653 0.00000000 0.00000000 0.1236074 0.0000000
    # 
    # $upper
    #            SBI        SPI       SII        LMI        MPI        ALT
    # SBI 0.00000000 0.00000000 0.0000000 0.06935723 0.00000000 0.00000000
    # SPI 0.00000000 0.00000000 0.0638653 0.07169038 0.21421052 0.04785965
    # SII 0.00000000 0.06386530 0.0000000 0.00000000 0.16401986 0.15228270
    # LMI 0.06935723 0.07169038 0.0000000 0.00000000 0.00000000 0.00000000
    # MPI 0.00000000 0.21421052 0.1640199 0.00000000 0.00000000 0.01209013
    # ALT 0.00000000 0.04785965 0.1522827 0.00000000 0.01209013 0.00000000

    par(mfrow = c(1,1))
    .assetsStarPlot(ans$lower, main = "Lower Tail Relations")
    .assetsStarPlot(ans$upper, main = "Lower Tail Relations")
}


################################################################################