Showing posts with label Credit Risk Modelling. Show all posts
Showing posts with label Credit Risk Modelling. Show all posts

## Sunday, April 21, 2019

### How to used Credit Risk Modelling

Credit Risk Modelling

Functions for modelling credit risk:

Bernoulli mixture model with prescribed default and joint default probabilities

Bernoulli mixture model with Clayton copula dependencies of default.

Probitnormal Mixture of Bernoullis

Beta-Binomial Distribution

Logitnormal-Binomial Distribution

Probitnormal-Binomial Distribution
library(QRM)

Attaching package: ‘QRM’

The following object is masked from ‘package:base’:

lbeta

Warning messages:
1: package ‘QRM’ was built under R version 3.5.3
2: package ‘gsl’ was built under R version 3.5.3
3: package ‘numDeriv’ was built under R version 3.5.2
> library ( fGarch )
Warning message:
package ‘fGarch’ was built under R version 3.5.3
> data(cac40)
GMT
CAC40
1994-01-03 2290.6
1994-01-04 2274.3
1994-01-05 2249.6
1994-01-06 2275.1
1994-01-07 2307.6
1994-01-10 2317.3
> pi.B <- 0.2
> pi2.B <- 0.05
> probitnorm.pars <- cal.probitnorm(pi.B, pi2.B)
> probitnorm.pars
mu      sigma  rho.asset
-0.8983228  0.3732059  0.1222547
> beta.pars <- cal.beta(pi.B, pi2.B)
> beta.pars
a  b
3 12
> claytonmix.pars <- cal.claytonmix(pi.B, pi2.B)
> claytonmix.pars
pi      theta
0.20000000 0.09982679
> q <- (1:1000) / 1001
> q <- q[q < 0.25]
> p.probitnorm <- pprobitnorm(q, probitnorm.pars[1],
+                             probitnorm.pars[2])
> p.beta <- pbeta(q, beta.pars[1], beta.pars[2])
> p.claytonmix <- pclaytonmix(q, claytonmix.pars[1],
+                             claytonmix.pars[2])
> scale <- range((1 - p.probitnorm), (1 - p.beta), (1 - p.claytonmix))
> plot(q, (1 - p.probitnorm), type = "l", log = "y", xlab = "q",
+      ylab = "P(Q > q)",ylim=scale)
> lines(q, (1 - p.beta), col = 2)
> lines(q, (1 - p.claytonmix), col = 3)

> legend("topright", c("Probit-normal", "Beta", "Clayton-Mixture"),
+        lty=rep(1,3),col = (1:3))
> pi.B <- 0.0489603
> pi2.B <- 0.003126529
> claytonmix.pars <- cal.claytonmix(pi.B, pi2.B)
> claytonmix.pars
pi      theta
0.04896030 0.03198761
> q <- (1:1000) / 1001
> q <- q[q < 0.25]
> d.claytonmix <- dclaytonmix(q, claytonmix.pars[1], claytonmix.pars[2])
[1] 0.001142736 0.021818856 0.103356236
[4] 0.285265923 0.592723408 1.035450932
> data(spdata.raw)
> attach(spdata.raw)
> BdefaultRate <- Bdefaults / Bobligors
> mod1a <- fit.binomial(Bdefaults, Bobligors)
> mod1b <- fit.binomialLogitnorm(Bdefaults, Bobligors)
> mod1c <- fit.binomialProbitnorm(Bdefaults, Bobligors)
>
> mod1d <- fit.binomialBeta(Bdefaults, Bobligors);
> momest(Bdefaults, Bobligors)
[1] 4.896030e-02 3.126529e-03
[3] 2.485062e-04 2.320990e-05
[5] 2.386235e-06 2.576897e-07
[7] 2.836485e-08 3.122301e-09
[9] 3.395502e-10 3.619895e-11
> pi.B <- momest(Bdefaults, Bobligors)[1]
> pi2.B <- momest(Bdefaults, Bobligors)[2]
>
> probitnorm.pars <- cal.probitnorm(pi.B, pi2.B)
> q <- (1:1000)/1001
> q <- q[ q < 0.25]
> d.probitnorm <- dprobitnorm(q, probitnorm.pars[1], probitnorm.pars[2])
> p <- c(0.90,0.95,0.975,0.99,0.995,0.999,0.9999,0.99999,0.999999)
> sigma <- 0.2 * 10000 / sqrt(250)
> VaR.t4 <- qst(p, df = 4, sd = sigma, scale = TRUE)
> VaR.t4
[1]  137.1341  190.6782  248.3328
[4]  335.1372  411.8028  641.5889
[7] 1165.7670 2086.8939 3718.8363
> detach(spdata.raw)
> pi <- 0.04896
> pi2 <- 0.00321
> beta.pars <- cal.beta(pi, pi2)
> probitnorm.pars <- cal.probitnorm(pi, pi2)
> n <- 1000
> m <- rep(500, n)
> mod2a <- rbinomial.mixture(n, m, "beta", shape1 = beta.pars[1],
+                            shape2 = beta.pars[2])
> mod2b <- rbinomial.mixture(n, m, "probitnorm",
+                            mu = probitnorm.pars[1],
+                            sigma = probitnorm.pars[2])

### Black-Scholes formula-R

Black-Scholes formula-R > BlackScholes <- function(TypeFlag = c("c", "p"), S, X, Time, r, b, sigma) { TypeFla...