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 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
|
# -*- tcl -*-
# statistics.test --
# Test cases for the ::math::statistics package
#
# Note:
# The tests assume tcltest 2.1, in order to compare
# floating-point results
# -------------------------------------------------------------------------
source [file join \
[file dirname [file dirname [file join [pwd] [info script]]]] \
devtools testutilities.tcl]
testsNeedTcl 8.5;# statistics,linalg!
testsNeedTcltest 2.1
support {
useLocal math.tcl math
useLocal linalg.tcl math::linearalgebra
useLocal optimize.tcl math::optimize
}
testing {
useLocal statistics.tcl math::statistics
}
# -------------------------------------------------------------------------
set ::data_uniform [list 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0]
set ::data_missing [list 1.0 1.0 1.0 {} 1.0 {} {} 1.0 1.0 1.0 1.0 1.0 1.0]
set ::data_linear [list 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0]
set ::data_empty [list {} {} {}]
set ::data_missing2 [list 1.0 2.0 3.0 {} 4.0 5.0 6.0 7.0 8.0 9.0 10.0]
#
# Create and register (in that order!) custom matching procedures
#
proc matchTolerant { expected actual } {
set match 1
foreach a $actual e $expected {
if { abs($e-$a)>0.0001*abs($e) &&
abs($e-$a)>0.0001*abs($a) } {
set match 0
break
}
}
return $match
}
proc matchTolerant2 { expected actual } {
set match 1
foreach a $actual e $expected {
if { abs($e-$a)>0.025*abs($e) &&
abs($e-$a)>0.025*abs($a) } {
set match 0
break
}
}
return $match
}
proc matchAlmostZero { expected actual } {
set match 1
foreach a $actual {
if { abs($a)>1.0e-6 } {
set match 0
break
}
}
return $match
}
customMatch tolerant matchTolerant
customMatch tolerant2 matchTolerant2
customMatch almostzero matchAlmostZero
#
# Test cases
#
test "BasicStats-1.0" "Basic statistics - uniform data" -match tolerant -body {
set all_data [::math::statistics::BasicStats all $::data_uniform]
} -result [list 1.0 1.0 1.0 [llength $::data_uniform] 0.0 0.0 0.0 0.0]
test "BasicStats-1.1" "Basic statistics - empty data" -match glob -body {
catch {
set all_data [::math::statistics::BasicStats all $::data_empty]
} msg
set msg
} -result "Too*"
#
# Result must be the same as for 1.0! Hence ::data_empty and ::data_uniform
#
test "BasicStats-1.2" "Basic statistics - missing data" -match tolerant -body {
set all_data [::math::statistics::BasicStats all $::data_missing]
} -result [list 1.0 1.0 1.0 [llength $::data_uniform] 0.0 0.0 0.0 0.0]
test "BasicStats-1.3" "Basic statistics - linear data - mean" -match tolerant -body {
set value [::math::statistics::mean $::data_linear]
} -result 5.5
test "BasicStats-1.4" "Basic statistics - linear data - min" -match tolerant -body {
set value [::math::statistics::min $::data_linear]
} -result 1.0
test "BasicStats-1.5" "Basic statistics - linear data - max" -match tolerant -body {
set value [::math::statistics::max $::data_linear]
} -result 10.0
test "BasicStats-1.6" "Basic statistics - linear data - number" -match tolerant -body {
set value [::math::statistics::number $::data_linear]
} -result 10
test "BasicStats-1.7" "Basic statistics - missing data - number" -match tolerant -body {
set value [::math::statistics::number $::data_missing2]
} -result 10
test "BasicStats-1.8" "Basic statistics - missing data - stdev" -match almostzero -body {
set value1 [::math::statistics::stdev $::data_linear]
set value2 [::math::statistics::stdev $::data_missing2]
expr {abs($value1-$value2)}
} -result 0.001 ;# Zero is impossible
test "BasicStats-1.9" "Basic statistics - missing data - var" -match almostzero -body {
set value1 [::math::statistics::stdev $::data_linear]
set value2 [::math::statistics::var $::data_missing2]
expr {$value1*$value1-$value2}
} -result 0.001 ;# Zero is impossible
test "BasicStats-1.10" "Basic statistics - missing data - pstdev" -match almostzero -body {
set value1 [::math::statistics::pstdev $::data_linear]
set value2 [::math::statistics::pstdev $::data_missing2]
expr {abs($value1-$value2)}
} -result 0.001 ;# Zero is impossible
test "BasicStats-1.11" "Basic statistics - missing data - pvar" -match almostzero -body {
set value1 [::math::statistics::pstdev $::data_linear]
set value2 [::math::statistics::pvar $::data_missing2]
expr {$value1*$value1-$value2}
} -result 0.001 ;# Zero is impossible
#
# This test was added because the calculation of the standard deviation
# could fail with uniform data (the difference of two almost equal
# values became a small negative number)
#
# Further extension: more stable computation if the values are very
# close together. Due to this change the variance should be independent
# of the mean, however large (up to a point)
#
test "BasicStats-2.1" "Basic statistics - uniform data caused sqrt domain error" -body {
set values [list]
set count 0
for { set i 0 } { $i < 20 } { incr i } {
lappend values 0.6
set value2 [::math::statistics::mean $values]
incr count
}
set count
} -result 20 ;# We can finish the loop
test "BasicStats-2.2" "Basic statistics - large almost identical values" -match glob -body {
catch {
set data [list 100001 100002 100003 100004]
set result_large [::math::statistics::BasicStats all $data]
set data [list 1 2 3 4]
set result_small [::math::statistics::BasicStats all $data]
matchTolerant [lrange $result_small 3 end] [lrange $result_large 3 end]
} msg
set msg
} -result 1
#
# Histograms
#
test "Histogram-1.0" "Histogram - uniform data" -match glob -body {
set values [::math::statistics::histogram {0 2} $::data_uniform]
} -result [list 0 [llength $::data_uniform] 0]
test "Histogram-1.1" "Histogram - missing data" -match glob -body {
set values [::math::statistics::histogram {0 2} $::data_missing]
} -result [list 0 [::math::statistics::number $::data_missing] 0]
test "Histogram-1.2" "Histogram - linear data" -match glob -body {
set values [::math::statistics::histogram {1.5 4.5 9.5} $::data_linear]
} -result {1 3 5 1}
test "Histogram-1.3" "Histogram - linear data 2" -match glob -body {
set values [::math::statistics::histogram {1.5 2.5 10.5} $::data_linear]
} -result {1 1 8 0}
#
# Adding two dummy values should not influence the histogram (ticket 05d055c2f5)
#
test "Histogram-1.4" "Histogram - linear data 2 with weights" -match glob -body {
set values [::math::statistics::histogram {1.5 2.5 10.5} [concat $::data_linear 0.0 0.0] \
[concat [lrepeat [llength $::data_linear] 1] 0 0]]
} -result {1 1 8 0}
test "Histogram-1.5" "Histogram - linear data 2 with weights" -match glob -body {
set values [::math::statistics::histogram {1.5 2.5} [concat $::data_linear 0.0 0.0] \
[concat [lrepeat [llength $::data_linear] 1] 0 0]]
} -result {1 1 8}
#
# Alternative definition of the intervals (ticket 1502400fff)
# Note the difference in the expected bin sizes for the two
#
test "Histogram-2.1" "Histogram - alternative interval bounds" -match glob -body {
set values [concat [::math::statistics::histogram-alt {5.0 7.0} $::data_linear] \
[::math::statistics::histogram {5.0 7.0} $::data_linear]]
} -result {4 2 4 5 2 3}
#
# Quantiles
# Bug #1272910: related to rounding 0.5 - use different levels instead
# because another bug was fixed, return to the original
# levels again
#
test "Quantiles-1.0" "Quantiles - raw data" -match tolerant -body {
set values [::math::statistics::quantiles $::data_linear {0.25 0.55 0.95}]
} -result {3.0 6.0 10.0}
test "Quantiles-1.1" "Quantiles - histogram" -match tolerant -body {
set limits {1.0 2.0 3.0 4.0}
set data_hist {0 10 20 10 0}
set values [::math::statistics::quantiles $limits $data_hist {0.25 0.5 0.9}]
} -result {2.0 2.5 3.6}
#
# Generate histogram limits
#
test "Limits-1.0" "Limits - based on mean/stdev" -match tolerant -body {
set values [::math::statistics::mean-histogram-limits 1.0 1.0 4]
} -result {0.0 0.75 1.25 2.0}
test "Limits-1.1" "Limits - based on mean/stdev" -match tolerant -body {
set values [::math::statistics::mean-histogram-limits 1.0 1.0 9]
} -result {-2.0 -1.0 0.0 0.75 1.0 1.25 2.0 3.0 4.0}
test "Limits-1.2" "Limits - based on mean/stdev" -match tolerant -body {
set values [::math::statistics::mean-histogram-limits 0.0 1.0 11]
} -result {-3.0 -2.4 -1.8 -1.2 -0.6 0.0 0.6 1.2 1.8 2.4 3.0}
test "Limits-2.0" "Limits - based on min/max" -match tolerant -body {
set values [::math::statistics::minmax-histogram-limits -2.0 2.0 9]
} -result {-2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0}
test "Limits-2.1" "Limits - based on min/max" -match tolerant -body {
set values [::math::statistics::minmax-histogram-limits -2.0 2.0 2]
} -result {-2.0 2.0}
#
# To do: design test cases for the following functions:
# - t-test-mean
# - estimate-mean-stdev
# - autocorr
# - crosscorr
# - linear-model
# - linear-residuals
# - pdf-*
# - cdf-*
# - random-*
# - histogram-*
#
# Crude test cases for Student's t test
#
test "Students-t-test-1.0" "Student's t - same sample" -match glob -body {
set sample [::math::statistics::random-normal 0.0 1.0 40]
set mean 0.0
set stdev 1.0
set confidence 0.95
set result [::math::statistics::t-test-mean $sample $mean $stdev $confidence]
} -result 1
test "Students-t-test-1.1" "Student's t - different sample" -match glob -body {
set sample [::math::statistics::random-normal 0.0 1.0 40]
set mean 10.0
set stdev 1.0
set confidence 0.95
set result [::math::statistics::t-test-mean $sample $mean $stdev $confidence]
} -result 0
test "Students-t-test-1.2" "Student's t - small sample" -match glob -body {
set sample [::math::statistics::random-normal 0.0 1.0 2]
set mean 2.0
set stdev 1.0
set confidence 0.90
set result [::math::statistics::t-test-mean $sample $mean $stdev $confidence]
} -result 1
#
# Test private procedures
#
test "Cdf-toms322-1.0" "TOMS322 - erf(x)" -match tolerant2 -body {
set result {}
foreach z {4.417 3.891 3.291 2.576 2.241 1.960 1.645 1.150 0.674
0.319 0.126 0.063 0.0125} {
set prob [::math::statistics::Cdf-toms322 1 5000 [expr {$z*$z}]]
lappend result [expr {1.0-$prob}]
}
set result
} -result {1.e-5 1.e-4 1.e-3 1.e-2 0.025 0.050 0.100 0.250 0.500
0.750 0.900 0.950 0.990 }
test "Cdf-toms322-2.0" "TOMS322 - inverse erf(x)" -match tolerant2 -body {
set result {}
foreach p {0.5120 0.5948 0.7019 0.7996 0.8997 0.9505 0.9901 0.9980 } {
set z [::math::statistics::Inverse-cdf-normal 0.0 1.0 $p]
lappend result $z
}
set result
} -result {0.03 0.24 0.53 0.84 1.28 1.65 2.33 2.88 }
#
# Correlation coefficients
#
test "Correlation-1.0" "Correlation - linear data" -match tolerant -body {
set corr [::math::statistics::corr $::data_linear $::data_linear]
} -result 1.0
test "Correlation-1.1" "Correlation - linear/uniform" -match almostzero -body {
set corr [::math::statistics::corr $::data_linear $::data_uniform]
} -result 0.0
#
# Test list procedures
#
proc matchListElements { expected actual } {
if { [llength $expected] != [llength $actual] } {
return 0
} else {
set match 1
foreach a $actual e $expected {
if { $a != $e } {
set match 0
break
}
}
}
return $match
}
customMatch matchList matchListElements
set ::data_list {1 2 3 4 5 6 7 8 9 10}
set ::data_pairs {{1 2} {3 4} {5 6} {7 8} {9 10}}
test "Filter-1.0" "True filter" -match matchList -body {
set data [::math::statistics::filter x $::data_list 1]
} -result $::data_list
test "Filter-1.1" "False filter" -match matchList -body {
set data [::math::statistics::filter x $::data_list 0]
} -result {}
test "Filter-1.2" "Even filter" -match matchList -body {
set data [::math::statistics::filter x $::data_list {$x%2==0}]
} -result {2 4 6 8 10}
test "Filter-2.1" "filter with parameter" -match matchList -body {
set param 3.0
set data [::math::statistics::filter x $::data_list {$x > $param}]
} -result {4 5 6 7 8 9 10}
test "Map-1.0" "Identity map" -match matchList -body {
set data [::math::statistics::map x $::data_list {$x}]
} -result $::data_list
test "Map-1.1" "Is-even map" -match matchList -body {
set data [::math::statistics::map x $::data_list {$x%2==0}]
} -result {0 1 0 1 0 1 0 1 0 1}
test "Map-1.2" "Double map" -match matchList -body {
set data [::math::statistics::map x $::data_list {$x*2}]
} -result {2 4 6 8 10 12 14 16 18 20}
test "Map-2.1" "map with parameter" -match matchList -body {
set param 3.0
set data [::math::statistics::map x $::data_list {$x + $param}]
} -result {4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0}
test "Samplescount-1.0" "Single sublist" -match matchList -body {
set data [::math::statistics::samplescount x [list $::data_list]]
} -result {10}
test "Samplescount-1.1" "List of singleton sublist" -match matchList -body {
set data [::math::statistics::samplescount x $::data_list]
} -result {1 1 1 1 1 1 1 1 1 1}
test "Samplescount-1.2" "Pairs sublist" -match matchList -body {
set data [::math::statistics::samplescount x $::data_pairs]
} -result {2 2 2 2 2}
test "Samplescount-1.3" "Select uneven sublist" -match matchList -body {
set data [::math::statistics::samplescount x $::data_pairs {$x%2}]
} -result {1 1 1 1 1}
test "Samplescount-2.1" "Count with parameter" -match matchList -body {
set param 3.0
set data [::math::statistics::samplescount x $::data_pairs {$x>$param}]
} -result {0 1 2 2 2}
test "Median-1.1" "Median - odd number of data" -body {
set data {1.0 3.0 2.0}
set median [::math::statistics::median $data]
} -result 2.0
test "Median-1.2" "Median - even number of data" -body {
set data {1.0 3.0 2.0 1.0}
set median [::math::statistics::median $data]
} -result 1.5
test "Median-1.3" "Median - missing data" -body {
set data {1.0 {} 3.0 2.0 1.0 {}}
set median [::math::statistics::median $data]
} -result 1.5
test "test-2x2-1.0" "Test 2x2" -match tolerant -body {
set data [::math::statistics::test-2x2 170 94 30 6]
} -result 5.1136364
test "test-xbar-1.0" "Test xbar procedure" -match exact -body {
set data {}
for { set i 0 } { $i < 500 } { incr i } {
lappend data [expr {rand()}]
}
set limits [::math::statistics::control-xbar $data]
set newdata {1.0 1.0 1.0 1.0 0.5 0.5 0.5 0.5 10.0 10.0 10.0 10.0}
set result [::math::statistics::test-xbar $limits $newdata]
} -result {0 2}
test "test-Rchart-1.0" "Test Rchart procedure" -match exact -body {
set data {}
for { set i 0 } { $i < 500 } { incr i } {
lappend data [expr {rand()}]
}
set limits [::math::statistics::control-Rchart $data]
set newdata {0.0 1.0 2.0 1.0 0.4 0.5 0.6 0.5 10.0 0.0 10.0 10.0}
set result [::math::statistics::test-Rchart $limits $newdata]
} -result {0 2}
#
# Testing for normal distribution
#
test "Testnormal-1.0" "Determine normality statistic for birth weight data" -match tolerant -body {
::math::statistics::lillieforsFit {72 112 111 107 119 92 126 80 81 84 115
118 128 128 123 116 125 126 122 126 127 86
142 132 87 123 133 106 103 118 114 94}
} -result 0.82827415657
test "Testnormal-1.0" "Test birthweight data for normality - 20% significance" -match exact -body {
::math::statistics::test-normal {72 112 111 107 119 92 126 80 81 84 115
118 128 128 123 116 125 126 122 126 127 86
142 132 87 123 133 106 103 118 114 94} 0.20
} -result 1
test "Testnormal-1.0" "Test birthweight data for normality - 5% significance" -match exact -body {
::math::statistics::test-normal {72 112 111 107 119 92 126 80 81 84 115
118 128 128 123 116 125 126 122 126 127 86
142 132 87 123 133 106 103 118 114 94} 0.05
} -result 0
test "Test-Duckworth-1.0" "Test Tukey-Duckworth - 5% significance" -match exact -body {
set list1 {10 2 3 4 6}
set list2 {12 3 4 6}
::math::statistics::test-Duckworth $list1 $list2 0.05
} -result 1
test "Test-Duckworth-1.1" "Test Tukey-Duckworth - symmetry" -match exact -body {
set list1 {1 2 3 4 5 6 7 8 9 10}
set list2 {6 7 8 9 10 11 12 13 14 15 16 17}
set result [list [::math::statistics::test-Duckworth $list1 $list2 0.05] \
[::math::statistics::test-Duckworth $list2 $list1 0.05]]
} -result {0 0}
test "Test-Duckworth-1.2" "Test Tukey-Duckworth - applicability" -match exact -body {
set list1 {2 3 4 6 20}
set list2 {12 3 4 6}
::math::statistics::test-Duckworth $list1 $list2 0.05
} -result -1
#
# Testing multivariate linear regression
#
# Provide some data
test "Testmultivar-1.0" "Ordinary multivariate regression - three independent variables" \
-match tolerant -body {
set data {
{ -.67 14.18 60.03 -7.5}
{ 36.97 15.52 34.24 14.61}
{-29.57 21.85 83.36 -7.}
{-16.9 11.79 51.67 -6.56}
{ 14.09 16.24 36.97 -12.84}
{ 31.52 20.93 45.99 -25.4}
{ 24.05 20.69 50.27 17.27}
{ 22.23 16.91 45.07 -4.3}
{ 40.79 20.49 38.92 -.73}
{-10.35 17.24 58.77 18.78}}
# Call the ols routine
set results [::math::statistics::mv-ols $data]
# Flatten the result (so that we can use the tolerant comparison method)
eval concat [eval concat $results]
} -result {0.887239767929 0.830859651893
3.33854942057 -1.58346976987 0.0362328113288 32.571621244
1.03305463908 0.237943867401 0.234143883673 19.4700016828
0.810755783819 5.86634305732
-2.16569743834 -1.00124210139 -0.536696631937 0.609162254594
-15.0697565684 80.2129990564}
test "Testmultivar-1.1" "Ordinary multivariate regression - zero variation" -body {
set results [::math::statistics::mv-ols {{0 25125 128} {0 23224 64} {0 37903 512} {0 21263 32}
{0 22053 64} {0 25745 256} {0 25745 256} {0 21557 32}
{0 24935 128} {0 22904 64} {0 21422 32} {0 21947 32}
{0 33244 512} {0 33244 512} {0 30060 512} {0 29691 256}
{0 30439 256} {0 23724 128} {0 22541 64} {0 23640 128}
{0 21422 32} {0 23640 128} {0 22249 64} {0 28247 512}
{0 23333 32} {0 29841 256} {0 23959 128} {0 30819 512}
{0 26333 256} {0 22145 32} {0 23863 128} {0 20772 32}
{0 28511 512} {0 22425 64} {0 21598 32} {0 26335 256}
{0 23816 128} {0 21157 32} {0 20973 32} {0 20973 32}
{0 35125 512} {0 20679 32} {0 21241 64} {0 25297 256}
{0 22301 32} {0 22007 32} {0 33351 512} {0 24115 128}
{0 24115 128} {0 22301 32} {0 22797 64} {0 22593 64}
{0 26439 256} {0 21255 32} {0 22645 32} {0 23447 128}
{0 24205 64} {0 25051 128} {0 21007 32} {0 28237 256}
{0 25546 128} {0 25669 256} {0 25669 256} {0 25669 256}
{0 21977 64} {0 21977 64} {0 26187 128} {0 38360 512}
{0 31846 256} {0 28349 256} {0 26450 128}}]
set r2 [lindex $results 0]
} -result 1.0
#
# pdf/cdf tests - transformed from the contributions by Eric K. Benedict
# Cf. the examples.
#
# Note: cases with integer numbers test if divisions are done in floating-point or not
#
test "uniform-distribution-1.0" "Test pdf-uniform" -match tolerant -body {
set x [list \
[::math::statistics::pdf-uniform 0 10 5] \
[::math::statistics::pdf-uniform 0.0 1.0 0.5] \
[::math::statistics::pdf-uniform -10.0 1.0 -4.5] \
[::math::statistics::pdf-uniform -2.0 2.0 1.0]]
} -result {0.1 1.0 0.0909090909 0.25}
test "uniform-distribution-1.1" "Test cdf-uniform" -match tolerant -body {
set x [list \
[::math::statistics::cdf-uniform 0 10 5] \
[::math::statistics::cdf-uniform 0.0 1.0 0.5] \
[::math::statistics::cdf-uniform -10.0 1.0 -4.5] \
[::math::statistics::cdf-uniform -2.0 2.0 1.0]]
} -result {0.5 0.5 0.5 0.75}
test "triangular-distribution-1.0" "Test pdf-triangular" -match tolerant -body {
set x [list \
[::math::statistics::pdf-triangular 0 1.0 0.5] \
[::math::statistics::pdf-triangular 1.0 0.0 0.5] \
[::math::statistics::pdf-triangular 0.0 1.0 0.25] \
[::math::statistics::pdf-triangular 1.0 0.0 0.25] \
[::math::statistics::pdf-triangular 0.0 2.0 0.0] \
[::math::statistics::pdf-triangular 0.0 2.0 1.0] \
[::math::statistics::pdf-triangular 0.0 2.0 2.0]]
} -result {1.0 1.0 1.5 0.5 2.0 1.0 0.0}
test "triangular-distribution-1.1" "Test cdf-triangular" -match tolerant -body {
set x [list \
[::math::statistics::cdf-triangular 0 1.0 0.5] \
[::math::statistics::cdf-triangular 1.0 0.0 0.5] \
[::math::statistics::cdf-triangular 0.0 1.0 0.25] \
[::math::statistics::cdf-triangular 1.0 0.0 0.25] \
[::math::statistics::cdf-triangular 0.0 2.0 0.0] \
[::math::statistics::cdf-triangular 0.0 2.0 1.0] \
[::math::statistics::cdf-triangular 0.0 2.0 2.0]]
} -result {0.75 0.25 0.4375 0.0625 0.0 0.75 1.0}
test "triangular-symmetric-distribution-1.0" "Test pdf-symmetric-triangular" -match tolerant -body {
set x [list \
[::math::statistics::pdf-symmetric-triangular 0.0 2.0 -0.5] \
[::math::statistics::pdf-symmetric-triangular 0.0 2.0 0.0] \
[::math::statistics::pdf-symmetric-triangular 0.0 2.0 0.5] \
[::math::statistics::pdf-symmetric-triangular 0.0 2.0 1.0] \
[::math::statistics::pdf-symmetric-triangular 0.0 2.0 1.5] \
[::math::statistics::pdf-symmetric-triangular 0.0 2.0 2.0] \
[::math::statistics::pdf-symmetric-triangular 0.0 2.0 2.5]]
} -result {0.0 0.0 0.5 1.0 0.5 0.0 0.0}
test "triangular-symmetric-distribution-1.1" "Test cdf-symmetric-triangular" -match tolerant -body {
set x [list \
[::math::statistics::cdf-symmetric-triangular 0.0 2.0 -0.5] \
[::math::statistics::cdf-symmetric-triangular 0.0 2.0 0.0] \
[::math::statistics::cdf-symmetric-triangular 0.0 2.0 0.5] \
[::math::statistics::cdf-symmetric-triangular 0.0 2.0 1.0] \
[::math::statistics::cdf-symmetric-triangular 0.0 2.0 1.5] \
[::math::statistics::cdf-symmetric-triangular 0.0 2.0 2.0] \
[::math::statistics::cdf-symmetric-triangular 0.0 2.0 2.5]]
} -result {0.0 0.0 0.125 0.5 0.875 1.0 1.0}
test "exponential-distribution-1.0" "Test pdf-exponential" -match tolerant -body {
set x [list \
[::math::statistics::pdf-exponential 2 1] \
[::math::statistics::pdf-exponential 1.0 1.0] \
[::math::statistics::pdf-exponential 2.0 2.0] \
[::math::statistics::pdf-exponential 2.0 1.0]]
} -result {0.3032653298563167 0.36787944117144233 0.18393972058572117 0.3032653298563167}
test "exponential-distribution-1.1" "Test cdf-exponential" -match tolerant -body {
set x [list \
[::math::statistics::cdf-exponential 2 1] \
[::math::statistics::cdf-exponential 1.0 1.0] \
[::math::statistics::cdf-exponential 2.0 2.0] \
[::math::statistics::cdf-exponential 2.0 1.0]]
} -result {0.3934693402873666 0.6321205588285577 0.6321205588285577 0.3934693402873666}
test "laplace-distribution-1.0" "Test pdf-laplace" -match tolerant -body {
set x [list \
[::math::statistics::pdf-laplace 1 1 1] \
[::math::statistics::pdf-laplace 1.0 1.0 2] \
[::math::statistics::pdf-laplace -1.0 2.0 0] \
[::math::statistics::pdf-laplace 1.5 2.5 3]]
} -result {0.5 0.18393972058572117 0.15163266492815836 0.1097623272188053}
test "laplace-distribution-1.1" "Test cdf-laplace" -match tolerant -body {
set x [list \
[::math::statistics::cdf-laplace 1 1 1] \
[::math::statistics::cdf-laplace 1.0 1.0 2] \
[::math::statistics::cdf-laplace -1.0 2.0 0] \
[::math::statistics::cdf-laplace 1.5 2.5 3]]
} -result {0.5 0.8160602794142788 0.6967346701436833 0.7255941819529867}
test "kumaraswamy-distribution-1.0" "Test pdf-kumaraswamy" -match tolerant -body {
set x [list \
[::math::statistics::pdf-kumaraswamy 1 1 0.5] \
[::math::statistics::pdf-kumaraswamy 0.5 0.5 0.5] \
[::math::statistics::pdf-kumaraswamy 1.0 2.0 0] \
[::math::statistics::pdf-kumaraswamy 1.0 2.0 0.5]]
} -result {1.0 0.6532814824381884 2.0 1.0}
test "kumaraswamy-distribution-1.1" "Test cdf-kumaraswamy" -match tolerant -body {
set x [list \
[::math::statistics::cdf-kumaraswamy 1 1 0.5] \
[::math::statistics::cdf-kumaraswamy 0.5 0.5 0.5] \
[::math::statistics::cdf-kumaraswamy 1.0 2.0 0] \
[::math::statistics::cdf-kumaraswamy 1.0 2.0 0.5]]
} -result {0.5 0.4588038998538031 0.0 0.75}
# Non-trivial examples from Wikipedia
test "negative-binomial-distribution-1.0" "Test pdf-negative-binomial" -match tolerant -body {
set x [list \
[::math::statistics::pdf-negative-binomial 1 0.3 0] \
[::math::statistics::pdf-negative-binomial 1 0.9 0] \
[::math::statistics::pdf-negative-binomial 5 0.4 0] \
[::math::statistics::pdf-negative-binomial 5 0.4 1] \
[::math::statistics::pdf-negative-binomial 5 0.4 2] \
[::math::statistics::pdf-negative-binomial 5 0.4 3] \
[::math::statistics::pdf-negative-binomial 5 0.4 5]]
} -result {0.7 0.1 0.07776 0.15552 0.186624 0.1741824 0.10032906}
test "negative-binomial-distribution-1.1" "Test cdf-negative-binomial" -match tolerant -body {
set x [list \
[::math::statistics::cdf-negative-binomial 5 0.4 3] \
[::math::statistics::cdf-negative-binomial 1 0.9 0] \
[::math::statistics::cdf-negative-binomial 1 0.9 1] \
[::math::statistics::cdf-negative-binomial 1 0.9 2]]
} -result {0.59408 0.1 0.19 0.271}
test "normal-distribution-1.0" "Test pdf-normal" -match tolerant -body {
set x [list \
[::math::statistics::pdf-normal 0 1 1] \
[::math::statistics::pdf-normal 0.0 1.0 1.0] \
[::math::statistics::pdf-normal 2.0 2.0 4.0] \
[::math::statistics::pdf-normal -2.0 2.0 0.0] \
[::math::statistics::pdf-normal 2.0 2.0 3.0]]
} -result {0.24197072451914337 0.24197072451914337 0.12098536225957168 0.12098536225957168 0.17603266338214976}
test "normal-distribution-1.1" "Test cdf-normal" -match tolerant -body {
set x [list \
[::math::statistics::cdf-normal 0 1 1] \
[::math::statistics::cdf-normal 0.0 1.0 1.0] \
[::math::statistics::cdf-normal 2.0 2.0 4.0] \
[::math::statistics::cdf-normal -2.0 2.0 0.0] \
[::math::statistics::cdf-normal 2.0 2.0 3.0]]
} -result {0.8413205502059895 0.8413205502059895 0.8413205502059895 0.8413205502059895 0.691451459572962}
#
# The values of the mean and the standard deviation are reconstructed from mu and sigma.
# If you use mu and sigma in pdf-normal/cdf-normal, the same numbers should arise
#
test "lognormal-distribution-1.0" "Test pdf-lognormal" -match tolerant -body {
foreach {mu sigma mean stdev} {0.0 1.0 mean1 stdev1 2.0 2.0 mean2 stdev2 -2.0 2.0 mean3 stdev3} {
set m [expr {exp($mu + $sigma*$sigma/2.0)}]
set $mean $m
set $stdev [expr {sqrt(exp($sigma*$sigma) - 1.0) * $m}]
}
set x [list \
[::math::statistics::pdf-lognormal $mean1 $stdev1 [expr {exp(1.0)}]] \
[::math::statistics::pdf-lognormal $mean2 $stdev2 [expr {exp(4.0)}]] \
[::math::statistics::pdf-lognormal $mean3 $stdev3 [expr {exp(0.0)}]] \
[::math::statistics::pdf-lognormal $mean2 $stdev2 [expr {exp(3.0)}]]]
} -result {0.24197072451914337 0.12098536225957168 0.12098536225957168 0.17603266338214976}
test "lognormal-distribution-1.1" "Test cdf-lognormal" -match tolerant -body {
foreach {mu sigma mean stdev} {0.0 1.0 mean1 stdev1 2.0 2.0 mean2 stdev2 -2.0 2.0 mean3 stdev3} {
set m [expr {exp($mu + $sigma*$sigma/2.0)}]
set $mean $m
set $stdev [expr {sqrt(exp($sigma*$sigma) - 1.0) * $m}]
}
set x [list \
[::math::statistics::cdf-lognormal $mean1 $stdev1 [expr {exp(1.0)}]] \
[::math::statistics::cdf-lognormal $mean2 $stdev2 [expr {exp(4.0)}]] \
[::math::statistics::cdf-lognormal $mean3 $stdev3 [expr {exp(0.0)}]] \
[::math::statistics::cdf-lognormal $mean2 $stdev2 [expr {exp(3.0)}]]]
} -result {0.8413205502059895 0.8413205502059895 0.8413205502059895 0.691451459572962}
test "gamma-distribution-1.0" "Test pdf-gamma" -match tolerant -body {
set x [list \
[::math::statistics::pdf-gamma 1.5 2.7 3.0] \
[::math::statistics::pdf-gamma 7.5 0.2 30.0] \
[::math::statistics::pdf-gamma 15.0 1.2 2.0]]
} -result {0.00263194027271168 0.0302770403110644 2.62677891379834e-07}
test "gamma-distribution-1.1" "Test cdf-gamma" -match tolerant -body {
set x [list \
[::math::statistics::cdf-gamma 1.9 0.45 2.5] \
[::math::statistics::cdf-gamma 45.0 2.2 32.7]]
} -result {0.340299345090375 0.999731419881902}
test "poisson-distribution-1.0" "Test pdf-poisson" -match tolerant -body {
set x [list \
[::math::statistics::pdf-poisson 100 130] \
[::math::statistics::pdf-poisson 27.2 37] \
[::math::statistics::pdf-poisson 7.3 11.2]]
} -result {0.000575252683815462 0.0134122817590761 0.0530940708960824}
test "poisson-distribution-1.1" "Test cdf-poisson" -match tolerant -body {
set x [list \
[::math::statistics::cdf-poisson 4 7] \
[::math::statistics::cdf-poisson 80 70] \
[::math::statistics::cdf-poisson 4.9 6.2]]
} -result {0.948866384207153 0.14338996716003 0.77665467292263}
test "chisquare-distribution-1.0" "Test pdf-chisquare" -match tolerant -body {
set x [list \
[::math::statistics::pdf-chisquare 3 1.75] \
[::math::statistics::pdf-chisquare 10 2.9] \
[::math::statistics::pdf-chisquare 4 17.45] \
[::math::statistics::pdf-chisquare 2.5 1.8]]
} -result {0.219999360547348 0.0216024880121444 0.000708787557977144 0.218446210041615}
test "chisquare-distribution-1.1" "Test cdf-chisquare" -match tolerant -body {
set x [list \
[::math::statistics::cdf-chisquare 2 3.5] \
[::math::statistics::cdf-chisquare 5 2.2] \
[::math::statistics::cdf-chisquare 5 100] \
[::math::statistics::cdf-chisquare 3.9 4.2] \
[::math::statistics::cdf-chisquare 1 2.0] \
[::math::statistics::cdf-chisquare 3 -2.0]]
} -result {0.826226056549555 0.179164030785504 1.0 0.634682741547709 0.842700792949715 0.0}
test "students-t-distribution-1.0" "Test pdf-students-t" -match tolerant -body {
set x [list \
[::math::statistics::pdf-students-t 1 0.1] \
[::math::statistics::pdf-students-t 0.5 0.1] \
[::math::statistics::pdf-students-t 4 3.2] \
[::math::statistics::pdf-students-t 3 2.0] \
[::math::statistics::pdf-students-t 3 7.5]]
} -result {0.315158303152268 0.265700672177405 0.0156821741652879 0.0675096606638929 0.000942291548015668}
test "beta-distribution-1.0" "Test pdf-beta" -match tolerant -body {
set x [list \
[::math::statistics::pdf-beta 1.3 2.4 0.2] \
[::math::statistics::pdf-beta 1 1 0.5] \
[::math::statistics::pdf-beta 3.7 0.9 0.0] \
[::math::statistics::pdf-beta 1.8 4.2 1.0] \
[::math::statistics::pdf-beta 320 400 0.4] \
[::math::statistics::pdf-beta 500 1 0.2] \
[::math::statistics::pdf-beta 1000 1000 0.50]]
} -result {1.68903180472449 1.0 0.0 0.0 1.18192376783860 0.0 35.6780222917086}
test "beta-distribution-1.1" "Test cdf-beta" -match tolerant -body {
set x [list \
[::math::statistics::cdf-beta 2.1 3.0 0.2] \
[::math::statistics::cdf-beta 4.2 17.3 0.5] \
[::math::statistics::cdf-beta 500 375 0.7] \
[::math::statistics::cdf-beta 250 760 0.2] \
[::math::statistics::cdf-beta 43.2 19.7 0.6] \
[::math::statistics::cdf-beta 500 640 0.3] \
[::math::statistics::cdf-beta 400 640 0.3] \
[::math::statistics::cdf-beta 0.1 30 0.1] \
[::math::statistics::cdf-beta 0.01 0.03 0.9] \
[::math::statistics::cdf-beta 2 3 0.9999] \
[::math::statistics::cdf-beta 249.9999 759.99999 0.2] \
[::math::statistics::cdf-beta 1000 1000 0.4] \
[::math::statistics::cdf-beta 1000 1000 0.499] \
[::math::statistics::cdf-beta 1000 1000 0.5] \
[::math::statistics::cdf-beta 1000 1000 0.7] \
[::math::statistics::cdf-beta 2 3 0.6]]
} -result {0.16220409275804 0.998630771123192 1.0 0.000125234318666948 0.0728881294218269
2.99872547567313e-23 3.07056696205524e-09 0.998641008671625 0.765865005703006
0.999999999996 0.000125237075575121 8.23161135486914e-20 0.464369443974288
0.5 1.0 0.8208}
#
# TODO: chose the tests with _integer_ arguments more carefully
#
test "gumbel-distribution-1.0" "Test pdf-gumbel" -match tolerant -body {
set x [list \
[::math::statistics::pdf-gumbel 1.0 1.0 0.0] \
[::math::statistics::pdf-gumbel 1.0 1.0 0.1] \
[::math::statistics::pdf-gumbel 1.0 1.0 0.2] \
[::math::statistics::pdf-gumbel 1.0 1.0 1.0] \
[::math::statistics::pdf-gumbel 1.0 1.0 2.0] \
[::math::statistics::pdf-gumbel 1.0 1.0 5.0] \
[::math::statistics::pdf-gumbel 0.1 2.0 0.0] \
[::math::statistics::pdf-gumbel 0.1 2.0 1.0] \
[::math::statistics::pdf-gumbel 0.1 2.0 2.0] \
[::math::statistics::pdf-gumbel 0.1 2.0 5.0] \
[::math::statistics::pdf-gumbel 1 1 5 ] ]
} -result {0.179374 0.210219 0.240378 0.367879 0.254646 0.017983 0.183706 0.168507 0.131350 0.039580 0.017983}
test "gumbel-distribution-1.1" "Test cdf-gumbel" -match tolerant -body {
set x [list \
[::math::statistics::cdf-gumbel 1.0 1.0 0.0] \
[::math::statistics::cdf-gumbel 1.0 1.0 0.2] \
[::math::statistics::cdf-gumbel 1.0 1.0 1.0] \
[::math::statistics::cdf-gumbel 1.0 1.0 2.0] \
[::math::statistics::cdf-gumbel 0.1 2.0 0.0] \
[::math::statistics::cdf-gumbel 0.1 2.0 1.0] \
[::math::statistics::cdf-gumbel 0.1 2.0 2.0] \
[::math::statistics::cdf-gumbel 1 1 2 ] ]
} -result {0.065988 0.108009 0.367879 0.692201 0.349493 0.528544 0.679266 0.692201}
test "weibull-distribution-1.0" "Test pdf-weibull" -match tolerant -body {
set x [list \
[::math::statistics::pdf-weibull 1.0 1.0 -1.0] \
[::math::statistics::pdf-weibull 1.0 1.0 0.0] \
[::math::statistics::pdf-weibull 1.0 1.0 0.1] \
[::math::statistics::pdf-weibull 1.0 1.0 0.2] \
[::math::statistics::pdf-weibull 1.0 1.0 1.0] \
[::math::statistics::pdf-weibull 1.0 1.0 2.0] \
[::math::statistics::pdf-weibull 1.0 1.0 5.0] \
[::math::statistics::pdf-weibull 2.0 2.0 0.0] \
[::math::statistics::pdf-weibull 2.0 2.0 1.0] \
[::math::statistics::pdf-weibull 2.0 2.0 2.0] \
[::math::statistics::pdf-weibull 2.0 2.0 5.0] ]
} -result {0 1.0 0.904837 0.818730 0.367879 0.135335 0.006738 0 0.389400 0.367879 0.004826}
test "weibull-distribution-1.1" "Test cdf-weibull" -match tolerant -body {
set x [list \
[::math::statistics::cdf-weibull 1.0 1.0 -1.0] \
[::math::statistics::cdf-weibull 1.0 1.0 0.0] \
[::math::statistics::cdf-weibull 1.0 1.0 0.2] \
[::math::statistics::cdf-weibull 1.0 1.0 1.0] \
[::math::statistics::cdf-weibull 1.0 1.0 2.0] \
[::math::statistics::cdf-weibull 2.0 2.0 0.0] \
[::math::statistics::cdf-weibull 2.0 2.0 1.0] \
[::math::statistics::cdf-weibull 2.0 2.0 2.0] \
[::math::statistics::cdf-weibull 2 2 2 ] ]
} -result {0 0 0.181269 0.632106 0.864665 0 0.221199 0.632121 0.632121}
test "pareto-distribution-1.0" "Test pdf-pareto" -match tolerant -body {
set x [list \
[::math::statistics::pdf-pareto 1.0 1.0 0.0] \
[::math::statistics::pdf-pareto 1.0 1.0 1.1] \
[::math::statistics::pdf-pareto 1.0 1.0 1.2] \
[::math::statistics::pdf-pareto 1.0 1.0 2.0] \
[::math::statistics::pdf-pareto 1.0 1.0 3.0] \
[::math::statistics::pdf-pareto 1.0 1.0 5.0] \
[::math::statistics::pdf-pareto 2.0 2.0 2.1] \
[::math::statistics::pdf-pareto 2.0 2.0 3.0] \
[::math::statistics::pdf-pareto 2.0 2.0 5.0] \
[::math::statistics::pdf-pareto 2.0 2.0 10.0] ]
} -result {0 0.826446 0.694444 0.25 0.111111 0.04 0.863838 0.296296 0.064 0.008}
test "pareto-distribution-1.1" "Test cdf-pareto" -match tolerant -body {
set x [list \
[::math::statistics::cdf-pareto 1.0 1.0 0.0] \
[::math::statistics::cdf-pareto 1.0 1.0 1.1] \
[::math::statistics::cdf-pareto 1.0 1.0 1.2] \
[::math::statistics::cdf-pareto 1.0 1.0 2.0] \
[::math::statistics::cdf-pareto 1.0 1.0 3.0] \
[::math::statistics::cdf-pareto 2.0 2.0 2.1] \
[::math::statistics::cdf-pareto 2.0 2.0 3.0] \
[::math::statistics::cdf-pareto 2.0 2.0 5.0] \
[::math::statistics::cdf-pareto 2 2 3 ] ]
} -result {0 0.090909 0.1666667 0.5 0.666667 0.092971 0.555556 0.84 0.555556}
test "cauchy-distribution-1.0" "Test pdf-cauchy" -match tolerant -body {
set x [list \
[::math::statistics::pdf-cauchy 1.0 1.0 0.0] \
[::math::statistics::pdf-cauchy 2.0 1.0 1.0] \
[::math::statistics::pdf-cauchy 1.0 2.0 2.0] \
[::math::statistics::pdf-cauchy 2.0 2.0 2.0] ]
} -result {0.1591555 0.1591555 0.1273240 0.1591550}
test "cauchy-distribution-1.1" "Test cdf-cauchy" -match tolerant -body {
set x [list \
[::math::statistics::cdf-cauchy 1.0 1.0 0.0] \
[::math::statistics::cdf-cauchy 2.0 1.0 1.0] \
[::math::statistics::cdf-cauchy 1.0 2.0 2.0] \
[::math::statistics::cdf-cauchy 2.0 2.0 2.0] ]
} -result {0.25 0.25 0.6475836 0.5}
test "F-distribution-1.1" "Test cdf-F" -match tolerant -body {
# Note: returned values are 1-0.05, 1-0.10 and 1-0.01
set x [list \
[::math::statistics::cdf-F 3 3 9.277] \
[::math::statistics::cdf-F 1 30 4.171] \
[::math::statistics::cdf-F 7 8 3.500] \
[::math::statistics::cdf-F 2 10 2.924] \
[::math::statistics::cdf-F 17 2 9.433] \
[::math::statistics::cdf-F 5 6 8.746] \
[::math::statistics::cdf-F 8 11 4.744] ]
} -result {0.95 0.95 0.95 0.90 0.90 0.99 0.99}
test "empirical-distribution-1.0" "Test empirical-distribution" -match tolerant -body {
set x {10 4 3 2 5 6 7}
set distribution [::math::statistics::empirical-distribution $x]
} -result {2 0.086207 3 0.224138 4 0.36207 5 0.5 6 0.637910 7 0.775862 10 0.913793}
#
# Crude tests for the random number generators
# Mainly to verify that there are no obvious errors
#
# To verify that the values are scaled properly, use a fixed seed
#
set ::rseed 1000000
test "random-numbers-1.0" "Test random-uniform" -body {
expr {srand($::rseed)}
set rnumbers [::math::statistics::random-uniform 0 10 100]
set inrange 1
foreach r $rnumbers {
if { $r < 0.0 || $r > 10.0 } {
set inrange 0
break
}
}
expr {srand($::rseed)}
set scaled 1
set rnumbers2 [::math::statistics::random-uniform 0 20 100]
foreach r1 $rnumbers r2 $rnumbers2 {
set scale [expr {$r2 / $r1}]
if { abs($scale - 2.0) > 0.00001 } {
set scaled 0
}
}
expr {srand($::rseed)}
set shifted 1
set rnumbers3 [::math::statistics::random-uniform 10 20 100]
foreach r1 $rnumbers r3 $rnumbers3 {
set shift [expr {$r3 - $r1}]
if { abs($shift - 10.0) > 0.00001 } {
set shifted 0
}
}
set result [list $inrange [llength $rnumbers] $scaled $shifted]
} -result {1 100 1 1}
test "random-numbers-1.1" "Test random-exponential" -body {
expr {srand($::rseed)}
set rnumbers [::math::statistics::random-exponential 1 100]
set inrange 1
foreach r $rnumbers {
if { $r < 0.0 } {
set inrange 0
break
}
}
expr {srand($::rseed)}
set scaled 1
set rnumbers2 [::math::statistics::random-exponential 2 100]
foreach r1 $rnumbers r2 $rnumbers2 {
set scale [expr {$r2 / $r1}]
if { abs($scale - 2.0) > 0.00001 } {
set scaled 0
}
}
set result [list $inrange [llength $rnumbers] $scaled]
} -result {1 100 1}
test "random-numbers-1.2" "Test random-normal" -body {
set rnumbers [::math::statistics::random-normal 0 1 100]
set result [llength $rnumbers]
} -result 100
test "random-numbers-1.3" "Test random-gamma" -body {
set rnumbers [::math::statistics::random-gamma 1.5 2.7 100]
set result [llength $rnumbers]
} -result 100
test "random-numbers-1.4" "Test random-poisson" -body {
set rnumbers [::math::statistics::random-poisson 2.5 100]
set result [llength $rnumbers]
} -result 100
test "random-numbers-1.5" "Test random-chisquare" -body {
set rnumbers [::math::statistics::random-chisquare 3 100]
set result [llength $rnumbers]
} -result 100
test "random-numbers-1.6" "Test random-students-t" -body {
set rnumbers [::math::statistics::random-students-t 3 100]
set result [llength $rnumbers]
} -result 100
test "random-numbers-1.7" "Test random-beta" -body {
set rnumbers [::math::statistics::random-beta 1.3 2.4 100]
set result 1
foreach r $rnumbers {
if { $r < 0.0 || $r > 1.0 } {
result 0
break
}
}
lappend result [llength $rnumbers]
} -result {1 100}
test "random-numbers-1.8" "Test random-gumbel" -body {
set rnumbers [::math::statistics::random-gumbel 1.0 3.0 100]
set result [llength $rnumbers]
} -result 100
test "random-numbers-1.9" "Test random-weibull" -body {
set rnumbers [::math::statistics::random-weibull 1.0 3.0 100]
set result [llength $rnumbers]
} -result 100
test "random-numbers-1.10" "Test random-pareto" -body {
set rnumbers [::math::statistics::random-pareto 1.0 3.0 100]
set result 1
foreach r $rnumbers {
if { $r < 1.0 } {
result 0
break
}
}
lappend result [llength $rnumbers]
} -result {1 100}
test "random-numbers-1.11" "Test random-lognormal" -body {
set rnumbers [::math::statistics::random-lognormal 1 1 100]
set result [llength $rnumbers]
} -result 100
test "random-numbers-1.11" "Test random-cauchy" -body {
set rnumbers [::math::statistics::random-cauchy 0 1 100]
set result [llength $rnumbers]
} -result 100
test "random-numbers-1.12" "Test random-triangular" -body {
set result 1
set rnumbers [::math::statistics::random-triangular -10 10 100]
# Check the scaling
foreach r $rnumbers {
if { $r < -10.0 || $r > 10.0 } {
set result 0
break
}
}
# Also the alternative triangle
set rnumbers [::math::statistics::random-triangular 10 -10 100]
# Check the scaling
foreach r $rnumbers {
if { $r < -10.0 || $r > 10.0 } {
set result 0
break
}
}
# The symmetric triangle
set rnumbers [::math::statistics::random-symmetric-triangular 10 -10 100]
# Check the scaling
foreach r $rnumbers {
if { $r < -10.0 || $r > 10.0 } {
set result 0
break
}
}
set result
} -result 1
test "random-numbers-2.1" "Test random/estimate-pareto" -match tolerant -body {
expr {srand($::rseed)}
set rnumbers [::math::statistics::random-pareto 1.0 3.0 100]
set result [::math::statistics::estimate-pareto $rnumbers]
} -result {1.000519 3.668162 0.3668162}
test "random-numbers-2.2" "Test random/estimate-exponential" -match tolerant -body {
expr {srand($::rseed)}
set rnumbers [::math::statistics::random-exponential 2.0 1000]
set result [::math::statistics::estimate-exponential $rnumbers]
} -result {1.9976327295438587 0.06317069353857727}
test "random-numbers-2.3" "Test random/estimate-laplace" -match tolerant -body {
expr {srand($::rseed)}
set rnumbers [::math::statistics::random-laplace 1.0 1.0 1000]
set result [::math::statistics::estimate-laplace $rnumbers]
} -result {1.0106101707362272 0.9319576942526239}
# Very simple test - just check that the numbers lie between 0 and 1
test "random-numbers-2.4" "Test random-kumaraswamy" -body {
expr {srand($::rseed)}
set result 1
foreach {a b} {0.5 0.5 1.0 1.0 0.5 1.0 1.0 0.5 2.0 0.1 0.1 2.0} {
set rnumbers [::math::statistics::random-kumaraswamy $a $b 1000]
foreach r $rnumbers {
if { $r < 0.0 || $r > 1.0 } {
set result 0
break
}
}
}
set result
} -result 1
# Simple test, exact matching
test "random-numbers-2.5" "Test random-negative-binomial" -body {
expr {srand($::rseed)}
set rnumbers [::math::statistics::random-negative-binomial 1 0.3 10]
} -result {0 0 3 0 0 0 1 0 1 1}
test "random-numbers-2.6" "Test random/estimate-negative-binomial" -match tolerant -body {
expr {srand($::rseed)}
set r 3
set rnumbers [::math::statistics::random-negative-binomial $r 0.5 1000]
set pvalue [::math::statistics::estimate-negative-binomial $r $rnumbers]
} -result 0.50190935
test "kruskal-wallis-1.0" "Test analysis Kruskal-Wallis" -match tolerant -body {
::math::statistics::analyse-Kruskal-Wallis {6.4 6.8 7.2 8.3 8.4 9.1 9.4 9.7} {2.5 3.7 4.9 5.4 5.9 8.1 8.2} {1.3 4.1 4.9 5.2 5.5 8.2}
} -result {9.83627087199 0.00731275323967}
test "kruskal-wallis-1.1" "Test test Kruskal-Wallis" -match tolerant -body {
::math::statistics::test-Kruskal-Wallis 0.95 {6.4 6.8 7.2 8.3 8.4 9.1 9.4 9.7} {2.5 3.7 4.9 5.4 5.9 8.1 8.2} {1.3 4.1 4.9 5.2 5.5 8.2}
} -result 1
# Data from Statistical methods in Engineering and Quality Assurance by Peter W.M. John
test "wilcoxon-1.0" "Test test Wilcoxon" -match tolerant -body {
::math::statistics::test-Wilcoxon {71.1 68.3 74.8 72.1 71.2 70.4 73.6 66.3 72.7 74.1 70.1 68.5} \
{73.3 70.9 74.6 72.1 72.8 74.2 74.7 69.2 75.5 75.8 70.0 72.1}
} -result -1.67431578065
# Very simple tests, merely to show that the procedure "works"
# (No numerical example available)
test "levene-brown-forsythe-1.0" "Test test Levene/Brown-Forsythe" -match tolerant -body {
set values {{1 2 3} {2 3 4} {5 6 7}}
::math::statistics::test-Levene $values
} -result 0.0
test "levene-brown-forsythe-1.1" "Test test Levene/Brown-Forsythe" -match tolerant -body {
set values {{1 2 3} {2 3 4} {5 6 7}}
::math::statistics::test-Brown-Forsythe $values
} -result 0.0
test "levene-brown-forsythe-1.2" "Test test Levene/Brown-Forsythe" -match tolerant -body {
set values {{1 2 2} {2 3 4} {5 6 7.4}}
::math::statistics::test-Levene $values
} -result 0.47937131630648294
test "levene-brown-forsythe-1.3" "Test test Levene/Brown-Forsythe" -match tolerant -body {
set values {{1 2 2} {2 3 4} {5 6 7.4}}
::math::statistics::test-Brown-Forsythe $values
} -result 0.4382022471910113
# Data taken from https://www.itl.nist.gov/div898/handbook/eda/section3/eda35a.htm
# Note: the webpage talks of the Levene test, but is actually the Brown-Forsythe
test "levene-brown-forsythe-1.4" "Test test Levene/Brown-Forsythe" -match tolerant -body {
set values {{ 1.006 0.996 0.998 1.000 0.992 0.993 1.002 0.999 0.994 1.000 }
{ 0.998 1.006 1.000 1.002 0.997 0.998 0.996 1.000 1.006 0.988 }
{ 0.991 0.987 0.997 0.999 0.995 0.994 1.000 0.999 0.996 0.996 }
{ 1.005 1.002 0.994 1.000 0.995 0.994 0.998 0.996 1.002 0.996 }
{ 0.998 0.998 0.982 0.990 1.002 0.984 0.996 0.993 0.980 0.996 }
{ 1.009 1.013 1.009 0.997 0.988 1.002 0.995 0.998 0.981 0.996 }
{ 0.990 1.004 0.996 1.001 0.998 1.000 1.018 1.010 0.996 1.002 }
{ 0.998 1.000 1.006 1.000 1.002 0.996 0.998 0.996 1.002 1.006 }
{ 1.002 0.998 0.996 0.995 0.996 1.004 1.004 0.998 0.999 0.991 }
{ 0.991 0.995 0.984 0.994 0.997 0.997 0.991 0.998 1.004 0.997 } }
::math::statistics::test-Brown-Forsythe $values
} -result 1.705910
# Data from the Wikipedia page on Spearman's rank correlation coefficient
test "spearman-rank-1.0" "Test Spearman rank correlation" -match tolerant -body {
::math::statistics::spearman-rank {106 86 100 101 99 103 97 113 112 110} \
{ 7 0 27 50 28 29 20 12 6 17}
} -result -0.175757575758
test "spearman-rank-extended-1.0" "Test extended Spearman rank correlation procedure" -match tolerant -body {
::math::statistics::spearman-rank-extended {106 86 100 101 99 103 97 113 112 110} \
{ 7 0 27 50 28 29 20 12 6 17}
} -result {-0.175757575758 10 -0.456397284}
#
# Note: for the uniform and the logistic kernel the sum deviates more from 1 than for the others.
# For the logistic kernel this is because the density function is very widespread. For the
# uniform kernel the reason is not quite clear. Hence the margin per kernel.
#
test "kernel-density-1.0" "Test various kernel functions" -body {
set data {1 2 3 4 5 6 7 8 9 10}
set roughlyOne {}
foreach kernel {gaussian uniform triangular epanechnikov biweight cosine logistic} \
margin {0.01 0.02 0.01 0.01 0.01 0.01 0.05 } {
set result [::math::statistics::kernel-density $data -kernel $kernel]
set sum 0.0
set xbegin [lindex $result 2 0]
set xend [lindex $result 2 1]
set number [llength [lindex $result 0]]
set dx [expr {($xend-$xbegin) / $number}]
#
# Integral should be roughly one
#
set sum 0.0
foreach v [lindex $result 1] {
set sum [expr {$sum + $dx * $v}]
}
lappend roughlyOne [expr {abs($sum-1.0) < $margin}]
}
return $roughlyOne
} -result {1 1 1 1 1 1 1}
test "kernel-density-1.1" "Test various options - just that they have effect" -body {
set subResults {}
set data {1 2 3 4 5 6 7 8 9 10}
set result [::math::statistics::kernel-density $data -number 20]
lappend subResults [llength [lindex $result 0]] ;# Number of bins
lappend subResults [llength [lindex $result 1]] ;# Number of density values
set result [::math::statistics::kernel-density $data -interval {0 20}]
lappend subResults [lindex $result 2 0] ;# Beginning of interval
lappend subResults [lindex $result 2 1] ;# End of interval
lappend subResults [expr {[lindex $result 0 0] > [lindex $result 2 0]}] ;# First bin -- beginning of interval
lappend subResults [expr {[lindex $result 0 0] < [lindex $result 2 1]}] ;# First bin -- end of interval
lappend subResults [expr {[lindex $result 0 end] > [lindex $result 2 0]}] ;# Last bin -- beginning of interval
lappend subResults [expr {[lindex $result 0 end] < [lindex $result 2 1]}] ;# Last bin -- end of interval
set result [::math::statistics::kernel-density $data -bandwidth 2]
lappend subResults [lindex $result 2 end] ;# Bandwidth
return $subResults
} -result {20 20 0 20 1 1 1 1 2}
test "kernel-density-1.2" "Dealing with missing values" -body {
set subResults {}
set data {1 2 3 4 {} 6 7 8 9 10}
set result [::math::statistics::kernel-density $data]
set sum 0.0
set xbegin [lindex $result 2 0]
set xend [lindex $result 2 1]
set number [llength [lindex $result 0]]
set dx [expr {($xend-$xbegin) / $number}]
#
# Integral should be roughly one
#
set sum 0.0
foreach v [lindex $result 1] {
set sum [expr {$sum + $dx * $v}]
}
return [expr {abs($sum-1.0) < 0.01}]
} -result 1
test "anova-one-way-1.1" "ANOVA test from Wikipedia" -body {
::math::statistics::test-anova-F 0.05 {6 8 4 5 3 4} {8 12 9 11 6 8} {13 9 11 8 7 12}
} -result 0
test "anova-one-way-1.2" "ANOVA test from Wikipedia - using nested list" -body {
::math::statistics::test-anova-F 0.05 {{6 8 4 5 3 4} {8 12 9 11 6 8} {13 9 11 8 7 12}}
} -result 0
#
# Data from http://www.itl.nist.gov/div898/handbook/prc/section4/prc436.htm#example1
# See also http://www.itl.nist.gov/div898/handbook/prc/section4/prc471.htm
# Caveat: the calculation produces slightly different confidence intervals. I checked whether
# I got the calculation of the pooled variance right against the example appearing on Wikipedia
# (https://en.wikipedia.org/wiki/Pooled_variance) and that seems okay.
# No idea where the numerical difference is coming from.
#
test "Tukey-range-test-1.1" "Tukey range test" -body {
set data {
Group 1 {6.9 5.4 5.8 4.6 4.0}
Group 2 {8.3 6.8 7.8 9.2 6.5}
Group 3 {8.0 10.5 8.1 6.9 9.3}
Group 4 {5.8 3.8 6.1 5.6 6.2}
}
set groupData {}
foreach {dummy label d} $data {
lappend groupData $d
}
set tukeyRange [::math::statistics::test-Tukey-range 0.05 $groupData]
set indications {}
foreach t $tukeyRange {
lappend indications [lindex $t 2]
}
set indications
} -result {1 1 0 0 0 1}
#
# Data from https://en.wikipedia.org/wiki/Dunnett's_test
# Note that the Wikipedia uses t = 2.94, whereas it should have been 2.88
#
test "Dunnet-test-1.1" "Dunnett test" -body {
set control {55 47 48}
set data {{55 64 64} {55 49 52} {50 44 41}}
set dunnett [::math::statistics::test-Dunnett 0.05 $control $data]
set indications {}
foreach t $dunnett {
lappend indications [lindex $t 0]
}
set indications
} -result {1 0 0}
#
# Bootstrap method to create new samples
#
test "Bootstrap-1.1" "Bootstrap - construct a single sample" -body {
set data {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20}
set sample [::math::statistics::bootstrap $data 10]
#
# Check the sample
#
set numberOkay 0
foreach value $sample {
incr numberOkay [expr {$value in $data}]
}
set result [list [llength $sample] $numberOkay]
} -result {10 10}
test "Bootstrap-1.2" "Bootstrap - variation in a single sample" -body {
set data 0
for {set i 0} {$i < 100} {incr i} {
lappend data $i
}
set sample [::math::statistics::bootstrap $data 10]
#
# It is improbable that all values are the same
#
set numberInHalf1 0
set numberInHalf2 0
set halfData1 [lrange $data 0 49]
set halfData2 [lrange $data 50 99]
foreach value $sample {
incr numberInHalf1 [expr {$value in $halfData1}]
incr numberInHalf2 [expr {$value in $halfData2}]
}
set result [list [expr {$numberInHalf1 > 0}] [expr {$numberInHalf2 > 0}]]
} -result {1 1}
test "Bootstrap-1.3" "Bootstrap - list of samples" -body {
set data 0
for {set i 0} {$i < 100} {incr i} {
lappend data $i
}
set sampleSize 10
set numberSamples 20
set samples [::math::statistics::bootstrap $data $sampleSize $numberSamples]
set result 1
if { [llength $samples] != $numberSamples } {
set result 0
}
foreach sample $samples {
if { [llength $sample] != $sampleSize } {
set result 0
}
}
set result
} -result 1
#
# Tests for Wasserstein distance and KL divergence
#
# Tests for error conditions
#
test "Wasserstein-1.1" "Error: lengths differ" -body {
set distance [::math::statistics::wasserstein-distance {0 1} {1 0 0 0}]
} -returnCodes 1 -result {Lengths of the probability histograms must be the same}
#
# Check the symmetry for arbitrary histograms
#
test "Wasserstein-1.2" "Test symmetry" -body {
set okay 1
for {set i 0} {$i < 10} {incr i} {
set histogram1 {}
set histogram2 {}
for {set j 0} {$j < 3*($i+1)} {incr j} {
lappend histogram1 [expr {rand()}]
lappend histogram2 [expr {rand()}]
}
set distance1 [::math::statistics::wasserstein-distance $histogram1 $histogram2]
set distance2 [::math::statistics::wasserstein-distance $histogram2 $histogram1]
if { abs($distance1-$distance2) > 1.0e-6 } {
set okay 0
}
}
return $okay
} -result 1
#
# Check the non-negativity for arbitrary histograms
#
test "Wasserstein-1.3" "Test non-negativity" -body {
set okay 1
for {set i 0} {$i < 10} {incr i} {
set histogram1 {}
set histogram2 {}
for {set j 0} {$j < 3*($i+1)} {incr j} {
lappend histogram1 [expr {rand()}]
lappend histogram2 [expr {rand()}]
}
set distance1 [::math::statistics::wasserstein-distance $histogram1 $histogram2]
if { $distance1 < 0.0 } {
set okay 0
}
}
return $okay
} -result 1
#
# Check the non-normalised histograms
#
test "Wasserstein-1.4" "Test non-normalised histograms" -match tolerant -body {
return [list [::math::statistics::wasserstein-distance {2 0 0} {0 0 0.5}] \
[::math::statistics::wasserstein-distance {1 0 0} {0 0 0.25}] ]
} -result {2.0 2.0}
#
# Check arbitrarily extended histograms
#
test "Wasserstein-1.5" "Test extended histograms" -match tolerant -body {
return [list [::math::statistics::wasserstein-distance {1 0 0} {0 0 1}] \
[::math::statistics::wasserstein-distance {0 0 1 0 0} {0 0 0 0 1}] \
[::math::statistics::wasserstein-distance {1 0 0 0 0} {0 0 1 0 0}] ]
} -result {2.0 2.0 2.0}
#
# Check numerical results
#
test "Wasserstein-1.6" "Test numerical results" -match tolerant -body {
return [list [::math::statistics::wasserstein-distance {1 0 0} {0 0.5 0.5}] \
[::math::statistics::wasserstein-distance {1 0 0 0 0} {0 0 0 0.5 0.5}] \
[::math::statistics::wasserstein-distance {1 0 0 0 0} {0 0.25 0.25 0.25 0.25}] ]
} -result {1.5 3.5 2.5}
#
# Tests for error conditions
#
test "KL-divergence-1.1" "Error: lengths differ" -body {
set distance [::math::statistics::kl-divergence {0 1} {1 0 0 0}]
} -returnCodes 1 -result {Lengths of the two probability histograms must be the same}
test "KL-divergence-1.2" "Error: unmatched zeroes" -body {
set distance [::math::statistics::kl-divergence {0.3 0.3 0.4} {1 0 0}]
} -returnCodes 1 -result {Second probability histogram contains unmatched zeroes}
test "KL-divergence-1.3" "Matched zeroes should be accepted" -match tolerant -body {
set distance [::math::statistics::kl-divergence {0.3 0.3 0.4 0} {0.7 0.2 0.1 0}]
} -returnCodes 0 -result 0.42196792
#
# Tests for equal histograms (not all normalised)
#
test "KL-divergence-1.4" "Equal histograms give zero divergence" -body {
set distance [::math::statistics::kl-divergence {0.3 0.3 0.4} {0.3 0.3 0.4}]
} -result 0.0
test "KL-divergence-1.5" "Non-normalised but equal histograms give zero divergence" -body {
set distance [::math::statistics::kl-divergence {0.3 0.3 0.4} {0.6 0.6 0.8}]
} -result 0.0
#
# Numerical tests - note: the expected values were taken from the implementation
# No independent source found
#
test "KL-divergence-1.6" "Shifted histograms" -match tolerant -body {
set distance [::math::statistics::kl-divergence {1.0e-8 0.3 0.3 0.3 0.1} {0.3 0.3 0.3 0.1 1.0e-8}]
} -result 1.9413931
test "KL-divergence-1.7" "Arbitrary histograms" -match tolerant -body {
set distance [::math::statistics::kl-divergence {0.4 0.2 0.3 0.1} {0.1 0.1 0.7 0.1}]
} -result 0.4389578
#
# Tests for logistic regression
#
set xdata {0.50 0.75 1.00 1.25 1.50 1.75 1.75 2.00 2.25 2.50 2.75 3.00 3.25 3.50 4.00 4.25 4.50 4.75 5.00 5.50}
set ydata {0 0 0 0 0 0 1 0 1 0 1 0 1 0 1 1 1 1 1 1 }
test "Logit-regression-1.0" "Logistic regression - coefficients" -match tolerant -body {
set coeffs [::math::statistics::logistic-model $xdata $ydata]
} -result {-4.07679035286303 1.5045982920571572}
test "Logit-regression-1.1" "Logistic regression - probabilities" -match tolerant -body {
set coeffs [::math::statistics::logistic-model $xdata $ydata]
set probabilities {}
foreach x {1 2 3 4 5} {
lappend probabilities [::math::statistics::logistic-probability $coeffs $x]
}
return $probabilities
} -result {0.07094967663389527 0.2558609520815849 0.6075450376084848 0.8745281239093334 0.9691176473773739}
# End of test cases
testsuiteCleanup
|