2015-08-26 10:45:55 +08:00
|
|
|
--
|
2015-08-26 10:58:31 +08:00
|
|
|
-- Calculate basic statistics of a given dataset
|
2015-08-26 10:45:55 +08:00
|
|
|
--
|
2015-08-26 10:58:31 +08:00
|
|
|
-- @param in_array A numeric array of numbers
|
2015-08-26 10:45:55 +08:00
|
|
|
--
|
2015-08-26 10:58:31 +08:00
|
|
|
-- Returns: statistical quantity chosen
|
2015-12-05 03:54:15 +08:00
|
|
|
--
|
2015-08-26 11:10:26 +08:00
|
|
|
-- References: http://www.itl.nist.gov/div898/handbook/eda/section3/eda35b.htm
|
2015-08-26 10:45:55 +08:00
|
|
|
--
|
|
|
|
|
2015-08-26 11:10:26 +08:00
|
|
|
-- Calculate kurtosis
|
2019-05-31 21:29:28 +08:00
|
|
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_Kurtosis ( in_array NUMERIC[] ) RETURNS NUMERIC as $$
|
2015-08-26 10:58:31 +08:00
|
|
|
DECLARE
|
2015-08-26 10:45:55 +08:00
|
|
|
a numeric;
|
|
|
|
c numeric;
|
|
|
|
k numeric;
|
|
|
|
BEGIN
|
2015-12-05 03:54:15 +08:00
|
|
|
SELECT AVG(e), COUNT(e)::numeric * power(stddev(e),4) INTO a, c FROM ( SELECT unnest(in_array) e ) x;
|
|
|
|
|
|
|
|
IF c=0 THEN
|
|
|
|
RETURN 0;
|
|
|
|
ELSE
|
2015-08-26 10:45:55 +08:00
|
|
|
|
2015-12-05 03:54:15 +08:00
|
|
|
EXECUTE 'SELECT sum(power($1 - e, 4)) / ($2 ) - 3
|
|
|
|
FROM (SELECT unnest($3) e ) x'
|
|
|
|
INTO k
|
|
|
|
USING a, c, in_array;
|
2015-08-26 10:45:55 +08:00
|
|
|
|
2015-12-05 03:54:15 +08:00
|
|
|
RETURN k;
|
|
|
|
END IF;
|
2015-08-26 10:45:55 +08:00
|
|
|
END;
|
2017-10-24 20:16:56 +08:00
|
|
|
$$ language plpgsql IMMUTABLE STRICT PARALLEL SAFE;
|
2015-08-26 10:58:31 +08:00
|
|
|
|
2015-08-26 11:10:26 +08:00
|
|
|
-- Calculate skewness
|
2019-05-31 21:29:28 +08:00
|
|
|
CREATE OR REPLACE FUNCTION @extschema@.CDB_Skewness ( in_array NUMERIC[] ) RETURNS NUMERIC as $$
|
2015-08-26 10:58:31 +08:00
|
|
|
DECLARE
|
|
|
|
a numeric;
|
|
|
|
c numeric;
|
|
|
|
sk numeric;
|
|
|
|
BEGIN
|
2015-12-05 03:54:15 +08:00
|
|
|
SELECT AVG(e), COUNT(e)::numeric * power(stddev(e),3) INTO a, c FROM ( SELECT unnest(in_array) e ) x;
|
|
|
|
IF c=0 THEN
|
|
|
|
RETURN 0;
|
|
|
|
ELSE
|
|
|
|
EXECUTE 'SELECT sum(power($1 - e, 3)) / ( $2 )
|
|
|
|
FROM (SELECT unnest($3) e ) x'
|
|
|
|
INTO sk
|
|
|
|
USING a, c, in_array;
|
2015-08-26 10:58:31 +08:00
|
|
|
|
2015-12-05 03:54:15 +08:00
|
|
|
RETURN sk;
|
|
|
|
END IF;
|
2015-08-26 10:58:31 +08:00
|
|
|
END;
|
2017-10-24 20:16:56 +08:00
|
|
|
$$ language plpgsql IMMUTABLE STRICT PARALLEL SAFE;
|