Skip to content

Instantly share code, notes, and snippets.

@Fi3
Last active March 27, 2020 18:23
Show Gist options
  • Save Fi3/766253ff5b0b0042b10cafc88f2006f8 to your computer and use it in GitHub Desktop.
Save Fi3/766253ff5b0b0042b10cafc88f2006f8 to your computer and use it in GitHub Desktop.
calcoli
#!/usr/bin/env python3
from decimal import *
def main():
getcontext().prec = 30
getcontext().rounding = ROUND_HALF_EVEN
s0 = Decimal(0.16670847)
sigma = Decimal(0.0427111)
k = Decimal(0.0630307823077064)
r = Decimal(0.02 / 365)
e = Decimal(2.7182818284590452353602874713526624977572)
dt = Decimal(1)
q = Decimal(0)
df = e**(-r * dt)
u = e**(sigma*dt.sqrt())
d = Decimal(1)/u
p = ((e**(r-q) * dt) - d) / (u - d)
lattice = lattice_evolution(s0, 1095, u, d)
reduced_l = lattice_valuation(lattice, k, p, df)
print(len(reduced_l))
print(len(reduced_l[0]))
v0 = reduced_l[-1]
print("aaa", v0)
def lattice_evolution(s0, days, u, d):
lattice = []
last = []
for day in range(days):
if day == 0:
last = [s0 * u, s0 * d]
lattice.append(last)
else:
head = last[0]
tail = last[1::]
head = [head * u, head * d]
tail = list(map(lambda x: x * d, tail))
last = head + tail
lattice.append(last)
lattice = [[3]] + lattice
return lattice
def lattice_valuation(lattice, k, p, df):
lattice.reverse()
reduced = []
last = []
for (i, day) in enumerate(lattice):
if i == 0:
last = list(map(lambda x: max((x - k), 0), day))
reduced.append(last)
else:
last = list(map(
lambda x:
max((x[1] - k), 0) + (last[x[0]] * p * df) + (last[x[0] + 1] * (1 - p) * df),
enumerate(day)))
reduced.append(last)
return reduced
def test_reduce_lattice():
k = 1
p = 4
df = 8
u = 10
d = 9
def rr(val, a, b):
return max((val - k), 0) + (a * p * df) + (b * (1 - p) * df)
lattice = [
[3],
[3*u, 3*d],
[3*u*u, 3*u*d, 3*d*d],
[3*u*u*u, 3*u*u*d, 3*u*d*d, 3*d*d*d],
[3*u*u*u*u, 3*u*u*u*d, 3*u*u*d*d, 3*u*d*d*d, 3*d*d*d*d]
]
expected = [
[ rr(
3,
rr(
3*u,
rr(
3*u*u,
rr(3*u*u*u,max((3*u*u*u*u - k), 0),max((3*u*u*u*d - k), 0)),
rr(3*u*u*d,max((3*u*u*u*d - k), 0),max((3*u*u*d*d - k), 0)),
),
rr(
3*u*d,
rr(3*u*u*d,max((3*u*u*u*d - k), 0),max((3*u*u*d*d - k), 0)),
rr(3*u*d*d,max((3*u*u*d*d - k), 0),max((3*u*d*d*d - k), 0)),
)
),
rr(
3*d,
rr(
3*u*d,
rr(3*u*u*d,max((3*u*u*u*d - k), 0),max((3*u*u*d*d - k), 0)),
rr(3*u*d*d,max((3*u*u*d*d - k), 0),max((3*u*d*d*d - k), 0)),
),
rr(
3*d*d,
rr(3*u*d*d,max((3*u*u*d*d - k), 0),max((3*u*d*d*d - k), 0)),
rr(3*d*d*d,max((3*u*d*d*d - k), 0),max((3*d*d*d*d - k), 0))
)
)
)
],
[
rr(
3*u,
rr(
3*u*u,
rr(3*u*u*u,max((3*u*u*u*u - k), 0),max((3*u*u*u*d - k), 0)),
rr(3*u*u*d,max((3*u*u*u*d - k), 0),max((3*u*u*d*d - k), 0)),
),
rr(
3*u*d,
rr(3*u*u*d,max((3*u*u*u*d - k), 0),max((3*u*u*d*d - k), 0)),
rr(3*u*d*d,max((3*u*u*d*d - k), 0),max((3*u*d*d*d - k), 0)),
)
),
rr(
3*d,
rr(
3*u*d,
rr(3*u*u*d,max((3*u*u*u*d - k), 0),max((3*u*u*d*d - k), 0)),
rr(3*u*d*d,max((3*u*u*d*d - k), 0),max((3*u*d*d*d - k), 0)),
),
rr(
3*d*d,
rr(3*u*d*d,max((3*u*u*d*d - k), 0),max((3*u*d*d*d - k), 0)),
rr(3*d*d*d,max((3*u*d*d*d - k), 0),max((3*d*d*d*d - k), 0))
)
)
],
[
rr(
3*u*u,
rr(3*u*u*u,max((3*u*u*u*u - k), 0),max((3*u*u*u*d - k), 0)),
rr(3*u*u*d,max((3*u*u*u*d - k), 0),max((3*u*u*d*d - k), 0)),
),
rr(
3*u*d,
rr(3*u*u*d,max((3*u*u*u*d - k), 0),max((3*u*u*d*d - k), 0)),
rr(3*u*d*d,max((3*u*u*d*d - k), 0),max((3*u*d*d*d - k), 0)),
),
rr(
3*d*d,
rr(3*u*d*d,max((3*u*u*d*d - k), 0),max((3*u*d*d*d - k), 0)),
rr(3*d*d*d,max((3*u*d*d*d - k), 0),max((3*d*d*d*d - k), 0))
)
],
[
rr(3*u*u*u,max((3*u*u*u*u - k), 0),max((3*u*u*u*d - k), 0)),
rr(3*u*u*d,max((3*u*u*u*d - k), 0),max((3*u*u*d*d - k), 0)),
rr(3*u*d*d,max((3*u*u*d*d - k), 0),max((3*u*d*d*d - k), 0)),
rr(3*d*d*d,max((3*u*d*d*d - k), 0),max((3*d*d*d*d - k), 0))
],
[
max((3*u*u*u*u - k), 0),
max((3*u*u*u*d - k), 0),
max((3*u*u*d*d - k), 0),
max((3*u*d*d*d - k), 0),
max((3*d*d*d*d - k), 0)
]
]
result = lattice_valuation(lattice, k, p, df)
expected.reverse()
if expected != result:
print(expected)
print(result)
assert False
else:
print("test_lattice_evolution OK")
def test_lattice_evolution():
start = 3
days = 4
u = 10
d = 9
expected = [
[3],
[3*u, 3*d],
[3*u*u, 3*u*d, 3*d*d],
[3*u*u*u, 3*u*u*d, 3*u*d*d, 3*d*d*d],
[3*u*u*u*u, 3*u*u*u*d, 3*u*u*d*d, 3*u*d*d*d, 3*d*d*d*d]
]
result = lattice_evolution(start, days, u, d)
if expected != result:
print(expected)
print(result)
assert False
else:
print("test_lattice_evolution OK")
if __name__ == "__main__":
test_lattice_evolution()
test_reduce_lattice()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment