Skip to content

Commit

Permalink
Small enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
alexiosg committed Nov 10, 2024
1 parent a30c72c commit 7bf48cc
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 9 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: tsgarch
Type: Package
Title: Univariate GARCH Models
Version: 1.0.3
Version: 1.0.4
Authors@R: c(person("Alexios", "Galanos", role = c("aut", "cre","cph"), email = "alexios@4dscape.com", comment = c(ORCID = "0009-0000-9308-0457")))
Maintainer: Alexios Galanos <alexios@4dscape.com>
Depends: R (>= 3.5.0), methods, tsmethods (>= 1.0.2)
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ importFrom(lubridate,tz)
importFrom(lubridate,weeks)
importFrom(lubridate,years)
importFrom(nloptr,nloptr)
importFrom(numDeriv,hessian)
importFrom(numDeriv,jacobian)
importFrom(progressr,handlers)
importFrom(progressr,progressor)
Expand Down
9 changes: 9 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# tsgarch 1.0.4

* Now returning the series name of the data in the spec object for use in
multivariate models.

* If the hessian cannot be calculated in the first step for the scaling, then
a numerical approximation is used instead. There seems to be some instability
in the egarch-nig model for some datasets.

# tsgarch 1.0.3

* Added the log-likelihood vector to the returned fitted and filtered object
Expand Down
5 changes: 4 additions & 1 deletion R/estimate.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,11 @@ solve_model <- function(init_pars, env, const, lower, upper, control) {
optimal_pars <- sol$solution
# check hessian and use for scaling
H <- tmb$he(optimal_pars)
if (any(is.na(H))) {
warning("\nunable to calculate hessian for parameter scaling in initial step. Reverting to numerical estimation.")
H <- hessian(env$fun, x = optimal_pars, env = env)
}
object$parmatrix <- pmatrix

scaled_solution <- .estimate_garch_model_scaled(optimal_pars, H, object, control, stationarity_constraint)
D <- solve(diag(scaled_solution$par_scale, length(scaled_solution$par_scale), length(scaled_solution$par_scale)))
pars <- scaled_solution$solution$solution * scaled_solution$solution$par_scale
Expand Down
35 changes: 35 additions & 0 deletions R/methods.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# estimate ---------------------------------------------------

#' Estimates an GARCH model given a specification object using maximum likelihood and autodiff
#'
#' @param object an object of class \dQuote{tsgarch.spec} or \dQuote{tsgarch.multispec}
Expand Down Expand Up @@ -67,6 +69,8 @@ estimate.tsgarch.spec <- function(object, solver = "nloptr", control = NULL, sta
return(out)
}

# coef ---------------------------------------------------

#' Extract Model Coefficients
#'
#' @description Extract the estimated coefficients of a model.
Expand All @@ -86,6 +90,8 @@ coef.tsgarch.estimate <- function(object, ...)
return(out)
}

# sigma ---------------------------------------------------

#' Extract Volatility (Conditional Standard Deviation)
#'
#' @description Extract the conditional standard deviation from a GARCH model.
Expand Down Expand Up @@ -125,6 +131,8 @@ sigma.tsgarch.multi_estimate <- function(object, ...)
return(out)
}

# fitted ---------------------------------------------------

#' Extract Model Fitted Values
#'
#' @description Extract the fitted values of the estimated model.
Expand Down Expand Up @@ -164,6 +172,7 @@ fitted.tsgarch.multi_estimate <- function(object, ...)
return(out)
}

# residuals ---------------------------------------------------

#' Extract Model Residuals
#'
Expand Down Expand Up @@ -210,6 +219,7 @@ residuals.tsgarch.multi_estimate <- function(object, standardize = FALSE, ...)
return(out)
}

# vcov ---------------------------------------------------

#' The Covariance Matrix of the Estimated Parameters
#'
Expand Down Expand Up @@ -252,6 +262,8 @@ vcov.tsgarch.estimate <- function(object, adjust = FALSE, type = c("H","OP","QML
return(V)
}

# confidence intervals ---------------------------------------------------

#' Confidence Intervals for Model Parameters
#'
#' @param object an object of class tsgarch.estimate.
Expand Down Expand Up @@ -298,6 +310,7 @@ confint.tsgarch.estimate <- function(object, parm, level = 0.95, vcov_type = "H"
return(ci)
}

# log likelihood ---------------------------------------------------

#' Extract Log-Likelihood
#'
Expand All @@ -323,6 +336,7 @@ logLik.tsgarch.estimate <- function(object, ...)
return(out)
}

# summary ---------------------------------------------------

#' GARCH Model Estimation Summary
#'
Expand Down Expand Up @@ -448,6 +462,8 @@ as_flextable.summary.tsgarch.estimate <- function(x, digits = max(3L, getOption(
return(out)
}

# equation ---------------------------------------------------

#' Model Equation (LaTeX)
#'
#' @description Generates a list of model equations in LaTeX.
Expand Down Expand Up @@ -502,6 +518,7 @@ tsequation.tsgarch.estimate <- function(object, ...)
return(out)
}

# persistence ---------------------------------------------------

#' Model Persistence
#'
Expand Down Expand Up @@ -550,6 +567,9 @@ persistence.tsgarch.spec <- function(object, ...)
return(.persistence(pars, env))
}


# unconditional ---------------------------------------------------

#' Unconditional Value
#'
#' @description Unconditional value of a GARCH model variance.
Expand Down Expand Up @@ -603,6 +623,8 @@ unconditional.tsgarch.spec <- function(object, ...)
return(out)
}

