# Package for Confidence Intervals?

**URL:** https://discourse.julialang.org/t/package-for-confidence-intervals/33902
**Category:** Statistics
**Tags:** question, statistics
**Created:** [January 28, 2020, 7:39pm UTC](https://discourse.julialang.org/t/package-for-confidence-intervals/33902 "2020-01-28T19:39:13Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Usor](https://avatars.discourse-cdn.com/v4/letter/u/8dc957/32.png) [@Usor](https://discourse.julialang.org/u/Usor)
#### Post date: [January 28, 2020, 7:39pm UTC](https://discourse.julialang.org/t/package-for-confidence-intervals/33902/1 "2020-01-28T19:39:13Z")

</div>

Is there a package dealing with these kinds of computations, or should I make my own functions for my purposes?

---

<div class="post-metadata">

### Author: ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)
#### Post date: [January 28, 2020, 7:59pm UTC](https://discourse.julialang.org/t/package-for-confidence-intervals/33902/2 "2020-01-28T19:59:58Z")

</div>

I use `Measurements.jl`.

---

<div class="post-metadata">

### Author: ![Usor](https://avatars.discourse-cdn.com/v4/letter/u/8dc957/32.png) [@Usor](https://discourse.julialang.org/u/Usor)
#### Post date: [January 28, 2020, 8:10pm UTC](https://discourse.julialang.org/t/package-for-confidence-intervals/33902/3 "2020-01-28T20:10:20Z")

</div>

Thanks for the reply.

I was looking at the package and wondering if it would fit what I want. At the moment it doesn’t look like it, but I could be wrong. Can you elaborate, and possibly give examples?

At the moment I’m at least looking, for example, for something that would simply let me plug values into functions to get confidence intervals for (arithmetic) means, proportions, standard deviations, differences and sums etc. in the context of statistical estimation theory.

It would be nice if it’d let me plug the values you would normally encounter if you calculate by hand, such as the arithmetic mean, n, N, p. etc.

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [January 28, 2020, 8:11pm UTC](https://discourse.julialang.org/t/package-for-confidence-intervals/33902/4 "2020-01-28T20:11:00Z")

</div>

Can you expand a bit on the application you have in mind? This way it’ll be easier to help you.

For example, if you have quantities with uncertainties and want to perform calculations with them you’re looking for error propagation in which case the mentioned [Measurements.jl](https://github.com/JuliaPhysics/Measurements.jl) or perhaps [MonteCarloMeasurements.jl](https://github.com/baggepinnen/MonteCarloMeasurements.jl) would be helpful. On the other hand, if you have a time series of data - perhaps even correlated - and want to estimate the standard error you might want to take a look at [BinningAnalysis.jl](https://github.com/crstnbr/BinningAnalysis.jl) or similar.

---

<div class="post-metadata">

### Author: ![Usor](https://avatars.discourse-cdn.com/v4/letter/u/8dc957/32.png) [@Usor](https://discourse.julialang.org/u/Usor)
#### Post date: [January 28, 2020, 8:13pm UTC](https://discourse.julialang.org/t/package-for-confidence-intervals/33902/5 "2020-01-28T20:13:02Z")

</div>

We posted simultaneously. Please see my previous post.

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [January 28, 2020, 8:14pm UTC](https://discourse.julialang.org/t/package-for-confidence-intervals/33902/6 "2020-01-28T20:14:00Z")

</div>

Are your values statistically independent or correlated?

Assuming that I understand your use case correctly, you could simply use the functions provided by the [`Statistics` standard library](https://docs.julialang.org/en/latest/stdlib/Statistics/#) and [`StatsBase.jl`](https://juliastats.org/StatsBase.jl/stable/). In the latter case, you could look at `BinningAnalysis.jl` linked above, which utilizes logarithmic binning to estimate the standard error of the mean of your values.

---

<div class="post-metadata">

### Author: ![Usor](https://avatars.discourse-cdn.com/v4/letter/u/8dc957/32.png) [@Usor](https://discourse.julialang.org/u/Usor)
#### Post date: [January 28, 2020, 8:28pm UTC](https://discourse.julialang.org/t/package-for-confidence-intervals/33902/7 "2020-01-28T20:28:10Z")

</div>

Thanks for the input.

My use case is simple. It is like I explained earlier. Imagine you have formulas for estimating the population parameters with confidence intervals, based on sample statistics (such as sample means, proportion) and are calculating exactly according to these formulae. Is there then a package that has these formulae, or should I rather make them myself?

---

<div class="post-metadata">

### Author: ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)
#### Post date: [January 28, 2020, 9:40pm UTC](https://discourse.julialang.org/t/package-for-confidence-intervals/33902/8 "2020-01-28T21:40:58Z")

</div>

For example when you want to estimate a rate from a binomial sample, then the posterior on the rate is a Beta distribution, and you can compute the CI on that, but you need to know that it’s a Beta and how the data goes into it with the prior.

I don’t know of a package that does that easily, usually I do it myself using Distributions.jl and [wikipedia](https://en.wikipedia.org/wiki/Poisson_distribution#Bayesian_inference), which is indeed not ideal.

---

<div class="post-metadata">

### Author: ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)
#### Post date: [January 28, 2020, 10:42pm UTC](https://discourse.julialang.org/t/package-for-confidence-intervals/33902/9 "2020-01-28T22:42:45Z")

</div>

In case you consider population parameters like `mean` or `variance`, there  
is a number of `confint` distributed over `Statistics`, `StatsBase` and in particular [`HypothesisTests`](https://juliastats.org/HypothesisTests.jl/latest/methods/#Confidence-interval-1):

```julia
# 12 methods for generic function "confint":
[1] confint(x::BinomialTest; level, tail, method) in HypothesisTests at /Users/smoritz/.julia/packages/HypothesisTests/wSEbN/src/binomial.jl:104
[2] confint(x::SignTest; level, tail) in HypothesisTests at /Users/smoritz/.julia/packages/HypothesisTests/wSEbN/src/binomial.jl:218
[3] confint(x::FisherExactTest; level, tail, method) in HypothesisTests at /Users/smoritz/.julia/packages/HypothesisTests/wSEbN/src/fisher.jl:181
[4] confint(x::PowerDivergenceTest; level, tail, method, correct, bootstrap_iters, GC) in HypothesisTests at /Users/smoritz/.julia/packages/HypothesisTests/wSEbN/src/power_divergence.jl:73
[5] confint(obj::StatisticalModel) in StatsBase at /Users/smoritz/.julia/packages/StatsBase/DyWPR/src/statmodels.jl:32
[6] confint(x::HypothesisTests.TTest; level, tail) in HypothesisTests at /Users/smoritz/.julia/packages/HypothesisTests/wSEbN/src/t.jl:37
[7] confint(x::HypothesisTests.ZTest; level, tail) in HypothesisTests at /Users/smoritz/.julia/packages/HypothesisTests/wSEbN/src/z.jl:37
[8] confint(x::ExactSignedRankTest; level, tail) in HypothesisTests at /Users/smoritz/.julia/packages/HypothesisTests/wSEbN/src/wilcoxon.jl:164
[9] confint(x::ApproximateSignedRankTest; level, tail) in HypothesisTests at /Users/smoritz/.julia/packages/HypothesisTests/wSEbN/src/wilcoxon.jl:239
[10] confint(test::CorrelationTest{T}) where T in HypothesisTests at /Users/smoritz/.julia/packages/HypothesisTests/wSEbN/src/correlation.jl:61
[11] confint(test::CorrelationTest{T}, level::Float64) where T in HypothesisTests at /Users/smoritz/.julia/packages/HypothesisTests/wSEbN/src/correlation.jl:61

```

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [January 29, 2020, 5:07am UTC](https://discourse.julialang.org/t/package-for-confidence-intervals/33902/10 "2020-01-29T05:07:25Z")

</div>

You can probably make [https://github.com/MikeInnes/Poirot.jl](https://github.com/MikeInnes/Poirot.jl) do what you want?

---

<div class="post-metadata">

### Author: ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)
#### Post date: [January 29, 2020, 5:52am UTC](https://discourse.julialang.org/t/package-for-confidence-intervals/33902/11 "2020-01-29T05:52:29Z")

</div>

I may be misunderstanding, but is sounds like you are under the impression that there is a single answer to the question of building confidence intervals. Unfortunately in Statistics this is not the case. The appropriate method in any application hinges critically on the true data generating process (e.g. is your data IID, weakly dependent, non-stationary, e.t.c.) in combination with the statistic of interest. We need to know these things to direct you to an appropriate package. If you are asking whether there is some package that universally builds appropriate confidence intervals for all data-types, well, I’m not aware of any such package in _any_ programming language.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [January 29, 2020, 9:29am UTC](https://discourse.julialang.org/t/package-for-confidence-intervals/33902/12 "2020-01-29T09:29:14Z")

</div>

> [@Usor](#):
>
> My use case is simple. It is like I explained earlier. Imagine you have formulas for estimating the population parameters with confidence intervals, based on sample statistics (such as sample means, proportion) and are calculating exactly according to these formulae.

You may not be aware of this, but there is no _single_ set of formulas that fits each application, except for some special cases under very, very specific assumptions.

There exist closed-form formulas for _some_ models and methodologies, for some others you can obtain them numerically rather easily, and of course for other models you have to use Monte Carlo methods.

It would be easier to help if you specified

- the statistical model you are interested in,
- the methodology you are using (Bayesian? frequentist?)
- the kind of confidence interval you want (frequentist CI, Bayesian HPD, etc)

---

<div class="post-metadata">

### Author: ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)
#### Post date: [January 29, 2020, 10:18am UTC](https://discourse.julialang.org/t/package-for-confidence-intervals/33902/13 "2020-01-29T10:18:35Z")

</div>

Thinking about it what’s missing it a way to get posterior on parameters when fitting a distribution, currently Distributions.jl’s `fit` only returns the MLE. That way if you want to estimate a frequency you could do something like that :

```julia
julia>dfit = my_fit(Binomial,100,[10])
Fitted{Binomial}(...)

julia>mle(dfit)
Binomial{Float64}(n=100, p=0.1)

julia>p = posterior(dfit, :p)
Beta{Float64}(α=10.0, β=90.0)

julia>confidence_interval(p, 0.9)
(0.05583217884206651, 0.15327514365732653)

```

In some relevant cases there’s closed-form formulas for the posteriors, but `posterior` could also return a sampled or approximated distribution when it’s not the case.

---

<div class="post-metadata">

### Author: ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)
#### Post date: [January 29, 2020, 10:45am UTC](https://discourse.julialang.org/t/package-for-confidence-intervals/33902/14 "2020-01-29T10:45:48Z")

</div>

And here, there is [GitHub - JuliaStats/ConjugatePriors.jl: A Julia package to support conjugate prior distributions.](https://github.com/JuliaStats/ConjugatePriors.jl)

---

<div class="post-metadata">

### Author: ![PharmCat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pharmcat/32/6953_2.png) [@PharmCat](https://discourse.julialang.org/u/PharmCat)
#### Post date: [February 5, 2020, 5:49pm UTC](https://discourse.julialang.org/t/package-for-confidence-intervals/33902/15 "2020-02-05T17:49:53Z")

</div>

> [@Usor](#):
>
> At the moment I’m at least looking, for example, for something that would simply let me plug values into functions to get confidence intervals for (arithmetic) means, proportions, standard deviations, differences and sums etc. in the context of statistical estimation theory.

For proportions you can see [https://github.com/PharmCat/ClinicalTrialUtilities.jl](https://github.com/PharmCat/ClinicalTrialUtilities.jl) or take code from there (ci.jl)

---

<div class="post-metadata">

### Author: ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)
#### Post date: [February 5, 2020, 7:55pm UTC](https://discourse.julialang.org/t/package-for-confidence-intervals/33902/16 "2020-02-05T19:55:28Z")

</div>

I wonder if you can suggest a book on confidence intervals of different models and methodologies…

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [February 6, 2020, 8:16am UTC](https://discourse.julialang.org/t/package-for-confidence-intervals/33902/17 "2020-02-06T08:16:11Z")

</div>

I am not aware of an introductory textbook that compares various approaches — each one usually deals with its own. But if you are really interested, I would recommend

```nohighlight
@article{berger1988likelihood,
  title={The likelihood principle},
  author={Berger, James O and Wolpert, Robert L and Bayarri, MJ and DeGroot, MH and Hill, Bruce M and Lane, David A and LeCam, Lucien},
  journal={Lecture notes-Monograph series},
  volume=6,
  year=1988,
  publisher={JSTOR}
}

```

which is great fun. Working through the book, you will learn a lot of useful facts about the principles of statistics, which you can weave into lunchtime conversations with colleagues up to the point that they will be inclined to dump a plate of lasagna on your head.

But the gist is really simple: frequentist (Neyman) CI usually don’t mean what people assume they mean, Bayesian HPD is a nice posterior visualization tool. I would go for posterior predictive checks instead for serious modeling, eg

```nohighlight
@article{gelman1996posterior,
  title={Posterior predictive assessment of model fitness via realized discrepancies},
  author={Gelman, Andrew and Meng, Xiao-Li and Stern, Hal},
  journal={Statistica sinica},
  pages={733--760},
  year=1996,
  publisher={JSTOR}
}

```

Incidentally, Andrew Gelman has a lot of neat articles on p-values.

---

<div class="post-metadata">

### Author: ![Usor](https://avatars.discourse-cdn.com/v4/letter/u/8dc957/32.png) [@Usor](https://discourse.julialang.org/u/Usor)
#### Post date: [February 16, 2020, 3:15pm UTC](https://discourse.julialang.org/t/package-for-confidence-intervals/33902/18 "2020-02-16T15:15:22Z")

</div>

Thanks for your replies.

My use case is based on a table having z critical values and their respective probability percentages (between 0 and 1), where I use common values.

I want to make my own functions (or use existing ones) to calculate confidence intervals, proportions, means, and the like, going from sample to population. A part will be to plug in a z critical value into the function. Calculations are made (I have no time to write out these formulas here now, maybe later.) I output the calculated information nicely, including saying what the confidence percentage is, and so on.

And I want to also simply plug in the desired confidence level. So this value must be converted to the z critical value, which may then be used in computation.

Suppose you have a table such as this (I hope it renders well; I don’t use this so often):

```julia
% Conf. Lev. .9973 .99 .98 68.27
z_c 3.00 2.58 2.33 1.00

etc.

```

If you’d input 3.00 for z\_c, what is the formula to yield .9973? If you input .99, what is the formula to yield 2.58?

In this case, if I make those functions myself, and perhaps share them with others if there’s interest, I am particularly interested in these two formulas.

---

<div class="post-metadata">

### Author: ![dave.f.kleinschmidt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dave.f.kleinschmidt/32/55_2.png) [@dave.f.kleinschmidt](https://discourse.julialang.org/u/dave.f.kleinschmidt)
#### Post date: [February 16, 2020, 10:08pm UTC](https://discourse.julialang.org/t/package-for-confidence-intervals/33902/19 "2020-02-16T22:08:44Z")

</div>

I think you want the `quantile` function from Distributions.jl:

```julia
julia> z = Normal()
Normal{Float64}(μ=0.0, σ=1.0)

julia> -quantile.(z, 0.5 .* (1 .- [.9973, .99, .98, .6827]))
4-element Array{Float64,1}:
 2.999976992703395
 2.5758293035489053
 2.326347874040846
 1.000021713322999

```

(note that to get the quantile from the 2-sided p value which you seem to be doing here, you have to adjust the p value to be half the distance from 1, hence the `0.5 .* (1 .- p)` bit)

---

<div class="post-metadata">

### Author: ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)
#### Post date: [February 16, 2020, 10:42pm UTC](https://discourse.julialang.org/t/package-for-confidence-intervals/33902/20 "2020-02-16T22:42:02Z")

</div>

Thanks for the book recommendation, I just started reading it (“The likelihood principle”). I wish I had read it a few years earlier 🙈
