Never been to DZone Snippets before?

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

Statistics Usage (See related posts)

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).

   1  
   2  #!/usr/bin/tclsh
   3  # statistics-usage.tcl
   4  #
   5  # 2005 by Sascha Tayefeh
   6  #
   7  # This is a little demonstration of how to use statistics
   8  # with TCL. Here, I provide two methods: 1. Trivially, some
   9  # PROCS that perform statistic calulations. However, you
  10  # are *NOT* supposed to use these, but use rather build-in
  11  # statistics from 2. math::statistics (provided by tcllib
  12  # that must be installed).
  13  #
  14  # for further information read
  15  # http://aspn.activestate.com/ASPN/docs/ActiveTcl/tcllib/math/statistics.html
  16  #
  17  #
  18  package require Tcl 8.4
  19  package require math::statistics
  20  
  21  proc sas_sum { valist } {
  22      set summe 0.0
  23      foreach val $valist {
  24  	set summe [ expr $summe + $val ]
  25      }	
  26      return $summe
  27  }
  28  
  29  proc sas_mean { valist } {
  30      set n [ llength $valist ]
  31      set sum [ sas_sum $valist ]
  32      set mean [ expr $sum / $n ]
  33      return $mean
  34  }
  35  
  36  proc sas_variance { valist } { 
  37      set variance 0.0
  38      set mean [ sas_mean $valist ]
  39      set n [ llength $valist ]
  40      foreach val $valist {
  41  	set buff [ expr $val - $mean ] 
  42  	set buff [ expr pow ($buff,2)]
  43  	set variance [ expr $variance + $buff ]
  44      }
  45      set variance [ expr $variance / $n ]
  46      return $variance
  47  }
  48  
  49  proc sas_deviation { valist } {
  50      set variance [ sas_variance $valist ] 
  51      set deviation [ expr sqrt ($variance) ]
  52  
  53      return $deviation
  54  }
  55  
  56  #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]
  57  #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]
  58  
  59  
  60  # fills data1 with normal-distributed values: <mean> <stdev> <n>
  61  set data1 [::math::statistics::random-normal 2.4 2 10]
  62  set data2 [::math::statistics::random-normal 5.4 2 10]
  63  
  64  set mean 	[ sas_mean $data1 ]
  65  set variance 	[ sas_variance $data1 ]
  66  set deviation 	[ sas_deviation $data1 ]
  67  
  68  puts "\nFrom Custom Procs:"
  69  puts "Mean: $mean, Variance: $variance, stDev: $deviation"
  70  
  71  puts "\nFrom ::math::statistics:: (needs tcllib)"
  72  set mean [  ::math::statistics::mean $data1 ]
  73  set variance [  ::math::statistics::var $data1 ]
  74  set deviation  [  ::math::statistics::stdev $data1 ]
  75  set corr [::math::statistics::corr $data1 $data2 ]
  76  set crosscorr [::math::statistics::crosscorr $data1 $data2 ]
  77  set autocorr [::math::statistics::autocorr $data1 ]
  78  #set confi [::math::statistics::interval-mean-stdev $data1 0.95]
  79  
  80  puts "Mean: $mean, Variance: $variance, stDev: $deviation"
  81  puts "Corr: $corr"
  82  
  83  puts "Autocorr: $autocorr\nCrosscorr: $crosscorr"
  84  
  85  

You need to create an account or log in to post comments to this site.


Click here to browse all 5556 code snippets

Related Posts