2
$\begingroup$

This is the R-bloggers article in which a t-distribution copula (?) is fitted to explain the dependency between fluctuations in two stocks tickers. I don't understand what they are trying to achieve, but it seems that the idea is to get parameters of a joint distribution that allow generating samples to test different scenarios.

I figured reproducing the example with two similar stocks would be the best way to get a sense for it, but I got an error:

    library('copula')
    library('evir')
    library('quantmod')
    library('VineCopula')
    
    getSymbols("MS", from="2010-01-02", to="2021-01-06", src="yahoo", auto.assign = getOption('loadSymbols.auto.assign',TRUE))
    ms.rtn=diff(log(`MS`))
    ms <- as.vector(-ms.rtn)
    ms <- na.omit(ms)
    
    getSymbols("GOOGL", from="2010-01-02", to="2021-01-06", src="yahoo", auto.assign = getOption('loadSymbols.auto.assign',TRUE))
    google.rtn=diff(log(`GOOGL`))
    google <- as.vector(-google.rtn)
    google <- na.omit(google)
    
    plot(ms, google,pch='.')
    abline(lm(google ~ ms), col='red', lwd=1)
    cor(ms, google, method='spearman')
    
    u <- pobs(as.matrix(cbind(ms,google)))[,1]
    v <- pobs(as.matrix(cbind(ms,google)))[,2]
    selectedCopula <- BiCopSelect(u,v,familyset=NA)
    selectedCopula
    
    t.cop <- tCopula(dim=2)
    set.seed(500)
    m <- pobs(as.matrix(cbind(ms,google)))
    fit <- fitCopula(t.cop, m, method='ml')

I see another question online on the same topic (different site) that went unanswered.

What is fn, and why am I running into problems reproducing this example?

$\endgroup$
6
  • 2
    $\begingroup$ fn is the second argument for optim, which here is logL. $\endgroup$ Commented Jun 17, 2021 at 11:14
  • 1
    $\begingroup$ @AccidentalStatistician Does that mean that under fitCopula() there are values too close to zero, and yield - infinity values when log transformed? Do you address that by placing a floor on the values? $\endgroup$ Commented Jun 17, 2021 at 12:23
  • 1
    $\begingroup$ logL will refer to log-likelihoods, so the copula fitting is running into the data having a very small likelihood. Simple examples where that can happen are there being no data (i.e. cree are google is zero-length), or the data being so large that the likelihood has to be so small that R treats it as zero. I can't say anything more specific without knowing more about how fitCopula works, I'm afraid. $\endgroup$ Commented Jun 17, 2021 at 12:36
  • 1
    $\begingroup$ @AccidentalStatistician > length(GOOGL) [1] 16626 Is that too long? $\endgroup$ Commented Jun 17, 2021 at 12:42
  • 3
    $\begingroup$ The data is typical of a t copula with very low degrees of freedom (near 1), and the optimizer is failing because you haven't set good starting parameters and it's using some default ones. If you set decent ones (e.g. start = c(4, 0.35)), you'll get somewhere. $\endgroup$
    – Chris Haug
    Commented Jun 17, 2021 at 14:04

1 Answer 1

1
$\begingroup$

The error encountered while fitting a t-distribution copula to the stock data in R seems to be related to optimisation issues during the model fitting process. The function fitCopula uses optimisation to find the best parameters that fit the copula model to your data. Let's break down the problem and potential solutions:

The optim function in R is used for general-purpose optimisation and is part of the copula fitting process. The error message involving L-BFGS-B needs finite values of fn suggests that the optimisation function is encountering non-finite (eg Inf or -Inf) values during the optimisation process.

If the likelihood of the data given certain parameter values is extremely small, taking its logarithm can lead to -Inf, which is problematic for the optimiser.

Potential Causes and Solutions: Data Issues: Ensure that your data doesn't contain extreme values or zeros that could cause the likelihood to be extremely small. You might need to preprocess your data to handle any such anomalies.

Starting Values: As mentioned in the comments, providing good starting values for the parameters can help the optimiser avoid areas of the parameter space that lead to problems. You can specify starting values in the fitCopula function using the start argument.

fit <- fitCopula(t.cop, m, method='ml', start=c(4, 0.35))

Optimisation Method: Changing the optimisation method might help. You can specify a different method in fitCopula using the optim.method argument.

Handling Ties: If your data contains ties, using ties.method="random" in the pobs function can help to handle these ties appropriately.

m <- pobs(as.matrix(cbind(cree,google)), ties.method="random")

Model Specification: Check if the t-copula model is appropriate for your data. Sometimes, the choice of copula might not be suitable for the given data characteristics.

Data Length: The length of the data shouldn't be a problem unless it's causing computational issues. However, ensure that the data fed into the copula model is correctly structured and processed.

$\endgroup$
1
  • $\begingroup$ +1 Thank you! It's been a while, and forgot about the background for the question. I see that one of the possible problems is the CREE ticker, so I just updated the OP with another random ticker that happens to generate the same error. I wonder if having eliminated the specific stock data retrieval problem, the solution to the error becomes easier to troubleshoot. I do see a lot of value in you answer as is. $\endgroup$ Commented Jan 3 at 22:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.