Prototype: variance(list)

Return type: real

Description: Return the variance of the numbers in list.

This function can accept many types of data parameters.

Arguments:

  • list: string - CFEngine variable identifier or inline JSON - in the range: .*

Use the eval() function to easily get the standard deviation (square root of the variance).

This is not part of a full statistical package but a convenience function.

Example:

code
body common control
{
      bundlesequence => { "test" };
}

bundle agent test
{
  vars:
      # the behavior will be the same whether you use a data container or a list
      # "mylist" slist => { "foo", "1", "2", "3000", "bar", "10.20.30.40" };
      "mylist" data => parsejson('["foo", "1", "2", "3000", "bar", "10.20.30.40"]');
      "mylist_str" string => format("%S", mylist);

      "max_int" string => max(mylist, "int");
      "max_lex" string => max(mylist, "lex");
      "max_ip" string => max(mylist, "ip");

      "min_int" string => min(mylist, "int");
      "min_lex" string => min(mylist, "lex");
      "min_ip" string => min(mylist, "ip");

      "mean" real => mean(mylist);
      "variance" real => variance(mylist);

  reports:
      "my list is $(mylist_str)";

      "mean is $(mean)";
      "variance is $(variance) (use eval() to get the standard deviation)";

      "max int is $(max_int)";
      "max IP is $(max_ip)";
      "max lexicographically is $(max_lex)";

      "min int is $(min_int)";
      "min IP is $(min_ip)";
      "min lexicographically is $(min_lex)";
}

Output:

code
R: my list is ["foo","1","2","3000","bar","10.20.30.40"]
R: mean is 502.200000
R: variance is 1497376.000000 (use eval() to get the standard deviation)
R: max int is 3000
R: max IP is 10.20.30.40
R: max lexicographically is foo
R: min int is bar
R: min IP is 1
R: min lexicographically is 1

History: Was introduced in version 3.6.0 (2014). The collecting function behavior was added in 3.9.

See also: sort(), mean(), sum(), max(), min(), about collecting functions, and data documentation.