Custom Aggregate Functions

Descriptions and examples of custom CGE aggregate functions.

CGE supports the following custom aggregate functions:
  • variance
  • standard deviation
  • geometric mean
  • mode
  • median
Table 1. Custom Aggregate Functions
FunctionPurpose
varianceReturns the variance of an expression.
standard deviationCalculates the standard deviation of a set of numeric values. Requires at least two values.
geometric meanCalculates the nth root of the product of the numbers, where n is the count of numbers.
modeReturns the most frequently occurring number in a group of supplied arguments.
medianCalculate the median, which is the value separating the higher half of a data sample, a population, or a probability distribution, from the lower half.

Examples

  • variance
    SELECT ?p (AGG<http://www.dotnetrdf.org/leviathan#variance>(?o) AS ?RESULT)
    WHERE
      { ?s ?p ?o}
    GROUP BY ?p
  • geometric mean
    SELECT ?p (AGG<http://www.dotnetrdf.org/leviathan#geometric_mean>(?o) AS ?RESULT)
    WHERE
      { ?s ?p ?o}
    GROUP BY ?p
  • standard deviation
    SELECT ?p (AGG<http://www.dotnetrdf.org/leviathan#standard_deviation>(?o) AS ?RESULT)
    WHERE
      { ?s ?p ?o}
    GROUP BY ?p
  • mode
    SELECT ?p (AGG<http://www.dotnetrdf.org/leviathan#mode>(?o) AS ?RESULT)
    WHERE
      { ?s ?p ?o}
    GROUP BY ?p
  • median
    SELECT ?p (AGG<http://www.dotnetrdf.org/leviathan#median>(?o) AS ?RESULT)
    WHERE
      { ?s ?p ?o}
    GROUP BY ?p
Custom aggregate functions can be freely mixed together or with standard SPARQL aggregate functions, such as:
  • SUM
  • MIN
  • MAX
  • SAMPLE
  • AVG
  • GROUPCONCAT
For example:
SELECT ?p 
    (AGG<http://www.dotnetrdf.org/leviathan#variance>(?o) AS ?RESULT1)

   (SUM (?o) AS ?RESULT2)
    (AGG<http://www.dotnetrdf.org/leviathan#median>(?o) AS ?RESULT3)
CAUTION: The DISTINCT flavors of custom aggregates are currently not supported.

Geometric Mean

Geometric mean is defined as the nth root of the product of n values. The product’s absolute value is used under the radical sign to avoid negative numbers. The result in this case will be zero. The product’s absolute value is used under the radical sign to avoid negative numbers. In other words, if the product happens to be negative, that value is negated to make it positive and then its root is retrieved.