I am looking for some GAUSS code to compute the RAS algorithm. For example, if I have a matrix:

```
10.14 2.118 22.17 8.13
5.80 13.00 0.35 4.38
16.99 8.59 7.22 5.70
1.92 3.35 0.95 4.77
```

and I want to balance the matrix such that the new row sums are:

```
42.55
23.53
38.50
11.00
```

and the new column sums are:

`34.85 27.06 30.69 22.98`

## 3 Answers

0

Here is a GAUSS procedure with a simple example for computing the RAS algorithm:

```
//Initial matrix
x = { 10.14 2.118 22.17 8.13,
5.80 13.00 0.35 4.38,
16.99 8.59 7.22 5.70,
1.92 3.35 0.95 4.77 };
//desired row sums
new_row_sum = { 42.55,
23.53,
38.50,
11.00 };
//desired column sums
new_col_sum = { 34.85 27.06 30.69 22.98 };
//Balance matrix to new row and column sums
new_x = RAS(X, new_row_sum, new_col_sum);
proc (1) = RAS(X, u, v);
local N, delta, A_odd, A_even, r, s, tol, max_iters;
//Convergence tolerance
tol = 1e-3;
//maximum iterations, so we don't go into infinite loop
max_iters = 1e7;
N = cols(X);
//pre-initialize to something non-zero
A_odd = ones(rows(x),cols(x));
for (1, max_iters, 1);
r = u ./ sumr(A_odd);
A_even = r .* A_odd;
s = v ./ sumc(A_even)';
A_odd = A_even .* s;
//Change from last iteration
delta = maxc(vecr(abs(A_odd-A_even)));
//If last update is less than tol, we are done
if delta < tol;
retp(A_odd);
endif;
endfor;
errorlogat "Warning : 'RAS' maximum number of iterations exceeded";
retp(A_odd);
endp;
```

0

Hi,

I am sorry to say that in my opinion this is not correct.

First of all the desired row and column sums are the actual row and column sums from the initial x-matrix except for a 0.01 difference in the third row sum. I do not know if this is the difference that is required to be moved by the RAS procedure?

Secondly, the suggested procedure uses a matrix of ones as the initial matrix, A_odd = ones(rows(x),cols(x)); which is an error. In this case you should start with the original matrix.

A_odd = x ;

Despite the fact that it is a very small difference that is to balanced away by the RAS procedure, your solution comes up with a completely different matrix, which a big mistake which is due to the use of a matrix of ones as the initial matrix

new_x

12.83 9.96 11.30 8.46

7.09 5.51 6.25 4.68

11.61 9.01 10.22 7.65

3.32 2.58 2.92 2.19

The aim of the RAS procedure is to change the initial matrix as ittle as possible while adapting to the new row and column totals.

0

Sorry, the 0.01 difference is both in the first and fourth row, not in the third.

## Your Answer

## 3 Answers

Here is a GAUSS procedure with a simple example for computing the RAS algorithm:

```
//Initial matrix
x = { 10.14 2.118 22.17 8.13,
5.80 13.00 0.35 4.38,
16.99 8.59 7.22 5.70,
1.92 3.35 0.95 4.77 };
//desired row sums
new_row_sum = { 42.55,
23.53,
38.50,
11.00 };
//desired column sums
new_col_sum = { 34.85 27.06 30.69 22.98 };
//Balance matrix to new row and column sums
new_x = RAS(X, new_row_sum, new_col_sum);
proc (1) = RAS(X, u, v);
local N, delta, A_odd, A_even, r, s, tol, max_iters;
//Convergence tolerance
tol = 1e-3;
//maximum iterations, so we don't go into infinite loop
max_iters = 1e7;
N = cols(X);
//pre-initialize to something non-zero
A_odd = ones(rows(x),cols(x));
for (1, max_iters, 1);
r = u ./ sumr(A_odd);
A_even = r .* A_odd;
s = v ./ sumc(A_even)';
A_odd = A_even .* s;
//Change from last iteration
delta = maxc(vecr(abs(A_odd-A_even)));
//If last update is less than tol, we are done
if delta < tol;
retp(A_odd);
endif;
endfor;
errorlogat "Warning : 'RAS' maximum number of iterations exceeded";
retp(A_odd);
endp;
```

Hi,

I am sorry to say that in my opinion this is not correct.

First of all the desired row and column sums are the actual row and column sums from the initial x-matrix except for a 0.01 difference in the third row sum. I do not know if this is the difference that is required to be moved by the RAS procedure?

Secondly, the suggested procedure uses a matrix of ones as the initial matrix, A_odd = ones(rows(x),cols(x)); which is an error. In this case you should start with the original matrix.

A_odd = x ;

Despite the fact that it is a very small difference that is to balanced away by the RAS procedure, your solution comes up with a completely different matrix, which a big mistake which is due to the use of a matrix of ones as the initial matrix

new_x

12.83 9.96 11.30 8.46

7.09 5.51 6.25 4.68

11.61 9.01 10.22 7.65

3.32 2.58 2.92 2.19

The aim of the RAS procedure is to change the initial matrix as ittle as possible while adapting to the new row and column totals.

Sorry, the 0.01 difference is both in the first and fourth row, not in the third.