models module
- class models.Ch_RealizedRisk(theta, n_points=8)[source]
Bases:
object
Filtering approach proposed in [1]. It is based on the assumption of iid subordinated returns following the t-distribution. The characteristic function approach is used to recover the low-frequency risk measures.
[1]: Gatta, F., & Lillo, F., & Mazzarisi, P. (2024). A High-frequency Approach to Risk Measures. TBD.
Parameters:
- theta: float
the target probability level.
- n_points: int, optional
Number of points used to evaluate the ES (which is computed as the average of equi-spaced quantile estimates at probability levels theta_j <= theta). Default is 8.
Example of usage
import numpy as np import pandas as pd from utils import price2params from models import Ch_RealizedRisk df = pd.read_csv('stock.csv') df.index = pd.to_datetime(df.index) price = df.Close fitted_pars = price2params(price, c=78, sub_type='tpv') #Fit the t-distribution parameters mdl = Ch_RealizedRisk(0.05) #Initialize the model qf, ef = mdl.fit(78, fitted_pars, jobs=4) #Run to obtain the filtered VaR and ES
Methods:
- fit(c, internal_state, jobs=1)[source]
Map the t-distribution parameters into the low-frequency risk measures.
- INPUTS:
- c: int
the number of time indexes to sample.
- internal_state: dict
the fitted t-distribution parameters. Every key correspond to a day. Every value is assumed to be a list [degrees of freedom, location, scale].
- jobs: int, optional
Number of simultaneous processes to run. Default is 1.
- OUTPUTS:
- qf: dict
the filtered VaR. It has the same keys as internal_state.
- ef: dict
the filtered ES. It has the same keys as internal_state.
- quantile_from_cf_wrapper(theta, nu, mu, scale, pipend)[source]
Compute the daily risk measures from the high-frequency characteristic function, by using the Gil-Pelaez formula. The intra-day returns are assumed to be iid, t-distributed with 0 mean. The routine is used in the multiprocessing framework.
- INPUTS:
- theta: float
the target probability level.
- nu: float
the degrees of freedom.
- mu: float
the location parameter of the t-distribution.
- scale: float
the scale parameter of the t-distribution.
- pipend: multiprocessing.Pipe
The pipe to communicate the result to the main process.
- class models.Ch_RealizedRisk_MA(theta, n_points=8)[source]
Bases:
object
Filtering approach proposed in [1]. It is based on the assumption of MA(1) subordinated returns with t-distributed innovations. The characteristic function approach is used to recover the low-frequency risk measures.
[1]: Gatta, F., & Lillo, F., & Mazzarisi, P. (2024). A High-frequency Approach to Risk Measures. TBD.
Parameters:
- theta: float
the target probability level.
- n_points: int, optional
Number of points used to evaluate the ES (which is computed as the average of equi-spaced quantile estimates at probability levels theta_j <= theta). Default is 8.
Example of usage
import numpy as np import pandas as pd from utils import price2params_ma from models import Ch_RealizedRisk_MA df = pd.read_csv('stock.csv') df.index = pd.to_datetime(df.index) price = df.Close fitted_pars = price2params_ma(price, c=78, sub_type='tpv') #Fit the t-distribution parameters mdl = Ch_RealizedRisk_MA(0.05) #Initialize the model qf, ef = mdl.fit(78, fitted_pars, jobs=4) #Run to obtain the filtered VaR and ES
Methods:
- fit(c, internal_state, jobs=1)[source]
Map the t-distribution parameters into the low-frequency risk measures.
- INPUTS:
- c: int
the number of time indexes to sample.
- internal_state: dict
the fitted t-distribution parameters. Every key correspond to a day. Every value is assumed to be a list [autoregressive parameter, degrees of freedom, location, scale].
- jobs: int, optional
Number of simultaneous processes to run. Default is 1.
- OUTPUTS:
- qf: dict
the filtered VaR. It has the same keys as internal_state.
- ef: dict
the filtered ES. It has the same keys as internal_state.
- quantile_from_cf_wrapper(theta, phi, nu, mu, scale, pipend)[source]
Compute the daily risk measures from the high-frequency characteristic function, by using the Gil-Pelaez formula. The intra-day returns are assumed to be iid, t-distributed with 0 mean. The routine is used in the multiprocessing framework.
- INPUTS:
- theta: float
the target probability level.
- phi: float
the autoregressive parameter.
- nu: float
the degrees of freedom.
- mu: float
the location parameter of the t-distribution.
- scale: float
the scale parameter of the t-distribution.
- pipend: multiprocessing.Pipe
The pipe to communicate the result to the main process.
- class models.DH_RealizedRisk(c, theta, H=0.5, sub_type='clock')[source]
Bases:
object
Filtering approach proposed in [1]. It is based on the assumption of self-similarity.
[1]: Dimitriadis, T., & Halbleib, R. (2022). Realized quantiles. Journal of Business & Economic Statistics, 40(3), 1346-1361.
Parameters:
- c: int
the number of time indexes to sample.
- theta: float
the target probability level.
- H: float, optional
Hurst exponent of the subordinated log-return process. Default is 0.5.
- sub_type: str, optional
Type of subordinator to use. Either ‘clock’ for the clock time; ‘tpv’ for the TriPower Variation; ‘vol’ for the volume; or ‘identity’ for the identity (that is, all the indexes are returned). Default is ‘clock’
Example of usage
import numpy as np import pandas as pd from models import DH_RealizedRisk df = pd.read_csv('stock.csv') df.index = pd.to_datetime(df.index) day_price = df.Close mdl = DH_RealizedRisk(78, 0.05, sub_type='tpv') #Initialize the model qf, ef = mdl.fit(day_price) #Run to obtain the filtered VaR and ES
Methods:
- fit(y, vol=None, tpv_int_min=15)[source]
Returns the filtered quantile and expected shortfall.
- INPUTS:
- y: pd.Series
the time series of intra-day prices, over all the day (that is, from 09:30 to 16:00)
- vol: pd.Series, optional
the volume series, on the same indexes as data. Only used when self.sub_type == ‘vol’
- tpv_int_min: int, optional
half-length of the window used for computing the tri-power variation. Only used when self.sub_type == ‘tpv’. Default is 15
- OUTPUTS:
- qf: dict
the filtered VaR. Every key correspond to a day of the y series.
- ef: dict
the filtered ES. Every key correspond to a day of the y series.
- class models.MC_RealizedRisk(theta)[source]
Bases:
object
Filtering approach proposed in [1]. It is based on the assumption of iid subordinated returns following the t-distribution. The Monte-Carlo approach is used to recover the low-frequency risk measures.
[1]: Gatta, F., & Lillo, F., & Mazzarisi, P. (2024). A High-frequency Approach to Risk Measures. TBD.
Parameters:
- theta: float or list
the target probability level, or a list of target probability levels.
Example of usage
import numpy as np import pandas as pd from utils import price2params from models import MC_RealizedRisk df = pd.read_csv('stock.csv') df.index = pd.to_datetime(df.index) price = df.Close fitted_pars = price2params(price, c=78, sub_type='tpv') #Fit the t-distribution parameters mdl = MC_RealizedRisk([0.05, 0.025]) #Initialize the model qf, ef = mdl.fit(78, fitted_pars) #Run to obtain the filtered VaR and ES
Methods:
- fit(N, c, internal_state, ant_v=True, seed=None)[source]
Map the t-distribution parameters into the low-frequency risk measures.
- INPUTS:
- N: int
the number of Monte-Carlo paths to simulate (if ant_v==True, the number is doubled).
- c: int
the number of time indexes to sample.
- internal_state: dict
the fitted t-distribution parameters. Every key correspond to a day. Every value is assumed to be a list [degrees of freedom, location, scale].
- ant_v: bool, optional
Flag to indicate if the antithetic variates should be used. Default is True.
- seed: int, optional
Seed for the random number generator. Default is None.
- OUTPUTS:
- qf: dict
the filtered VaR. If self.theta is a float, it has the same keys as internal_state. If self.theta is a list, it has a key for every element in the list. Every value is a dict itself with the same keys as internal_state.
- ef: dict
the filtered ES. If self.theta is a float, it has the same keys as internal_state. If self.theta is a list, it has a key for every element in the list. Every value is a dict itself with the same keys as internal_state.
- simulate_iid(N, c, nu, mu, sigma, ant_v=True, seed=None)[source]
Monte-Carlo simulation for extracting the distribution of the low-frequency return.
- INPUTS:
- N: int
the number of Monte-Carlo paths to simulate (if ant_v==True, the number is doubled).
- c: int
the number of time indexes to sample.
- nu: float
the degrees of freedom.
- mu: float
the location parameter of the t-distribution.
- sigma: float
the scale parameter of the t-distribution.
- ant_v: bool, optional
Flag to indicate if the antithetic variates should be used. Default is True.
- seed: int, optional
Seed for the random number generator. Default is None.
- OUTPUTS:
- samples: ndarray
the simulated low-frequency returns.
- class models.MC_RealizedRisk_MA(theta)[source]
Bases:
object
Filtering approach proposed in [1]. It is based on the assumption of MA(1) subordinated returns with t-distributed innovations. The Monte-Carlo approach is used to recover the low-frequency risk measures.
[1]: Gatta, F., & Lillo, F., & Mazzarisi, P. (2024). A High-frequency Approach to Risk Measures. TBD.
Parameters:
- theta: float or list
the target probability level, or a list of target probability levels.
Example of usage
import numpy as np import pandas as pd from utils import price2params_ma from models import MC_RealizedRisk_MA df = pd.read_csv('stock.csv') df.index = pd.to_datetime(df.index) price = df.Close fitted_pars = price2params_ma(price, c=78, sub_type='tpv') #Fit the t-distribution parameters mdl = MC_RealizedRisk_MA([0.05, 0.025]) #Initialize the model qf, ef = mdl.fit(78, fitted_pars) #Run to obtain the filtered VaR and ES
Methods:
- fit(N, c, internal_state, ant_v=True, seed=None)[source]
Map the t-distribution parameters into the low-frequency risk measures.
- INPUTS:
- N: int
the number of Monte-Carlo paths to simulate (if ant_v==True, the number is doubled).
- c: int
the number of time indexes to sample.
- internal_state: dict
the fitted t-distribution parameters. Every key correspond to a day. Every value is assumed to be a list [degrees of freedom, location, scale].
- ant_v: bool, optional
Flag to indicate if the antithetic variates should be used. Default is True.
- seed: int, optional
Seed for the random number generator. Default is None.
- OUTPUTS:
- qf: dict
the filtered VaR. If self.theta is a float, it has the same keys as internal_state. If self.theta is a list, it has a key for every element in the list. Every value is a dict itself with the same keys as internal_state.
- ef: dict
the filtered ES. If self.theta is a float, it has the same keys as internal_state. If self.theta is a list, it has a key for every element in the list. Every value is a dict itself with the same keys as internal_state.
- simulate_ma(N, c, phi, nu, mu, sigma, ant_v=True, seed=None)[source]
Monte-Carlo simulation for extracting the distribution of the low-frequency return.
- INPUTS:
- N: int
the number of Monte-Carlo paths to simulate (if ant_v==True, the number is doubled).
- c: int
the number of time indexes to sample.
- phi: float
the autoregressive coefficient.
- nu: float
the degrees of freedom.
- mu: float
the location parameter of the t-distribution.
- sigma: float
the scale parameter of the t-distribution.
- ant_v: bool, optional
Flag to indicate if the antithetic variates should be used. Default is True.
- seed: int, optional
Seed for the random number generator. Default is None.
- OUTPUTS:
- samples: ndarray
the simulated low-frequency returns.