detect.bayes: Bayesian Stopping Rule for Continuous Data

Description Usage Arguments Value Examples

View source: R/bayes.R

Description

Change-point detection for continuous data with unknown post-change distributions using the Bayesian stopping rule.

Usage

1
2
3
4
5
6
detect.bayes(GEN, alpha, nulower = NULL, nuupper = NULL,
  score = "hyvarinen", c = 0.5, n = 1000, lenth, thlower = NULL,
  thupper = NULL, GENTH = NULL, ULPTH = NULL, ULP0 = NULL,
  GLP0 = NULL, LLP0 = NULL, ULP1 = NULL, GLP1 = NULL,
  LLP1 = NULL, par0 = NULL, par1 = NULL, lenx = NULL,
  lower0 = NULL, upper0 = NULL, lower1 = NULL, upper1 = NULL)

Arguments

GEN

A function of time that returns an observation.

alpha

A numeric parameter in (0,1) that controls the probability of false alarm.

nulower, nuupper

Optional nonnegative numerics: The earliest and latest time of change-point based on prior belief. The default is nulower=0 and nuupper=18 which corresponds to the geometric prior distribution with p=0.1.

score

An optional character specifying the type of score to be used: The default "hyvarinen" or the conventional "logarithmic". Can be abbreviated. Case insensitive.

c, n

Optional parameters of the Sequentital Monte Carlo algorithm: ESS threshold 0<c<1 and sample size n>0. Default is c=0.5 and n=1000. The resample-move step is triggered whenever the ESS goes below c*n.

lenth

A positive numeric: The length of the variable of the unknown parameter in the post-change model.

thlower, thupper

Optional numeric vectors of length lenth: The lower and upper limits of the unknown parameter in the post-change model. The defaults are infinite.

GENTH

An optional function that takes a sample size and returns a random sample from the prior distribution of the unknown parameter in the post-change model. Default is standard normal on the unconstrained space. Required if ULPTH is specified.

ULPTH

An optional function: The log unnormalized probability function of the prior distribution of the unknown parameter in the post-change model. Default is standard normal on the unconstrained space. Required if GENTH is specified.

ULP0, GLP0, LLP0

Functions of an observation: The log unnormalized probability function, its gradient and its laplacian for the pre-change model. If score="hyvarinen", either {GLP0,LLP0} or ULP0 is required. The former is recommended. In the latter case, {GLP0,LLP0} will be computed via grad and laplacian. If score="logarithmic", only ULP0 is required.

ULP1, GLP1, LLP1

Functions of an observation and a numeric parameter: The log unnormalized probability function, its gradient and its laplacian for the post-change model. If score="hyvarinen", either {GLP1,LLP1} or ULP1 is required. The former is recommended. In the latter case, {GLP1,LLP1} will be computed via grad and laplacian. If score="logarithmic", only ULP1 is required.

par0, par1

Optional numeric parameters for the pre-change (par0) and post-change (par1) models, except if score="logarithmic" that par1 is a function of the unknown parameter in the post-change model. If score="hyvarinen", the positive tuning parameter with a default of 1. If score="logarithmic", the negative log normalizing constant. If omitted, will be computed via integrate (if lenx=1) or hcubature (if lenx>1).

lenx

A positive numeric: The length of the variable of an obervation. Optional if score="hyvarinen" or if score="logarithmic" and par0,par1 are specified.

lower0, upper0, lower1, upper1

Optional numeric vectors of length lenx: The lower and upper limits of an observation from the pre-change (lower0,upper0) and post-change (lower1,upper1) models. The defaults are infinite.

Value

A named numeric vector with components

  1. t A positive numeric: The stopping time.

  2. LER A numeric in (0,1): The low ESS rate, i.e., the proportion of iterations that ESS drops below c*n.

  3. AAR A numeric in (0,1): The average acceptance rate of the Metropolis-Hastings sampling in the move step. NaN if ESS never drops below c*n.

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
##Change from N(0,1) to 2*N(0,1)+1 at t=15.
##Prior knowledge suggests change occurs between 10 and 25.
##The mean and standard deviation of the post-change normal distribution are unknown.

GEN=function(t){ if(15>=t) rnorm(1) else 2*rnorm(1)+1 }
ULP1=function(x,th) -(x-th[1])^2/2/th[2]^2
GLP1=function(x,th) -(x-th[1])/th[2]^2
LLP1=function(x,th) -1/th[2]^2
ULP0=function(x) ULP1(x,c(0,1))
GLP0=function(x) GLP1(x,c(0,1))
LLP0=function(x) LLP1(x,c(0,1))
par0=log(2*pi)/2
par1=function(th) log(2*pi)/2+log(th[2])

#using hyvarinen score
detect.bayes(GEN=GEN,alpha=0.1,nulower=10,nuupper=25,lenth=2,
thlower=c(-Inf,0),GLP0=GLP0,LLP0=LLP0,GLP1=GLP1,LLP1=LLP1)

#using log score, normalizing constant unknown
detect.bayes(GEN=GEN,alpha=0.1,nulower=10,nuupper=25,lenth=2,
thlower=c(-Inf,0),score="log",ULP0=ULP0,ULP1=ULP1,lenx=1)

#using log score, normalizing constant known
detect.bayes(GEN=GEN,alpha=0.1,nulower=10,nuupper=25,lenth=2,
thlower=c(-Inf,0),score="log",ULP0=ULP0,ULP1=ULP1,par0=par0,par1=par1)

daining0905/chgdetn documentation built on May 25, 2019, 4:01 a.m.