File: brownian_motion.py

package info (click to toggle)
labplot 2.12.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 78,500 kB
  • sloc: cpp: 241,048; ansic: 6,324; python: 915; xml: 400; yacc: 237; sh: 221; awk: 35; makefile: 11
file content (59 lines) | stat: -rwxr-xr-x 1,174 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/python

import time
from scipy.stats import norm
import numpy as np

LIVE_DATA = True
LIVE_DATA_DEBUG = True

fileName = "out.txt" # name of the output file
sleepInterval = 1 # sleep interval in seconds
itersTotal = 100000 # total number of iterations to compute
iters = 1000 # number of iterations done before the results are written out to the file
pathes = 1 # Number of pathes to compute
delta = 0.25
dt = 0.1


# trajectories
x = np.zeros(pathes)

# generate header
out = "t"
for p in range(pathes):
    out += "\tx" + str(p+1)

# Iterate to compute the steps of the Brownian motion.
i = 0
for k in range(itersTotal):
	if out != "":
		out += "\n"

	out += str(k*dt)
	for p in range(pathes):
		x[p] += norm.rvs(scale=delta**2*dt)
		out += "\t" + str(x[p])

	i += 1

	if LIVE_DATA and i >= iters:
		datafile = open(fileName, "a")
		datafile.write(out)
		datafile.close()
		if LIVE_DATA_DEBUG:
			print(out)
		out = ""
		i = 0
		time.sleep(sleepInterval)

if LIVE_DATA and out != "":
	datafile = open(fileName, "a")
	datafile.write(out)
	datafile.close()
	if LIVE_DATA_DEBUG:
		print(out)

if not LIVE_DATA:
	datafile = open(fileName, "a")
	datafile.write(out)