# Recording an MCMC Chain

**URL:** https://discourse.julialang.org/t/recording-an-mcmc-chain/90243
**Category:** General Usage
**Tags:** question, package, bayesian-inference, chain, mcmc
**Created:** [November 14, 2022, 3:05pm UTC](https://discourse.julialang.org/t/recording-an-mcmc-chain/90243 "2022-11-14T15:05:06Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![clinton](https://avatars.discourse-cdn.com/v4/letter/c/7ba0ec/32.png) [@clinton](https://discourse.julialang.org/u/clinton)
#### Post date: [November 14, 2022, 3:05pm UTC](https://discourse.julialang.org/t/recording-an-mcmc-chain/90243/1 "2022-11-14T15:05:06Z")

</div>

I’ve written my own MCMC code. MCMCChains provides some helper functions wrt working with already completed chains.

As I iterate through the chain, what is the best way to store the each pass? E.g., if I complete non-burn pass 1757/10,000, and state contains vectors and scalars, say state is (;a=3.0, b=[4.0,6.0,7.0]), should I just push the draws to a DataFrame, or is there a better purpose-built solution?

Thanks!

---

<div class="post-metadata">

### Author: ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)
#### Post date: [November 14, 2022, 4:02pm UTC](https://discourse.julialang.org/t/recording-an-mcmc-chain/90243/2 "2022-11-14T16:02:34Z")

</div>

Without code it is hard to answer. But my advice is to allocate the whole data frame before running anything and then filling it instead of pushing.

---

<div class="post-metadata">

### Author: ![clinton](https://avatars.discourse-cdn.com/v4/letter/c/7ba0ec/32.png) [@clinton](https://discourse.julialang.org/u/clinton)
#### Post date: [November 14, 2022, 4:11pm UTC](https://discourse.julialang.org/t/recording-an-mcmc-chain/90243/3 "2022-11-14T16:11:16Z")

</div>

Code @lrnv :

```nohighlight
function recordstate(;i,state,records)
  #put state into records
end

records = #some data type
recordstate(;i=1757, state=(;a=3.0, b=[4.0,6.0,7.0]),records)

```

---

<div class="post-metadata">

### Author: ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)
#### Post date: [November 14, 2022, 4:12pm UTC](https://discourse.julialang.org/t/recording-an-mcmc-chain/90243/4 "2022-11-14T16:12:25Z")

</div>

Yes, that’ll work.

What I would do is have record preallocated (maybe with a bunch of zeros) before launching the chain.

---

<div class="post-metadata">

### Author: ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)
#### Post date: [November 14, 2022, 4:24pm UTC](https://discourse.julialang.org/t/recording-an-mcmc-chain/90243/5 "2022-11-14T16:24:29Z")

</div>

Isn’t there some actual API for pushing new values into the chain itself rather than producing some intermediate data structure

---

<div class="post-metadata">

### Author: ![clinton](https://avatars.discourse-cdn.com/v4/letter/c/7ba0ec/32.png) [@clinton](https://discourse.julialang.org/u/clinton)
#### Post date: [November 14, 2022, 4:32pm UTC](https://discourse.julialang.org/t/recording-an-mcmc-chain/90243/6 "2022-11-14T16:32:30Z")

</div>

@dlakelan That would be great, but I can’t seem to find it in the docs- am I missing something obvious?

---

<div class="post-metadata">

### Author: ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)
#### Post date: [November 14, 2022, 5:22pm UTC](https://discourse.julialang.org/t/recording-an-mcmc-chain/90243/7 "2022-11-14T17:22:14Z")

</div>

It looks like even the AdvancedMH creates an intermediate structure that subtypes AbstractTransition, then all at once bundles the samples afterwards [AdvancedMH.jl/mcmcchains-connect.jl at master · TuringLang/AdvancedMH.jl · GitHub](https://github.com/TuringLang/AdvancedMH.jl/blob/master/src/mcmcchains-connect.jl)

---

<div class="post-metadata">

### Author: ![clinton](https://avatars.discourse-cdn.com/v4/letter/c/7ba0ec/32.png) [@clinton](https://discourse.julialang.org/u/clinton)
#### Post date: [November 14, 2022, 7:27pm UTC](https://discourse.julialang.org/t/recording-an-mcmc-chain/90243/8 "2022-11-14T19:27:23Z")

</div>

Might just be a case where a standardized solution is more trouble than it is worth. To be fair, it is not like an intermediate structure is a huge burden to code up.

After some additional experimentation, it does seem possible to keep a handle to the underlying arrays in the chain e.g. `a=rand(10); c=Chains(a,:a);` creates the chain without copying. Then indexing directly into the three dimensional arrays seems to work (e.g. `c[2,:a,1]=1.0`) .

A bit cumbersome but serviceable if needed. However, seems like the interface intends the user to convert at the end so I might just do that, especially since I’m not sure how the copying/referencing will work in future releases.

---

<div class="post-metadata">

### Author: ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)
#### Post date: [November 14, 2022, 7:32pm UTC](https://discourse.julialang.org/t/recording-an-mcmc-chain/90243/9 "2022-11-14T19:32:21Z")

</div>

> [@clinton](#):
>
> To be fair, it is not like an intermediate structure is a huge burden to code up.

In fact it might well be less code burden than trying to fill in an array… But it is a lot of memory, particularly if you’re doing something like sampling 10k samples in 4 chains of a model with 10k parameters.

---

<div class="post-metadata">

### Author: ![clinton](https://avatars.discourse-cdn.com/v4/letter/c/7ba0ec/32.png) [@clinton](https://discourse.julialang.org/u/clinton)
#### Post date: [November 14, 2022, 9:35pm UTC](https://discourse.julialang.org/t/recording-an-mcmc-chain/90243/10 "2022-11-14T21:35:15Z")

</div>

Yeah- I ended up creating a wrapper object around the chain instead of an intermediate object. The wrapper contains views for accessing the chains for each of my parameters. Then I overloaded `getproperty` so I can use the “.” syntax for further ease of access- best of all I can still use the MCMCChain analytics without needing to create a new object.

Thanks for helping me think this through.
