---
title: "Bayesian Unit Root Testing for Models with Maintained Trend"
author: "Shikhar Tyagi, Arvind Pandey, Bhupendra Singh, Vrijesh Tripathi"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Bayesian Unit Root Testing for Models with Maintained Trend}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(BayesURTrend)
```

# Introduction

The `BayesURTrend` package implements the Bayesian unit root test for time series models with maintained polynomial trend components as proposed by **Chaturvedi and Kumar (2005)**.

Unlike classical Augmented Dickey-Fuller (ADF) or Phillips-Perron (PP) tests which rely on asymptotic approximations, the Bayesian framework calculates exact finite-sample posterior probabilities and Bayes factors for unit root hypotheses against stationary alternatives.

# Mathematical Model

Consider a time series $\{y_t, t = 1, 2, \dots, T\}$ governed by:
$$y_t = \mu_0 + \sum_{i=1}^p \mu_i t^i + u_t$$
where the stochastic error term $u_t$ follows an augmented $\text{AR}(1)$ process:
$$u_t = \rho u_{t-1} + \sum_{j=1}^k \psi_j \Delta y_{t-j} + \epsilon_t, \quad \epsilon_t \sim \text{iid } N(0, \sigma^2)$$

The unit root hypothesis is $H_0: \rho = 1$ versus the stationary alternative $H_1: \rho \in (a, 1)$ where $-1 < a < 1$. Under $H_0$, the polynomial trend degree reduces by 1, yielding a maintained trend in first differences.

# Example Usage

## Testing a Unit Root Process (Random Walk)

```{r random-walk}
set.seed(123)
y_rw <- cumsum(rnorm(100))
res_rw <- bayes_ur_test(y_rw, p = 1, k = 1)
print(res_rw)
summary(res_rw)
plot(res_rw)
```

## Testing a Stationary Process

```{r stationary-ar1}
set.seed(456)
y_stat <- numeric(100)
for (t in 2:100) {
  y_stat[t] <- 0.6 * y_stat[t - 1] + rnorm(1)
}

res_stat <- bayes_ur_test(y_stat, p = 1, k = 1)
print(res_stat)
summary(res_stat)
plot(res_stat)
```

# References

- Chaturvedi, A., & Kumar, J. (2005). Bayesian unit root test for model with maintained trend. *Statistics & Probability Letters*, 74(1), 109--115. <doi:10.1016/j.spl.2005.04.044>
- Schotman, P., & van Dijk, H. K. (1991). A Bayesian analysis of the unit root in real exchange rates. *Journal of Econometrics*, 49(1-2), 195--238. <doi:10.1016/0304-4076(91)90038-F>
