# Discrete empirical distribution

**URL:** <https://discourse.julialang.org/t/discrete-empirical-distribution/98835>\
**Category:** General Usage\
**Created:** [May 14, 2023, 1:51pm UTC](https://discourse.julialang.org/t/discrete-empirical-distribution/98835 "2023-05-14T13:51:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pino](https://avatars.discourse-cdn.com/v4/letter/p/d2c977/32.png) [@Pino](https://discourse.julialang.org/u/Pino)\
**Post date:** [May 14, 2023, 1:51pm UTC](https://discourse.julialang.org/t/discrete-empirical-distribution/98835/1 "2023-05-14T13:51:37Z")

</div>

Silly question: if I have a histogram such as:

```julia
histogram(rand(Normal(0,1),10000),nbins=100)

```

how can I save the frequency per bin such as to have it as measure for the discretized series?

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [May 14, 2023, 2:23pm UTC](https://discourse.julialang.org/t/discrete-empirical-distribution/98835/2 "2023-05-14T14:23:59Z")

</div>

Using EmpiricalDistributions package:

```julia
using Distributions, StatsBase, EmpiricalDistributions

hist = fit(Histogram, rand(Normal(0,1), 10_000); nbins = 100)
empirical = UvBinnedDist(hist)

```

and now the following:

```julia
julia> rand(empirical,3)
3-element Vector{Float64}:
 1.5824386481474937
 2.1219654599525595
 1.8102351705174078

julia> mean(empirical)
0.0030899999999999457

julia> var(empirical)
1.0016724519000006

julia> pdf(empirical, 0.5)
0.358

julia> cdf(empirical, 0.0)
0.49810000000000004

```

---

<div class="post-metadata">

**Author:** ![Pino](https://avatars.discourse-cdn.com/v4/letter/p/d2c977/32.png) [@Pino](https://discourse.julialang.org/u/Pino)\
**Post date:** [May 14, 2023, 3:31pm UTC](https://discourse.julialang.org/t/discrete-empirical-distribution/98835/3 "2023-05-14T15:31:57Z")

</div>

thanks, that works but I’m not able to pass to the pdf a vector of points. It seems that the error might be due to the data type as I’m trying to pass to the pdf array data and it seems to expect abstract array. Wonder if you can help me with that too

Mean something across these lines:

```julia
d = Normal(0, 1)
x = [1.0, 2.0, 3.0]
pdf_vals = pdf(d, x)

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [May 14, 2023, 6:38pm UTC](https://discourse.julialang.org/t/discrete-empirical-distribution/98835/4 "2023-05-14T18:38:23Z")

</div>

> [@Pino](#):
>
> `pdf_vals = pdf(d, x)`

using broadcasting, you can do the following (note the extra `.`):

```julia
julia> pdf_vals = pdf.(d, x)
3-element Vector{Float64}:
 0.24197072451914337
 0.05399096651318806
 0.0044318484119380075

```
