# The probability of density function

**URL:** https://discourse.julialang.org/t/the-probability-of-density-function/53725
**Category:** General Usage
**Created:** [January 21, 2021, 2:01pm UTC](https://discourse.julialang.org/t/the-probability-of-density-function/53725 "2021-01-21T14:01:09Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![marouane1994](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@marouane1994](https://discourse.julialang.org/u/marouane1994)
#### Post date: [January 21, 2021, 2:01pm UTC](https://discourse.julialang.org/t/the-probability-of-density-function/53725/1 "2021-01-21T14:01:09Z")

</div>

I want to test an MCMC code for a Gaussian distribution witha standard deviation of `0.1`and for random values of the mean

```julia
using Distributions
using LinearAlgebra
using Plots

sigma=0.1
log_target(mu)= log((1/sigma*sqrt(pi*2^2))*exp((-(x-mu)^2)/(2*sigma^2)))
proposal(mu)=Normal(mu,sigma)
initial=0
Iteration=10000
Chain=fill(0.0,Iteration)
Chain[1]=initial
select=[]
for i in 2:Iteration
    current= Chain[i-1]
    proposed= rand(proposal(current))
            
    
    C= min(1, log_traget(proposed)/log_traget(current))

    if rand() < C
        Chain[i]= proposed
        push!(select, Chain[i])
        
        
    else
        Chain[i]=current
        
    end    
end
select

```

the code gives me the selected values but i know it’s not enough i want to plot the density and i have no clue how to do it i tried but it didn’t work, i’m still a beginner at mcmc

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [January 21, 2021, 2:13pm UTC](https://discourse.julialang.org/t/the-probability-of-density-function/53725/2 "2021-01-21T14:13:13Z")

</div>

You plot a histogram: `histogram(select)`

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [January 21, 2021, 2:35pm UTC](https://discourse.julialang.org/t/the-probability-of-density-function/53725/3 "2021-01-21T14:35:11Z")

</div>

If I may, I have a simple package to plot densities and other simple fits ([EasyFit](https://github.com/m3g/EasyFit)):

```julia
julia> using Plots, EasyFit

julia> x = randn(1000);

julia> fit = fitdensity(x)
 
 ------------------- Density -------------

  d contains the probability of finding data points within x ± 0.0294

 ----------------------------------------- 

julia> plot(fit.x,fit.d)

julia> savefig("./plot.png")

```

Result:

![plot](https://global.discourse-cdn.com/julialang/original/3X/9/4/949db065819764f3aa08f2896bc8c2b0742db549.png)

You can control some options, like the bin width:

```julia
julia> fit = fitdensity(x,step=0.5)
 
 ------------------- Density -------------

  d contains the probability of finding data points within x ± 0.25

 ----------------------------------------- 

julia> plot(fit.x,fit.d)

```

Result:

![plot](https://global.discourse-cdn.com/julialang/original/3X/4/9/492f7e22395e66d9473a3cfde6ec6cc34032dc7b.png)

---

<div class="post-metadata">

### Author: ![marouane1994](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@marouane1994](https://discourse.julialang.org/u/marouane1994)
#### Post date: [January 21, 2021, 2:42pm UTC](https://discourse.julialang.org/t/the-probability-of-density-function/53725/4 "2021-01-21T14:42:59Z")

</div>

what if i want without a histogram only the plot

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [January 21, 2021, 2:52pm UTC](https://discourse.julialang.org/t/the-probability-of-density-function/53725/5 "2021-01-21T14:52:51Z")

</div>

I’m not sure I understand you. But if I do, then you may want to look into something like EasyFit or other ways of kernel density estimation. But usually in MCMC, one just plots the histograms, sometimes 2D ones to see the correlations between varibles. Have a look at [GitHub - JuliaPlots/StatsPlots.jl: Statistical plotting recipes for Plots.jl](https://github.com/JuliaPlots/StatsPlots.jl)

---

<div class="post-metadata">

### Author: ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)
#### Post date: [January 21, 2021, 3:30pm UTC](https://discourse.julialang.org/t/the-probability-of-density-function/53725/6 "2021-01-21T15:30:04Z")

</div>

Check out the MCMCChains.jl package. You can do something like

```julia
# visualize results
chn = Chains(chain, ["μ₁","μ₂","σ₁","σ₂","p"])
display(chn)
plot(chn)

```

and see summary information for the chain and marginal density plots.
