Using QuantLib to generate a Brownian Bridge

This is a very short article illustrating how to use the QuantLib library Brownian bridge code. The reason there can be some confusion is that the code takes a sequence of random variates as input for the bridge construction code. The first variate in this sequence is taken to represent the global step of the bridge, i.e., how much the value of the underlying changes over the entire period (for bridge of length of unit time only). If the first variate is set to zero, then the standard Brownian bridge that begins and ends at zero will be created.

The code

Here is a simple program that uses the QuantLib library to generate a Brownian bridge:

   Copyright (C) 2009 Bojan Nikolic <>

   Output a realisation of a Brownian Bridge from QuantLib


#include <iostream>

#include <time.h>
#include <boost/random/normal_distribution.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/variate_generator.hpp>

#include <ql/quantlib.hpp>

/// Prints the bridge, including the cumulative sum of the variations
/// which is the actual path of the underlying
void printBridge(const std::vector<double> &b)
  double cumsum=0;
  for(size_t i=0; i<b.size(); ++i)

void bbridgeout(void)
  const size_t n=500;

  QuantLib::BrownianBridge bridge(n);

  std::vector<double> inp(n);

  // This is the key fact: the first variate in the input sequency is
  // used to construct a global step. If you want the usual Brownian
  // bridge, you need to set this to zero.

  // Generate the rest of the variates as standard Gaussian random
  // values
  boost::variate_generator<boost::mt19937, boost::normal_distribution<> >

  for(size_t i=1; i<inp.size(); ++i)

  std::vector<double> out(n);


int main(void)
  return 0;

A sample realisation

Here a sample path produced by the Brownian bridge program:

A sample realisation of the Brownian bridge processes