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

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

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> level = 0.95
0.95

Then, I would define a significance level, normally denoted Ī±:

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> 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> quantile(d, Ī±/2), quantile(d, 1-Ī±/2)
(-1.9599639845400576, 1.9599639845400576)

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

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:

"""
    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:

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:

confint(d; Ī±)
2 Likes

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

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

1 Like

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.