User Guide¶
Installing ajdmom¶
Install ajdmom from the Python Package Index through
Command Prompt (cmd.exe) or Anaconda Prompt,
depending on how Python is installed in your Windows OS:
pip install ajdmom
The package can be installed similarly through Terminal if you are working on a Linux or macOS.
Alternatively, you can install directly from the GitHub repository:
pip install git+https://github.com/xmlongan/ajdmom
Heston SV model¶
The most well-known example of AJD model is
probably the Heston SV model which serves here
as a demonstration example. In the ajdmom package, it is treated as the
baseline model and described by the following
SDEs [1] ,
where \(s(t)\) is the asset price at time \(t\), and refer to the Theory page for details. The return \(y_i\) over the i-th interval of length \(h\) is defined as,
The moment derivation for the Heston SV model is implemented in the mdl_1fsv
subpackage of the ajdmom package.
Formula Derivation¶
The moment and covariance formulas are encoded in objects of class
Poly which is a customized dictionary data structure
derived from the
UserDict class in the Python Standard Library
collections.
Moment formulas
To get the formula for the first moment \(E[y_n]\):
>>> from ajdmom import mdl_1fsv # mdl_1fsv -> mdl_1fsvj, mdl_2fsv, mdl_2fsvj
>>> from pprint import pprint
>>>
>>> m1 = mdl_1fsv.moment_y(1) # 1 in moment_y(1) -> 2,3,4...
>>>
>>> # moment_y() -> cmoment_y() : central moment
>>> # dpoly(m1, wrt), wrt = 'k','theta',... : partial derivative
>>>
>>> msg = "which is a Poly with attribute keyfor = \n{}"
>>> print("moment_y(1) = "); pprint(m1); print(msg.format(m1.keyfor))
moment_y(1) =
{(0, 1, 0, 0, 1, 0, 0, 0): Fraction(-1, 2),
(0, 1, 0, 1, 0, 0, 0, 0): Fraction(1, 1)}
which is a Poly with attribute keyfor =
('e^{-kh}', 'h', 'k^{-}', 'mu', 'theta', 'sigma_v', 'rho', 'sqrt(1-rho^2)')
where the two key-value pairs within the returned poly of moment_y(1)
stand for
respectively. Adding together these two terms gives the first moment of the Heston SV model, i.e., \(E[y_n] = (\mu-\theta/2)h\).
Covariance formulas
The covariances considered in this package are that between \(y_n\) and its lag-1 counterpart \(y_{n+1}\) with orders \((l_1,l_2)\), i.e., \(cov(y_n^{l_1}, y_{n+1}^{l_2})\).
To derive the formula for the covariance \(cov(y_n^2,y_{n+1})\):
>>> from ajdmom import mdl_1fsv # mdl_1fsv -> mdl_1fsvj, mdl_2fsv, mdl_2fsvj
>>> from pprint import pprint
>>>
>>> cov21 = mdl_1fsv.cov_yy(2,1) # (2,1) -> (1,1), (1,2), (2,2), (3,2), ...
>>>
>>> # dpoly(cov21, wrt), wrt = 'k','theta',... : partial derivative
>>>
>>> msg = "which is a Poly with attribute keyfor =\n{}"
>>> print("cov_yy(2,1) = "); pprint(cov21); print(msg.format(cov21.keyfor))
cov_yy(2,1) =
{(0, 0, 3, 0, 1, 2, 0, 2): Fraction(-1, 4),
(0, 0, 3, 0, 1, 2, 2, 0): Fraction(-5, 4),
(0, 0, 4, 0, 1, 3, 1, 0): Fraction(3, 4),
(0, 0, 5, 0, 1, 4, 0, 0): Fraction(-1, 8),
(0, 1, 2, 0, 2, 1, 1, 0): Fraction(1, 2),
(0, 1, 2, 1, 1, 1, 1, 0): Fraction(-1, 1),
(0, 1, 3, 0, 2, 2, 0, 0): Fraction(-1, 8),
(0, 1, 3, 1, 1, 2, 0, 0): Fraction(1, 4),
(1, 0, 3, 0, 1, 2, 0, 2): Fraction(1, 2),
(1, 0, 3, 0, 1, 2, 2, 0): Fraction(5, 2),
(1, 0, 4, 0, 1, 3, 1, 0): Fraction(-3, 2),
(1, 0, 5, 0, 1, 4, 0, 0): Fraction(1, 4),
(1, 1, 2, 0, 1, 2, 2, 0): Fraction(1, 1),
(1, 1, 2, 0, 2, 1, 1, 0): Fraction(-1, 1),
(1, 1, 2, 1, 1, 1, 1, 0): Fraction(2, 1),
(1, 1, 3, 0, 1, 3, 1, 0): Fraction(-3, 4),
(1, 1, 3, 0, 2, 2, 0, 0): Fraction(1, 4),
(1, 1, 3, 1, 1, 2, 0, 0): Fraction(-1, 2),
(1, 1, 4, 0, 1, 4, 0, 0): Fraction(1, 8),
(2, 0, 3, 0, 1, 2, 0, 2): Fraction(-1, 4),
(2, 0, 3, 0, 1, 2, 2, 0): Fraction(-5, 4),
(2, 0, 4, 0, 1, 3, 1, 0): Fraction(3, 4),
(2, 0, 5, 0, 1, 4, 0, 0): Fraction(-1, 8),
(2, 1, 2, 0, 1, 2, 2, 0): Fraction(-1, 1),
(2, 1, 2, 0, 2, 1, 1, 0): Fraction(1, 2),
(2, 1, 2, 1, 1, 1, 1, 0): Fraction(-1, 1),
(2, 1, 3, 0, 1, 3, 1, 0): Fraction(3, 4),
(2, 1, 3, 0, 2, 2, 0, 0): Fraction(-1, 8),
(2, 1, 3, 1, 1, 2, 0, 0): Fraction(1, 4),
(2, 1, 4, 0, 1, 4, 0, 0): Fraction(-1, 8)}
which is a Poly with attribute keyfor =
('e^{-kh}', 'h', 'k^{-}', 'mu', 'theta', 'sigma_v', 'rho', 'sqrt(1-rho^2)')
Moment Values¶
Given an exact set of parameter values, values of the central moments, moments and covariances, and their partial derivatives w.r.t. a parameter can also be computed.
Moments and Central Moments
To compute the exact value of the third moment \(E[y_n^3]\), given \((\mu=0.125, k=0.1, \theta=0.25, \sigma_v=0.1, \rho=-0.7, h=1)\):
>>> ## Moments and Central Moments
>>> from ajdmom.mdl_1fsv.mom import m, dm # for moments
>>> from ajdmom.mdl_1fsv.cmom import cm, dcm # for central moments
>>>
>>> parameters = {'mu':0.125, 'k':0.1, 'theta':0.25,
... 'sigma_v':0.1, 'rho':-0.7, 'h': 1}
>>>
>>> # 3rd moment as an example
>>> moment = m(l=3, par=parameters) # cm: central moment
>>> # partial derivative w.r.t. parameter 'k'
>>> dmoment = dm(l=3, par=parameters, wrt='k') # dcm: central moment
>>> moment
-0.04489260315929133
>>> dmoment
0.20556366585696395
Covariances
To compute the exact value of covariance \(cov(y_n^2, y_{n+1}^2)\), given \((\mu=0.125, k=0.1, \theta=0.25, \sigma_v=0.1, \rho=-0.7, h=1)\):
>>> ## Covariance
>>> from ajdmom.mdl_1fsv.cov import cov, dcov
>>>
>>> parameters = {'mu':0.125, 'k':0.1, 'theta':0.25,
... 'sigma_v':0.1, 'rho':-0.7, 'h': 1}
>>>
>>> # covariance cov(y_n^2, y_{n+1}^2) as an example
>>> covariance = cov(l1=2, l2=2, par=parameters)
>>> # partial derivative w.r.t. parameter 'k'
>>> dcovariance = dcov(l1=2, l2=2, par=parameters, wrt='k')
>>> covariance
0.0149529894520537
>>> dcovariance
-0.15904979864793667
AJD Extensions¶
In addition to the Heston SV model, there are several extensions, which are summarized in the following table:
Model |
Description |
|---|---|
mdl_1fsv |
|
mdl_1fsvj |
|
mdl_2fsv |
|
mdl_2fsvj |
|
mdl_svvj |
|
mdl_svij |
|
mdl_svcj |
|
mdl_srjd |
|
Notes: SRD is short for Square-Root Diffusion.
Besides unconditional moment derivation, the ajdmom package also supports
conditional moment derivation with the initial state (\(v_0\)) of the variance process given beforehand,
conditional moment derivation with both the initial state (\(v_0\)) and the realized jump times and jump sizes of the variance process given beforehand, for those models having the corresponding jump component.
Most of the derivations have already been implemented within the ajdmom package.
Please refer to the individual subpackages for details. Under some cases, you
probably need to implement by yourself with additional efforts for untypical models.