Title: | Modeling and Forecasting Financial Intraday Signals |
---|---|
Description: | Models, analyzes, and forecasts financial intraday signals. This package currently supports a univariate state-space model for intraday trading volume provided by Chen (2016) <doi:10.2139/ssrn.3101695>. |
Authors: | Shengjie Xiu [aut], Yifan Yu [aut], Daniel P. Palomar [cre, aut, cph] |
Maintainer: | Daniel P. Palomar <[email protected]> |
License: | Apache License (== 2.0) |
Version: | 0.0.1.9000 |
Built: | 2024-11-17 05:12:08 UTC |
Source: | https://github.com/convexfi/intradaymodel |
This package uses state-of-the-art state-space models to facilitate the modeling, analyzing and forecasting of financial intraday signals. It currently offers a univariate model for intraday trading volume, with new features on intraday volatility and multivariate models in development.
fit_volume
,
decompose_volume
,
forecast_volume
,
generate_plots
For a quick help see the README file: GitHub-README.
Shengjie Xiu, Yifan Yu and Daniel P. Palomar
This function decomposes the intraday volume into daily, seasonal, and intraday dynamic components according to (Chen et al., 2016).
If purpose = “analysis”
(aka Kalman smoothing), the optimal components are conditioned on both the past and future observations.
Its mathematical expression is ,
where
is the total number of bins in the dataset.
If purpose = “forecast”
(aka Kalman forecasting), the optimal components are conditioned on only the past observations.
Its mathematical expression is .
Three measures are used to evaluate the model performance:
Mean absolute error (MAE):
;
Mean absolute percent error (MAPE):
;
Root mean square error (RMSE):
.
decompose_volume(purpose, model, data, burn_in_days = 0)
decompose_volume(purpose, model, data, burn_in_days = 0)
purpose |
String |
model |
A model object of class " |
data |
An n_bin * n_day matrix or an |
burn_in_days |
Number of initial days in the burn-in period for forecast. Samples from the first |
A list containing the following elements:
original_signal
: A vector of original intraday volume;
smooth_signal
/ forecast_signal
: A vector of smooth/forecast intraday volume;
smooth_components
/forecast_components
: A list of smooth/forecast components: daily, seasonal, intraday dynamic, and residual components.
error
: A list of three error measures: mae, mape, and rmse.
Shengjie Xiu, Yifan Yu and Daniel P. Palomar
Chen, R., Feng, Y., and Palomar, D. (2016). Forecasting intraday trading volume: A Kalman filter approach. Available at SSRN 3101695.
library(intradayModel) data(volume_aapl) volume_aapl_training <- volume_aapl[, 1:20] volume_aapl_testing <- volume_aapl[, 21:50] model_fit <- fit_volume(volume_aapl_training, fixed_pars = list(a_mu = 0.5, var_mu = 0.05), init_pars = list(a_eta = 0.5)) # analyze training volume analysis_result <- decompose_volume(purpose = "analysis", model_fit, volume_aapl_training) # forecast testing volume forecast_result <- decompose_volume(purpose = "forecast", model_fit, volume_aapl_testing) # forecast testing volume with burn-in forecast_result <- decompose_volume(purpose = "forecast", model_fit, volume_aapl[, 1:50], burn_in_days = 20)
library(intradayModel) data(volume_aapl) volume_aapl_training <- volume_aapl[, 1:20] volume_aapl_testing <- volume_aapl[, 21:50] model_fit <- fit_volume(volume_aapl_training, fixed_pars = list(a_mu = 0.5, var_mu = 0.05), init_pars = list(a_eta = 0.5)) # analyze training volume analysis_result <- decompose_volume(purpose = "analysis", model_fit, volume_aapl_training) # forecast testing volume forecast_result <- decompose_volume(purpose = "forecast", model_fit, volume_aapl_testing) # forecast testing volume with burn-in forecast_result <- decompose_volume(purpose = "forecast", model_fit, volume_aapl[, 1:50], burn_in_days = 20)
The main function for defining and fitting a univaraite state-space model on intraday trading volume. The model is proposed in (Chen et al., 2016) as
where
is the hidden state vector containing the log daily component and the log intraday dynamic component;
is the state transition matrix with
is the observation matrix;
is the corresponding element from
, which is the log seasonal component;
represents the i.i.d. Gaussian noise in the state transition, with a time-varying covariance matrix
and
is the i.i.d. Gaussian noise in the observation;
is the initial state at
, and it follows
.
In the model,
are treated as parameters.
The model is fitted by expectation-maximization (EM) algorithms. The implementation follows (Chen et al., 2016), and the accelerated scheme is provided in (Varadhan and Roland, 2008).
The algorithm terminates when
maxit
is reached or the condition is satisfied.
fit_volume( data, fixed_pars = NULL, init_pars = NULL, verbose = 0, control = NULL )
fit_volume( data, fixed_pars = NULL, init_pars = NULL, verbose = 0, control = NULL )
data |
An n_bin * n_day matrix or an |
fixed_pars |
A list of parameters' fixed values. The allowed parameters are listed below,
|
init_pars |
A list of unfitted parameters' initial values. The parameters are the same as |
verbose |
An integer specifying the print level of information during the algorithm (default
|
control |
A list of control values of EM algorithm:
|
A list of class "volume_model
" with the following elements (if the algorithm converges):
par
: A list of parameters' fitted values.
init
: A list of valid initial values from users.
par_log
: A list of intermediate parameters' values if log_switch = TRUE
.
converged
: A list of logical values indicating whether each parameter is fitted.
Shengjie Xiu, Yifan Yu and Daniel P. Palomar
Chen, R., Feng, Y., and Palomar, D. (2016). Forecasting intraday trading volume: A Kalman filter approach. Available at SSRN 3101695.
Varadhan, R., and Roland, C. (2008). Simple and globally convergent methods for accelerating the convergence of any EM algorithm. Scandinavian Journal of Statistics, 35(2), 335–353.
library(intradayModel) data(volume_aapl) volume_aapl_training <- volume_aapl[, 1:20] # fit model with no prior knowledge model_fit <- fit_volume(volume_aapl_training) # fit model with fixed_pars and init_pars model_fit <- fit_volume(volume_aapl_training, fixed_pars = list(a_mu = 0.5, var_mu = 0.05), init_pars = list(a_eta = 0.5)) # fit model with other control options model_fit <- fit_volume(volume_aapl_training, verbose = 2, control = list(acceleration = FALSE, maxit = 1000, abstol = 1e-4, log_switch = FALSE))
library(intradayModel) data(volume_aapl) volume_aapl_training <- volume_aapl[, 1:20] # fit model with no prior knowledge model_fit <- fit_volume(volume_aapl_training) # fit model with fixed_pars and init_pars model_fit <- fit_volume(volume_aapl_training, fixed_pars = list(a_mu = 0.5, var_mu = 0.05), init_pars = list(a_eta = 0.5)) # fit model with other control options model_fit <- fit_volume(volume_aapl_training, verbose = 2, control = list(acceleration = FALSE, maxit = 1000, abstol = 1e-4, log_switch = FALSE))
This function forecasts one-bin-ahead intraday volume.
Its mathematical expression is .
It is a wrapper of
decompose_volume()
with purpose = "forecast"
.
forecast_volume(model, data, burn_in_days = 0)
forecast_volume(model, data, burn_in_days = 0)
model |
A model object of class " |
data |
An n_bin * n_day matrix or an |
burn_in_days |
Number of initial days in the burn-in period. Samples from the first |
A list containing the following elements:
original_signal
: A vector of original intraday volume;
forecast_signal
: A vector of forecast intraday volume;
forecast_components
: A list of the three forecast components: daily, seasonal, intraday dynamic, and residual components.
error
: A list of three error measures: mae, mape, and rmse.
Shengjie Xiu, Yifan Yu and Daniel P. Palomar
Chen, R., Feng, Y., and Palomar, D. (2016). Forecasting intraday trading volume: A Kalman filter approach. Available at SSRN 3101695.
library(intradayModel) data(volume_aapl) volume_aapl_training <- volume_aapl[, 1:20] volume_aapl_testing <- volume_aapl[, 21:50] model_fit <- fit_volume(volume_aapl_training, fixed_pars = list(a_mu = 0.5, var_mu = 0.05), init_pars = list(a_eta = 0.5)) # forecast testing volume forecast_result <- forecast_volume(model_fit, volume_aapl_testing) # forecast testing volume with burn-in forecast_result <- forecast_volume(model_fit, volume_aapl[, 1:50], burn_in_days = 20)
library(intradayModel) data(volume_aapl) volume_aapl_training <- volume_aapl[, 1:20] volume_aapl_testing <- volume_aapl[, 21:50] model_fit <- fit_volume(volume_aapl_training, fixed_pars = list(a_mu = 0.5, var_mu = 0.05), init_pars = list(a_eta = 0.5)) # forecast testing volume forecast_result <- forecast_volume(model_fit, volume_aapl_testing) # forecast testing volume with burn-in forecast_result <- forecast_volume(model_fit, volume_aapl[, 1:50], burn_in_days = 20)
Generate plots for the analysis and forecast results.
generate_plots(analysis_forecast_result)
generate_plots(analysis_forecast_result)
analysis_forecast_result |
Analysis/forecast result from |
A list of patchwork
objects:
components
: Plot of components of intraday volume;
log_components
: Plot of components of intraday volume in their log10 scale;
original_and_smooth
/ original_and_forecast
: Plot of the original and the smooth/forecast intraday volume.
Shengjie Xiu, Yifan Yu and Daniel P. Palomar
library(intradayModel) data(volume_aapl) volume_aapl_training <- volume_aapl[, 1:20] volume_aapl_testing <- volume_aapl[, 21:50] # obtain analysis and forecast result model_fit <- fit_volume(volume_aapl_training, fixed_pars = list(a_mu = 0.5, var_mu = 0.05), init_pars = list(a_eta = 0.5)) analysis_result <- decompose_volume(purpose = "analysis", model_fit, volume_aapl_training) forecast_result <- forecast_volume(model_fit, volume_aapl_testing) # plot the analysis and forecast result generate_plots(analysis_result) generate_plots(forecast_result)
library(intradayModel) data(volume_aapl) volume_aapl_training <- volume_aapl[, 1:20] volume_aapl_testing <- volume_aapl[, 21:50] # obtain analysis and forecast result model_fit <- fit_volume(volume_aapl_training, fixed_pars = list(a_mu = 0.5, var_mu = 0.05), init_pars = list(a_eta = 0.5)) analysis_result <- decompose_volume(purpose = "analysis", model_fit, volume_aapl_training) forecast_result <- forecast_volume(model_fit, volume_aapl_testing) # plot the analysis and forecast result generate_plots(analysis_result) generate_plots(forecast_result)
A 26 * 124 matrix including 15-min trading volume of AAPL from 2019-01-02 to 2019-06-28.
data(volume_aapl)
data(volume_aapl)
A 26 * 124 matrix.
An xts
object including 15-min trading volume of FDX from 2019-07-01 to 2019-12-31.
data(volume_fdx)
data(volume_fdx)
An xts
object.