adding skewness

This commit is contained in:
Andy Eschbacher 2015-08-25 22:58:31 -04:00
parent 49c4cea4e7
commit db323f3e13

View File

@ -1,18 +1,15 @@
-- --
-- Calculate the Pearson kurtosis of the input data -- Calculate basic statistics of a given dataset
-- --
-- @param in_array A numeric array of numbers to determine the best -- @param in_array A numeric array of numbers
-- to determine the bin boundary
-- --
-- @param breaks The number of bins you want to find. -- Returns: statistical quantity chosen
--
--
-- Returns: upper edges of bins
-- --
-- --
-- Calculate Pearson's moment coefficient of kurtosis
CREATE OR REPLACE FUNCTION CDB_Kurtosis ( in_array NUMERIC[] ) RETURNS NUMERIC as $$ CREATE OR REPLACE FUNCTION CDB_Kurtosis ( in_array NUMERIC[] ) RETURNS NUMERIC as $$
DECLARE DECLARE
a numeric; a numeric;
c numeric; c numeric;
s numeric; s numeric;
@ -22,12 +19,32 @@ BEGIN
RAISE NOTICE 'avg: %, cnt: %, std: %', a, c, s; RAISE NOTICE 'avg: %, cnt: %, std: %', a, c, s;
EXECUTE ' EXECUTE 'SELECT sum(power($1 - e, 4)) / ( $2 * power($3, 4))
SELECT sum(power($1 - e,4)) / ( $2 * power($3, 4)) FROM (SELECT unnest($4) e ) x'
FROM (SELECT unnest($4) e ) x'
INTO k INTO k
USING a, c, s, in_array; USING a, c, s, in_array;
RETURN k; RETURN k;
END; END;
$$ language plpgsql IMMUTABLE; $$ language plpgsql IMMUTABLE;
-- Calculate Pearson's moment coefficient of skewness
CREATE OR REPLACE FUNCTION CDB_Skewness ( in_array NUMERIC[] ) RETURNS NUMERIC as $$
DECLARE
a numeric;
c numeric;
s numeric;
sk numeric;
BEGIN
SELECT AVG(e), COUNT(e)::numeric, stddev(e) INTO a, c, s FROM ( SELECT unnest(in_array) e ) x;
RAISE NOTICE 'avg: %, cnt: %, std: %', a, c, s;
EXECUTE 'SELECT sum(power($1 - e, 3)) / ( $2 * power($3, 3))
FROM (SELECT unnest($4) e ) x'
INTO sk
USING a, c, s, in_array;
RETURN sk;
END;
$$ language plpgsql IMMUTABLE;