Skip to content

Commit

Permalink
Updated cpt.var tests to account for CROPS now outputting the upper t…
Browse files Browse the repository at this point in the history
…esting bound
  • Loading branch information
rkillick committed Nov 3, 2024
1 parent 86773e4 commit 4846aff
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
Binary file modified tests/testthat/Rplots.pdf
Binary file not shown.
35 changes: 21 additions & 14 deletions tests/testthat/test-examples.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ context("man file example tests")
set.seed(1)
x=c(rnorm(100,0,1),rnorm(100,0,10))
ansvar=cpt.var(x)
test_that('var1',expect_identical(cpts(ansvar),100))
test_that('var1',expect_equal(cpts(ansvar),100))

# change in mean
set.seed(1)
y=c(rnorm(100,0,1),rnorm(100,5,1))
ansmean=cpt.mean(y)
test_that('mean1',expect_identical(cpts(ansmean),100))
test_that('mean1',expect_equal(cpts(ansmean),100))

# change in mean and variance
set.seed(1)
z=c(rnorm(100,0,1),rnorm(100,2,10))
ansmeanvar=cpt.meanvar(z)
test_that('meanvar1',expect_identical(cpts(ansmeanvar),100))
test_that('meanvar1',expect_equal(cpts(ansmeanvar),100))



Expand All @@ -31,14 +31,14 @@ set.seed(1)
x=c(rnorm(100,0,1),rnorm(100,10,1))
test_that('mean2',expect_equivalent(cpt.mean(x,penalty="SIC",method="AMOC",class=FALSE),c(100,1)))
ans=cpt.mean(x,penalty="Asymptotic",pen.value=0.01,method="AMOC")
test_that('mean3',expect_identical(cpts(ans),100))
test_that('mean3',expect_equal(cpts(ans),100))
ans=cpt.mean(x,penalty="Manual",pen.value=0.8,method="AMOC",test.stat="CUSUM")
test_that('mean4',expect_equivalent(cpts(ans),101))

# Example of multiple changes in mean at 50,100,150 in simulated normal data
set.seed(1)
x=c(rnorm(50,0,1),rnorm(50,5,1),rnorm(50,10,1),rnorm(50,3,1))
test_that('mean5',expect_identical(cpt.mean(x,penalty="Manual",pen.value="2*log(n)",method="BinSeg",Q=5,class=FALSE),c(50,100,150,200)))
test_that('mean5',expect_equal(cpt.mean(x,penalty="Manual",pen.value="2*log(n)",method="BinSeg",Q=5,class=FALSE),c(50,100,150,200)))