# information criteria ---------------------------------------------------

#' Akaike's An Information Criterion
#'
#' @description Extract the AIC from an estimated model.
Expand Down Expand Up @@ -642,6 +664,8 @@ BIC.tsgarch.estimate <- function(object, ...)
}


# nobs ---------------------------------------------------

#' Extract the Number of Observations
#'
#' @description Extract the number of observations from an estimated model.
Expand All @@ -661,6 +685,7 @@ nobs.tsgarch.estimate <- function(object, ...)
return(object$nobs)
}

# news impact ---------------------------------------------------

#' News Impact Curve
#'
Expand Down Expand Up @@ -720,6 +745,7 @@ plot.tsgarch.newsimpact <- function(x, y = NULL, ...)
return(invisible(x))
}

# plots ---------------------------------------------------

#' Estimated Model Plots
#'
Expand Down Expand Up @@ -763,6 +789,7 @@ plot.tsgarch.estimate <- function(x, y = NULL, ...)
return(invisible(x))
}

# filtering ---------------------------------------------------

#' Model Filtering
#'
Expand Down Expand Up @@ -812,6 +839,7 @@ tsfilter.tsgarch.spec <- function(object, y = NULL, newxreg = NULL, newvreg = NU
return(.filter.tsgarch.spec(object, y = y, newxreg = newxreg, newvreg = newvreg))
}

# predict ---------------------------------------------------

#' Model Prediction
#'
Expand Down Expand Up @@ -877,6 +905,7 @@ predict.tsgarch.estimate <- function(object, h = 1, newxreg = NULL, newvreg = NU
return(p)
}

# probability integral transform ---------------------------------------------------

#' Probability Integral Transform (PIT)
#'
Expand Down Expand Up @@ -910,6 +939,7 @@ pit.tsgarch.estimate <- function(object, ...)
return(p)
}

# half life ---------------------------------------------------

#' Half Life
#'
Expand All @@ -934,6 +964,9 @@ halflife.tsgarch.estimate <- function(object, ...)
return(out)
}


# omega ---------------------------------------------------

#' Omega (Variance Equation Intercept)
#'
#' @description Returns the intercept of a GARCH model.
Expand Down Expand Up @@ -978,6 +1011,8 @@ omega.tsgarch.spec <- function(object, ...)
return(target_omega)
}

# simulate ---------------------------------------------------

#' Model Simulation
#'
#' @description Simulates paths of a GARCH model.
Expand Down
3 changes: 0 additions & 3 deletions R/multispec.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,6 @@ to_multi_estimate <- function(object, ...)
invalid_class <- which(!check_class)
stop(paste0("\nnot all objects in list of class `tsgarch.estimate` :", invalid_class))
}
if (is.null(names(object))) {
names(object) <- paste0("series_",1:length(object))
}
anchor_date <- index(fitted(object[[1]]))
check_dates <- sapply(2:length(object), function(i) all.equal(anchor_date, index(fitted(object[[i]]))))
attr(object, "index_match") <- TRUE
Expand Down
5 changes: 5 additions & 0 deletions R/specification.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ garch_modelspec <- function(y, model = "garch", constant = FALSE,
if (!is.xts(y)) {
stop("y must be an xts object")
}
if (NCOL(y) > 1) {
stop("y must be a univariate time series")
}
series_name <- colnames(y)
spec <- initialize_data(y)
spec$target$series_name <- series_name
# 2. validate arguments
model <- match.arg(model[1], choices = valid_garch_models())
distribution <- match.arg(distribution[1], choices = valid_distributions())
Expand Down
2 changes: 1 addition & 1 deletion R/tsgarch-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#' @importFrom zoo na.fill coredata index is.zoo `coredata<-` as.zoo
#' @importFrom xts xts as.xts is.xts merge.xts
#' @importFrom sandwich estfun bwNeweyWest vcovHAC vcovOPG bread
#' @importFrom numDeriv jacobian
#' @importFrom numDeriv jacobian hessian
#' @importFrom nloptr nloptr
#' @importFrom flextable flextable as_flextable set_caption italic fontsize separate_header add_footer_row add_footer_lines append_chunks as_chunk as_equation as_paragraph compose colformat_double set_header_labels padding bold align autofit hline width
#' @importFrom graphics grid layout lines par
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

[![R-CMD-check](https://github.com/tsmodels/tsgarch/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/tsmodels/tsgarch/actions/workflows/R-CMD-check.yaml)
[![Last-changedate](https://img.shields.io/badge/last%20change-2024--09--22-yellowgreen.svg)](/commits/master)
[![packageversion](https://img.shields.io/badge/Package%20version-1.0.3-orange.svg?style=flat-square)](commits/master)
[![R-CMD-check](https://github.com/tsmodels/tsgarch/actions/workflows/rcmdcheck.yaml/badge.svg)](https://github.com/tsmodels/tsgarch/actions/workflows/rcmdcheck.yaml)
[![Last-changedate](https://img.shields.io/badge/last%20change-2024--11--10-yellowgreen.svg)](/commits/master)
[![packageversion](https://img.shields.io/badge/Package%20version-1.0.4-orange.svg?style=flat-square)](commits/master)
[![CRAN_Status_Badge](https://www.r-pkg.org/badges/version/tsgarch)](https://cran.r-project.org/package=tsgarch)

# tsgarch
Expand Down

0 comments on commit 7bf48cc

Please sign in to comment.