File: changepoint.test

package info (click to toggle)
tcllib 2.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 83,560 kB
  • sloc: tcl: 306,798; ansic: 14,272; sh: 3,035; xml: 1,766; yacc: 1,157; pascal: 881; makefile: 124; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (105 lines) | stat: -rw-r--r-- 2,320 bytes parent folder | download | duplicates (3)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# -*- tcl -*-
# Tests for changepoint library.
#
# RCS: @(#) $Id: geometry.test,v 1.13 2010/04/06 17:02:25 andreas_kupries Exp $

# -------------------------------------------------------------------------

source [file join \
        [file dirname [file dirname [file join [pwd] [info script]]]] \
        devtools testutilities.tcl]

testsNeedTcl     8.6
testsNeedTcltest 1.0

support {
    useLocal math.tcl math
    useLocal linalg.tcl math::linearalgebra
    useLocal statistics.tcl math::statistics
}
testing {
    useLocal changepoint.tcl math::changepoint
}

# setRand --
#     Make sure we always get the same results:
#     Use srand() to ensure that the random numbers follow
#     the same sequence.
#
proc setRand {} {
    expr {srand(100000)}
}

# test data NIST --
#     From https://www.itl.nist.gov/div898/handbook/pmc/section3/pmc323.htm
#
set testdata {
    324.93
    324.68
    324.73
    324.35
    325.35
    325.23
    324.13
    324.53
    325.23
    324.60
    324.63
    325.15
    328.33
    327.25
    327.83
    328.50
    326.68
    327.78
    326.88
    328.35
}

# test data for binary sgmentation
#
setRand
set series       [concat [lrepeat 20 0.0] [lrepeat 20 1.0]]
set noise        [::math::statistics::random-normal 0.0 1.5 40]
set binsegSeries [lrepeat 40 0.0]

for {set n 0} {$n < 40} {incr n} {
    lset binsegSeries $n [expr {[lindex $series $n] + [lindex $noise $n]}]
}

# actual tests
#
test cusum-1.0 {examine entire data set} -body {
    global testdata

    set location [::math::changepoint::cusum-detect $testdata -target 325 -tolerance 0.635]
} -result 12

test cusum-1.1 {examine the data set one value at a time} -body {
    global testdata

    set cpd [::math::changepoint::cusum-online new -target 325 -tolerance 0.635]

    set loc 0
    foreach value $testdata {
        if { [$cpd examine $value] } {
            break
        }

        incr loc
    }
    set location $loc
} -result 12


test binseg-1.0 {binary segmentation for a series with clear shift} -body {
    global binsegSeries

    ::math::changepoint::binary-segmentation $series
} -result 19

test binseg-1.1 {binary segmentation for a series with clear shift - longer minimum length} -body {
    global binsegSeries

    ::math::changepoint::binary-segmentation $series -minlength 10
} -result 19