Advertisement IC Markets

The Markov Chain Monte Carlo Engine

Share, develop, and backtest custom MQL4/MQL5 Expert Advisors, Python data-scraping scripts, trading bots, and automated market alert systems.
FTtrader
Posts: 1845
Joined: Mon Aug 03, 2026 2:43 pm

Re: The Markov Chain Monte Carlo Engine

Post by FTtrader »

To architect this in C# using the Task Parallel Library (TPL), three structural rules must be enforced:

Isolated State: System.Random is not thread-safe. Sharing a single instance across chains causes thread contention and silently corrupts the pseudo-random sequence, destroying the mathematical validity of the walk. Every chain must instantiate its own isolated RNG.

Overdispersed Initialization: Parallel chains should not start at the same coordinates. Inject them at widely dispersed starting points across the parameter space to guarantee the entire target distribution is explored and no chain is trapped in a localized peak.

Local Burn-In: Burn-in must be discarded at the individual chain level before the arrays are merged, not after aggregation.
Recommended broker for automated trading & scalping IC Markets
FTtrader
Posts: 1845
Joined: Mon Aug 03, 2026 2:43 pm

Re: The Markov Chain Monte Carlo Engine

Post by FTtrader »

The Parallel MCMC Manager

This implementation wraps the existing MetropolisHastingsEngine in an asynchronous manager using Task.WhenAll.

Code: Select all

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BayesianInference
{
    public class ParallelMcmcManager
    {
        private readonly IProbabilityModel _model;
        
        public ParallelMcmcManager(IProbabilityModel model)
        {
            _model = model;
        }

        /// <summary>
        /// Spawns independent MCMC chains, processes their burn-in, and aggregates the valid posterior samples.
        /// </summary>
        public async Task<List<double>> RunParallelChainsAsync(
            double[] data, 
            int numChains, 
            int iterationsPerChain, 
            int burnIn,
            double proposalStdDev)
        {
            var tasks = new List<Task<List<double>>>();
            
            for (int i = 0; i < numChains; i++)
            {
                // 1. Overdisperse the starting points (e.g., spreading them between -10.0 and 10.0)
                // In production, this dispersion range should be scaled to your specific Prior.
                double initialParam = -10.0 + (i * 20.0 / Math.Max(1, numChains - 1)); 
                
                // 2. Generate a highly unique seed for thread-safe isolation
                int seed = Guid.NewGuid().GetHashCode(); 
                
                tasks.Add(Task.Run(() => 
                {
                    // The engine and its internal Random instance are isolated to this thread
                    var engine = new MetropolisHastingsEngine(_model, seed);
                    
                    var rawSamples = engine.Run(data, iterationsPerChain, initialParam, proposalStdDev);
                    
                    // 3. Discard the burn-in phase locally before passing data back
                    return rawSamples.Skip(burnIn).ToList();
                }));
            }

            // Await execution of all CPU threads concurrently
            var chainResults = await Task.WhenAll(tasks);

            // 4. Aggregate results into a single pooled posterior distribution
            int validSamplesPerChain = iterationsPerChain - burnIn;
            var aggregatedPosterior = new List<double>(numChains * validSamplesPerChain);
            
            foreach (var chainSamples in chainResults)
            {
                aggregatedPosterior.AddRange(chainSamples);
            }

            return aggregatedPosterior;
        }
    }
}
FTtrader
Posts: 1845
Joined: Mon Aug 03, 2026 2:43 pm

Re: The Markov Chain Monte Carlo Engine

Post by FTtrader »

Execution and Aggregation

To execute the manager, await it in your calling code.

Code: Select all

var model = new GaussianMeanModel();
var parallelManager = new ParallelMcmcManager(model);

// Spawn 10 independent chains, running 10,000 iterations each, discarding the first 2,000.
// This yields 80,000 total valid posterior samples.
var aggregatedSamples = await parallelManager.RunParallelChainsAsync(
    data: observations, 
    numChains: 10, 
    iterationsPerChain: 10000, 
    burnIn: 2000, 
    proposalStdDev: 0.5);

Console.WriteLine($"Pooled Posterior Mean: {aggregatedSamples.Average():F4}");
FTtrader
Posts: 1845
Joined: Mon Aug 03, 2026 2:43 pm

Re: The Markov Chain Monte Carlo Engine

Post by FTtrader »

Verifying Convergence (The Gelman-Rubin Statistic)

When running parallel chains, you must mathematically verify that all independent chains have converged to the exact same target distribution before trusting the aggregated data.

Verification: Calculate the $\hat{R}$ (R-hat) statistic (the Gelman-Rubin diagnostic). This formula compares the variance between the different chains to the variance within the individual chains. If your calculated $\hat{R}$ value is $< 1.05$, you have successfully verified that the chains have converged and the aggregated posterior is statistically valid. If it is $> 1.05$, the chains are still exploring different mathematical spaces; you must increase the number of iterations or tune the proposal standard deviation.
FTtrader
Posts: 1845
Joined: Mon Aug 03, 2026 2:43 pm

