I found usefull JS function that I want to implement into snowflake's UDF in order to move complex computation to the dwh.
http://trentrichardson.com/2010/04/06/compute-linear-regressions-in-javascript/
Attempt to rework above as SQL procedure:
CREATE OR REPLACE PROCEDURE JS_LNR_REG(a array, b array)
RETURNS ARRAY
LANGUAGE javascript
AS
$$
function linearRegression(y,x){
var lr = {};
var n = y.length;
var sum_x = 0;
var sum_y = 0;
var sum_xy = 0;
var sum_xx = 0;
var sum_yy = 0;
for (var i = 0; i < y.length; i++) {
sum_x += x[i];
sum_y += y[i];
sum_xy += (x[i]*y[i]);
sum_xx += (x[i]*x[i]);
sum_yy += (y[i]*y[i]);
}
lr['slope'] = (n * sum_xy - sum_x * sum_y) / (n*sum_xx - sum_x * sum_x);
lr['intercept'] = (sum_y - lr.slope * sum_x)/n;
return lr;} ; $$;
Execution fails with error: SQL compilation error: Invalid identifier
which is odd since procedure has been created.
SELECT JS_LNR_REG('[2,4,5,3,1]', '[5.2, 5.7, 5.0, 4.2]');