File indexing completed on 2024-04-28 15:14:08
0001 #!/usr/bin/python 0002 0003 import time 0004 from scipy.stats import norm 0005 import numpy as np 0006 0007 LIVE_DATA = True 0008 LIVE_DATA_DEBUG = True 0009 0010 fileName = "out.txt" # name of the output file 0011 sleepInterval = 1 # sleep interval in seconds 0012 itersTotal = 100000 # total number of iterations to compute 0013 iters = 1000 # number of iterations done before the results are written out to the file 0014 pathes = 1 # Number of pathes to compute 0015 delta = 0.25 0016 dt = 0.1 0017 0018 0019 # trajectories 0020 x = np.zeros(pathes) 0021 0022 # generate header 0023 out = "t" 0024 for p in range(pathes): 0025 out += "\tx" + str(p+1) 0026 0027 # Iterate to compute the steps of the Brownian motion. 0028 i = 0 0029 for k in range(itersTotal): 0030 if out != "": 0031 out += "\n" 0032 0033 out += str(k*dt) 0034 for p in range(pathes): 0035 x[p] += norm.rvs(scale=delta**2*dt) 0036 out += "\t" + str(x[p]) 0037 0038 i += 1 0039 0040 if LIVE_DATA and i >= iters: 0041 datafile = open(fileName, "a") 0042 datafile.write(out) 0043 datafile.close() 0044 if LIVE_DATA_DEBUG: 0045 print(out) 0046 out = "" 0047 i = 0 0048 time.sleep(sleepInterval) 0049 0050 if LIVE_DATA and out != "": 0051 datafile = open(fileName, "a") 0052 datafile.write(out) 0053 datafile.close() 0054 if LIVE_DATA_DEBUG: 0055 print(out) 0056 0057 if not LIVE_DATA: 0058 datafile = open(fileName, "a") 0059 datafile.write(out)