# Example of using the CROPS penalty in data set above
set.seed(1)
Expand All @@ -48,7 +48,7 @@ truth=matrix(NA,ncol=7,nrow=7); truth[1:6,1]=50;truth[1:5,2]=c(96,96,100,100,150
truth[1:4,3]=c(100,100,133,150);truth[1:3,4]=c(133,133,150);truth[1:2,5]=c(150,150)
truth[1,6]=159;truth[1,7]=180
test_that('crops1',expect_equivalent(cpts.full(out),truth))
truth=c(4.000000, 4.332496, 4.385247, 4.684254 ,559.366988, 646.962719,1311.335695)
truth=c(4.000000, 4.332496, 4.385247, 4.684254 ,559.366988, 646.962719,1311.335695,1500)
test_that('crops2',expect_equal(pen.value.full(out),truth,tolerance=1e-6))

# Example multiple datasets where the first row has multiple changes in mean and the second row has
Expand All @@ -57,7 +57,8 @@ set.seed(1)
x=c(rnorm(50,0,1),rnorm(50,5,1),rnorm(50,10,1),rnorm(50,3,1))
y=rnorm(200,0,1)
z=rbind(x,y)
test_that('mean6',expect_equal(cpt.mean(z,penalty="Asymptotic",pen.value=0.01,method="SegNeigh",Q=5,class=FALSE),list(c(50,100,150,200),200)))
ans=cpt.mean(z,penalty="Asymptotic",pen.value=0.01,method="SegNeigh",Q=5,class=FALSE)
test_that('mean6',expect_equal(ans,list(c(50,100,150,200),200)))
ans=cpt.mean(z,penalty="Asymptotic",pen.value=0.01,method="PELT")
test_that('mean7',expect_equal(cpts(ans[[1]]),c(50,100,150)))
test_that('mean8',expect_equal(cpts(ans[[2]]),numeric()))
Expand Down Expand Up @@ -89,7 +90,7 @@ truth=matrix(NA,ncol=9,nrow=6);truth[1:5,1]=c(rep(15,2),rep(50,3));truth[1:4,2]=
truth[1:4,3]=c(22,22,133,150);truth[1:3,4]=c(44,50,151);truth[1:2,5]=c(46,100)
truth[1:2,6]=c(50,133);truth[1:2,7]=c(100,151);truth[1,8]=133;truth[1,9]=151
test_that('crops3',expect_equal(cpts.full(out),truth))
truth=c(10.59663, 10.68431, 11.31088, 11.38307, 119.78669, 191.42622)
truth=c(10.59663, 10.68431, 11.31088, 11.38307, 119.78669, 191.42622,529.83174)
test_that('crops4',expect_equal(pen.value.full(out),truth,tolerance=1e-6))

# Example multiple datasets where the first row has multiple changes in mean and variance and the
Expand All @@ -98,7 +99,8 @@ set.seed(1)
x=c(rnorm(50,0,1),rnorm(50,5,3),rnorm(50,10,1),rnorm(50,3,10))
y=rnorm(200,0,1)
z=rbind(x,y)
test_that('meanvar5',expect_equivalent(cpt.meanvar(z,penalty="Asymptotic",pen.value=0.01,method="SegNeigh",Q=5,class=FALSE),list(c(50,100,150,200),200)))
ans=cpt.meanvar(z,penalty="Asymptotic",pen.value=0.01,method="SegNeigh",Q=5,class=FALSE)
test_that('meanvar5',expect_equivalent(ans,list(c(50,100,150,200),200)))
ans=cpt.meanvar(z,penalty="Asymptotic",pen.value=0.01,method="PELT")
test_that('meanvar6',expect_equivalent(cpts(ans[[1]]),c(50,100,150)))
test_that('meanvar7',expect_equivalent(cpts(ans[[2]]),numeric()))
Expand All @@ -120,7 +122,8 @@ test_that('class2',expect_equivalent(cpts(x),c(10,50,100)))
set.seed(1)
x=c(rnorm(50,0,1),rnorm(50,0,10),rnorm(50,0,5),rnorm(50,0,1))
out=cpt.var(x,pen.value=c(log(length(x)),10*log(length(x))),penalty="CROPS",method="PELT")
test_that('class3',expect_equivalent(logLik(out,ncpts=3),c(925.8085, 947.0578))) # raw likelihood of the data with changepoints, second value is likelihood + penalty
ll=logLik(out,ncpts=3)
test_that('class3',expect_equivalent(ll,c(925.8085, 947.0578))) # raw likelihood of the data with changepoints, second value is likelihood + penalty



Expand Down Expand Up @@ -166,7 +169,7 @@ out=cpt.var(x,pen.value=c(log(length(x)),100*log(length(x))),penalty="CROPS",met
truth=matrix(NA,ncol=7,nrow=7);truth[1:6,1]=50;truth[1:5,2]=c(77,rep(99,3),150);truth[1:4,3]=c(79,114,140,150)
truth[1:3,4]=c(99,133,150);truth[1:2,5]=c(114,150);truth[1,6]=133;truth[1,7]=150
test_that('var5',expect_equivalent(cpts.full(out),truth))
truth=c(5.298317,5.548538,6.149305,7.083099,26.592259,142.417161,145.146279)
truth=c(5.298317,5.548538,6.149305,7.083099,26.592259,142.417161,145.146279,529.831737)
test_that('var6',expect_equivalent(pen.value.full(out),truth))

# Example multiple datasets where the first row has multiple changes in variance and the second row
Expand All @@ -176,7 +179,8 @@ x=c(rnorm(50,0,1),rnorm(50,0,10),rnorm(50,0,5),rnorm(50,0,1))
y=rnorm(200,0,1)
z=rbind(x,y)
truth=list();truth[[1]]=c(50,100,149,200);truth[[2]]=200
test_that('var7',expect_equivalent(cpt.var(z,penalty="Asymptotic",pen.value=0.01,method="SegNeigh",Q=5,class=FALSE),truth))
ans=cpt.var(z,penalty="Asymptotic",pen.value=0.01,method="SegNeigh",Q=5,class=FALSE)
test_that('var7',expect_equivalent(ans,truth))
ans=cpt.var(z,penalty="Asymptotic",pen.value=0.01,method="PELT")
test_that('var8',expect_equivalent(cpts(ans[[1]]),c(50,100,149)))
test_that('var9',expect_equivalent(cpts(ans[[2]]),numeric()))
Expand Down Expand Up @@ -303,7 +307,8 @@ test_that('class23',expect_equivalent(distribution(x),"normal"))
set.seed(1)
x=c(rnorm(50,0,1),rnorm(50,0,10),rnorm(50,0,5),rnorm(50,0,1))
out=cpt.var(x,penalty="Manual",pen.value="2*log(n)",method="BinSeg",Q=5)
test_that('logLik1',expect_equivalent(likelihood(out),c(925.8085, 957.5984)))
ll=likelihood(out)
test_that('logLik1',expect_equivalent(ll,c(925.8085, 957.5984)))



Expand All @@ -313,7 +318,8 @@ test_that('logLik1',expect_equivalent(likelihood(out),c(925.8085, 957.5984)))
set.seed(1)
x=c(rnorm(50,0,1),rnorm(50,0,10),rnorm(50,0,5),rnorm(50,0,1))
out=cpt.var(x,penalty="Manual",pen.value="2*log(n)",method="BinSeg",Q=5)
test_that('logLik1',expect_equivalent(logLik(out),c(925.8085, 957.5984)))
ll=logLik(out)
test_that('logLik1',expect_equivalent(ll,c(925.8085, 957.5984)))



Expand Down Expand Up @@ -485,3 +491,4 @@ test_that('class42',expect_equivalent(test.stat(x),character()))
x=new("cpt") # new cpt object
test.stat(x)<-"normal" # replaces the current test.stat slot of x with "normal"
test_that('class43',expect_equivalent(test.stat(x),"normal"))

0 comments on commit 4846aff

Please sign in to comment.