# SPRT.default: Wald's Sequential Probability Ratio Test In SPRT: Wald's Sequential Probability Ratio Test

## Description

Perform Wald's Sequential Probability Ratio Test on variables with a Normal, Bernoulli, Exponential or Poisson distribution. Returns an object with support for `print` and `plot` methods.

## Usage

 ```1 2 3``` ```## Default S3 method: SPRT(distribution = "bernoulli", type1 = 0.05, type2 = 0.2, h0, h1, values = NULL, n = NULL, k = NULL) ```

## Arguments

 `distribution` a character string specifying the distribution. Must be one of `"bernoulli"` (default), `"normal"`, `"exponential"` or `"poisson"`. `type1` the type I error. A number between 0 and 1. `type2` the type II error. A number between 0 and 1. `h0` the expected value of the random variable under the null hypothesis. `h1` the expected value of the random variable under the alternative hypothesis. `values` an optional vector containing values of the random variable. A logical vector when distribution is `"bernoulli"` and a numerical vector otherwise. `n` an optional numerical scalar for the number of observations of the random variable. `n` is optional and can be used as an alternative to `values`. `k` an optional numerical scalar for the cumulative sum of the random variable. `k` is optional and can be used as an alternative to `values`.

## Details

Perform Wald's Sequential Probability Test on a simple hypothesis test of the null against the alternative.

The null hypothesis tested is that the expected value of the random variable is equal to `h0`. The alternative hypothesis tested is that the expected value of the random variable is equal to `h1`.

The expected value of the variable is the probability of success of a Bernoulli variable, the mean of a Normal variable, the mean (as well as the variance) of a Poisson variable, and the mean (as well as the standard deviation and scale parameter) of an Exponential distribution.

Optionally, specify `values`, a vector with observations of the random variable in the order in which they occurred. `values` is a logical vector of TRUE or FALSE observations in the case of a Bernoulli variable, and a numerical vector otherwise.

Or specify `n` and `k` as an alternative to `values`. `n` is the number of observations, and `k` is the cumulative sum of the random variable across observations (or the number of successes in the case of a Bernoulli variable). When `values` is given, SPRT infers `n` and `k`, and removes any `NA` values in the process.

## Value

A list with class `"SPRT"` containing the following components:

 `distribution` equal to `distribution`. `n` equal to `n` if given, or the length of the `values` vector otherwise. `k` equal to `k` if given, or the cumulative sum of the `values` vector otherwise. `h0` equal to `h0`. `h1` equal to `h1`. `wald.A` the natural logarithm of Wald's A boundary (see `waldBoundary`). `wald.B` the natural logarithm of Wald's B boundary (see `waldBoundary`). `k.boundaries` a numerical 2x2 matrix with the slope and intercept of H0 and H1 acceptance regions under the null and alternative hypotheses. `llr.coefficients` a numerical vector of the `n` and `k` coefficients behind the random variable's log-likelihood ratio. `llr` a numerical scalar of the random variable's log-likelihood ratio. `decision` the outcome of the Sequential Probability Ratio Test. Returns `FALSE` when `llr` >= `wald.A`, `TRUE` when `llr` <= `wald.B` and `NA` otherwise. `interpretation` a character vector interpreting the outcome of the Sequential Probability Ratio Test. Returns `"Accept H1"` when `decision` is `FALSE`, `"Accept H0"` when `decision` is `TRUE` and `"Continue testing"` otherwise. `data.llr` a data frame comparing the random variable's log-likelihood ratio against the natural logarithm of Wald's A and B boundaries. The data frame's columns are named `n`, `values`, `k`, `wald.B`, `wald.A` and `llr`. It contains a row for every observation of the random variable. This output is only available when you specify `values`. `data.sum` a data frame comparing the random variable's cumulative sum, `k`, against acceptance boundaries for the null and alternative hypotheses. The data frame's columns are named `n`, `values`, `k`, `h0` and `h1`. It contains a row for every observation of the random variable. This output is only available when you specify `values`. `llr.fn` a function that returns the value of the random variable's log-likelihood ratio for different `n` and `k`. This function is a closure; it encapsulates your `distribution`, `type1`, `type2`, `h0` and `h1` settings. `h0.fn` a function that returns the k acceptance boundary for the null hypothesis for different `n`. This function is a closure; it encapsulates your `distribution`, `type1`, `type2`, `h0` and `h1` settings. `h1.fn` a function that returns the k acceptance boundary for the alternative hypothesis for different `n`. This function is a closure; it encapsulates your `distribution`, `type1`, `type2`, `h0` and `h1` settings.

