File indexing completed on 2024-11-03 06:41:20

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 = 2 # Number of pathes to compute
0015 delta = 0.25
0016 dt = 0.1
0017 
0018 
0019 # trajectories
0020 x = np.zeros(pathes)
0021 
0022 # Iterate to compute the steps of the Brownian motion.
0023 i = 0
0024 out = ""
0025 for k in range(itersTotal):
0026     if out != "":
0027         out += "\n"
0028 
0029     out += str(k*dt)
0030     for p in range(pathes):
0031         x[p] += norm.rvs(scale=delta**2*dt)
0032         out += "\t" + str(x[p])
0033 
0034     i += 1
0035 
0036     if LIVE_DATA and i >= iters:
0037         if LIVE_DATA_DEBUG:
0038             print (out)
0039         # generate header
0040         outh = "t"
0041         for p in range(pathes):
0042             outh += "\tx" + str(p+1)
0043 
0044         datafile = open(fileName, "w")
0045 
0046         datafile.write(outh)
0047         datafile.write('\n')
0048         datafile.write(out)
0049         datafile.close()
0050 
0051         out = ""
0052         i = 0
0053         time.sleep(sleepInterval)
0054 
0055 if LIVE_DATA and out != "":
0056     datafile = open(fileName, "a")
0057     datafile.write(out)
0058     datafile.close()
0059     if LIVE_DATA_DEBUG:
0060         print (out)
0061 
0062 if not LIVE_DATA:
0063     datafile = open(fileName, "a")
0064     datafile.write(out)