Given a matrix:
ClearAll[mat, refR, refC];
mat = {
{21.2, 29, 19.9},
{9, 48.8, 22.4},
{49.8, 62.2, 7.8}
};
I want to adjust the elements of mat
to construct a new matrix satisfying the following row refR
and column refC
sums:
refR = {75, 82, 125};
refC = {87, 150, 45};
At 1st stage I adjust the rows by the following code:
mat1 = Table[(mat[[i, j]]/Total[mat, {2}][[i]])*refR[[i]], {i,3}, {j, 3}];
Then adjust the columns by:
mat2 = Table[(mat1[[j, i]]/Total[mat1][[i]])*refC[[i]], {j, 3}, {i, 3}];
Thereafter, I repeatedly carry out row and column adjustments:
mat3 = Table[(mat2[[i, j]]/Total[mat2, {2}][[i]])*refR[[i]], {i, 3}, {j, 3}];
mat4 = Table[ (mat3[[j, i]]/Total[mat3][[i]])*refC[[i]], {j, 3}, {i, 3}];
mat5 = Table[ (mat4[[i, j]]/Total[mat4, {2}][[i]])*refR[[i]], {i, 3}, {j, 3}];
mat6 = Table[ (mat5[[j, i]]/Total[mat5][[i]])*refC[[i]], {j, 3}, {i, 3}];
mat7 = Table[ (mat6[[i, j]]/Total[mat6, {2}][[i]])*refR[[i]], {i, 3}, {j, 3}];
mat8 = Table[ (mat7[[j, i]]/Total[mat7][[i]])*refC[[i]], {j, 3}, {i, 3}];
mat9 = Table[ (mat8[[i, j]]/Total[mat8, {2}][[i]])*refR[[i]], {i, 3}, {j, 3}];
mat10 = Table[ (mat9[[j, i]]/Total[mat9][[i]])*refC[[i]], {j, 3}, {i, 3}];
mat11 = Table[ (mat10[[i, j]]/Total[mat10, {2}][[i]])*refR[[i]], {i, 3}, {j, 3}];
mat12 = Table[ (mat11[[j, i]]/Total[mat11][[i]])*refC[[i]], {j, 3}, {i, 3}];
mat13 = Table[ (mat12[[i, j]]/Total[mat12, {2}][[i]])*refR[[i]], {i, 3}, {j, 3}];
mat14 = Table[ (mat13[[j, i]]/Total[mat13][[i]])*refC[[i]], {j, 3}, {i, 3}];
until the following conditions hold TRUE:
Total[mat14, {2}] == refR
Total[mat14] == refC
There must be a clever way of doing these iterations to balance the matrix mat
to the new reference row and column sums as above.
Can a single function be created? Such as
matrixBalance[m_Matrix, refR_, refC_, mp_]:=
where m_Matrix
is the original unbalanced matrix, refR
is the reference vector of row totals, refC
is the reference vector of column totals, and mp
is the number of matrix iterations. The outcome of this function should be the final balanced matrix satisfying refR
and refC
. In economics, this repeated iteration method is called RAS method and is widely used in the balancing of Input-Output matrix and/or Social Accounting Matrix.
Any improvement on the above code is appreciated...
Total[refR]=Total[refC]
. $\endgroup$refR
andrefC
the conditionTotal[refR]=Total[refC]
is not satisfied (and your iterative scheme oscillates indefinitely). Is it acceptable to pre-processrefR
andrefC
to ensureTotal[refR]=Total[refC]
? $\endgroup$refC[[1]]=87
is the correct input. It is necessary to pre-process the conditionTotal[refR] = Total[refC]
. The type of matrices I have has non-negative elements (i.e., input coefficients of a production function), meaning (1) if the original matrix element is positive, the resulting number cannot be0
and must be positive but maybe very small, (2) if the original matrix element is equal tozero
, it has to remain aszero
. Zero elements cannot be positive after iterations. $\endgroup$