docs-v2/content/flux/v0.x/stdlib/universe/covariance.md

2.3 KiB

title description aliases menu weight flux/v0.x/tags related introduced
covariance() function The `covariance()` function computes the covariance between two columns.
/influxdb/v2.0/reference/flux/functions/transformations/aggregates/covariance
/influxdb/v2.0/reference/flux/functions/built-in/transformations/aggregates/covariance/
/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/aggregates/covariance/
/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/covariance/
/influxdb/cloud/reference/flux/stdlib/built-in/transformations/covariance/
flux_0_x_ref
name parent
covariance universe
102
transformations
/flux/v0.x/stdlib/universe/cov/
0.7.0

The covariance() function computes the covariance between two columns.

Output data type: Float

covariance(columns: ["column_x", "column_y"], pearsonr: false, valueDst: "_value")

Parameters

columns

({{< req >}}) A list of two columns on which to operate.

pearsonr

Normalized results to the Pearson R coefficient. Default is false.

valueDst

The column into which the result will be placed. Defaults to "_value".

tables

Input data. Default is piped-forward data (<-).

Examples

The following example uses generate.from() to generate sample data and show how covariance() transforms data.

import "generate"

data = generate.from(count: 5, fn: (n) => n * n, start: 2021-01-01T00:00:00Z, stop: 2021-01-01T00:01:00Z)
    |> toFloat()
    |> map(fn: (r) => ({_time: r._time, x: r._value, y: r._value * r._value / 2.0}))

data
    |> covariance(columns: ["x", "y"])

{{% expand "View input and output" %}} {{< flex >}} {{% flex-content %}}

Input data
_time x y
2021-01-01T00:00:00Z 0.0 0.0
2021-01-01T00:00:12Z 1.0 0.5
2021-01-01T00:00:24Z 4.0 8.0
2021-01-01T00:00:36Z 9.0 40.5
2021-01-01T00:00:48Z 16.0 128.0

{{% /flex-content %}} {{% flex-content %}}

Output data
_value
345.75

{{% /flex-content %}} {{< /flex >}} {{% /expand %}}