You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
786 B
35 lines
786 B
FUNCTION covariance (x , y ) {
|
|
xmean := avg (x) ;
|
|
ymean := avg (y) ;
|
|
avg (( x - xmean ) * (y - ymean ))
|
|
}
|
|
|
|
|
|
FUNCTION sd ( x) {
|
|
sqrt ( covariance (x , x) )
|
|
}
|
|
|
|
AGGREGATION FUNCTION covariances(x, y, w){
|
|
static xmeans := 0, ymeans := 0, cnt := 0;
|
|
if (cnt < w) { xmeans += x; }
|
|
else {
|
|
xmeans += (x - x.vec[cnt - w]) / w;
|
|
ymeans += (y - y.vec[cnt - w]) / w;
|
|
}
|
|
avg (( x.vec(x.len-w, x.len) - xmean ) * (y.vec(y.len - w, y.len) - ymean ))
|
|
}
|
|
|
|
FUNCTION pairCorr (x , y ) {
|
|
covariance (x , y ) / ( sd (x) * sd (y ))
|
|
}
|
|
|
|
CREATE TABLE test1(a INT, b INT, c INT, d INT)
|
|
|
|
LOAD DATA INFILE "test.csv"
|
|
INTO TABLE test1
|
|
FIELDS TERMINATED BY ","
|
|
|
|
SELECT pairCorr(c, b) * d, sum(a), b
|
|
FROM test1
|
|
group by c,b,d
|
|
order by b ASC |