-
Notifications
You must be signed in to change notification settings - Fork 7
/
array_to_kurtosis.c
148 lines (126 loc) · 4.24 KB
/
array_to_kurtosis.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
Datum array_to_kurtosis(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(array_to_kurtosis);
/**
* Returns the kurtosis from an array of numbers.
* by Paul A. Jungwirth
*/
Datum
array_to_kurtosis(PG_FUNCTION_ARGS)
{
// Our arguments:
ArrayType *vals;
// The array element type:
Oid valsType;
// The array element type widths for our input array:
int16 valsTypeWidth;
// The array element type "is passed by value" flags (not really used):
bool valsTypeByValue;
// The array element type alignment codes (not really used):
char valsTypeAlignmentCode;
// The array contents, as PostgreSQL "Datum" objects:
Datum *valsContent;
// List of "is null" flags for the array contents (not used):
bool *valsNullFlags;
// The size of the input array:
int valsLength;
float8 mean = 0;
float8 stddev = 0;
float8 ret = 0;
int i;
if (PG_ARGISNULL(0)) {
ereport(ERROR, (errmsg("Null arrays not accepted")));
}
vals = PG_GETARG_ARRAYTYPE_P(0);
if (ARR_NDIM(vals) == 0) {
PG_RETURN_NULL();
}
if (ARR_NDIM(vals) > 1) {
ereport(ERROR, (errmsg("One-dimesional arrays are required")));
}
if (array_contains_nulls(vals)) {
ereport(ERROR, (errmsg("Array contains null elements")));
}
// Determine the array element types.
valsType = ARR_ELEMTYPE(vals);
if (valsType != INT2OID &&
valsType != INT4OID &&
valsType != INT8OID &&
valsType != FLOAT4OID &&
valsType != FLOAT8OID) {
ereport(ERROR, (errmsg("Kurtosis subject must be SMALLINT, INTEGER, BIGINT, REAL, or DOUBLE PRECISION values")));
}
valsLength = (ARR_DIMS(vals))[0];
if (valsLength == 0) PG_RETURN_NULL();
get_typlenbyvalalign(valsType, &valsTypeWidth, &valsTypeByValue, &valsTypeAlignmentCode);
// Extract the array contents (as Datum objects).
deconstruct_array(vals, valsType, valsTypeWidth, valsTypeByValue, valsTypeAlignmentCode,
&valsContent, &valsNullFlags, &valsLength);
switch (valsType) {
case INT2OID:
for (i = 0; i < valsLength; i++) {
mean += (DatumGetInt16(valsContent[i]) - mean) / (i + 1);
}
for (i = 0; i < valsLength; i++) {
stddev += pow(DatumGetInt16(valsContent[i]) - mean, 2);
}
stddev = sqrt(stddev / valsLength);
for (i = 0; i < valsLength; i++) {
ret += pow(DatumGetInt16(valsContent[i]) - mean, 4) / valsLength;
}
break;
case INT4OID:
for (i = 0; i < valsLength; i++) {
mean += (DatumGetInt32(valsContent[i]) - mean) / (i + 1);
}
for (i = 0; i < valsLength; i++) {
stddev += pow(DatumGetInt32(valsContent[i]) - mean, 2);
}
stddev = sqrt(stddev / valsLength);
for (i = 0; i < valsLength; i++) {
ret += pow(DatumGetInt32(valsContent[i]) - mean, 4) / valsLength;
}
break;
case INT8OID:
for (i = 0; i < valsLength; i++) {
mean += (DatumGetInt64(valsContent[i]) - mean) / (i + 1);
}
for (i = 0; i < valsLength; i++) {
stddev += pow(DatumGetInt64(valsContent[i]) - mean, 2);
}
stddev = sqrt(stddev / valsLength);
for (i = 0; i < valsLength; i++) {
ret += pow(DatumGetInt64(valsContent[i]) - mean, 4) / valsLength;
}
break;
case FLOAT4OID:
for (i = 0; i < valsLength; i++) {
mean += (DatumGetFloat4(valsContent[i]) - mean) / (i + 1);
}
for (i = 0; i < valsLength; i++) {
stddev += pow(DatumGetFloat4(valsContent[i]) - mean, 2);
}
stddev = sqrt(stddev / valsLength);
for (i = 0; i < valsLength; i++) {
ret += pow(DatumGetFloat4(valsContent[i]) - mean, 4) / valsLength;
}
break;
case FLOAT8OID:
for (i = 0; i < valsLength; i++) {
mean += (DatumGetFloat8(valsContent[i]) - mean) / (i + 1);
}
for (i = 0; i < valsLength; i++) {
stddev += pow(DatumGetFloat8(valsContent[i]) - mean, 2);
}
stddev = sqrt(stddev / valsLength);
for (i = 0; i < valsLength; i++) {
ret += pow(DatumGetFloat8(valsContent[i]) - mean, 4) / valsLength;
}
break;
default:
ereport(ERROR, (errmsg("Kurtosis subject must be SMALLINT, INTEGER, BIGINT, REAL, or DOUBLE PRECISION values")));
break;
}
// TODO: What if stddev is zero?
ret /= pow(stddev, 4);
PG_RETURN_FLOAT8(ret);
}