# Statistical Functions

## Overview

The univariate statistical functions listed below perform calculations on an array of numeric values stored in a window.

The functions cannot be invoked in filter expressions.

Functions such as `countIf`

, `avgIf`

, and `sumIf`

can perform calculations on a subset of values in the current window based on a boolean condition.

## Reference

`avg`

`mean`

`sum`

`min`

`max`

`wavg`

`wtavg`

`count`

`percentile`

`median`

`variance`

`stdev`

`intercept`

`first`

`last`

`diff`

`delta`

`new_maximum`

`new_minimum`

`threshold_time`

`threshold_linear_time`

`rate_per_second`

`rate_per_minute`

`rate_per_hour`

`slope`

`slope_per_second`

`slope_per_minute`

`slope_per_hour`

`countIf`

`avgIf`

`sumIf`

`avg`

```
avg() double
```

Calculates average value. For example, `avg()`

for a `5-minute`

time-based window returns the average value for all samples received within this period of time.

`mean`

```
mean() double
```

Calculates average value. Same as `avg()`

.

`sum`

```
sum() double
```

Sums all included values.

`min`

```
min() double
```

Returns minimum value.

`max`

```
max() double
```

Returns maximum value.

`wavg`

```
wavg() double
```

Calculates weighted average. Weight is the sample index which starts from `0`

for the first sample.

`wtavg`

```
wtavg() double
```

Calculates weighted time average. `Weight = (sample.time - first.time)/(last.time - first.time + 1)`

.

Time measured in Unix seconds.

`count`

```
count() long
```

Value count.

`percentile`

```
percentile(double n) double
```

Calculates `n`

-th percentile. `n`

multiplied by the number of values is the index for the function. `n`

can be a fractional number.

`median`

```
median() double
```

Returns 50% percentile (median). Same as `percentile(50)`

.

`variance`

```
variance() double
```

Calculates variance.

`stdev`

```
stdev() double
```

Returns standard deviation. Aliases: `stdev`

, `std_dev`

.

`intercept`

```
intercept() double
```

Calculates linear regression intercept.

`first`

```
first() double
```

Returns first series value. Same as `first(0)`

.

`first(integer i)`

```
first(integer i) double
```

Returns `i`

-th value from start. First value has index of `0`

.

`last`

```
last() double
```

Returns last value. Same as `last(0)`

.

`last(integer i)`

```
last(integer i) double
```

Returns `i`

-th value from final value. Last value has index of `0`

.

`diff`

```
diff() double
```

Calculates difference between `last`

and `first`

values. Same as `last() - first()`

.

`diff(integer i)`

```
diff(integer i) double
```

Calculates difference between `last(integer i)`

and `first(integer i)`

values. Same as `last(integer i)-first(integer i)`

.

`diff(string i)`

```
diff(string i) double
```

Calculates difference between the last value and value at `currentTime - interval`

.

Interval `i`

specified as `count unit`

, for example `5 minute`

.

`delta`

```
delta() double
```

Calculates difference between `last`

and `first`

values. Same as `diff()`

.

`new_maximum`

```
new_maximum() boolean
```

Returns `true`

if last value is greater than any previous value.

`new_minimum`

```
new_minimum() boolean
```

Returns `true`

if last value is smaller than any previous value.

`threshold_time`

```
threshold_time(double t) double
```

Forecasts the number of minutes until the sample value reaches the specified threshold `t`

based on extrapolation of the difference between the last and first value.

`threshold_linear_time`

```
threshold_linear_time(double t) double
```

Forecasts the number of minutes until the sample value reaches the specified threshold `t`

based on linear extrapolation.

`rate_per_second`

```
rate_per_second() double
```

Calculates the difference between last and first value per second. Same as `diff()/(last.time-first.time)`

. Time measured in Unix seconds.

`rate_per_minute`

```
rate_per_minute() double
```

Calculates the difference between last and first value per minute. Same as `rate_per_second()/60`

.

`rate_per_hour`

```
rate_per_hour() double
```

Calculates the hourly difference between last and first value input. Same as `rate_per_second()/3600`

.

`slope`

```
slope() double
```

Calculates linear regression slope.

`slope_per_second`

```
slope_per_second() double
```

Calculates linear regression slope.

`slope_per_minute`

```
slope_per_minute() double
```

Calculates `slope_per_second()/60`

.

`slope_per_hour`

```
slope_per_hour() double
```

Calculates `slope_per_second()/3600`

.

`countIf`

```
countIf(string c [, string i | integer n]) long
```

Counts elements matching the specified condition `c`

within interval `i`

or within the last `n`

samples.

Examples:

```
/* For values [0, 15, 5, 40] the function returns 2. */
countIf('value > 10')
```

```
/* Count of values exceeding 5 within the last 10 samples. */
countIf('value > 5', 10)
```

`avgIf`

```
avgIf(string c [, string i | integer n]) double
```

Calculates average of elements matching the specified condition `c`

within interval `i`

or within the last `n`

samples.

`sumIf`

```
sumIf(string c [, string i | integer n]) double
```

Sums elements matching the specified condition `c`

within interval `i`

or within the last `n`

samples.

## Interval Selection

By default, statistical functions calculate results based on all samples stored in a window. The range of samples can be adjusted by passing an optional argument - specified as sample count `c`

or interval `i`

- in which case the function calculates the result based on the most recent samples.

```
avg([string i | integer c]) double
```

`avg(5)`

: Average value for the last 5 samples.`avg('1 HOUR')`

: Average value for the last 1 hour.`max('2 minute')`

: Maximum value for the last 2 minutes.`percentile(95, '1 hour')`

: 95% percentile for the last hour.`countIf('value > 5', 10)`

: Count of values exceeding 5 within the last 10 samples.

Example:

The condition evaluates to `true`

if the 1-minute average is greater than the 1-hour average by more than `20`

and a maximum is reached in the last 5 samples.

```
avg('1 minute') - avg() > 20 && max(5) = max()
```