-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD_operator.py
30 lines (26 loc) · 1.02 KB
/
D_operator.py
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
# D_operator.py
from scipy.integrate import cumtrapz
from sympy import Symbol, diff, lambdify
from H_operator import Hi
import numpy as np
def Dk(Fik, f0, directionFunc, F, tRangeForBond):
"""
For bond's row Fik, partial derivative ~
- sum_{i} [ e^{-Hi(f0)} * Fik[i] * \int_{0}^{t_i} F'( f0(t) ) * directionFunc(t) dt ]
implemented discretely via cumtrapz.
"""
# 1) Make diffF = F'(x):
x = Symbol('x')
diffedF = lambdify(x, diff(F(x), x), "numpy")
# 2) Evaluate e^{-H_i(f0)}
discount = np.exp(-Hi(f0, F, tRangeForBond))
# 3) Evaluate integrals of F'(f0(t)) * directionFunc(t) up to each point
# So inside cumtrapz we pass diffedF(f0(tRangeForBond)) * directionFunc(tRangeForBond).
integrals = cumtrapz(
diffedF(f0(tRangeForBond)) * directionFunc(tRangeForBond),
tRangeForBond,
initial=0
)
# 4) Multiply each i-th bond payment with discount_i times integrals_i,
# sum up, then put a negative sign
return -np.sum(discount * Fik * integrals)