File: pca.test

package info (click to toggle)
tcllib 1.20%2Bdfsg-1
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 68,064 kB
  • sloc: tcl: 216,842; ansic: 14,250; sh: 2,846; xml: 1,766; yacc: 1,145; pascal: 881; makefile: 107; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (288 lines) | stat: -rw-r--r-- 8,410 bytes parent folder | download | duplicates (4)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# -*- tcl -*-

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 pca.tcl math::PCA
}

#package require math::statistics

#
# Matching procedure - flatten the lists
#
proc matchNumbers {expected actual} {
    if {$actual=="" && $expected!=""} then {
        return 0
    }
    if {$actual!="" && $expected==""} then {
        return 0
    }
    set match 1
    if { [llength [lindex $expected 0]] > 1 } {
        foreach a $actual e $expected {
            set match [matchNumbers $e $a]
            if { $match == 0 } {
                break
            }
        }
    } else {

        foreach a $actual e $expected {
            if {[string is double $a]==0  || [string is double $e]==0} then {
                return 0
            }
            if {abs($a-$e) > 0.1e-6} {
                set match 0
                break
            }
        }
    }
    return $match
}

customMatch numbers matchNumbers

# Test the normalise/denormalise procedures

test normalise-1.0 "Normalise a vector" -match numbers -body {
    ::math::PCA::Normalise {1.0 2.0 3.0} {0.0 1.0 2.0} {2.0 2.0 2.0}
} -result {0.5 0.5 0.5}

test normalise-1.1 "Denormalise a vector" -match numbers -body {
    ::math::PCA::Denormalise {0.5 0.5 0.5} {0.0 1.0 2.0} {2.0 2.0 2.0}
} -result {1.0 2.0 3.0}

set plusminus   {{ 3.0 -3.0 -3.0  3.0}
                 {-3.0  3.0  3.0 -3.0}
                 { 0.0  0.0  0.0  0.0}
                 {-3.0  3.0 -3.0  3.0}
                 { 3.0 -3.0  3.0 -3.0}}

set transformed {{ 1.0 -1.0 -1.0  1.0}
                 {-1.0  1.0  1.0 -1.0}
                 { 0.0  0.0  0.0  0.0}
                 {-1.0  1.0 -1.0  1.0}
                 { 1.0 -1.0  1.0 -1.0}}

set zeroes      {0.0 0.0 0.0 0.0}
set threes      {3.0 3.0 3.0 3.0}

set data        {{7 4 3}
                 {4 1 8}
                 {6 3 5}
                 {8 6 1}
                 {8 5 7}
                 {7 2 9}
                 {5 3 3}
                 {9 5 8}
                 {7 4 5}
                 {8 2 2}}
set ones        {1.0 1.0 1.0}

test normalise-1.2 "Check transformation - transformed matrix" -match numbers -body {
    lindex [::math::PCA::Transform $plusminus 1] 0
} -result $transformed

test normalise-1.3 "Check transformation - averages" -match numbers -body {
    lindex [::math::PCA::Transform $plusminus 1] 1
} -result $zeroes

test normalise-1.4 "Check transformation - scale values" -match numbers -body {
    lindex [::math::PCA::Transform $plusminus 1] 2
} -result $threes

test normalise-1.5 "Check transformation - unit standard deviations" -match numbers -body {
    set transformedData  [lindex [::math::PCA::Transform $data 1] 0]
    set transposedMatrix [::math::linearalgebra::transpose $transformedData]

    set stdevs {}
    foreach vector $transposedMatrix {
        lappend stdevs [::math::statistics::stdev $vector]
    }
    set stdevs
} -result $ones

test normalise-1.6 "Check transformation - retain lengths" -match numbers -body {
    lindex [::math::PCA::Transform $data 0] 2
} -result $ones

test create-1.1 "Create a PCA object correctly" -match glob -setup {
    set pca {}
} -body {
    set pca [::math::PCA::createPCA $data]
} -cleanup {
    if { $pca ne "" } {
        $pca destroy
    }
} -returnCodes 0 -result *

test create-1.2 "Create a PCA object correctly - 2" -match glob -setup {
    set pca {}
} -body {
    set pca [::math::PCA::createPCA $data -covariance 0]
} -cleanup {
    if { $pca ne "" } {
        $pca destroy
    }
} -returnCodes 0 -result *

test create-1.3 "Create a PCA object incorrectly" -match glob -setup {
    set pca {}
} -body {
    ::math::PCA::createPCA $zeroes
} -cleanup {
    if { $pca ne "" } {
        $pca destroy
    }
} -returnCodes 1 -result *

