# Function to return the periodogram of data series
myperiodogram <- function (data, max.freq = 0.5,
twosided = FALSE, keep = 1){
data.fft <- fft(data)
N <- length(data)
tmp <- Mod(data.fft[2:floor(N/2)])^2/N
freq <- ((1:(floor(N/2) - 1))/N)
tmp <- tmp[1:floor(length(tmp) * keep)]
freq <- freq[1:floor(length(freq) * keep)]
if (twosided) {
tmp <- c(rev(tmp), tmp)
freq <- c(-rev(freq), freq)
}
return(list(spec = tmp, freq = freq))
}
# Function to compute the standard error based the periodogram of
# the influence functions time series
SE.Gamma <- function(data, d = 7, alpha = 0.5, keep = 1){
N <- length(data)
# Compute the periodograms
my.periodogram <- myperiodogram(data)
my.freq <- my.periodogram$freq
my.periodogram <- my.periodogram$spec
# Remove values of frequency 0 as it does not contain information
# about the variance
my.freq <- my.freq[-1]
my.periodogram <- my.periodogram[-1]
# Implement cut-off
nfreq <- length(my.freq)
my.freq <- my.freq[1:floor(nfreq*keep)]
my.periodogram <- my.periodogram[1:floor(nfreq*keep)]
# GLM with BFGS optimization
# Create 1, x, x^2, ..., x^d
x.mat <- rep(1,length(my.freq))
for(col.iter in 1:d){
x.mat <- cbind(x.mat,my.freq^col.iter)
}
# Fit the Exponential or Gamma model
res <- fit.glmGammaNet(x.mat, my.periodogram, alpha.EN = alpha)
# Return the estimated variance
return(sqrt(exp(res[1])/N))
}
# Loading hedge fund data from PA
data(edhec, package = "PerformanceAnalytics")
colnames(edhec)
# Computing the expected shortfall for the time series of returns
# library(RPEIF)
# test.mat <- apply(edhec, 2, IF.ES)
# test.mat <- apply(test.mat, 2, as.numeric)
# Returning the standard errors from the Gamma distribution fit
# apply(test.mat, 2, SE.Gamma)
Run the code above in your browser using DataLab