# Can ROOT do a MCMC sampling?

**URL:** <https://root-forum.cern.ch/t/can-root-do-a-mcmc-sampling/48485>\
**Category:** Newbie\
**Tags:** python, root\
**Created:** [January 21, 2022, 12:55pm UTC](https://root-forum.cern.ch/t/can-root-do-a-mcmc-sampling/48485 "2022-01-21T12:55:05Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daumann](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@Daumann](https://root-forum.cern.ch/u/Daumann)\
**Post date:** [January 21, 2022, 12:55pm UTC](https://root-forum.cern.ch/t/can-root-do-a-mcmc-sampling/48485/1 "2022-01-21T12:55:05Z")

</div>

Hi,

I need to scan a two and another four dimensional phase spaces using MCMC. I know roostats can do a mcmc sampling, but i am more familiar with PyROOT until now. So i wonder, does ROOT have a built in tool to do a mcmc sampling? If no, does any examples on MCMC sampling in rootstats using pyhton exists? I only found c++ ones until now.

ps: Although i looking for a mcmc, if there is another option to multi-dimensional sampling in root, i would be glad to know.

Thank for your attention!

Best Regards,  
Daumann.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [January 21, 2022, 2:35pm UTC](https://root-forum.cern.ch/t/can-root-do-a-mcmc-sampling/48485/2 "2022-01-21T14:35:24Z")

</div>

May be @moneta can help.

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [January 21, 2022, 3:05pm UTC](https://root-forum.cern.ch/t/can-root-do-a-mcmc-sampling/48485/3 "2022-01-21T15:05:10Z")

</div>

Hi,

You should be able to use RooStats MCMC classes from Python such as RooStats::MetropolisHasting which construct a RooStats::MarkovChain, containing list of sample points with corresponding weight. If you have any issue using these classes from Python please let me know.

In ROOT you can also sample multi-dimensional functions using the UNU.RAN package. It contains also a method based on MCMC. .And we have also Foam. A tutorial example showing the usage of both (still in C++, but it should be easy to translate in Python) is  
this one: [https://root.cern.ch/doc/master/unuranFoamTest\_8C.htmlhttps://root.cern.ch/doc/master/unuranFoamTest\_8C.htmlhttps://root.cern.ch/doc/master/unuranFoamTest\_8C.html](https://root.cern.ch/doc/master/unuranFoamTest_8C.htmlhttps://root.cern.ch/doc/master/unuranFoamTest_8C.htmlhttps://root.cern.ch/doc/master/unuranFoamTest_8C.html)

Cheers

Lorenzo

---

<div class="post-metadata">

**Author:** ![Daumann](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@Daumann](https://root-forum.cern.ch/u/Daumann)\
**Post date:** [January 23, 2022, 6:47pm UTC](https://root-forum.cern.ch/t/can-root-do-a-mcmc-sampling/48485/4 "2022-01-23T18:47:06Z")

</div>

Hi @moneta ,

I trying to follow the UNU.RAN tutorial you linked, i am looking at the multidimensional sampling part of the code, but i am having trouble with one thing, which is defining the gaus3d function as a TF3. Here is what i wrote:

```auto
import ROOT as root
from math import sqrt,exp
from array import array

def gaus3d(x,p):
    sigma_x = p[0]
    sigma_y = p[1]
    sigma_z = p[2]
    rho = p[2]
    u = x[0] / sigma_x 
    v = x[1] / sigma_y 
    w = x[2] / sigma_z 
    c = 1 - rho*rho 
    result = (1 / (2 * root.TMath.Pi() * sigma_x * sigma_y * sigma_z * sqrt(c)))* exp (-(u * u - 2 * rho * u * v + v * v + w*w) / (2 * c))
    return result

f = root.TF3("f",gaus3d,-10,10,-10,10,-10,10,3,3)

```

When i try to run this i get this error →

 ![Screenshot from 2022-01-23 15-43-57](https://root-forum.cern.ch/uploads/default/original/3X/b/5/b54ae2c2e2312e55e470f698a018a2e0ead447ea.png)

This looks some path error, could i have messed up the installation? I been using root for a while now, a don’t remember seeing this error before.

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [January 24, 2022, 1:38pm UTC](https://root-forum.cern.ch/t/can-root-do-a-mcmc-sampling/48485/5 "2022-01-24T13:38:54Z")

</div>

Hi,  
Yes it looks to me an issue with your Python support in ROOT. Can you run other Python examples ?  
I don’t have problems running your code

Cheers

Lorenzo

---

<div class="post-metadata">

**Author:** ![Daumann](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@Daumann](https://root-forum.cern.ch/u/Daumann)\
**Post date:** [January 24, 2022, 4:37pm UTC](https://root-forum.cern.ch/t/can-root-do-a-mcmc-sampling/48485/6 "2022-01-24T16:37:25Z")

</div>

Hi,

It really was a problem in the installation, i tested in another computer and it worked.

But now i am having another problem, with the [.SampleMulti(double\* x)] member. i wrote the following code so far:

```auto
import ROOT as root
from math import sqrt,exp
from array import array

def gaus3d(x,p):
    sigma_x = p[0]
    sigma_y = p[1]
    sigma_z = p[2]
    rho = p[2]
    u = x[0] / sigma_x 
    v = x[1] / sigma_y 
    w = x[2] / sigma_z 
    c = 1 - rho*rho 
    result = (1 / (2 * root.TMath.Pi() * sigma_x * sigma_y * sigma_z * sqrt(c)))* exp (-(u * u - 2 * rho * u * v + v * v + w*w) / (2 * c))
    return result

f = root.TF3("f",gaus3d,-10,10,-10,10,-10,10,3,3)
f.SetParameters(2,2,0.5)

dist = root.TUnuranMultiContDist(f)
unr = root.TUnuran(root.gRandom)

method = "hitro"
unr.Init(dist,method)

x = root.TArrayD(3)

h1 = root.TH3D("h13D","gaussian 3D distribution from Unuran",50,-10,10,50,-10,10,50,-10,10)

for i in range(1000):
      unr.SampleMulti(root.AddressOf(x))
      h1.Fill(x[0],x[1],x[2])

```

And i am having the following error:

```auto
Traceback (most recent call last):
  File "test.py", line 33, in <module>
    unr.SampleMulti(root.AddressOf(x))
TypeError: bool TUnuran::SampleMulti(double* x) =>
    TypeError: could not convert argument 1 (could not convert argument to buffer or nullptr)

```

I tried to input different forms of python and root arrays in the .SampleMulti(double\* x) object, but none worked. What i am doing wrong here?

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [January 24, 2022, 5:11pm UTC](https://root-forum.cern.ch/t/can-root-do-a-mcmc-sampling/48485/7 "2022-01-24T17:11:13Z")

</div>

Hi,  
Don’t use ROOT TArrayD, but instead you should be able to use a numpy array or a Python array. See for example the case of a Tree where a pointer is passed when reding it in `SetBranchAddress`. Search for PyROOT in [ROOT: TTree Class Reference](https://root.cern.ch/doc/master/classTTree.html#pro-static-attribs).

Cheers

Lorenzo

---

<div class="post-metadata">

**Author:** ![Daumann](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@Daumann](https://root-forum.cern.ch/u/Daumann)\
**Post date:** [January 24, 2022, 5:35pm UTC](https://root-forum.cern.ch/t/can-root-do-a-mcmc-sampling/48485/8 "2022-01-24T17:35:05Z")

</div>

It worked with the numpy array, thank you @moneta !

I will now try to use the RooStats classes in pyroot. But in terms of speed and overall performance, should i expect better results from UNU.RAN or the RooStats MCMC?

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [January 24, 2022, 7:24pm UTC](https://root-forum.cern.ch/t/can-root-do-a-mcmc-sampling/48485/9 "2022-01-24T19:24:13Z")

</div>

Hi,  
If you want to perform a Bayesian calculation to estimate a posterior distribution it is better using RooStats. Instead, if you want to sample from a multi-dimensional distribution then it is more efficient to use UNU.RAN

Cheers

Lorenzo
