# Documentation

### This is machine translation

Translated by
Mouseover text to see original. Click the button below to return to the English verison of the page.

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

## 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 the saved portfolio data.

```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 CVaR __ ______ ______ 1 16.397 254.12 2 9.1179 134.31 3 5.7873 236.84 4 6.4235 338.23 5 22.739 544.69 6 10.776 704.29 7 2.9046 551.4 8 12.152 265.97 9 2.1567 26.112 10 1.7495 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 ```