Program Design¶
A Short Story¶
It’s not straightforward for ordinary programmers to write codes to automate the moment and covariance derivation in the Theory page.
The recursive equations (3) in the Theory page contain integral operations. The integrals are not that concise. What makes things worse is that the integrals grow recursively. Meanwhile, \(v_{n-1}\) will get buried in the results. It seems Symbolic Computation is needed to tidy the final expression such that we can make use of equation (1) in the Theory page. However, to the best of my knowledge, none of the current programming languages (Mathematica, MATLAB) or packages (SymPy) have offered supports in their Symbolic Computation for manipulating Itô processes, let alone computing their expectations, which are essential in the moment and covariance derivation of AJD models.
Thus, I believed expertise in compiler design is required to solve the problems from the bottom. And I do have tried to learn corresponding courses over several months but finally realized this demands time and lots of practice, so I quitted.
Fortunately, I come up a different solution (may also be simpler) afterwards which will be explained in the following sections.
Insights¶
I observe some features that allow me to bypass the compiler design approach. One of the features is that all \(E[I\!E_{n-1,t}^{n_3}I_{n-1,t}^{n_4}|v_{n-1}]\) can be represented as a “Polynomial” of following form
where \(b_{ijlopq}\) is the item coefficient. And \(E[I\!E_{n-1,t}^{n_3}I_{n-1,t}^{n_4}I_{n-1,t}^{*n_5}|v_{n-1}]\) can be represented similarly.
To facilitate the representation and corresponding operations, I designed
a new class called Poly which is a tailored
dictionary data structure, derived from the
UserDict class in the Python Standard Library
collections.
Essential Integral¶
The essential computation in recursive equation (3) of the Theory page is that of the following integral
For the indefinite integral, we have
where \(c_{nm0} \triangleq \frac{1}{n}\) and
Coefficient \(c_{nmi}\) is implemented as function
c_nmi() which returns a
Fraction instead of a
decimal (float number).
For the definite integral,
where \(F(t) = \int e^{nkt} t^m dt\). The definite integral is implemented
as function int_et().
Polynomial Representation¶
The result of the integral, returned by int_et(),
is represented as a “polynomial” of the following form
which is encoded in a Poly with
keyfor = ('e^{k[t-(n-1)h]}','[t-(n-1)h]','k^{-}') which is a derived
UserDict
with key = \((i,j^{'},l)\) and value = \(c_{ij^{'}l}\).
Code Design¶
Itô process moment¶
With \(E[I\!E_{n-1,t}^{n_3}I_{n-1,t}^{n_4}I_{n-1,t}^{*n_5}|v_{n-1}]\) represented as a “polynomial” of the following form
consequently, we have
Therefore, it’s profitable to consider the following generic integral
where
Implementation:
Function
int_e_poly()in moduleito_momis defined to accomplish the computation in equation (2).Function
recursive_IEII()in moduleito_momis defined to realize the recursive step in equation (3) of the Theory page.Function
moment_IEII()in moduleito_momis implemented to calculate \(E[I\!E_n^{n_3}I_n^{n_4}I_{n-1,t}^{*n_5}|v_{n-1}]\).
To demonstration, I re-write the following initial three moments in Itô process Moments - I in the Theory page according to the “polynomial” representation
Moments¶
Central Moments¶
where \(\boldsymbol{n} = (n_1,n_2,n_3,n_4,n_5)\) and \(\sum_{i=1}^5n_i=l\),
Equation (5) is further represented as a
Poly with
attribute
keyfor = ('e^{-kh}', 'k^{-}', 'theta', 'sigma_v', 'rho', 'sqrt(1-rho^2)'),key= \((i,n_1+n_2+n_3+j,n_1,n_3+j,n_4-j,n_5)\) andvalue= \(C_{n_1+n_2}^i C_{n_4}^j (-1)^{n_2+i+j} \frac{1}{2^{n_1+n_2+n_3+j}}\),
i.e.,
And we have
Implementation:
Define
c_n()andb_n()inajdmom.mdl_1fsv.cmomto implement equation (4) and (5), respectively.Define
moment_comb()for computing the moment under an exact combination of \((n_1,n_2,n_3,n_4,n_5)\).Define
sub_v()andcmoment_y()for computing the central moment \(E[\overline{y}_{n}^l]\).
Moments¶
where \(\boldsymbol{n}\) and \(c(\boldsymbol{n})\) are the same as these in (3) while
Implementation:
Define
b_n()in moduleajdmom.mdl_1fsv.momto implement equation (7).Define
moment_comb()in moduleajdmom.mdl_1fsv.momas a counterpart ofmoment_comb()inajdmom.mdl_1fsv.cmom.Define
moment_y()for computing the moment \(E[y_n^l]\).
Covariances¶
in which \(E[y_n^{l_1}]\) and \(E[y_{n+1}^{l_2}]\)
can be computed through moment_y()
in module ajdmom.mdl_1fsv.mom.
Thus, I only need to present the computation of
\(E[y_n^{l_1}y_{n+1}^{l_2}].\)
Co-Moments¶
where I used
Note that
Function ve_IEII_vn() is defined to accomplish
above computation and expand \(v_n\) which returns a poly with
keyfor
= (‘e^{-knh}IE_n’,‘e^{-kh}’,‘h’,‘v_{n-1}’,‘k^{-}’,‘theta’,‘sigma_v’), i.e.,
The expansion of \(v_n\) is done through,
(taking \(v_n^m\) as an example), where \(\boldsymbol{m} = (m_1,m_2,m_3)\), \(m_1+m_2+m_3 = m\), and
Implementation:
Define
ve_IEII_vn()in moduleajdmom.mdl_1fsv.cov.Define
moment_inner_comb()(in moduleajdmom.mdl_1fsv.cov) to compute the moment when the inner combination \(\boldsymbol{m}=(m_1,m_2,m_3,m_4,m_5)\) is fixed under an exact outer combination \(\boldsymbol{n}=(n_1,n_2,n_3,n_4,n_5)\).Define
moment_outer_comb()(in moduleajdmom.mdl_1fsv.cov) to compute the moment when only the combination of the \(\boldsymbol{n}=(n_1,n_2,n_3,n_4,n_5)\), \(\sum_{i=1}^5n_i=l_2\) is given.Define
moment_yy()(in moduleajdmom.mdl_1fsv.cov) for equation (9).Define
cov_yy()(in moduleajdmom.mdl_1fsv.cov) for equation (8).