Re: The Markov Chain Monte Carlo Engine

Post by FTtrader »

The Gelman-Rubin $\hat{R}$ (R-hat) diagnostic is a variance ratio test. To prove that multiple Markov chains have converged on the same target distribution, it compares the variance within each individual chain against the variance between the different chains.

Mathematically, it relies on three metrics computed after the burn-in phase has been discarded:

$W$ (Within-chain variance): The average of the sample variances of each chain. This tends to underestimate the true variance if the chains haven't fully explored the space.

$B$ (Between-chain variance): The variance of the individual chain means.

$V$ (Estimated variance): A weighted combination of $W$ and $B$ that provides an unbiased estimate of the true marginal posterior variance.As the chains converge and perfectly mix, $B$ shrinks toward zero, and the ratio of $V$ to $W$ approaches 1.0.
FTtrader
Posts: 1845
Joined: Mon Aug 03, 2026 2:43 pm

Re: The Markov Chain Monte Carlo Engine

Post by FTtrader »

C# Implementation

To use this diagnostic in the previous architecture, you must run it on the collection of independent chains before you pool their results into a single aggregated array.

Code: Select all

using System;
using System.Collections.Generic;
using System.Linq;

namespace BayesianInference
{
    public static class ConvergenceDiagnostics
    {
        /// <summary>
        /// Calculates the Gelman-Rubin R-hat statistic across multiple independent MCMC chains.
        /// Target threshold: R-hat < 1.05 indicates acceptable convergence.
        /// </summary>
        public static double CalculateRHat(List<List<double>> parallelChains)
        {
            if (parallelChains == null || parallelChains.Count < 2)
                throw new ArgumentException("At least two parallel chains are required to calculate R-hat.");

            int M = parallelChains.Count;      // Number of chains
            int N = parallelChains[0].Count;   // Number of valid samples per chain

            if (N < 2)
                throw new ArgumentException("Chains must contain at least two valid samples post-burn-in.");

            var chainMeans = new double[M];
            var chainVariances = new double[M];

            // 1. Calculate the mean and variance for each individual chain
            for (int i = 0; i < M; i++)
            {
                var chain = parallelChains[i];
                if (chain.Count != N)
                    throw new ArgumentException("All chains must have the exact same number of samples.");

                double mean = chain.Average();
                chainMeans[i] = mean;

                // Unbiased sample variance (N-1)
                double sumSquaredDiffs = chain.Sum(x => (x - mean) * (x - mean));
                chainVariances[i] = sumSquaredDiffs / (N - 1);
            }

            // 2. Calculate W: The mean of the within-chain variances
            double W = chainVariances.Average();

            if (W == 0.0)
                throw new InvalidOperationException("Within-chain variance is zero. The walker is completely stuck.");

            // 3. Calculate B: The between-chain variance
            double grandMean = chainMeans.Average();
            double betweenChainSumSq = chainMeans.Sum(m => (m - grandMean) * (m - grandMean));
            double B = (N / (double)(M - 1)) * betweenChainSumSq;

            // 4. Calculate V: The estimated marginal posterior variance
            double V = ((N - 1) / (double)N) * W + (1.0 / N) * B;

            // 5. Calculate R-hat (Potential Scale Reduction Factor)
            double rHat = Math.Sqrt(V / W);

            return rHat;
        }
    }
}
FTtrader
Posts: 1845
Joined: Mon Aug 03, 2026 2:43 pm

Re: The Markov Chain Monte Carlo Engine

Post by FTtrader »

Integration with the Parallel Manager

When calling this utility from the parallel manager, intercept the chainResults from the Task.WhenAll block.

Code: Select all

// Wait for all core threads to finish
var chainResults = await Task.WhenAll(tasks);

// Convert array of arrays to a List of Lists for the diagnostic
var unpooledChains = chainResults.Select(c => c.ToList()).ToList();

double rHat = ConvergenceDiagnostics.CalculateRHat(unpooledChains);
Console.WriteLine($"Gelman-Rubin R-hat: {rHat:F4}");

if (rHat > 1.05)
{
    Console.WriteLine("WARNING: Chains have not converged. Increase iterations or adjust proposal standard deviation.");
}
// Proceed to flatten unpooledChains into the final aggregated List...
WhitG47
Posts: 46
Joined: Wed Oct 21, 2026 5:37 am

Re: The Markov Chain Monte Carlo Engine

Post by WhitG47 »

FTtrader wrote:When calling this utility from the parallel manager, intercept the chainResults from the Task.WhenAll block.
FTtrader, fair enough on the R-hat point... I had a similar thing years back when I dabbled in some basic Monte Carlo stuff for stock valuation models, only ran two chains and they looked fine on paper but one was just stuck circling a local peak the whole time. Mind you I wasnt checking anything as rigorous as Gelman-Rubin, just eyeballing the output and getting lucky it was obvious.

G.
Post Reply