> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify-poc.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# covariance()

> Calculate the covariance from a two-dimensional statistical aggregate

<Icon icon="tag" iconType="duotone" /> Since [1.3.0][toolkit-1.3.0]

Calculate the covariance from a two-dimensional statistical aggregate. The calculation uses the standard least-squares
fitting for linear regression.

## Samples

Calculate the covariance of independent variable `y` and dependent variable `x` for each 15-minute time bucket:

```sql theme={"dark"}
SELECT
    id,
    time_bucket('15 min'::interval, ts) AS bucket,
    covariance(stats_agg(y, x)) AS summary
FROM foo
GROUP BY id, time_bucket('15 min'::interval, ts)
```

## Arguments

The syntax is:

```sql theme={"dark"}
covariance(
    summary StatsSummary2D,
    [ method TEXT ]
) RETURNS DOUBLE PRECISION
```

| Name    | Type           | Default  | Required | Description                                                                                                                                |
| ------- | -------------- | -------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
| summary | StatsSummary2D | -        | ✔        | The statistical aggregate produced by a `stats_agg` call                                                                                   |
| method  | TEXT           | `sample` | -        | The method used for calculating the covariance. The two options are `population` and `sample`, which can be abbreviated to `pop` or `samp` |

## Returns

| Column     | Type             | Description                                  |
| ---------- | ---------------- | -------------------------------------------- |
| covariance | DOUBLE PRECISION | The covariance of the least-squares fit line |

[toolkit-1.3.0]: https://github.com/timescale/timescaledb-toolkit/releases/tag/1.3.0
