Created
May 25, 2015 21:10
-
-
Save nightuser/6bf8d9943966ba480067 to your computer and use it in GitHub Desktop.
HW05: Wiener process
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
import bisect | |
import numpy as np | |
from functools import total_ordering | |
from numpy.random import normal | |
from pylab import * | |
@total_ordering | |
class Elem: | |
def __init__(self, key, value): | |
self.key = key | |
self.value = value | |
def __eq__(self, other): | |
return self.key == other.key | |
def __lt__(self, other): | |
return self.key < other.key | |
def __str__(self): | |
return '<%s, %s>' % (self.key, self.value) | |
def generator(): | |
values = [Elem(0, 0)] | |
def inner(x): | |
nonlocal values | |
elem = Elem(x, None) | |
ind = bisect.bisect_left(values, elem) | |
l = values[ind - 1] | |
if ind < len(values): | |
if values[ind] == elem: | |
return values | |
r = values[ind] | |
sigma12 = x - l.key | |
sigma22 = r.key - x | |
d = np.abs(l.value - r.value) | |
a = d * sigma12 / (sigma12 + sigma22) | |
sigma = np.sqrt(sigma12 * sigma22 / (sigma12 + sigma22)) | |
else: | |
a = 0 | |
sigma = np.sqrt(x - values[ind - 1].key) | |
elem.value = l.value + normal(a, sigma) | |
bisect.insort_left(values, elem) | |
return values | |
return inner | |
def main(): | |
gen = generator() | |
for i in range(10): | |
vs = gen(i) | |
vs = gen(7.3) | |
vs = gen(0.1) | |
print(';\n'.join(str(v) for v in vs)) | |
x = [] | |
y = [] | |
for elem in vs: | |
x.append(elem.key) | |
y.append(elem.value) | |
plot(x, y) | |
show() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment