# Fitting a 1D distribution using Gaussian Mixtures

**URL:** <https://discourse.julialang.org/t/fitting-a-1d-distribution-using-gaussian-mixtures/28113>\
**Category:** General Usage\
**Tags:** statistics\
**Created:** [August 28, 2019, 3:00pm UTC](https://discourse.julialang.org/t/fitting-a-1d-distribution-using-gaussian-mixtures/28113 "2019-08-28T15:00:59Z")\
**Posts on this page:** 1\
**Showing post:** 14

<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:** [August 29, 2019, 12:09pm UTC](https://discourse.julialang.org/t/fitting-a-1d-distribution-using-gaussian-mixtures/28113/14 "2019-08-29T12:09:30Z")

</div>

For the sake of completeness, here is the fit and the comparison of the fit to the data using `GaussianMixtures.jl`:

```julia
using GaussianMixtures
using PGFPlotsX
using StatsBase
using Random
Random.seed!(23);

d₁ = randn(20000, 1) .- 5
d₂ = randn(100000, 1) .+ 23
d₊ = vcat(d₁, d₂);

n = length(d₊)

gmm = GMM(2, d₊)

n_bins = 100

h = fit(Histogram, d₊[:,1], nbins=n_bins)
h₀ = fit(Histogram, rand(gmm, length(d₊))[:], nbins=n_bins)

fig = @pgf Axis(
    Plot({"ybar interval"},
        Coordinates(h.edges[1][2:end], h.weights)
    ),
    Plot({color="red"},
        Coordinates(h₀.edges[1][2:end].+(h₀.edges[1].step.hi/2), h₀.weights)
    )
)

```

![gmm_fit](https://global.discourse-cdn.com/julialang/original/3X/0/c/0c8cc1951694d28d00ab1f675f6fd1e2cbec6d36.png)

---

_[View the full topic](https://discourse.julialang.org/t/fitting-a-1d-distribution-using-gaussian-mixtures/28113)._
