# Confidence Level function

Helloo

I am here asking is there a function in influxdb for confidence level. If there are any hints or guidance, please do feel free to comment in this!

Much appreciated!

Hello @KaiJ17,
There isnβt a confidence interval function in the stdlib but lets write one!

Iβm assuming youβre using 2.x and Flux.
Well also assume that your data follows a normal distribution and has a sample size > 30. Our alpha is 0.05 and our z score is 1.96.

The formula is:
mean(x) +/- (z * stddev / sqrt(n))

Here is what our Flux might look like:

``````import "experimental/array"
import "math"
data = from(bucket: "noaa")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "average_temperature")
|> filter(fn: (r) => r["_field"] == "degrees")
|> filter(fn: (r) => r["location"] == "coyote_creek")
//  |> yield(name: "raw")

z = 1.96
_mean = (data |> mean()
|> findRecord(idx: 0 , fn: (key) => true))._value

_stddev = (data |>stddev()
|> findRecord(idx: 0 , fn: (key) => true))._value

_sqrt_n = (data |> count()
|> map(fn: (r) => ({ r with _value: math.sqrt(x: float(v: r._value)) }))
|> findRecord(idx: 0 , fn: (key) => true))._value

pos = _mean + (z * _stddev / _sqrt_n)
neg = _mean - (z * _stddev / _sqrt_n)

// to visualize your confidence intervals
array.from(rows: [{pos: pos, neg: neg}])

// to filter for data in between confidence intervals
data
|> filter(fn: (r) => r._value >= neg and r._value <= pos)
|> yield(name: "between confidence intervals")
``````

@KaiJ17,
We can also write it like a function like this:

``````import "experimental/array"
import "math"
data = from(bucket: "noaa")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "average_temperature")
|> filter(fn: (r) => r["_field"] == "degrees")
|> filter(fn: (r) => r["location"] == "coyote_creek")
//   |> yield(name: "raw")

CI = (tables=<-) => { tables

z = 1.96
_mean = (tables |> mean()
|> findRecord(idx: 0 , fn: (key) => true))._value

_stddev = (tables |>stddev()
|> findRecord(idx: 0 , fn: (key) => true))._value

_sqrt_n = (tables |> count()
|> map(fn: (r) => ({ r with _value: math.sqrt(x: float(v: r._value)) }))
|> findRecord(idx: 0 , fn: (key) => true))._value

pos = _mean + (z * _stddev / _sqrt_n)
neg = _mean - (z * _stddev / _sqrt_n)
array.from(rows: [{pos: pos, neg: neg}])

return
tables
|> filter(fn: (r) => r._value >= neg and r._value <= pos)
|> yield(name: "between confidence intervals")
}

data |> CI()
``````
1 Like