Bridge sampling for Brownian motion

Viewed 384

I am producing code for bridge sampling for Brownian motion to simulate sample paths but I keep getting all zeros for my answer. I have got my code and picture algorithm below.

#Brownian bridge for GBM
 Z<-rnorm(1, mean=0,sd=1)
 T=1
 W_[0]=0
 W_[T]=sqrt(T)*Z
 k=5
 j<-2^(k-1)
 W_=rep(NA,nt-1)
 for(k in 1:K){
 h=h/2
 for(j in 1:j){
  Z<-rnorm(1,mean=0,sd=1)
  W_[2*(j-1)*h]=0.5*(W_[2*(j-1)*h]+W_[2*j*h])+sqrt(h)*Z
 print(W_[2*(j-1)*h])
 }
 }

The algorithm is below: enter image description here

1 Answers

If I am not mistaken with the formulas, in Python it should be something like this:

import numpy as np
import pandas as pd

# Samples a Brownian bridge in the [a,b] interval, see https://en.wikipedia.org/wiki/Brownian_bridge#General_case
def BB(
    T: int = 252,
    a: float = 0,
    b: float = 0,
    alpha: float = 0,
    sigma: float = 1
) -> pd.Series:
    X0 = a
    X = pd.Series([X0])
    t1 = 0
    t2 = T
    for i in np.arange(1, T):
        t = i
        mu = (a + (t - t1)/(t2 - t1)*(b - a))
        s = ((t2 - t)*(t - t1)/(t2 - t1))
        ei = np.random.normal(alpha+mu, sigma+s)
        X.loc[i] = ((T - t)/T**0.5)*ei*((t/(T-t))**0.5)
    XT = b
    X[T] = XT
    return X

Show it:

from matplotlib import pyplot as plt
plt.plot(BB(a=0,b=0, T=100))
Related