使用bootstrap计算符合wiener模型的一维时序数据的漂移系数和扩散系数的分布,已有原始数据,求Python代码
时间: 2024-03-02 22:53:22 浏览: 90
首先,我们需要准备Wiener模型的一维时序数据,可以使用以下代码生成:
```python
import numpy as np
T = 1.0
N = 1000
dt = T/N
t = np.linspace(0.0, N*dt, N+1)
W = np.random.standard_normal(size=N+1)
W[0] = 0.0
for i in range(1, N+1):
W[i] = W[i-1] + np.sqrt(dt)*W[i]
```
接下来,我们可以使用bootstrap方法来计算漂移系数和扩散系数的分布。以下是Python实现代码:
```python
import numpy as np
import matplotlib.pyplot as plt
def bootstrap(data, n_bootstraps):
n_samples = len(data)
bootstrap_samples = np.random.choice(data, size=(n_samples, n_bootstraps), replace=True)
bootstrap_means = np.mean(bootstrap_samples, axis=0)
bootstrap_std = np.std(bootstrap_samples, axis=0)
return bootstrap_means, bootstrap_std
T = 1.0
N = 1000
dt = T/N
t = np.linspace(0.0, N*dt, N+1)
W = np.random.standard_normal(size=N+1)
W[0] = 0.0
for i in range(1, N+1):
W[i] = W[i-1] + np.sqrt(dt)*W[i]
dW = np.diff(W)/np.sqrt(dt)
dW = np.insert(dW, 0, 0)
n_bootstraps = 1000
M = np.zeros(n_bootstraps)
S = np.zeros(n_bootstraps)
for i in range(n_bootstraps):
dW_boot = np.random.choice(dW, size=N+1, replace=True)
W_boot = np.cumsum(dW_boot)*np.sqrt(dt)
M[i] = np.mean(W_boot)
S[i] = np.std(W_boot)
M_mean, M_std = bootstrap(M, n_bootstraps)
S_mean, S_std = bootstrap(S, n_bootstraps)
plt.hist(M, bins=50, density=True, alpha=0.5)
plt.hist(S, bins=50, density=True, alpha=0.5)
plt.axvline(np.mean(M), color='r', linestyle='dashed', linewidth=2)
plt.axvline(np.mean(S), color='b', linestyle='dashed', linewidth=2)
plt.show()
```
代码中使用了bootstrap方法计算漂移系数和扩散系数的分布,并绘制了直方图和平均值的垂直线。注意,bootstrap方法的结果可能会因为随机性而略有不同,但整体趋势应该是相似的。
阅读全文
相关推荐


















