This is an R Markdown document. We use it to run our codes and get results in one document.

#Turning the package on for wavelet MODWT. If you haven't installed yet, then go to Packages --> Click on Install --> Search for "waveslim" --> Ok
library(waveslim)
#Import your data
Data<- read.csv("https://dl.dropboxusercontent.com/u/18255955/Tutorials/Commodities.csv")
#Attach your data so that you can access variables by their names
attach(Data)

Specify variables: Variable A is ISLX, and Variable B is CONX.

#Let's define which variable is A and which is B
A=ISLX
B=CONX
#Define the number of scales
J <- 8
#Choose the type of wavelet filter: la8 or d4
wf <- "la8"

Let’s first test variances, covariance, and correlation using normal functions:

Statistics Result
Variance A 0.00002140393
Variance B 0.00001945059
Covariance 0.00001989321
Correlation 0.974971

Now, let’s run Wavelet to get multi-scale outcomes and compare with these results:

\newpage

Part 1: Wavelet Variance

Getting wavelet variance for variable A in graphic form:

## VARIABLE A Calculate MODWT for variable A
A.modwt <- modwt(A, wf, J)
# Find Brick Wall for variable A
A.modwt.bw <- brick.wall(A.modwt, wf)
# Calculate Variance on Non-Gaussian
WVARA1 <- wave.variance(A.modwt.bw, type = "nongaussian")
# Calculate Variance on Gaussian
WVARA2 <- wave.variance(A.modwt.bw, type = "gaussian")
# Show results for Wavelet Non-Gaussian Variance
WVARA1
# Saving results onto your computer in csv format
write.table(WVARA1, file = "WVARA1_NonGaus.csv", sep = ",")
# Show results for Wavelet Gaussian Variance
WVARA2
# Saving results onto your computer in csv format
write.table(WVARA2, file = "WVARA2_Gaus.csv", sep = ",")
# Delete 'results='hide'' if you want to see the results for wavelet
# covariance for variable A.
## The next code is to create the graphs
par(mfrow=c(1,1), las=0, mar=c(5,4,4,2)+.1);

minWVARA<-min(WVARA2[,1][which(WVARA2[,1] > 0)])
maxWVARA<-max(WVARA1[,3])

matplot(2^(1:J), WVARA2[-(J+1),], type="b", log="xy",xaxt='n',
        ylim=c(minWVARA,maxWVARA), pch="*LU", lty=1, lwd=2, 
        col=c(1), xlab="Wavelet Scale", ylab="Variance");
matlines(2^(1:J), as.matrix(WVARA1[-(J+1),2:3]), 
         type="b", pch="LU", lty=1, col=8)
axis(side=1, at=2^(1:J))
legend("bottomleft",c("Wavelet variance", "Gaussian CI", "Non-Gaussian CI"), lty=1, col=c(1,8,8), bty="n")

title('Wavelet Variance A')

Getting wavelet variance for variable B in graphic form:

## VARIABLE B
# Calculate MODWT for variable B
B.modwt <- modwt(B, wf, J)
# Find Brick Wall for variable B
B.modwt.bw <- brick.wall(B.modwt, wf)
# Calculate Variance on Non-Gaussian
WVARB1<-wave.variance(B.modwt.bw, type="nongaussian")
# Calculate Variance on Gaussian
WVARB2<-wave.variance(B.modwt.bw, type="gaussian")
# Show results for Wavelet Non-Gaussian Variance
#WVARB1
# Save results into csv file
write.table(WVARB1, file="WVARB1_NonGaus.csv",sep=",")
# Show results for Wavelet Gaussian Variance
#WVARB2
# Save results into csv file
write.table(WVARB2, file="WVARB2_NonGaus.csv",sep=",")

#Delete "results='hide'" if you want to see the results for wavelet covariance for variable B.
## The next code is to create the graphs
par(mfrow=c(1,1), las=0, mar=c(5,4,4,2)+.1);

minWVARB<-min(WVARB2[,1][which(WVARB2[,1] > 0)])
maxWVARB<-max(WVARB1[,3])

matplot(2^(1:J), WVARB2[-(J+1),], type="b", log="xy",
        xaxt='n', ylim=c(minWVARB, maxWVARB), pch="*LU", 
        lty=1, lwd=2, col=c(1,4,4), xlab="Wavelet Scale", ylab="Variance");

