New Parallel For Loops

The new parallel for loops provided by the new GAUSS keyword threadFor, in version 15 give you a compact method to add threading to your GAUSS program.

Rolling Regression Example

Rolling regression is a natural candidate for parallelization. First, we load some data and prepare our variables for the rolling regression:

//Load AR(1) data
filename = "ar1data.xlsx";
range = "a2:b501";
sheet = 1;
data = SpreadsheetReadM(filename,range,sheet);

//Create x and y variable
x = packr(lag1(data[.,2]));
y = data[2:rows(data),2];

//Window for rolling regression
window = 50;

Now we are ready to perform the regression. Here is an implementation using a standard for loop:

//Pre-initialize vector to hold estimates
b = zeros(rows(x)-window,1);

//Estimate each window
for i(1,rows(x)-window,1);
    b[i] = y[i:i+window]/x[i:i+window];
endfor;

//Combine and print out our results
print "average beta"; meanc(b);

Adding parallelization

The threads created by threadFor all share the same memory, so each thread can access any of the GAUSS global variables. There is no problem reading from the same global variable in multiple threads. With the threadFor loop in GAUSS you can even assign to the same global variable from multiple threads as long as you are not assigning to the same elements from more than one thread. Therefore, all we can parallelize the above loop, by simply change the for to threadFor and the endfor to threadEndFor like this:
//Pre-initialize vector to hold estimates
b = zeros(rows(x)-window,1);

//Estimate each window in parallel
threadFor i(1,rows(x)-window,1);
    b[i] = y[i:i+window]/x[i:i+window];
threadEndFor;

//Combine and print out our results
print "average beta"; meanc(b);

Have a Specific Question?

Get a real answer from a real person

Need Support?

Get help from our friendly experts.

Try GAUSS for 14 days for FREE

See what GAUSS can do for your data

© Aptech Systems, Inc. All rights reserved.

Privacy Policy