# How to calculate confidence interval?

**URL:** <https://discourse.julialang.org/t/how-to-calculate-confidence-interval/98876>\
**Category:** General Usage\
**Tags:** question, package\
**Created:** [May 15, 2023, 11:46am UTC](https://discourse.julialang.org/t/how-to-calculate-confidence-interval/98876 "2023-05-15T11:46:38Z")\
**Posts on this page:** 6\
**Page:** 1

<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 15, 2023, 11:46am UTC](https://discourse.julialang.org/t/how-to-calculate-confidence-interval/98876/1 "2023-05-15T11:46:38Z")

</div>

Dear All,

I have a random sample, let’s say, `smpl = {1, 4, 2, 6, 10, 18, 3, 5, 20}`. I wish to calculate a 99% confidence interval on `[minimum(smpl), maximum(smpl)]=[1, 20]`. I am able to calculate a 99% confidence interval for a `mean(smpl)`, but don’t know how to do it for the lower and upper values of a sample. Could you please help me to solve it? In the actual sample, I have more than 1000 elements. I am using `Distributions.jl, IntervalArithmetic.jl, HypothesisTests.jl` packages in `Pluto.jl` notebook.

Thank you in advance

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [May 15, 2023, 12:25pm UTC](https://discourse.julialang.org/t/how-to-calculate-confidence-interval/98876/2 "2023-05-15T12:25:58Z")

</div>

[https://juliastats.org/HypothesisTests.jl/stable/methods/#Confidence-interval](https://juliastats.org/HypothesisTests.jl/stable/methods/#Confidence-interval)

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 15, 2023, 1:59pm UTC](https://discourse.julialang.org/t/how-to-calculate-confidence-interval/98876/3 "2023-05-15T13:59:45Z")

</div>

When you say “don’t know how to do it for the lower and upper values of a sample” I suppose you mean you want to construct a confidence interval on the population minimum and maximum from a sample? If that’s the case then that won’t be possible in general, so you might need to provide some more context for what you’re actually after.

---

<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 15, 2023, 2:32pm UTC](https://discourse.julialang.org/t/how-to-calculate-confidence-interval/98876/4 "2023-05-15T14:32:36Z")

</div>

Yes @nilshg, I want to construct a confidence interval on the population minimum and maximum from a sample. An example is shown below-

```julia
using Distributions.jl, IntervalArithmetic.jl, HypothesisTests.jl
       
        N = 1000
	
		a = rand(Uniform(10, 20), N);
		b = rand(Uniform(5, 10), N);
		c = rand(Uniform(25, 50), N);
	
		sample = a.^2 + b.^2 + c.^2

		limits = [minimum(sample), maximum(sample)]	

```

I am looking for the 95% or 99% confidence interval on `limits=[772.775, 2889.33]` value.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 15, 2023, 3:32pm UTC](https://discourse.julialang.org/t/how-to-calculate-confidence-interval/98876/5 "2023-05-15T15:32:03Z")

</div>

Okay, so I think what you’re trying to do doesn’t work (although maybe @johnmyleswhite has a clever idea, his certainly a better statistician than me!)

The population minimum (or maximum) is a unique (for a continuous distribution) value and as such a sample in and of itself without some distributional assumptions can’t really tell you much about it - the sample either includes the minimum or it doesn’t, and you don’t know which it is.

You could look into quantile estimators and confidence intervals around quantiles instead.

Your example is interesting because it’s all bounded distributions so you know the theoretical population minimum is 10^2 +5^2 + 25^2 = 750. You can create a fake population to check how well a sample of 1,000 captures the extremes of the population distribution:

```julia
In [2]: using Distributions

In [3]: population = [rand(Uniform(10, 20))^2 + rand(Uniform(5, 10))^2 + rand(Uniform(25, 50))^2 for _ ∈ 1:1_000_000];

In [4]: sample_mins = [minimum(rand(population, 1_000)) for _ ∈ 1:10_000];

In [5]: minimum(population), minimum(sample_mins)
(754.667335428114, 754.667335428114)

```

here I’ve created a population of size 1m and drawn 10,000 samples of size 1,000 from it. Indeed at least some of these sample include the population minimum. You can also look at the distribution of sample minimums to get a sense of how this estimator behaves, here are the 0.5th, 1st, 5th and 10th quantiles:

```julia
In [6]: quantile(sample_mins, [0.005, 0.01, 0.05, 0.1])
4-element Vector{Float64}:
 757.8221704938558
 759.6185780206479
 766.9632199757921
 771.232780534815

```

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [May 16, 2023, 12:16am UTC](https://discourse.julialang.org/t/how-to-calculate-confidence-interval/98876/6 "2023-05-16T00:16:11Z")

</div>

You’re right - I had misunderstood the request. Statistical inference for population minimum and maximum are very messy: the asymptotic normality you get for interior quantiles doesn’t apply cleanly, so constructing CI’s is very hard. And the population minimum and maximum might not even exist without some assumptions, whereas the interior quantiles always do.

The topic is tricky enough that some of the top Google results are incorrect: for example, [Max R. P. Grossmann: Distribution of the maximum of random variables](https://max.pm/posts/max_dist/) involves confusion about what a confidence interval is.
