MATLAB Examples

# creditMigrationCopula Simulation Workflow

This example shows a common workflow for using a creditMigrationCopula object for a portfolio of counterparty credit ratings.

## Step 1. Create a creditMigrationCopula object with a 4-factor model

```load CreditMigrationData.mat; ```

Scale the bond prices for portfolio positions for each bond.

```migrationValues = migrationPrices .* numBonds; ```

Create a creditMigrationCopula object with a 4-factor model using creditMigrationCopula.

```cmc = creditMigrationCopula(migrationValues,ratings,transMat,... lgd,weights,'FactorCorrelation',factorCorr) ```
```cmc = creditMigrationCopula with properties: Portfolio: [250x5 table] FactorCorrelation: [4x4 double] RatingLabels: [8x1 string] TransitionMatrix: [8x8 double] VaRLevel: 0.9500 PortfolioValues: [] ```

## Step 2. Set the VaRLevel to 99%.

Set the VarLevel property for the creditMigrationCopula object to 99% (the default is 95%).

``` cmc.VaRLevel = 0.99; ```

## Step 3. Run a simulation.

Use the simulate function to simulate 100,000 scenarios.

``` cmc = simulate(cmc,1e5) ```
```cmc = creditMigrationCopula with properties: Portfolio: [250x5 table] FactorCorrelation: [4x4 double] RatingLabels: [8x1 string] TransitionMatrix: [8x8 double] VaRLevel: 0.9900 PortfolioValues: [1x100000 double] ```

## Step 4. Generate a report for the portfolio risk.

Use the portfolioRisk function to obtain a report for risk measures and confidence intervals for EL, Std, VaR, and CVaR.

```[portRisk,RiskConfidenceInterval] = portfolioRisk(cmc) ```
```portRisk = 1x4 table EL Std VaR CVaR ______ _____ _____ _____ 4573.9 13039 56515 84463 RiskConfidenceInterval = 1x4 table EL Std VaR CVaR ________________ ______________ ______________ ______________ 4493.1 4654.7 12982 13096 55043 58038 82485 86441 ```

## Step 5. Visualize the distribution.

View a histogram of the portfolio values.

```figure h = histogram(cmc.PortfolioValues,125); title('Distribution of Portfolio Values'); ```

## Step 6. Overlay the value if all counterparties maintain current credit ratings.

Overlay the value that the portfolio object (cmc) takes if all counterparties maintain their current credit ratings.

```CurrentRatingValue = portRisk.EL + mean(cmc.PortfolioValues); hold on plot([CurrentRatingValue CurrentRatingValue],[0 max(h.Values)],'LineWidth',2); grid on ```

## Step 7. Generate a risk contributions report.

Use the riskContribution function to display the risk contribution. The risk contributions, EL and CVaR, are additive. If you sum each of these two metrics over all the counterparties, you get the values reported for the entire portfolio in the portfolioRisk table.

```rc = riskContribution(cmc); disp(rc(1:10,:)) ```
``` ID EL Std VaR CVaR __ ______ ______ ______ ______ 1 16.397 40.977 192.11 254.12 2 9.1179 21.417 83.3 134.31 3 5.7873 24.887 99.573 236.84 4 6.4235 57.71 192.06 338.23 5 22.739 72.371 289.12 544.69 6 10.776 111.12 327.96 704.29 7 2.9046 88.98 324.91 551.4 8 12.152 42.123 189.38 265.97 9 2.1567 4.0432 3.2359 26.112 10 1.7495 2.4593 11.003 15.933 ```

## Step 8. Simulate the risk exposure with a t copula.

To use a t copula with 10 degrees of freedom, use the simulate function with optional input arguments. Save the results to a new creditMigrationCopula object (cmct).

```cmct = simulate(cmc,1e5,'Copula','t','DegreesOfFreedom',10) ```
```cmct = creditMigrationCopula with properties: Portfolio: [250x5 table] FactorCorrelation: [4x4 double] RatingLabels: [8x1 string] TransitionMatrix: [8x8 double] VaRLevel: 0.9900 PortfolioValues: [1x100000 double] ```

## Step 9. Generate a report for the portfolio risk for the t copula.

Use the portfolioRisk function to obtain a report for risk measures and confidence intervals for EL, Std, VaR, and CVaR.

```[portRisk2,RiskConfidenceInterval2] = portfolioRisk(cmct) ```
```portRisk2 = 1x4 table EL Std VaR CVaR ______ _____ _____ __________ 4553.6 17158 72689 1.2545e+05 RiskConfidenceInterval2 = 1x4 table EL Std VaR CVaR ________________ ______________ ______________ ________________________ 4447.2 4659.9 17083 17233 70834 75063 1.2144e+05 1.2947e+05 ```

## Step 10. Visualize the distribution for the t copula.

View a histogram of the portfolio values.

```figure h = histogram(cmct.PortfolioValues,125); title('Distribution of Portfolio Values for t Copula'); ```

## Step 11. Overlay the value if all counterparties maintain current credit ratings for t copula.

Overlay the value that the portfolio object (cmct) takes if all counterparties maintain their current credit ratings.

```CurrentRatingValue2 = portRisk2.EL + mean(cmct.PortfolioValues); hold on plot([CurrentRatingValue2 CurrentRatingValue2],[0 max(h.Values)],'LineWidth',2); grid on ```