# An example using the fitness model

##

###

# An example using the fitness model

This document describe a toy example for the use of the package systemicrisk.

    set.seed(1238190) # arbitrary seed
    library(systemicrisk)

Suppose we observe the following vector of total liabilities and todal assets.

    l <- c(714,745,246, 51,847)
    a <- c(872, 412, 65, 46,1208)

The following sets up a model for 5 banks:

    mod <- Model.additivelink.exponential.fitness(n=5,alpha=-2.5,beta=0.3,gamma=1.0,
                     lambdaprior=Model.fitness.genlambdaparprior(ratescale=500))

Choosing thinning to ensure sample is equivalent to number of

    thin <- choosethin(l=l,a=a,model=mod,silent=TRUE)

    ## Warning in findFeasibleMatrix_targetmean(l, a, p = u$p, targetmean =
    ## mean(genL(model)$L > : Desired mean degree is less than minimal degree that is
    ## necessary.

    thin

    ## [1] 120

Running the sampler to produce 1000 samples.

    res <- sample_HierarchicalModel(l=l,a=a,model=mod,nsamples=1e3,thin=thin,silent=TRUE)

    ## Warning in findFeasibleMatrix_targetmean(l, a, p = u$p, targetmean =
    ## mean(genL(model)$L > : Desired mean degree is less than minimal degree that is
    ## necessary.

Some examples of the matrics generated are below.

    res$L[[1]]

    ##            [,1]     [,2]      [,3]      [,4]      [,5]
    ## [1,]   0.000000 199.9657 45.479710  0.000000 468.55457
    ## [2,] 237.798093   0.0000  8.281059 42.077228 456.84362
    ## [3,]   3.746609   0.0000  0.000000  0.000000 242.25339
    ## [4,]  10.651586   0.0000  0.000000  0.000000  40.34841
    ## [5,] 619.803712 212.0343 11.239231  3.922772   0.00000

    res$L[[2]]

    ##          [,1]      [,2]     [,3] [,4]     [,5]
    ## [1,]   0.0000  21.65717  0.00000    0 692.3428
    ## [2,] 361.4501   0.00000 18.17048    0 365.3794
    ## [3,]   0.0000  95.72222  0.00000    0 150.2778
    ## [4,]   0.0000  51.00000  0.00000    0   0.0000
    ## [5,] 510.5499 243.62062 46.82952   46   0.0000

The sampler produces samples from the conditional distribution of matrix and parameter values given the observed data. To see the posterior distribution of the liabilities of Bank 1 towards Bank 2:

    plot(ecdf(sapply(res$L,function(x)x[1,2])))

## Diagnostic of the R-output

All the caveats of MCMC algorithms apply. In particular the samples are dependent.

Some automatic diagnostic can be generated via the function diagnose.

    diagnose(res)

    ## Analysis does not consider 5 entries of matrix
    ## that are deterministic (diagonal elements, row/column sum=0 or forced result).

    ## All remaining elements of the liabilities matrix have moved during sample run.

    ## Summary of ESS in matrix: Min.= 839.4 1st Qu.= 976.9 Median=1000.0 Mean= 975.4 3rd Qu.=1000.0 Max.=1222.3

    ## Summary of ESS in theta: Min.= 677.8 1st Qu.= 811.8 Median=1000.0 Mean= 900.2 3rd Qu.=1000.0 Max.=1000.0

Trace plots of individual liabilities also shoud show rapid mixing - as seems to be the case for the liabilities of Bank 1 towards Bank 2.

    plot(sapply(res$L,function(x)x[1,2]),type="b")

Trace plot of the fitness of bank 1.

    plot(res$theta[1,],type="b")

Also, the autocorrelation function should decline quickly. Again, considering the liabilities between bank 1 and bank 2:

    acf(sapply(res$L,function(x)x[1,2]))

In this case it decays quickly below the white-noise threshold (the horizontal dashed lines).