# Fitting transition matrices in R by solving for coefficient

I'm using various matrices to impute a fitted transition matrix for credit ratings by solving for a variable [S]. Essentially the idea is to determine a base matrix and stress matrix to compare to a single point of time [t1] to create a fitted transition matrix [t1 fitted]. With the data below, [t1 fitted] and [t1 variance] is derived from the following formula:

[t1 fitted] = (1-S)*[Base Matrix] + S*[Stress Matrix]
[t1 variance] = ([t1] - [t1 fitted])^2


I would like to know how or a direction I can take to solve for this [S] variable where the sum of [t1 variance] is the lowest value. I'd appreciate any direction or solution (functions to use or sample code) as I am not too familiar with R functions/packages and simply recreating the approach as a challenge of the results.

Solution from the sample data below where I derived it using Excel's Solver function by using the GRG Nonlinear method solving for the min value of [S]. (Excel document can be provided if necessary)

 [S]=41.8962055525792%
sum[t1 variance]=2.56220853052315%


****************************** Base Matrix (t1-t4) ******************************
From/To:      AAA       AA        A      BBB       BB        B      CCC  Default
AAA   91.90%    7.39%    0.72%    0.00%    0.00%    0.00%    0.00%    0.00%
AA    1.13%   91.26%    7.09%    0.31%    0.21%    0.00%    0.00%    0.00%
A    0.10%    2.56%   91.19%    5.33%    0.62%    0.21%    0.00%    0.00%
BBB    0.00%    0.21%    5.36%   87.94%    5.46%    0.83%    0.10%    0.10%
BB    0.00%    0.11%    0.43%    5.00%   85.12%    7.33%    0.43%    1.59%
B    0.00%    0.11%    0.11%    0.54%    5.97%   82.19%    2.17%    8.90%
CCC    0.00%    0.44%    0.44%    0.87%    2.51%    5.90%   67.80%   22.05%
Default    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%  100.00%

******************************* Stress Matrix (t2) ******************************
From/To:      AAA       AA        A      BBB       BB        B      CCC  Default
AAA   90.79%    8.29%    0.72%    0.10%    0.10%    0.00%    0.00%    0.00%
AA    0.10%   91.22%    7.85%    0.62%    0.10%    0.10%    0.00%    0.00%
A    0.92%    2.36%   90.04%    5.44%    0.72%    0.31%    0.10%    0.10%
BBB    0.00%    0.32%    5.94%   86.95%    5.30%    1.17%    0.12%    0.21%
BB    0.00%    0.11%    0.66%    7.69%   80.55%    8.79%    0.99%    1.21%
B    0.00%    0.11%    0.23%    0.45%    6.47%   82.75%    4.09%    5.90%
CCC    0.23%    0.00%    0.23%    1.25%    2.28%   12.86%   60.64%   22.53%
Default    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%  100.00%

*********************************** t1 Matrix ***********************************
From/To:      AAA       AA        A      BBB       BB        B      CCC  Default
AAA   94.16%    5.84%    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%
AA    4.56%   90.89%    4.34%    0.11%    0.00%    0.11%    0.00%    0.00%
A    0.00%   10.10%   87.04%    2.67%    0.06%    0.00%    0.13%    0.00%
BBB    0.00%    0.19%    7.42%   88.44%    3.67%    0.28%    0.00%    0.00%
BB    0.00%    0.00%    0.19%    8.25%   83.49%    7.32%    0.75%    0.00%
B    0.10%    0.00%    0.00%    0.20%    6.54%   83.71%    9.46%    0.00%
CCC    0.00%    0.00%    0.00%    0.00%    0.00%    8.13%   68.62%   23.25%
Default    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%  100.00%

******************************** t1 Fitted Matrix *******************************
From/To:      AAA       AA        A      BBB       BB        B      CCC  Default
AAA   91.43%    7.76%    0.72%    0.04%    0.04%    0.00%    0.00%    0.00%
AA    0.70%   91.25%    7.41%    0.44%    0.16%    0.04%    0.00%    0.00%
A    0.45%    2.48%   90.71%    5.38%    0.66%    0.25%    0.04%    0.04%
BBB    0.00%    0.25%    5.60%   87.52%    5.40%    0.97%    0.11%    0.15%
BB    0.00%    0.11%    0.52%    6.12%   83.21%    7.94%    0.66%    1.43%
B    0.00%    0.11%    0.16%    0.51%    6.18%   82.43%    2.97%    7.65%
CCC    0.10%    0.25%    0.35%    1.03%    2.41%    8.81%   64.80%   22.25%
Default    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%  100.00%

