Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

matrix multiplication: problem with matrix inverse; suggest to use solve() instead #67

Open
MicheleCai opened this issue Dec 9, 2020 · 7 comments

Comments

@MicheleCai
Copy link

I have some problem with the code :
ms2.garch <- CreateSpec(variance.spec = list(model =c("eGARCH")),
distribution.spec = list(distribution = c("ged")),
switch.spec = list(do.mix=FALSE,K=2))
set.seed(12345)
fit.mcmc=FitMCMC(ms2.garch,data = z)

error: matrix multiplication: problem with matrix inverse; suggest to use solve() instead
Error in f_RCPP_adaptMCMC(theta0 = par0, acc_rate = 0.25, sigma = diag(length(par0)), :
Evaluation error: matrix multiplication: problem with matrix inverse; suggest to use solve() instead.

Can you help me solve the problem?

@MislavSag
Copy link

I have just got the same error. I am using intraday (hour) returns as input. The estimation procedure replicate one in the paper:

n.its <- 2000  # number of insample observations
n.ots <- nrow(market_data) - n.its  # number of outofsample observations
alpha <- 0.05  # alpha in VaR
k.update <- 1000  # how often to reestimate the model
y <- zoo::coredata(market_data$returns)


VaR <- matrix(NA, nrow = n.ots, ncol = length(models))
y.ots <- matrix(NA, nrow = n.ots, ncol = 1)
model.fit <- vector(mode = "list", length = length(models))
for (i in 1:n.ots) {
  cat("Backtest - Iteration: ", i, "\n")
  y.its <- y[i:(n.its + i - 1)]
  y.ots[i] <- y[n.its + i]
  for (j in 1:length(models)) {
    if (k.update == 1 || i %% k.update == 1) {
      cat("Model", j, "is reestimated\n")
      model.fit[[j]] <- FitML(spec = models[[j]], data = y.its,
                              ctr = list(do.se = FALSE))
    }
    VaR[i, j] <- Risk(model.fit[[j]]$spec, par = model.fit[[j]]$par,
                      data = y.its, n.ahead = 1, alpha = alpha, do.es = FALSE,
                      do.its = FALSE)$VaR
  }
}

If I set n.its to 1500, I get the following error:

Error: $ operator is invalid for atomic vectors
In addition: Warning message:
In log((x[i, ] - lower)/(upper - x[i, ])) : NaNs produced

The same code worked with daliy data!

I can send you the data via e-mail. There are cca 25.000 rows.

@MicheleCai
Copy link
Author

No,thank you! I noticed that you used the MLE.The same code worked with MLE in my problem. In addtion,when I changed the "eGARCH" to "sGARCH",the code succeed.

@MislavSag
Copy link

@MicheleCai , I didn't try yo solve your problem. I have just posted the issues that was the same as yours :) I hope someone will answer soon.

@keblu
Copy link
Owner

keblu commented Dec 11, 2020

Typically, most estimation errors arise from issues with the data.

First: is the return data (if this is stock return) scale by 100. There are some parameter bounds that are set in such a way that we assume that stock returns have been scaled by 100.

Second, are there lots of zeros in the return? We have tested a lot with daily returns of highly liquid assets and estimation errors were very rare. A series with a lot of zeros in a row however is problematic, at least from what I have observed.

Third, it is difficult to solve errors like that since it's often case by case. However, I noticed that the EGARCH GED model generates estimation errors more often than the other model. In any case, hard to fix unless you send me the code and data by email such that I can take a closer look. Often the fix is not changing the code, but setting up the data correctly.

@MislavSag
Copy link

@keblu , I use hour returns on SPY as my data.

First: When you say to scale the returns, you mean multiply by 100 (e.g. not 0.01 for 1% but 1)?

Second: there are not a lot of zeroes but, as we talk about hour returns, they can be very close to 0 in short period.

Third: I can send you code and data if you provide an email.

@keblu
Copy link
Owner

keblu commented Dec 14, 2020

@MicheleCai sorry, I cannot answer you by email since you sent it to my old one (only have redirection here and cannot get your full email address). Are you using the latest version of the package? My email should be updated here.

If MLE works, I would suggest perhaps using the MLE parameters as the starting point for the MCMC chain, this might help. I see that you have a negative price from April of this year, which may be an issue as this is a very large outlier (-300 return when scaled). If you remove that observation, there is no error anymore for MCMC.

Also, your time series is rather short (1000 obs), if you can, I would add data points prior to that.

@MislavSag
First: yes
Second: Might not be an issue in that case.
Third: yes, you can at [email protected]

@MislavSag
Copy link

@keblu Can you please confirm that you got the mail?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants