While循环在不同时重复相同的输出

时间:2019-07-26 04:16:16

标签: python pandas

我不明白为什么下面的代码从SECOND循环中为simulated_returns_pr输出相同的随机变量(与函数中的两个图表相同)。实际上,我删除了一些代码,但随后所有应不同的变量也与SECOND循环相同。我错过了一些东西,但听不懂。任何贡献将不胜感激。

我的代码:

logR= timeseries

i=1
while i < 5:
  simulated_returns_pr= np.random.normal(loc=mean(logR)*30, scale=stdev(logR)*np.sqrt(30.), size=30) 

  seed = 2
  N = 30                           

  def Brownian(seed, N):
    np.random.seed(seed)                         
    dt = 1./N                                    # time step
    b = simulated_returns_pr*np.sqrt(dt)                       
    W = np.cumsum(b)                             # brownian path
    return W, b

  b = Brownian(seed, N)[1]
  W = Brownian(seed, N)[0]
  W = np.insert(W, 0, 0.)                      

  plt.rcParams['figure.figsize'] = (10,8)
  xb = np.linspace(1, len(b), len(b))
  plt.plot(xb, b)
  plt.title('Brownian Increments')
  plt.show()

  xw = np.linspace(1, len(W), len(W))
  plt.plot(xw, W)
  plt.title('Brownian Motion')
  plt.show()
  i += 1

输出simulated_returns_pr

[ 0.012191    1.16322303 -0.23225735 -0.12357125  0.35687974  1.02187274
  0.25248517  0.74665974  0.54373161  0.43677913  0.69960184 -0.81226681
  0.50380517 -0.25108897  0.47459444  0.49541601  0.79958083 -0.20233765
  0.5142276  -0.31340253  0.46332258  0.48350956  0.06662023  0.53800548
 -0.01440759 -0.23280276 -0.07377719 -0.29948791  0.15798112  0.10707121]
[-0.10796927  0.07350919 -0.97356921  0.9275805  -0.80101665 -0.32191758
  0.35499571 -0.52506813 -0.43075947 -0.35577774  0.37944815  1.25577886
  0.12274682 -0.4609512   0.37320789 -0.19828379  0.09220437  0.69335439
 -0.27465829  0.10637854 -0.3402222   0.02308293  0.2309978  -0.3959363
 -0.06873477 -0.01706476 -0.21917336 -0.49603296 -0.61363441  0.02456247]
[-0.10796927  0.07350919 -0.97356921  0.9275805  -0.80101665 -0.32191758
  0.35499571 -0.52506813 -0.43075947 -0.35577774  0.37944815  1.25577886
  0.12274682 -0.4609512   0.37320789 -0.19828379  0.09220437  0.69335439
 -0.27465829  0.10637854 -0.3402222   0.02308293  0.2309978  -0.3959363
 -0.06873477 -0.01706476 -0.21917336 -0.49603296 -0.61363441  0.02456247]
[-0.10796927  0.07350919 -0.97356921  0.9275805  -0.80101665 -0.32191758
  0.35499571 -0.52506813 -0.43075947 -0.35577774  0.37944815  1.25577886
  0.12274682 -0.4609512   0.37320789 -0.19828379  0.09220437  0.69335439
 -0.27465829  0.10637854 -0.3402222   0.02308293  0.2309978  -0.3959363
 -0.06873477 -0.01706476 -0.21917336 -0.49603296 -0.61363441  0.02456247]

0 个答案:

没有答案