matlines(2^(1:J), as.matrix(WVARB1[-(J+1),2:3]), 
         type="b", pch="LU", lty=1, col=8)
axis(side=1, at=2^(1:J))

legend("bottomleft",c("Wavelet variance", "Gaussian CI", "Non-Gaussian CI"), lty=1, col=c(1,8,8), bty="n")

title('Wavelet Variance B')

Part 2: Wavelet Covariance

Here we are looking at the covariances between two variables A and B at different scales.

#Calculate Wavelet Covariance
WCOV <- wave.covariance(A.modwt.bw, B.modwt.bw)
# Show results for Wavelet Covariance
WCOV 
# Saving results onto your computer in csv format
write.table(WCOV, file="WCOV.csv",sep=",") 

##Delete "results='hide'" if you want to see the results for wavelet covariance.
## The next code is to create the graphs

#par(mfrow=c(2,1), las=0, pty="m", mar=c(2,3,1,0)+.1)

matplot(2^(1:J), WCOV[-(J+1),], type="b", log="x", 
        pch="*LU", xaxt="n", lty=1, lwd=2, col=c(1,8,8), 
        xlab="Wavelet Scale", ylab="Covariance", main="Wavelet Covariance");

axis(side=1, at=2^(1:J));

Part 3: Wavelet Correlation

The methodology aids in visualizing how correlation among the multivariate financial assets changes with the change in frequency. Here we have correlations between variables A and B on different scales.

#Calculate the total number of observations
N<- length(A)
#Calculate Wavelet Correlation
WCOR<-wave.correlation(A.modwt.bw, B.modwt.bw, N, 0.975)
#Show results for Wavelet Correlation
WCOR 
#Saving results onto your computer in csv format
write.table(WCOR, file="WCOR.csv",sep=",") 
#Delete "results='hide'" if you want to see the results for wavelet correlation.
## The next code is to generate graphs
matplot(2^(1:J), WCOR[-(J+1),], type="b", log="x", 
        pch="*LU", xaxt="n", lty=1, lwd=2, col=c(1,8,8), 
        xlab="Wavelet Scale", ylab="Correlation", main="Wavelet Correlation");
abline(h=0)
axis(side=1, at=2^(1:J));

Part 4: Wavelet Cross-Correlation

Cross-correlation is a measure of similarity of two series as a function of the lag of one relative to the other. The following graphs represent cross-correlations between variables A and B at different scales:

#Define number of lags, e.g. 33 days = 1.5 months
lmax<- 33 

returns.cross.cor<- NULL
for(i in 1:J) {
spin<- spin.correlation(A.modwt.bw[[i]], B.modwt.bw[[i]], lmax)
returns.cross.cor<- cbind(returns.cross.cor, spin)
}

returns.cross.cor<- -ts(as.matrix(returns.cross.cor), start=-lmax, freq=1)

dimnames(returns.cross.cor)<- list(NULL, paste("Level", 1:J))
## The next code is to create the graphs

lags<- length(-lmax:lmax)

lower.ci<-tanh(atanh(returns.cross.cor) - qnorm(0.975)/sqrt(matrix(trunc(N/2^(1:J)),
                            nrow=lags, ncol=J, byrow=TRUE) -3))

upper.ci<- tanh(atanh(returns.cross.cor) + qnorm(0.975) / sqrt(matrix(trunc(N/2^(1:J)),
                            nrow=lags, ncol=J, byrow=TRUE) -3))

par(mfrow=c(4,2), las=1, pty="m", mar=c(2,3,1,0)+.1)

for(i in J:1) {
plot(returns.cross.cor[,i], ylim=c(-1,1), xaxt="n", xlab="Lag", ylab="",
     main=dimnames(returns.cross.cor)[[2]][i], lwd=2)
axis(side=1, at=seq(-lmax,lmax,by=1))
lines(lower.ci[,i], lty=1,col=8)
lines(upper.ci[,i], lty=1, col=8)
abline(h=0, v=0)
}

#Make sure that every level fits into a graph by changing matrix, for example, mfrow=c(3,3) to fit 9 graphs (3x3).

For your reference:

Article 1, Article 2, Article 3.