# Statistics reference

> All 16 statistics in AlgoBarsX, each with its parameters, defaults, ranges and an example: stdev, mean, median, variance, percentile, percent_rank, zscore, returns, correlation, covariance and 6 more.

Source: https://algobarsx.com/docs/ref-fn-statistics/

### `stdev(source = close, length = 20)` → distance of source

Standard deviation.

#### Parameters

| Name | Type | Default | Range | What it is |
| --- | --- | --- | --- | --- |
| `source` | series<number> | `close` |  | Series to calculate from. |
| `length` | int | `20` | 1 to 5000 | Number of bars in the calculation. |

```algobarsx
dispersion = stdev(close, 20)
```

Works in: strategy, indicator, alert, library. Since 1.0.

### `mean(source = close, length = 20)` → like source

Mean value.

#### Parameters

| Name | Type | Default | Range | What it is |
| --- | --- | --- | --- | --- |
| `source` | series<number> | `close` |  | Series to calculate from. |
| `length` | int | `20` | 1 to 5000 | Number of bars in the calculation. |

```algobarsx
avg = mean(close, 20)
```

Works in: strategy, indicator, alert, library. Since 1.0.

### `median(source = close, length = 20)` → like source

Median value.

#### Parameters

| Name | Type | Default | Range | What it is |
| --- | --- | --- | --- | --- |
| `source` | series<number> | `close` |  | Series to calculate from. |
| `length` | int | `20` | 1 to 5000 | Number of bars in the calculation. |

```algobarsx
mid = median(close, 20)
```

Works in: strategy, indicator, alert, library. Since 1.0.

### `variance(source = close, length = 20)` → series<number>

Variance.

#### Parameters

| Name | Type | Default | Range | What it is |
| --- | --- | --- | --- | --- |
| `source` | series<number> | `close` |  | Series to calculate from. |
| `length` | int | `20` | 1 to 5000 | Number of bars in the calculation. |

```algobarsx
var_now = variance(close, 20)
```

Works in: strategy, indicator, alert, library. Since 1.0.

### `percentile(source = close, length = 100, percent = 50%)` → like source

Value at a percentile of recent values.

#### Parameters

| Name | Type | Default | Range | What it is |
| --- | --- | --- | --- | --- |
| `source` | series<number> | `close` |  | Series to calculate from. |
| `length` | int | `100` | 1 to 5000 | Number of bars in the calculation. |
| `percent` | percent | `50%` |  | Percentile to return. |

```algobarsx
p90 = percentile(close, 100, percent: 90%)
```

Works in: strategy, indicator, alert, library. Since 1.0.

### `percent_rank(source = close, length = 100)` → series<number>

How many of the recent values are below this one, as a percentage from 0 to 100.

#### Parameters

| Name | Type | Default | Range | What it is |
| --- | --- | --- | --- | --- |
| `source` | series<number> | `close` |  | Series to calculate from. |
| `length` | int | `100` | 1 to 5000 | Number of bars in the calculation. |

```algobarsx
standing = percent_rank(close, 100)
```

Works in: strategy, indicator, alert, library. Since 1.0.

### `zscore(source = close, length = 20)` → series<number>

Standard score of the current value.

#### Parameters

| Name | Type | Default | Range | What it is |
| --- | --- | --- | --- | --- |
| `source` | series<number> | `close` |  | Series to calculate from. |
| `length` | int | `20` | 1 to 5000 | Number of bars in the calculation. |

```algobarsx
z = zscore(close, 20)
```

Works in: strategy, indicator, alert, library. Since 1.0.

### `returns(source = close, periods = 1)` → series<number>

Fractional change between bars.

#### Parameters

| Name | Type | Default | Range | What it is |
| --- | --- | --- | --- | --- |
| `source` | series<number> | `close` |  | Series to calculate from. |
| `periods` | int | `1` | 1 to 5000 | Bars between the two values. |

```algobarsx
r1 = returns(close)
```

Works in: strategy, indicator, alert, library. Since 1.0.

### `correlation(a, b, length = 50)` → series<number>

Pearson correlation.

#### Parameters

