# Approximating a Numerically Computed Distribution

**URL:** https://discourse.julialang.org/t/approximating-a-numerically-computed-distribution/77972
**Category:** Statistics
**Created:** [March 16, 2022, 2:10pm UTC](https://discourse.julialang.org/t/approximating-a-numerically-computed-distribution/77972 "2022-03-16T14:10:44Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![marcpabst](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@marcpabst](https://discourse.julialang.org/u/marcpabst)
#### Post date: [March 16, 2022, 2:10pm UTC](https://discourse.julialang.org/t/approximating-a-numerically-computed-distribution/77972/1 "2022-03-16T14:10:44Z")

</div>

I have a very simple, numerically computed posterior (note that I’m not randomly sampling from the posterior but actually calculate it’s (log-)densities over a given interval) that I know converges to a Normal distribution for high sample sizes. For a somewhat simpler case, I actually have a closed-form approximation, but I now want to generalize my solution (and also possible check any other closed-form approximations).

So I was wondering if there is an elegant way to approximate a pdf, e.g. using the `Distributions.jl` package? My naive idea would be to use `ApproxFun.jl` but I was kinda hoping for a more straightforward solution. I could also sample from my posterior and then use `fit(Normal, ...)` but that can’t be the way to do this…

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [March 16, 2022, 3:10pm UTC](https://discourse.julialang.org/t/approximating-a-numerically-computed-distribution/77972/2 "2022-03-16T15:10:29Z")

</div>

Do you mean that you have computed it over a grid of values? In that case, maybe what you want is to interpolate between grid points?

---

<div class="post-metadata">

### Author: ![marcpabst](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@marcpabst](https://discourse.julialang.org/u/marcpabst)
#### Post date: [March 16, 2022, 3:17pm UTC](https://discourse.julialang.org/t/approximating-a-numerically-computed-distribution/77972/3 "2022-03-16T15:17:38Z")

</div>

I’m actually dealing with a dimension case here, so I’m not sure if a “grid” is a fair description, but yeah, that is basically the idea. But I actually want to fit a Gaussian, i.e. estimating the two parameters `μ` and `σ`.

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [March 16, 2022, 7:32pm UTC](https://discourse.julialang.org/t/approximating-a-numerically-computed-distribution/77972/4 "2022-03-16T19:32:39Z")

</div>

take a look at [Distributions.jl/stable/fit](https://juliastats.org/Distributions.jl/stable/fit/) and  
[how to fit a normal distribution](https://discourse.julialang.org/t/how-to-fit-a-normal-approximation-to-data-in-julia/35045/2)

---

<div class="post-metadata">

### Author: ![marcpabst](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@marcpabst](https://discourse.julialang.org/u/marcpabst)
#### Post date: [March 17, 2022, 1:24pm UTC](https://discourse.julialang.org/t/approximating-a-numerically-computed-distribution/77972/5 "2022-03-17T13:24:10Z")

</div>

But this assumes that I have observations sampled from some distribution, doesn’t it? Actually, I have a numerically computed pdf over a grid / number of evenly spaced points.

Of course, I can just sample from my pdf and then take the data’s moments (mean, variance) or use `fit()` to get my normal distribution. It just seems a bit unnecessary to go through this… I guess I was justing hoping for a more elegant way to do this.

(I usually would just go with MCMC and then just just `fit()` on the samples, but for the simple 1-d case, Turing is order of magnitude slower than just computing the posterior over a grid)

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [March 17, 2022, 2:13pm UTC](https://discourse.julialang.org/t/approximating-a-numerically-computed-distribution/77972/6 "2022-03-17T14:13:43Z")

</div>

I still don’t quite understand what you want. My latest interpretation is that you have computed some pdf values over a (1D) grid, now you want to compute the mean and standard deviation of that pdf. Is that right? Depending on how much of the support you have covered with your grid points, you could interpolate to create a “filled in” pdf and then just compute the mean and standard deviation using `QuadGK` with the integral formulas of those statistics. This is much more general than fitting a Gaussian.

Alternatively, if you for some reason don’t have enough points for that, you could do a least squares fit to the closest Normal (or some other) distribution. You could minimize the distance between your `pdf(x)` values and `pdf(Normal(m,s),x)` where you are optimizing over `m` and `s`. There are a couple of loss functions you can use to do this.
