|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +//! Standard deviation enums for confidence bounds |
| 19 | +//! |
| 20 | +//! This module provides types for specifying confidence levels when computing |
| 21 | +//! upper and lower bounds for sketch estimates. |
| 22 | +
|
| 23 | +#[allow(clippy::excessive_precision)] |
| 24 | +const DELTA_OF_NUM_STD_DEVS: [f64; 4] = [ |
| 25 | + 0.5000000000000000000, // = 0.5 (1 + erf(0)) |
| 26 | + 0.1586553191586026479, // = 0.5 (1 + erf((-1/sqrt(2)))) |
| 27 | + 0.0227502618904135701, // = 0.5 (1 + erf((-2/sqrt(2)))) |
| 28 | + 0.0013498126861731796, // = 0.5 (1 + erf((-3/sqrt(2)))) |
| 29 | +]; |
| 30 | + |
| 31 | +/// Number of standard deviations for confidence bounds |
| 32 | +/// |
| 33 | +/// This enum specifies the number of standard deviations to use when computing |
| 34 | +/// upper and lower bounds for cardinality estimates. Higher values provide wider |
| 35 | +/// confidence intervals with greater certainty that the true cardinality falls |
| 36 | +/// within the bounds. |
| 37 | +#[repr(u8)] |
| 38 | +#[derive(Debug, Clone, Copy, PartialEq)] |
| 39 | +pub enum NumStdDev { |
| 40 | + /// One standard deviation (\~68% confidence interval) |
| 41 | + One = 1, |
| 42 | + /// Two standard deviations (\~95% confidence interval) |
| 43 | + Two = 2, |
| 44 | + /// Three standard deviations (\~99.7% confidence interval) |
| 45 | + Three = 3, |
| 46 | +} |
| 47 | + |
| 48 | +impl NumStdDev { |
| 49 | + /// Returns the tail probability (delta) for this confidence level |
| 50 | + pub const fn tail_probability(&self) -> f64 { |
| 51 | + DELTA_OF_NUM_STD_DEVS[*self as usize] |
| 52 | + } |
| 53 | +} |
0 commit comments