# to make simulations reproducible
set.seed(0)
# Code from Section 3 on "Stationarity Test"
#
# Load the costat library
library("costat")
#
# Load data sets that get used for the examples
data("sret", "fret")
#
#
# Plot the time series
#
ts.plot(sret)
lines(fret, col=2)
#
# Perform bootstrap test of stationarity on sret series
#
sret.TOS <- BootTOS(sret)
#
# Print out useful information about results of bootstrap test
#
sret.TOS
#
# Let's now do the bootstrap test on a iid Gaussian sequence (where we
# know the answers) then plot and print the answers.
#
x <- rnorm(512)
x.TOS <- BootTOS(x)
plot(x.TOS)
x.TOS
#
# Code from Section 4 on "Local autocovariance"
#
# Simulate a stationary AR(1) model with parameter 0.8.
# Then compute and print the regular ACF computed on the realization
vsim <- arima.sim(model=list(ar=0.8), n=1024)
vsim.acf <- acf(vsim, plot=FALSE)
vsim.acf
#
# Now compute the localized autocovariance (up to a maximum of 100 lags,
# using a wavelet filter of 4 [this latter choice should not matter too
# much]. Then print out the localized acf object.
#
vsim.lacv <- lacv(vsim, filter.number=4, lag.max=100)
vsim.lacv
#
# Now plot the localized autocovariance (albeit for a realization from a
# stationary process). Draw a horizontal line at y=0.8 to give guidance
# as to where the 0.8 value is (the theoretical acf at lag 1).
#
plot(vsim.lacv, lags=0:7, lcol=1:8)
abline(h=0.8, lty=2)
#
# Apply BootTOS to vsim.
BootTOS(vsim)
#
# Now plot the localized acf at time=512 in the style of the classicial
# acf plot, and superimpose the theoretical values of the acf
#
plot(vsim.lacv,type="acf", the.time=512)
for(i in 1:20) text(i, 0.8^i, "*")
#
# Commands from Section 4.2 on a simulated locally stationary process
#
# Create a function that can simulate a time-varying AR process.
#
tvar1sim <- function (sd = 1)
{
n <- 512
arvec <- seq(from = 0.9, to = -0.9, length = 512)
x <- c(rnorm(1, mean = 0, sd = sd), rep(0, n - 1))
for (i in 2:n) x[i] <- arvec[i] * x[i - 1] + rnorm(1, mean = 0,
sd = sd)
return(x)
}
#
# Now simulate a realization from this TVAR process and plot it.
#
tvar1.sim <- tvar1sim()
ts.plot(tvar1.sim)
#
# Now compute and plot the time-localized autocovariance
#
tvar1.lacv <- lacv(tvar1.sim, filter.number=4, lag.max=50)
plot(tvar1.lacv, lags=0:2, lcol=1:3)
#
# Commands from Section 4.3 on a time series thought to be locally stationary
#
# Compute localized autocovariance on the sret log-returns series and plot
#
sret.lacv <- lacv(sret, filter.number=4, lag.max=50)
plot(sret.lacv, lags=0:5, lcol=1:6)
#
# Remove certain outliers
#
sret.noout <- sret
sret.noout[abs(sret) > 0.04] <- 0
#
# Recompute the localized acf on the series without the outliers
#
sret.noout.lacv <- lacv(sret.noout, filter.number=4, lag.max=50)
plot(sret.noout.lacv, lags=0:5, lcol=1:6)
#
# Commands from Section 5: Discovering Costationarity
#
#
# Find costationary solutions between sret and fret series (only 10 sims)
#
sretfret.fss <- findstysols(Nsims=10, tsx=sret, tsy=fret)
#
# Look at the convergence codes
#
sretfret.fss$convergence
#
# Plot dendrogram associated with solution set
#
plot(sretfret.fss, ALLplotscale=FALSE)
#
# Plot information about solution 6
#
def.par <- par(no.readonly = TRUE) # For resetting
nf <- layout(matrix(1:4, 2,2, byrow=TRUE))
plot(sretfret.fss, solno=6)
par(def.par) # Resets plot to single plot, if required