The Diagrammatic Monte Carlo method (**DiagMC**) is a relatively new approach of solving differential equations, which are based on integral expressions. Since my PhD thesis studies the application of a more advanced version (the **BMC**, or Bold Diagrammatic Monte Carlo, method) to examine observables within the theory of Quantum Chromodynamics, I have to become familiar with the DiagMC method.

To apply the algorithm we need to prepare our analytical expression. We need to identify various parts and calculate some values. In this case we pick a spherical symmetrical potential *U(r)* with the exact form

_{0}θ(r

_{0}-r).

Now we have to calculate the Fourier transform of this potential, i.e.

^{3}

**r**U(r) exp(i

**qr**),

which results in

_{0}/ q

^{3}* (sin(q r

_{0})-q r

_{0}cos(q r

_{0})).

Here we also need to calculate an expression for small *q*, i.e. *q→0*,

_{q→0}u(q)=2 U

_{0}r

_{0}

^{3}/ 3.

Now that we prepared a few functions we need to work out our differential equation, which needs to be evaluated by using the Diagrammatic Monte Carlo method.

We want to solve a differential equation for *f(q)*,

_{-1}

^{1}dχ ∫

_{0}

^{∞}u(|

**q**-

**q'**|) f(q') dq'.

Here we can use that

**q**-

**q'**| = (q

^{2}+q'

^{2}-2qq'χ)

^{1/2}.

So we identify:

- D
_{1}(q) = -u(q) - D
_{2}(q,q',χ) = - 1/π u((q^{2}+q'^{2}-2qq'χ)^{1/2})f(q')

Calculating the normalization *I* can be a tough problem. Introducing another (non-physical) diagram we can simplify this:

- D
_{0}= const.

Now the number of order 0 diagrams *Z _{0}* is helpful. Using

*Z*approaches

_{0}/ Z_{steps}*D*(here

_{0}/ I*I*is the normalization, which takes

*D*into account!). Using this we can estimate

_{0}_{bin (s)}f(q) dq = H

_{s}D

_{0}/ Z

_{0},

where *H _{s}* is the value (sum of signs) in the s-th bin.

Finally we have to plugin everything into a working code, thinking of some (statistical) important steps:

- Everything has to be ergodic, i.e. we need to generate Markov chains that follow the equation of detailed balance.
- We need to normalize everything properly.
- Sufficient statistics are required before using the DiagMC sampling.

Considering these steps and following the DiagMC method we obtain the following algorithm:

- Initialize all variables
- Perform an infinite (or long enough) outer loop
- Measure in each step
- Perform an accept / reject step in every iteration
- Maybe print or save the statistics
- Maybe end the loop

The measuring is quite straight forward. Here we have to do the following:

- If we are in the 0th-order we exclude the measurement (this diagram was brought in artificially and must be excluded from measurements) and increment the value of the number of 0th-order measurements
- Otherwise we look at the current value of
*q*and select the corresponding index for binning. The index can be calculated by*(int)(q / Δq)*, whereΔq = q The value of the selected bin has to add the value of the current sign._{max}/ N_{bins}.

The accept / reject step is just an applied Metropolis algorithm, with respect to the current and the old diagram. Here we perform:

- If we are in the lowest order we compute the value of the next higher order
- If we are in the highest order we compute the value of the next lower order
- Otherwise we decide between all possible
*n*changes with a chance of*1/n*for each change and compute the value of the picked order - Now we calculate the ratio of the computed value over the current value
- If a random number (between 0 and 1) is smaller than this ratio we accept the step: the new order is the picked one and the current value is the computed one
- Finally we have to save the sign of the current order

The full C code can be downloaded below. Using the GNU C compiler (**gcc**) we can built the code (**swave.c**) by calling:

`gcc swave.c -lm`

So we must include the math library for the code to run. If you use a different compiler (like the one from Intel, **icc**), then remember to specify the correct parameters.