---
title: "Introduction"
author: "Zhongjun Qu"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Introduction}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 7,
  fig.height = 5
)
```

## Introduction

A scalar process is said to have **long memory** if its spectral density at frequency $λ$ is proportional to $λ^{-2d}$ $(d ≠ 0)$ as $\lambda \rightarrow 0$ . Long memory processes are characterized by slowly decaying autocorrelations and unbounded spectral densities at the origin. A prominent example is the $ARFIMA(p,d,q)$ model

$$
A(L)(1-L)^d x_t = B(L)\epsilon_t
$$

### The Problem of Spurious Long Memory

However, apparent long memory can arise from two fundamentally different sources:

1.  **Genuine long memory**: True fractional integration where shocks have persistent, hyperbolically decaying effects
2.  **Spurious long memory**: Short-memory processes contaminated by structural features such as:
    -   Occasional level shifts or regime changes
    -   Smooth deterministic trends

Distinguishing between these cases is crucial

### The Test Methodology

This package implements the test proposed by Qu (2011), which exploits the fact that genuine and spurious long memory exhibit different spectral properties across frequency bands.

The test statistic is based on the derivatives of the local Whittle likelihood function and is of the Kolmogorov-Smirnov type. It does not require specifying:

-   The number of structural breaks under the alternative
-   The locations of breaks
-   The functional form of trends

## Installation

```{r eval=FALSE}
# From CRAN (once available)
install.packages("SpuriousMemory")
```

Load the package:

```{r}
library(SpuriousMemory)
```

## The RV5min Dataset

The package includes realized volatility data for Japanese Yen/US dollar spot exchange rates from Dec 1, 1986 to Jun 30, 1999 (`RV5min`).

```{r}
data(RV5min)
n <- length(RV5min)
cat("Sample size:", n, "\n")
cat("Mean:", round(mean(RV5min), 4), "\n")
cat("Std. dev:", round(sd(RV5min), 4), "\n")
```

Visualize the series:

```{r fig.width=7, fig.height=4}
plot(RV5min, type = "l", 
     main = "Realized Volatility: Japanese Yen/US Dollar",
     xlab = "Time (days)", ylab = "Log RV", 
     col = "steelblue", lwd = 0.8)
grid()
```

The persistent fluctuations and high autocorrelations suggest possible long memory, but we need to test whether this is genuine or spurious.

## Conducting the Test

### Basic Usage

The `Longmemorytest()` function performs the hypothesis test:

**H₀**: x_t is a stationary long-memory process\
**H₁**: x_t is affected by level shifts or smooth trends (spurious long memory)

```{r}
result <- Longmemorytest(log(RV5min), demean = TRUE, alpha = 0.05)
```

### Understanding the Output

The test provides two test statistics with different trimming parameters $\epsilon = 0.02, 0.05$

**Interpretation:**

-   **Reject H₀** : Evidence of **spurious** long memory
    -   Long memory likely induced by level shifts, breaks, or trends
-   **Fail to reject H₀** : Evidence of **genuine** long memory
    -   Consistent with fractional integration
    -   ARFIMA models are appropriate

### Technical Details

The test uses:

-   **Bandwidth**: $m = n^{0.7}$
-   **Pre-whitening**: Automatic filtering using AIC-selected $ARFIMA(p,d,q)$ with $p,q ∈ {0,1}$
-   **Critical values**: Simulated from asymptotic distribution

For the RV5min data, both tests fail to reject at the 5% level, suggesting the long memory is genuine.

### Accessing Results

Extract detailed results programmatically:

```{r}
# Local Whittle estimates
result$d_estimate

# Test statistics
result$test_stat_eps02  # W statistic (ε = 0.02)
result$test_stat_eps05  # W statistic (ε = 0.05)

# Critical values
result$critical_value_eps02
result$critical_value_eps05

# Rejection decisions
result$reject_eps02
result$reject_eps05
```

## Simulated Examples

We examine two scenarios to illustrate the test's behavior under different DGP.

### Example 1: Genuine Long Memory (ARFIMA)

Generate an $ARFIMA(0, 0.4, 0)$ process:

```{r message=FALSE}
library(fracdiff)
set.seed(123)

# Simulate ARFIMA(0, d, 0) with d = 0.4
x_genuine <- fracdiff.sim(n = 1000, d = 0.4)$series

# Visualize
oldpar <- par(mfrow = c(1, 2))
plot(x_genuine, type = "l", main = "ARFIMA(0, 0.4, 0) Series", 
     ylab = "Value", col = "darkgreen", lwd = 0.8)
grid()

# ACF shows hyperbolic decay
acf(x_genuine, main = "ACF: Genuine Long Memory", 
    col = "darkgreen", lwd = 2)

par(oldpar)
```

Apply the test:

```{r}
result_genuine <- Longmemorytest(x_genuine, demean = TRUE, alpha = 0.05, 
                                  print_results = TRUE)
```

**Result**: Both tests fail to reject $H₀$, correctly identifying genuine long memory.

### Example 2: Level Shift (Spurious)

Generate white noise with an abrupt level shift:

```{r}
set.seed(456)
n <- 1000
shift_point <- 500

# White noise with level shift
x_spurious <- rnorm(n)
x_spurious[shift_point:n] <- x_spurious[shift_point:n] + 3

# Visualize
oldpar <- par(mfrow = c(1, 2))
plot(x_spurious, type = "l", main = "White Noise + Level Shift",
     ylab = "Value", col = "darkred", lwd = 0.8)
abline(v = shift_point, lty = 2, col = "blue", lwd = 2)
grid()

# ACF shows spurious persistence
acf(x_spurious, main = "ACF: Spurious Long Memory",
    col = "darkred", lwd = 2)

par(oldpar)
```

Apply the test:

```{r}
result_spurious <- Longmemorytest(x_spurious, demean = TRUE, alpha = 0.05)
```

**Result**: Both tests reject $H₀$, correctly detecting spurious long memory caused by the level shift.

## Significance Levels

The test supports $α ∈ \{0.01, 0.025, 0.05, 0.10\}$. Critical values are tabulated from simulated asymptotic distributions:

```{r echo=FALSE}
cv_table <- data.frame(
  Alpha = c("10%", "5%", "2.5%", "1%"),
  `ε = 0.02` = c(1.118, 1.252, 1.374, 1.517),
  `ε = 0.05` = c(1.022, 1.155, 1.277, 1.426),
  check.names = FALSE
)
knitr::kable(cv_table, align = "c", 
             caption = "Asymptotic Critical Values")
```

## References

**Primary Reference:**

Qu, Z. (2011). A Test Against Spurious Long Memory. *Journal of Business & Economic Statistics*, 29(3), 423-438. DOI: [10.1198/jbes.2010.09153](https://doi.org/10.1198/jbes.2010.09153)

## Session Info

```{r}
sessionInfo()
```
