-- -*- mode: eiffel -*-
-- $Id: moments.se,v 1.2 2001/05/23 18:29:33 doug Exp $
-- http://www.bagley.org/~doug/shootout/
-- from Steve Thompson


-- <LOC-OFF>
indexing
   description: "This class performs the statistical moments test" 
   author : Steve Thompson
   email  : "Steve_Thompson@prodigy.net"
   date   : February 18, 2001
   compile: "compile -clean -boost -no_split -O3 main.e -o main"
   run    : "main < Input"
-- <LOC-ON>

class MOMENTS

creation make

feature -- Creation

   make is
      local
     index: INTEGER
     number: INTEGER
     mid: INTEGER
     deviation: DOUBLE
     sorter: COLLECTION_SORTER[INTEGER]
      do
     !!values.make(0,499)
     read_values
     mean := sum / values.count
     
     from index := values.lower until index > values.upper loop
        number := values @ index
        deviation := number - mean
        average_deviation := average_deviation + deviation.abs
        variance := variance + deviation ^ 2
        skew := skew + deviation ^ 3
        kurtosis := kurtosis + deviation ^ 4
        index := index + 1
     end
     
     average_deviation := average_deviation / values.count
     variance := variance / (values.count - 1)
     standard_deviation := variance.sqrt --math.sqrt(variance)
     if variance > 0.0 then
        skew := skew / (values.count * variance * standard_deviation)
        kurtosis := kurtosis / (values.count * variance * variance) - 3.0                
     end
     sorter.sort(values)
     mid := values.count // 2
     if (values.count \\ 2) = 0 then
        median := (values.item(mid) + values.item(mid - 1)) / 2
     else
        median := values @ mid
     end
     
     print ("n:                  " + values.count.to_string + "%N")
     print ("median:             " + median.to_string + "%N")
     print ("mean:               " + mean.to_string + "%N")
     print ("average_deviation:  " + average_deviation.to_string + "%N")
     print ("standard_deviation: " + standard_deviation.to_string + "%N")
     print ("variance:           " + variance.to_string + "%N")
     print ("skew:               " + skew.to_string + "%N")
     print ("kurtosis:           " + kurtosis.to_string + "%N")
      end -- make
   
feature -- Queries
   
   sum: INTEGER
   
   mean,
   median,
   average_deviation,
   standard_deviation,
   variance,
   skew,
   kurtosis: DOUBLE     
   
   values: ARRAY[INTEGER]
     -- Values read from stdin
   
   read_values is
      local
     value: INTEGER
     index: INTEGER
      do

     from
        index := values.lower 
        std_input.read_line 
     until std_input.end_of_input loop
        value := std_input.last_string.to_integer
        values.force(value, index)
        std_input.read_line
        sum := sum + value
        index := index + 1
     end 

      end -- get_stdin
   
end