def test_construct_streaming(self): # adapted from https://gist.github.com/raddy/bd0e977dc8437a4f8276 # spot, strike, vol, days till expiry, interest rate, call or put (1,-1) spot, strike, vol, dte, rate, cp = sy.symbols('spot strike vol dte rate cp') T = dte / 260. N = syNormal('N', 0.0, 1.0) d1 = (sy.ln(spot / strike) + (0.5 * vol ** 2) * T) / (vol * sy.sqrt(T)) d2 = d1 - vol * sy.sqrt(T) TimeValueExpr = sy.exp(-rate * T) * (cp * spot * cdf(N)(cp * d1) - cp * strike * cdf(N)(cp * d2)) PriceClass = ts.construct_streaming(TimeValueExpr) def strikes(): strike = 205 while strike <= 220: yield strike strike += 2.5 price = PriceClass(spot=tss.Const(210.59), # strike=tss.Print(tss.Const(205), text='strike'), strike=tss.Foo(strikes, interval=1), vol=tss.Const(14.04), dte=tss.Const(4), rate=tss.Const(.2175), cp=tss.Const(-1)) ret = tss.run(tss.Print(price._node)) time.sleep(2) assert len(ret) == 7
def test_deep_bfs(self): a = ts.Const(1, count=1) b = ts.Random() c = ts.Curve([1, 2, 3]) d = a + b e = a + c f = b + c g = ts.Print(d) h = ts.Print(e) i = ts.Print(f) def _ids(lst): return set([elem._id for elem in lst]) def _ids_ids(lst_of_list): ret = [] for lst in lst_of_list: ret.append(_ids(lst)) return ret assert _ids(a._deep_bfs(tops_only=True)) == _ids([a, b, c]) assert _ids(b._deep_bfs(tops_only=True)) == _ids([a, b, c]) assert _ids(c._deep_bfs(tops_only=True)) == _ids([a, b, c]) assert _ids(d._deep_bfs(tops_only=True)) == _ids([a, b, c]) assert _ids(e._deep_bfs(tops_only=True)) == _ids([a, b, c]) assert _ids(f._deep_bfs(tops_only=True)) == _ids([a, b, c]) assert _ids(g._deep_bfs(tops_only=True)) == _ids([a, b, c]) assert _ids(h._deep_bfs(tops_only=True)) == _ids([a, b, c]) assert _ids(i._deep_bfs(tops_only=True)) == _ids([a, b, c]) for x in (a, b, c, d, e, f, g, h, i): for y in (a, b, c, d, e, f, g, h, i): assert _ids_ids(x._deep_bfs()) == _ids_ids(y._deep_bfs())
def test_Sum(self): """ f = x+x+2 f' = 2 """ expected = [(x + x + 2, 2) for x in rng] t = ts.Timer(foo_range, count=len(rng), use_dual=True) t2 = ts.Timer(foo_range, count=len(rng), use_dual=True) c = ts.Const((2, 0), use_dual=True) out = ts.Sum(t, t2, c) assert ts.run(out) == expected
def test_Average(self): """ f = (x + x + 1)/3 f' = 2/3 """ expected = [((x + x + 1) / 3, 2 / 3) for x in rng] t = ts.Timer(foo_range, count=len(rng), use_dual=True) t2 = ts.Timer(foo_range, count=len(rng), use_dual=True) c = ts.Const((1, 0), use_dual=True) out = ts.Average(t, t2, c) assert ts.run(out) == expected
def test_Equal(self): t = ts.Timer(foo, count=2) c = ts.Const(1) out = ts.Equal(t, c) assert ts.run(out) == [True, False]
def test_Average(self): t = ts.Timer(foo, count=1) t2 = ts.Timer(foo, count=1) c = ts.Const(1) out = ts.Average(t, t2, c) assert ts.run(out) == [1]
def test_Sum(self): t = ts.Timer(foo, count=2) t2 = ts.Timer(foo, count=2) c = ts.Const(2) out = ts.Sum(t, t2, c) assert ts.run(out) == [4, 6]
def test_Pow(self): t = ts.Timer(foo, count=2) c = ts.Const(2) out = ts.Pow(t, c) assert ts.run(out) == [1, 4]
def test_Mod(self): t = ts.Timer(foo, count=5) c = ts.Const(3) out = ts.Mod(t, c) assert ts.run(out) == [1, 2, 0, 1, 2]
def test_Arccos(self): t = ts.Timer(foo, count=2) c = ts.Const(value=3) out = ts.Arccos(ts.Div(t, c)) assert ts.run(out) == [math.acos(1 / 3), math.acos(2 / 3)]
def test_Ge(self): t = ts.Timer(foo, count=2) c = ts.Const(2) out = ts.Ge(t, c) assert ts.run(out) == [False, True]
def test_run_simple(self): t = ts.Const(value=1, count=1) assert ts.run(t) == [1]
def test_Lt(self): t = ts.Timer(foo_range, count=2, use_dual=True) c = ts.Const((1, 1), use_dual=True) out = ts.Lt(c, t) assert ts.run(out) == [False, False]
def test_const_1(self): t = ts.Const(value=1, count=1) assert ts.run(t) == [1]
def test_const_2(self): t = ts.Const(value=1, count=5) assert ts.run(t) == [1, 1, 1, 1, 1]
def test_Arcsin(self): t = ts.Timer(func, count=2) c = ts.Const(value=3) out = ts.Arcsin(ts.Div(t, c)) assert ts.run(out) == [math.asin(1 / 3), math.asin(2 / 3)]
def test_Gt(self): t = ts.Timer(func, count=2) c = ts.Const(1) out = ts.Gt(t, c) assert ts.run(out) == [False, True]
def test_Lt(self): t = ts.Timer(func, count=2) c = ts.Const(1) out = ts.Lt(c, t) assert ts.run(out) == [False, True]
def test_Le(self): t = ts.Timer(func_range, count=2, use_dual=True) c = ts.Const((-9, 1), use_dual=True) out = ts.Le(c, t) assert ts.run(out) == [False, True]
def test_NotEqual(self): t = ts.Timer(foo_range, count=2, use_dual=True) c = ts.Const((-10, 1), use_dual=True) out = ts.NotEqual(t, c) assert ts.run(out) == [False, True]
def test_NotEqual(self): t = ts.Timer(foo, count=2) c = ts.Const(1) out = ts.NotEqual(t, c) assert ts.run(out) == [False, True]
def test_Ge(self): t = ts.Timer(foo_range, count=2, use_dual=True) c = ts.Const((-9, 1), use_dual=True) out = ts.Ge(t, c) assert ts.run(out) == [False, True]
def test_Le(self): t = ts.Timer(foo, count=2) c = ts.Const(2) out = ts.Le(c, t) assert ts.run(out) == [False, True]