# Discrete empirical distribution

Silly question: if I have a histogram such as:

``````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?

Using EmpiricalDistributions package:

``````using Distributions, StatsBase, EmpiricalDistributions

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

and now the following:

``````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
``````
1 Like

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:

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

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

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