---
title: "Scalar mean inference"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Scalar mean inference}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

This vignette uses a **synthetic** series.  It shows the scalar case of
affine-equivariant adjusted-range self-normalization, where the construction
reduces exactly to adjusted-range self-normalization: the scaled estimation
error is divided by the adjusted range (maximum minus minimum) of the
centered partial-sum path.

## Setup

The series is a stationary first-order autoregression with mean 0.3 and
autoregressive coefficient 0.5.  The parameter of interest is the mean.

```{r}
library(aersn)
set.seed(2026)
n <- 300
y <- 0.3 + as.numeric(arima.sim(list(ar = 0.5), n))
fit <- aersn_mean(y)
fit
```

The influence contribution of observation *t* for the sample mean is
$Y_t - \bar Y_n$.  The centered path
$\hat G_n(k/n) = n^{-1/2}\sum_{t \le k}(Y_t - \bar Y_n)$ starts and ends at
zero:

```{r}
plot(fit$path)
```

## Test of a point null

For $H_0: \theta_0 = 0$ the statistic is
$T_n(0) = |\sqrt n(\bar Y_n - 0)| / \{\max_k \hat G_n(k/n) - \min_k \hat G_n(k/n)\}$.
Its limit is $|M| = |Z| / R$, where $Z$ is standard normal and $R$ is the
range of an independent standard Brownian bridge.  Two reference laws are
available:

* the closed-form continuous-path law (`reference = "continuous"`), whose
  five-percent critical value is 1.7058; and
* the matched-grid law, simulated for a Brownian bridge on the same grid of
  `n` intervals as the sample path.  Matched-grid quantiles are larger
  because a path observed on a grid has a smaller range than the continuous
  path.  The manuscript uses matched-grid quantiles as critical values.

```{r}
aersn_test(fit, null = 0, reference = "continuous")
aersn_test(fit, null = 0, draws = 20000, seed = 1)
```

The matched-grid p-value is a Monte Carlo estimate; its standard error and
resolution (one over the number of draws) are reported.  A one-sided
alternative uses the signed ratio:

```{r}
aersn_test(fit, null = 0, alternative = "greater", reference = "continuous")
```

## Confidence interval

The confidence interval is $\bar Y_n \pm (c/\sqrt n)\,\mathcal R(\hat G_n)$,
where $c$ is the reference quantile.

```{r}
confint(fit, level = 0.95, draws = 20000, seed = 1)
confint(fit, level = 0.95, reference = "continuous")
```

The interval endpoints are exactly the boundary of the set of null values
that the test does not reject:

```{r}
ci <- confint(fit, draws = 20000, seed = 1)
aersn_gauge(fit, ci[1, "upper"])      # equals the critical value
attr(ci, "critical.value")
```

## What is and is not guaranteed

The test is asymptotically valid when the partial sums of $Y_t - \theta_0$
satisfy a functional central limit theorem with positive long-run variance.
No bandwidth, kernel, or block length is chosen.  Under strong persistence
the finite-sample null rejection rate exceeds the nominal level; the
manuscript's simulations report, for example, 14.4 percent at
autoregressive coefficient 0.9 with `n = 500` and `q = 2`.
