Procedure for RAS Algorithm in GAUSS

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

1 Answer



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;

aptech

1,323

Your Answer


You must login to post answers.

Have a Specific Question?

Get a real answer from a real person

Need Support?

Get help from our friendly experts.

Try GAUSS for 30 days for FREE

See what GAUSS can do for your data

© Aptech Systems, Inc. All rights reserved.

Privacy Policy | Sitemap