gpt4 book ai didi

r - 布朗运动的桥梁采样

转载 作者:行者123 更新时间:2023-12-05 07:04:55 25 4
gpt4 key购买 nike

我正在为布朗运动的桥接采样生成代码以模拟采样路径,但我的答案总是全为零。下面是我的代码和图片算法。

#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])
}
}

算法如下: enter image description here

最佳答案

如果我没记错公式,在 Python 中它应该是这样的:

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

显示它:

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

关于r - 布朗运动的桥梁采样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62842356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com