test eigenvectors-1.1 "Get the eigenvectors - all" -match numbers -setup {
    set pca [::math::PCA::createPCA $data]
} -body {
    set eigenvectors [$pca eigenvectors]
} -cleanup {
    if { $pca ne "" } {
        $pca destroy
    }
} -result {
    {0.6420045763498947 -0.38467229168844363 -0.6632174243435957}
    {0.6863616413605773 -0.0971303301343052 0.7207450285897333}
    {-0.3416691692479824 -0.9179286066874492 0.20166618906061484}}

test eigenvectors-1.2 "Get the eigenvalues - all" -match numbers -setup {
    set pca [::math::PCA::createPCA $data]
} -body {
    set eigenvalues [$pca eigenvalues]
} -cleanup {
    if { $pca ne "" } {
        $pca destroy
    }
} -result {1.768774136103425 0.9270759168988945 0.30414994699768233}

test eigenvectors-1.3 "Restrict to two components - eigenvectors" -match numbers -setup {
    set pca [::math::PCA::createPCA $data]
} -body {
    $pca using 2
    set eigenvectors [$pca eigenvectors]
} -cleanup {
    if { $pca ne "" } {
        $pca destroy
    }
} -result {
    {0.6420045763498947 -0.38467229168844363}
    {0.6863616413605773 -0.0971303301343052}
    {-0.3416691692479824 -0.9179286066874492}}

test eigenvectors-1.4 "Restrict to two components - eigenvalues" -match numbers -setup {
    set pca [::math::PCA::createPCA $data]
} -body {
    $pca using 2
    set eigenvalues [$pca eigenvalues]
} -cleanup {
    if { $pca ne "" } {
        $pca destroy
    }
} -result {1.768774136103425 0.9270759168988945}

test eigenvectors-1.5 "Restrict to two components - get all eigenvectors" -match numbers -setup {
    set pca [::math::PCA::createPCA $data]
} -body {
    global pca
    $pca using 2
    set eigenvectors [$pca eigenvectors -all]
} -cleanup {
    if { $pca ne "" } {
        $pca destroy
    }
} -result {
    {0.6420045763498947 -0.38467229168844363 -0.6632174243435957}
    {0.6863616413605773 -0.0971303301343052 0.7207450285897333}
    {-0.3416691692479824 -0.9179286066874492 0.20166618906061484}}

test eigenvectors-1.6 "Restrict to two components - get all eigenvalues" -match numbers -setup {
    set pca [::math::PCA::createPCA $data]
} -body {
    $pca using 2
    set eigenvalues [$pca eigenvalues -all]
} -cleanup {
    if { $pca ne "" } {
        $pca destroy
    }
} -result {1.768774136103425 0.9270759168988945 0.30414994699768233}

test eigenvectors-1.7 "Use all components again" -match numbers -setup {
    set pca [::math::PCA::createPCA $data]
} -body {
    $pca using 3
    set eigenvectors [$pca eigenvectors]
} -cleanup {
    if { $pca ne "" } {
        $pca destroy
    }
} -result {
    {0.6420045763498947 -0.38467229168844363 -0.6632174243435957}
    {0.6863616413605773 -0.0971303301343052 0.7207450285897333}
    {-0.3416691692479824 -0.9179286066874492 0.20166618906061484}}

test approximation-1.1 "Approximate an observation" -match numbers -setup {
    set pca [::math::PCA::createPCA $data]
} -body {
    $pca using 2
    set approximation [$pca approximate [lindex $::data 0]]
} -cleanup {
    if { $pca ne "" } {
        $pca destroy
    }
} -result {7.03386897006181 3.961810336299144 2.981031668611465}

test approximation-1.2 "Approximate an observation, get the scores" -match numbers -setup {
    set pca [::math::PCA::createPCA $data]
} -body {
    $pca using 2
    set scores [$pca scores [lindex $::data 0]]
} -result {0.5148128141212557 0.630835559461938}

test approximation-1.3 "Approximate an observation, get the Q statistic" -match numbers -setup {
    set pca [::math::PCA::createPCA $data]
} -body {
    $pca using 2
    set qstatistic [$pca qstatistic [lindex $::data 0]]
} -cleanup {
    if { $pca ne "" } {
        $pca destroy
    }
} -result 0.001123022217614874

test approximation-1.4 "Approximate an observation, get the corrected Q statistic" -match numbers -setup {
    set pca [::math::PCA::createPCA $data]
} -body {
    $pca using 2
    set qstatistic [$pca qstatistic [lindex $::data 0] -original]
} -cleanup {
    if { $pca ne "" } {
        $pca destroy
    }
} -result 0.0016043174537355342

test approximation-2.1 "Approximate all original data (actually reconstruct)" -match numbers -setup {
    set pca [::math::PCA::createPCA $data]
} -body {
    # Note: as we use all components, we reconstruct the original data "exactly"
    $pca using 3
    set approximation [$pca approximateOriginal]
} -cleanup {
    if { $pca ne "" } {
        $pca destroy
    }
} -result $data