******************************* t1 Variance Matrix ******************************
From/To:      AAA       AA        A      BBB       BB        B      CCC  Default
AAA    0.07%    0.04%    0.01%    0.00%    0.00%    0.00%    0.00%    0.00%
AA    0.15%    0.00%    0.09%    0.00%    0.00%    0.00%    0.00%    0.00%
A    0.00%    0.58%    0.13%    0.07%    0.00%    0.00%    0.00%    0.00%
BBB    0.00%    0.00%    0.03%    0.01%    0.03%    0.00%    0.00%    0.00%
BB    0.00%    0.00%    0.00%    0.05%    0.00%    0.00%    0.00%    0.02%
B    0.00%    0.00%    0.00%    0.00%    0.00%    0.02%    0.42%    0.58%
CCC    0.00%    0.00%    0.00%    0.01%    0.06%    0.00%    0.15%    0.01%
Default    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%    0.00%


You can do this using the optim function in R. One possible solution is as follows:

base <- c(0.9190, 0.0739, 0.0072, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0113, 0.9126, 0.0709, 0.0031, 0.0021, 0.0000, 0.0000, 0.0000,
0.0010, 0.0256, 0.9119, 0.0533, 0.0062, 0.0021, 0.0000, 0.0000,
0.0000, 0.0021, 0.0536, 0.8794, 0.0546, 0.0083, 0.0010, 0.0010,
0.0000, 0.0011, 0.0043, 0.0500, 0.8512, 0.0733, 0.0043, 0.0159,
0.0000, 0.0011, 0.0011, 0.0054, 0.0597, 0.8219, 0.0217, 0.0890,
0.0000, 0.0044, 0.0044, 0.0087, 0.0251, 0.0590, 0.6780, 0.2205,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000)

stress <- c(0.9079, 0.0829, 0.0072, 0.0010, 0.0010, 0.0000, 0.0000, 0.0000,
0.0010, 0.9122, 0.0785, 0.0062, 0.0010, 0.0010, 0.0000, 0.0000,
0.0092, 0.0236, 0.9004, 0.0544, 0.0072, 0.0031, 0.0010, 0.0010,
0.0000, 0.0032, 0.0594, 0.8695, 0.0530, 0.0117, 0.0012, 0.0021,
0.0000, 0.0011, 0.0066, 0.0769, 0.8055, 0.0879, 0.0099, 0.0121,
0.0000, 0.0011, 0.0023, 0.0045, 0.0647, 0.8275, 0.0409, 0.0590,
0.0023, 0.0000, 0.0023, 0.0125, 0.0228, 0.1286, 0.6064, 0.2253,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000)

t1 <- c(0.9416, 0.0584, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0456, 0.9089, 0.0434, 0.0011, 0.0000, 0.0011, 0.0000, 0.0000,
0.0000, 0.1010, 0.8704, 0.0267, 0.0006, 0.0000, 0.0013, 0.0000,
0.0000, 0.0019, 0.0742, 0.8844, 0.0367, 0.0028, 0.0000, 0.0000,
0.0000, 0.0000, 0.0019, 0.0825, 0.8349, 0.0732, 0.0075, 0.0000,
0.0010, 0.0000, 0.0000, 0.0020, 0.0654, 0.8371, 0.0946, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0813, 0.6862, 0.2325,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000)

base <- matrix(base, nrow=8, ncol=8, byrow=TRUE)
stress <- matrix(stress, nrow=8, ncol=8, byrow=TRUE)
t1 <- matrix(t1, nrow=8, ncol=8, byrow=TRUE)

sum.variance <- function(s, base, stress, t1) {
t1.fitted <- (1-s)*base + s*stress
t1.variance <- (t1 - t1.fitted)^2
sum(t1.variance)
}

res <- optim(par=0.5, fn=sum.variance, method="BFGS", base=base, stress=stress, t1=t1)

sprintf("[S]=%f%%", res$par*100) sprintf("sum[t1 variance]=%f%%", res$value*100)


[1] "[S]=41.872702%"
[1] "sum[t1 variance]=2.561079%"