DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Statistics Usage

01.17.2007
| 2143 views |
  • submit to reddit
        This is a little demonstration of how to use statistics with TCL. Here, I provide two methods: 1. Trivially, some PROCS that perform statistic calulations. However, you are *NOT* supposed to use these, but use rather build-in statistics from 2. math::statistics (provided by tcllib that must be installed).

#!/usr/bin/tclsh
# statistics-usage.tcl
#
# 2005 by Sascha Tayefeh
#
# This is a little demonstration of how to use statistics
# with TCL. Here, I provide two methods: 1. Trivially, some
# PROCS that perform statistic calulations. However, you
# are *NOT* supposed to use these, but use rather build-in
# statistics from 2. math::statistics (provided by tcllib
# that must be installed).
#
# for further information read
# http://aspn.activestate.com/ASPN/docs/ActiveTcl/tcllib/math/statistics.html
#
#
package require Tcl 8.4
package require math::statistics

proc sas_sum { valist } {
    set summe 0.0
    foreach val $valist {
	set summe [ expr $summe + $val ]
    }	
    return $summe
}

proc sas_mean { valist } {
    set n [ llength $valist ]
    set sum [ sas_sum $valist ]
    set mean [ expr $sum / $n ]
    return $mean
}

proc sas_variance { valist } { 
    set variance 0.0
    set mean [ sas_mean $valist ]
    set n [ llength $valist ]
    foreach val $valist {
	set buff [ expr $val - $mean ] 
	set buff [ expr pow ($buff,2)]
	set variance [ expr $variance + $buff ]
    }
    set variance [ expr $variance / $n ]
    return $variance
}

proc sas_deviation { valist } {
    set variance [ sas_variance $valist ] 
    set deviation [ expr sqrt ($variance) ]

    return $deviation
}

#set data1 [ list 5 2.4 5.3 2.3 4.3 2.3 3.3 4.4 5.4 3.4 5.4 2.3 1.2 3.4]
#set data2 [ list 1.9 -2.4 -5.3 2.3 4.3 2.3 2.3 4.4 5.4 3.4 4.4 2.3 1.2 3.4]


# fills data1 with normal-distributed values: <mean> <stdev> <n>
set data1 [::math::statistics::random-normal 2.4 2 10]
set data2 [::math::statistics::random-normal 5.4 2 10]

set mean 	[ sas_mean $data1 ]
set variance 	[ sas_variance $data1 ]
set deviation 	[ sas_deviation $data1 ]

puts "\nFrom Custom Procs:"
puts "Mean: $mean, Variance: $variance, stDev: $deviation"

puts "\nFrom ::math::statistics:: (needs tcllib)"
set mean [  ::math::statistics::mean $data1 ]
set variance [  ::math::statistics::var $data1 ]
set deviation  [  ::math::statistics::stdev $data1 ]
set corr [::math::statistics::corr $data1 $data2 ]
set crosscorr [::math::statistics::crosscorr $data1 $data2 ]
set autocorr [::math::statistics::autocorr $data1 ]
#set confi [::math::statistics::interval-mean-stdev $data1 0.95]

puts "Mean: $mean, Variance: $variance, stDev: $deviation"
puts "Corr: $corr"

puts "Autocorr: $autocorr\nCrosscorr: $crosscorr"