| Name | Type | Default | Range | What it is |
| --- | --- | --- | --- | --- |
| `a` | series<number> | required |  | First series. |
| `b` | series<number> | required |  | Second series. |
| `length` | int | `50` | 1 to 5000 | Number of bars in the calculation. |

```algobarsx
corr = correlation(close_of(EURUSD), close_of(GBPUSD), 50)
```

Works in: strategy, indicator, alert, library. Since 1.0.

### `covariance(a, b, length = 50)` → series<number>

Covariance.

#### Parameters

| Name | Type | Default | Range | What it is |
| --- | --- | --- | --- | --- |
| `a` | series<number> | required |  | First series. |
| `b` | series<number> | required |  | Second series. |
| `length` | int | `50` | 1 to 5000 | Number of bars in the calculation. |

```algobarsx
cov_now = covariance(returns(close_of(EURUSD)), returns(close_of(GBPUSD)), 50)
```

Works in: strategy, indicator, alert, library. Since 1.0.

### `beta(asset, benchmark, length = 100)` → series<number>

Beta of an asset against a benchmark.

#### Parameters

| Name | Type | Default | Range | What it is |
| --- | --- | --- | --- | --- |
| `asset` | series<number> | required |  | Asset returns. |
| `benchmark` | series<number> | required |  | Benchmark returns. |
| `length` | int | `100` | 1 to 5000 | Number of bars in the calculation. |

```algobarsx
b = beta(returns(close_of(EURUSD)), returns(close_of(GBPUSD)), 100)
```

Works in: strategy, indicator, alert, library. Since 1.0.

### `skew(source = close, length = 50)` → series<number>

Skewness.

#### Parameters

| Name | Type | Default | Range | What it is |
| --- | --- | --- | --- | --- |
| `source` | series<number> | `close` |  | Series to calculate from. |
| `length` | int | `50` | 1 to 5000 | Number of bars in the calculation. |

```algobarsx
asymmetry = skew(returns(close), 50)
```

Works in: strategy, indicator, alert, library. Since 1.0.

### `kurtosis(source = close, length = 50)` → series<number>

Excess kurtosis.

#### Parameters

| Name | Type | Default | Range | What it is |
| --- | --- | --- | --- | --- |
| `source` | series<number> | `close` |  | Series to calculate from. |
| `length` | int | `50` | 1 to 5000 | Number of bars in the calculation. |

```algobarsx
tails = kurtosis(returns(close), 50)
```

Works in: strategy, indicator, alert, library. Since 1.0.

### `rank(source = close, length = 50)` → series<number>

Rank of the current value among recent values.

#### Parameters

| Name | Type | Default | Range | What it is |
| --- | --- | --- | --- | --- |
| `source` | series<number> | `close` |  | Series to calculate from. |
| `length` | int | `50` | 1 to 5000 | Number of bars in the calculation. |

```algobarsx
position_rank = rank(close, 50)
```

Works in: strategy, indicator, alert, library. Since 1.0.

### `autocorrelation(source = close, length = 50, lag = 1)` → series<number>

Autocorrelation at a lag.

#### Parameters

| Name | Type | Default | Range | What it is |
| --- | --- | --- | --- | --- |
| `source` | series<number> | `close` |  | Series to calculate from. |
| `length` | int | `50` | 1 to 5000 | Number of bars in the calculation. |
| `lag` | int | `1` | 1 to 500 | Bars of lag. |

```algobarsx
ac = autocorrelation(returns(close), 50, lag: 1)
```

Works in: strategy, indicator, alert, library. Since 1.0.

### `hurst(source = close, length = 100)` → series<number>

Hurst exponent, on returns rather than prices. Prices trend by construction, so hurst(close, n) reads high for a random walk, a trend and a mean-reverting market alike and cannot tell them apart; hurst(returns(close), n) can.

#### Parameters

| Name | Type | Default | Range | What it is |
| --- | --- | --- | --- | --- |
| `source` | series<number> | `close` |  | Series to calculate from. |
| `length` | int | `100` | 1 to 5000 | Number of bars in the calculation. |

```algobarsx
persistence = hurst(returns(close), 100)
```

Works in: strategy, indicator, alert, library. Since 1.0.