## Note

This function returns an object with support for `print` and `plot` methods.

## Author(s)

Stephane Mikael Bottine

## References

Ghosh, B.K. and Sen, P.K. (1991). Handbook of Sequential Analysis, Marcel Dekker, New York. Wald, A. (1947). Sequential Analysis, Dover, New York.

`plot.SPRT`, `print.SPRT`, `waldBoundary`

## Examples

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26``` ```# SPRT on a normally distributed random variable set.seed(123) test <- SPRT(distribution = "normal", type1 = 0.05, type2 = 0.20, h0 = 0, h1 = 1, values = rnorm(10)) # Test outcome test # Cumulative sum of the random variable vs H0 and H1 boundaries test\$data.sum plot(test) # Sequential log-likelihood ratio vs Wald's A and B constants test\$data.llr plot(test, log = "y") # Calculate the log-likelihood ratio across scalars or vectors set.seed(123) test\$llr.fn(n = 10, sum(rnorm(10))) set.seed(123) test\$llr.fn(n = seq(1,10,1), k = cumsum(rnorm(10))) # Calculate H0 and H1 boundaries test\$h0.fn(n = seq(1,10,1)) test\$h1.fn(n = seq(1,10,1)) ```

### Example output

```Wald's Sequential Probability Ratio Test (SPRT)

Decision:
Accept H0

Distribution: normal
n: 10, k: 0.7462564
h0: 0, h1: 1

Wald boundaries (log):
> B boundary:       -1.558
> A boundary:        2.773
> Likelihood ratio:  -4.254

Preview k boundaries:
n  values      k      h0   h1
1 -0.5605 -0.560 -1.0581 3.27
2 -0.2302 -0.791 -0.5581 3.77
3  1.5587  0.768 -0.0581 4.27
4  0.0705  0.839  0.4419 4.77
5  0.1293  0.968  0.9419 5.27
n      values          k          h0       h1
1   1 -0.56047565 -0.5604756 -1.05814462 3.272589
2   2 -0.23017749 -0.7906531 -0.55814462 3.772589
3   3  1.55870831  0.7680552 -0.05814462 4.272589
4   4  0.07050839  0.8385636  0.44185538 4.772589
5   5  0.12928774  0.9678513  0.94185538 5.272589
6   6  1.71506499  2.6829163  1.44185538 5.772589
7   7  0.46091621  3.1438325  1.94185538 6.272589
8   8 -1.26506123  1.8787713  2.44185538 6.772589
9   9 -0.68685285  1.1919184  2.94185538 7.272589
10 10 -0.44566197  0.7462564  3.44185538 7.772589
n      values          k    wald.B   wald.A        llr
1   1 -0.56047565 -0.5604756 -1.558145 2.772589 -1.0604756
2   2 -0.23017749 -0.7906531 -1.558145 2.772589 -1.7906531
3   3  1.55870831  0.7680552 -1.558145 2.772589 -0.7319448
4   4  0.07050839  0.8385636 -1.558145 2.772589 -1.1614364
5   5  0.12928774  0.9678513 -1.558145 2.772589 -1.5321487
6   6  1.71506499  2.6829163 -1.558145 2.772589 -0.3170837
7   7  0.46091621  3.1438325 -1.558145 2.772589 -0.3561675
8   8 -1.26506123  1.8787713 -1.558145 2.772589 -2.1212287
9   9 -0.68685285  1.1919184 -1.558145 2.772589 -3.3080816
10 10 -0.44566197  0.7462564 -1.558145 2.772589 -4.2537436
[1] -4.253744
[1] -1.0604756 -1.7906531 -0.7319448 -1.1614364 -1.5321487 -0.3170837
[7] -0.3561675 -2.1212287 -3.3080816 -4.2537436
[1] -1.05814462 -0.55814462 -0.05814462  0.44185538  0.94185538  1.44185538
[7]  1.94185538  2.44185538  2.94185538  3.44185538
[1] 3.272589 3.772589 4.272589 4.772589 5.272589 5.772589 6.272589 6.772589
[9] 7.272589 7.772589
```

SPRT documentation built on May 2, 2019, 6:35 a.m.