# How to compute/calculate a Confidence Interval (CI)?

**URL:** <https://discourse.julialang.org/t/how-to-compute-calculate-a-confidence-interval-ci/74794>\
**Category:** General Usage\
**Tags:** statistics, distributions, hypothesis-tests\
**Created:** [January 18, 2022, 11:48am UTC](https://discourse.julialang.org/t/how-to-compute-calculate-a-confidence-interval-ci/74794 "2022-01-18T11:48:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)\
**Post date:** [January 18, 2022, 11:48am UTC](https://discourse.julialang.org/t/how-to-compute-calculate-a-confidence-interval-ci/74794/1 "2022-01-18T11:48:02Z")

</div>

How can I calculate a confidence interval for a given distribution?

---

<div class="post-metadata">

**Author:** ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)\
**Post date:** [January 18, 2022, 12:01pm UTC](https://discourse.julialang.org/t/how-to-compute-calculate-a-confidence-interval-ci/74794/2 "2022-01-18T12:01:48Z")

</div>

First, you can decide on the interval width. Normal values are 90%, 95% and 99%, with 95% being the most common by far. This is commonly called the “confidence level”:

```julia
julia> level = 0.95
0.95

```

Then, I would define a significance level, normally denoted α:

```julia
julia> α = 1-level
0.050000000000000044 # Some floating point error. Can be avoided by defining α directly.

```

Finally, define the distribution you wish to calculate confidence intervals for:

```julia
julia> using Distributions

julia> d = Normal() # Defaults to the standard normal distribution.
Normal{Float64}(μ=0.0, σ=1.0)

```

You all now all set to compute a confidence interval, using the `quantile` function provided by `Distributions.jl`:

```julia
julia> quantile(d, α/2), quantile(d, 1-α/2)
(-1.9599639845400576, 1.9599639845400576)

```

You could give the interval a tail in the following manner:

```julia
julia> -Inf, quantile(d, 1-α) # A so-called "left tail"
(-Inf, 1.6448536269514717)

julia> quantile(d, α), Inf # A so-called "right tail"
(-1.6448536269514717, Inf)

```

All the intervals above, defined by their limits as a Tuple, contain 95% of the probability, so to speak.

Below is a function I made to automate the process, which can be copied directly:

```julia
"""
    confint(d, width; tail)
    confint(d; α, tail)

Return a confidence interval of the distribution `d`.

Use the positional argument `width` or the keyword-argument α
to set the width of the interval, where `width=1-α`.

For a one-sided interval, where one "tail" extends to infinity, set
which tail should extend to infinity with the keyword argument `tail`.
Possible values are `:none` (default value), `:left` and `:right`.

Example:
julia> using Distributions

d = Normal();

julia> confint(d, 0.95)
(-1.9599639845400576, 1.9599639845400576)

julia> confint(d, α = 0.05)
(-1.9599639845400592, 1.9599639845400576)
"""
function confint(d::Distribution; α::AbstractFloat, tail::Symbol = :none)
    !(0 ≤ α ≤ 1) && error("The given interval width ($(1-α)) is not between 0 and 1.")
    if tail == :none
        return (quantile(d, α / 2), quantile(d, 1 - α / 2))
    elseif tail == :left
        return (-Inf, quantile(d, 1 - α))
    elseif tail == :right
        return (quantile(d, α), Inf)
    else
        error("Tail not recognized. Chose from :none, :left or :right.")
    end
end
confint(d::Distribution, width::AbstractFloat; kwargs...) = confint(d; α = 1 - width, kwargs...)

```

Note that if you are also using `HypothesisTests.jl` or `StatsBase.jl`, they also export `confint`, which leads to a conflict. This can be avoided by importing the function from the other library before defining your own:

```julia
using HypothesisTests
import HypothesisTests: confint

#Now define the function yourself

```

Because α is a keyword argument, if you have defined a variable `α`, you can call it with the following syntax:

```julia
confint(d; α)

```

---

<div class="post-metadata">

**Author:** ![Ashu](https://avatars.discourse-cdn.com/v4/letter/a/4bbf92/32.png) [@Ashu](https://discourse.julialang.org/u/Ashu)\
**Post date:** [May 16, 2023, 6:47am UTC](https://discourse.julialang.org/t/how-to-compute-calculate-a-confidence-interval-ci/74794/3 "2023-05-16T06:47:55Z")

</div>

Dear @TheLateKronos,

Is the confidence interval obtained using this code applicable to both normal and Gaussian distribution? If yes, do I need to modify something in the code? If not, what needs to be modify in the code for Guassian distrubution of samples?

Thank you

---

<div class="post-metadata">

**Author:** ![vettert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vettert/32/30599_2.png) [@vettert](https://discourse.julialang.org/u/vettert)\
**Post date:** [May 16, 2023, 6:59am UTC](https://discourse.julialang.org/t/how-to-compute-calculate-a-confidence-interval-ci/74794/4 "2023-05-16T06:59:41Z")

</div>

A Gaussian distribution IS a normal distribution, so the code is applicable without change.

---

<div class="post-metadata">

**Author:** ![Ashu](https://avatars.discourse-cdn.com/v4/letter/a/4bbf92/32.png) [@Ashu](https://discourse.julialang.org/u/Ashu)\
**Post date:** [May 16, 2023, 10:54am UTC](https://discourse.julialang.org/t/how-to-compute-calculate-a-confidence-interval-ci/74794/5 "2023-05-16T10:54:59Z")

</div>

Thank you.  
I got confused with word “Normal”. By “Normal” I meant to say for normal random sampling of parameters, I calculated steady states of a function.
