2

I am pretty new to R and I have been trying to compute a rolling Hurst exponent with no avail. I have installed packages fArma (for the Hurst) and zoo (for the rollapply). The data is in a dataframe called 'data' and is variable 'returns'. The following code for the Hurst works great;

rsFit(data$returns, levels = 50, minnpts = 3, cut.off = 10^c(0.7, 2.5), + doplot = FALSE, trace = FALSE, title = NULL, description = NULL)

Below is my attempt at a rolling Hurst exponent of window size 230, which generates an error.

rollapply(data$returns, 230, (rsFit(data$returns, levels = 50, minnpts = 3, cut.off = 10^c(0.7, 2.5), + doplot = FALSE, trace = FALSE, title = NULL, description = NULL)))

Any help with the code would be much appreciated. I am trying to calculate the Hurst exponent over a 230 period window, that rolls forward 1 period at a time.

The data is;

    returns 
1   -0.002002003    
2   -0.002006019    
3   0.000000000 
4   0.000000000 
5   -0.009077218    
6   -0.003044142    
7   -0.002034589    
8   0.004065046 
9   0.002026343 
10  0.001011634 
11  0.001010612 
12  0.000000000     
13  -0.001010612    
14  -0.001011634    
15  0.003031837 
16  -0.001009591    
17  0.001009591 
18  -0.002020203        
aur
  • 33
  • 4
  • Please provide a sample of your data so that [your example is reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – nrussell Nov 07 '14 at 14:30
  • I have had another go at it and saved the RS Hurst from fArma as a function called 'HURSTRS'. I have then applied; rollinghurst <- rollapply(x, 230, HURSTRS, by=230, align="right") But I still get errors. Any suggestions? Thanks – aur Nov 11 '14 at 11:51
  • @aur I have provided a lot of matlab codes here: http://prorum.com/index.php/2173/calcular-expoente-dependencia-dependence-series-temporais – DanielTheRocketMan Feb 09 '16 at 15:40

1 Answers1

3

I'm really not familiar with the fArma package or its functions, but I noticed a couple of major issues with your code.

  1. You are trying to use rollapply incorrectly; specifically your third argument FUN=rsFun(data$returns). In general, if you are applying a (one-parameter) function foo on a data object x with rollapply, your function call should be rollapply(x,some_integer,foo).

So in your case, you would have

rollapply(data$returns,230,rsFit)

since it is acceptable to call rsFit with only one argument (the first one, x, as shown in the help file ?rsFit).

  1. The width of 230 that you are specifying in rollapply is much too large - your sample data, data$returns, only has a length of 18 - the window size has to be less than the length of the data. One option is to use a smaller width: I tried a couple of small values (5,10,...) with your data, but this produced errors. Like I said, I'm not familiar with the functions in fArma, but I suspect rsFit requires more than 5 or 10 observations at a time. A better solution would be to use a larger sample of data, which is shown below.
  2. Even having made the changes described above, you will encounter one more issue. From the Value section (i.e. return value) in ?rollapply:

    "A object of the same class as data with the results of the rolling function."

    Typically this is some type of simple object, e.g. a vector, matrix, etc... depending on your input. However, rsFit returns an S4 class fHURST object, which rollapply is apparently not able to deal with. This is not surprising, since fHURST objects have a fairly complex structure - try running str(rsFit(data$returns)) and note all of the various slots it contains. Basically, the simple solution for this is instead of returning the entire fHURST object calculated in rollapply, just return the component / slot that you need. Again, I've never used rsFit and I don't have to time to read into the theoretical underpinnings of Hurst exponents, but below I assumed you were mainly concerned with the estimated coefficient values occupying the @hurst slot of the fHURST objects.

As noted above, I made a toy data set that is much larger than 18 observations so that I could keep the width=230 in rollapply.

library(fArma)
library(zoo)
##
set.seed(123)
data2 <- rnorm(690)
##
data2.ra <- rollapply(data2,230,function(x){
  hSlot <- rsFit(x)@hurst
  result <- data.frame(
    H = hSlot$H,
    beta = hSlot$beta,
    Estimate.intercept = hSlot$diag[1,1],
    Estimate.X = hSlot$diag[2,1])
  result
})
##
> head(data2.ra)
          H      beta Estimate.intercept Estimate.X
1 0.6257476 0.6257476       -0.143363281  0.6257476
2 0.6627804 0.6627804       -0.193806373  0.6627804
3 0.6235309 0.6235309       -0.133828565  0.6235309
4 0.5683417 0.5683417       -0.055960572  0.5683417
5 0.5520769 0.5520769       -0.027270395  0.5520769
6 0.5334170 0.5334170       -0.003523383  0.5334170
> dim(data2.ra)
[1] 461   4
> 690 - (230-1)
[1] 461

Which is an object of length 461, since the output of using rollapply with a window size k on an object of length n is n - (k-1). Of course, you can change the body of the anonymous function (function(x){...}) used in rollapply above to suit your needs.

nrussell
  • 18,382
  • 4
  • 47
  • 60