# Analysis and Diagnostics for MCMC

**URL:** https://discourse.julialang.org/t/analysis-and-diagnostics-for-mcmc/3104
**Category:** Statistics
**Created:** [April 7, 2017, 2:07pm UTC](https://discourse.julialang.org/t/analysis-and-diagnostics-for-mcmc/3104 "2017-04-07T14:07:37Z")
**Posts on this page:** 8
**Page:** 2

<div class="post-metadata">

### Author: ![gideonsimpson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gideonsimpson/32/1928_2.png) [@gideonsimpson](https://discourse.julialang.org/u/gideonsimpson)
#### Post date: [August 3, 2022, 11:26pm UTC](https://discourse.julialang.org/t/analysis-and-diagnostics-for-mcmc/3104/21 "2022-08-03T23:26:27Z")

</div>

Did anyone ever make any progress on this? I’d like to have an estimator that’s consistent with what is outlined in the Flegal & Jones paper.

---

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [August 4, 2022, 12:11am UTC](https://discourse.julialang.org/t/analysis-and-diagnostics-for-mcmc/3104/22 "2022-08-04T00:11:37Z")

</div>

You might consider using [MCMCChains.jl](https://github.com/TuringLang/MCMCChains.jl).

---

<div class="post-metadata">

### Author: ![gideonsimpson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gideonsimpson/32/1928_2.png) [@gideonsimpson](https://discourse.julialang.org/u/gideonsimpson)
#### Post date: [August 4, 2022, 4:30pm UTC](https://discourse.julialang.org/t/analysis-and-diagnostics-for-mcmc/3104/23 "2022-08-04T16:30:29Z")

</div>

It’s a little unclear to me from the documentation which method it uses for estimating the statistic. Also, I don’t want to switch over entirely to `MCMCChains` - is there a way to call the autocorrelation time function on a given time series (of scalars)?

---

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [August 4, 2022, 4:38pm UTC](https://discourse.julialang.org/t/analysis-and-diagnostics-for-mcmc/3104/24 "2022-08-04T16:38:28Z")

</div>

I understand why you may not want to commit the a Chain object in MCMCChains. About a year ago, the package maintainers split the diagnostics from MCMCChains and put them in standalone package: [GitHub - TuringLang/MCMCDiagnosticTools.jl](https://github.com/TuringLang/MCMCDiagnosticTools.jl)

I think this should have the features you want.

---

<div class="post-metadata">

### Author: ![gideonsimpson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gideonsimpson/32/1928_2.png) [@gideonsimpson](https://discourse.julialang.org/u/gideonsimpson)
#### Post date: [August 8, 2022, 1:32am UTC](https://discourse.julialang.org/t/analysis-and-diagnostics-for-mcmc/3104/25 "2022-08-08T01:32:55Z")

</div>

Indeed, this might be what I want, but I’m unsure of how to use it without falling back to the `chain` datastructure. For instance, how would I get `MCMCDiagnosticTools` to work with the following?

```julia
samples = cumsum(randn(10000));
ess_hat(samples)

```

just generates an error:

```julia
ERROR: MethodError: no method matching ess_rhat(::Vector{Float64})
Closest candidates are:
  ess_rhat(::AbstractArray{<:Union{Missing, Real}, 3}; method, maxlag) at ~/.julia/packages/MCMCDiagnosticTools/kKCOf/src/ess.jl:208
Stacktrace:
 [1] top-level scope
   @ REPL[12]:1

```

---

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [August 8, 2022, 8:20pm UTC](https://discourse.julialang.org/t/analysis-and-diagnostics-for-mcmc/3104/26 "2022-08-08T20:20:06Z")

</div>

According to the documentation, the function expects a three dimensional array.

```julia
help?> ess_rhat
search:

  ess_rhat(
      samples::AbstractArray{<:Union{Missing,Real},3}; method=ESSMethod(), maxlag=250
  )

  Estimate the effective sample size and the potential scale reduction of the samples of shape (draws, parameters, chains) with the
  method and a maximum lag of maxlag.

  See also: ESSMethod, FFTESSMethod, BDAESSMethod

```

**Example**

```julia
using MCMCDiagnosticTools: ess_rhat

x = rand(1000, 10, 3)

ess,rhat = ess_rhat(x)

```

---

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [August 8, 2022, 8:25pm UTC](https://discourse.julialang.org/t/analysis-and-diagnostics-for-mcmc/3104/27 "2022-08-08T20:25:00Z")

</div>

In your example, you do the following:

```julia
using MCMCDiagnosticTools: ess_rhat

samples = cumsum(randn(10000, 1, 1), dims=1);

ess,rhat = ess_rhat(samples)

```

You can also add more dimensions outside of `cumsum`:

```julia
samples = [samples ;;;]
ess,rhat = ess_rhat(samples)

```

---

<div class="post-metadata">

### Author: ![jarapo](https://avatars.discourse-cdn.com/v4/letter/j/6bbea6/32.png) [@jarapo](https://discourse.julialang.org/u/jarapo)
#### Post date: [October 24, 2022, 10:05am UTC](https://discourse.julialang.org/t/analysis-and-diagnostics-for-mcmc/3104/28 "2022-10-24T10:05:44Z")

</div>

I had the same question as

> [@gideonsimpson](#):
>
> For instance, how would I get `MCMCDiagnosticTools` to work with the following?
> 
> ```julia
> samples = cumsum(randn(10000));
> ess_hat(samples)
> 
> ```

Although this example was extremely helpful:

> [@Christopher\_Fisher](#):
>
> ```julia
> samples = cumsum(randn(10000, 1, 1), dims=1);
> 
> ```

I still find this instruction in the documentation confusing,

> [@Christopher\_Fisher](#):
>
> `the samples of shape (draws, parameters, chains) `

in that I am not sure what cumsum(randn(10000, 1, 1), dims=1) is supposed to represent given randn(10000, 1, 1) at hand, which to me already looks like a chain of random variables of length 10000…

[Previous page](https://discourse.julialang.org/t/analysis-and-diagnostics-for-mcmc/3104.md?page=1)
