quickstats 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ac2e3d968dc03d8cb72302f87223688d0d5a09bb
4
- data.tar.gz: bfafe2b7c3ead6fcd1adecfd1fa4251254f567e1
3
+ metadata.gz: f2547d05b38fa8d8820572ba11508cd563789eef
4
+ data.tar.gz: 837718c9b373634a5b4a66c214402f1c00f4414c
5
5
  SHA512:
6
- metadata.gz: b2d9738f3f324d4af82b7f63ff83579d914e069a0896be2aab85b488534fa5a124cfffcd4f2ac54d70c430f8cdc2252c53f76ff1da0acaee0a4c7f56f0763e36
7
- data.tar.gz: d4bbb8f80d04754db907f3fe3138ff2c7b5c8c98719113d572a841657412e693383866033c0f2ff4362a0463d140635bb38e769e08df780172272917737baffa
6
+ metadata.gz: dd983feb6859c1d233fad61a82af33d9b5d30db587373383d47396adba50940b29da9fd935f92481fb7065160451f0d4cd03ff576f77be49a2f0050fa9ce2eb2
7
+ data.tar.gz: 0c35ab0c1a82464499adb9ae85d10301e21b43830ff07bc842cfa3f5315554d6869160713c593510f63aadf38ea1dd83a8aafd747d757d5399bd7b95cbf82f5b
data/lib/quickstats.rb CHANGED
@@ -1,20 +1,19 @@
1
1
  #!/usr/bin/env ruby -w
2
2
 
3
3
  # Computationally stable and efficient basic descriptive statistics.
4
- # This class uses Kalman Filter updating to tally sample mean and
5
- # sample variance, along with min, max, and sample size. Standard
6
- # deviation and standard error are calculated on demand.
4
+ # This class uses Kalman Filter updating to tally sample mean and sum
5
+ # of squares, along with min, max, and sample size. Sample variance,
6
+ # standard deviation and standard error are calculated on demand.
7
7
  #
8
8
  # Author:: Paul J Sanchez (mailto:pjs@alum.mit.edu)
9
9
  # Copyright:: Copyright (c) Paul J Sanchez
10
10
  # License:: LGPL
11
11
  #
12
12
  class QuickStats
13
- attr_reader :n, :sample_mean, :sample_variance, :min, :max
13
+ attr_reader :n, :sample_mean, :min, :max
14
14
 
15
15
  alias average sample_mean
16
16
  alias avg sample_mean
17
- alias var sample_variance
18
17
  alias sample_size n
19
18
 
20
19
  # Initialize state vars in a new QuickStats object.
@@ -28,42 +27,42 @@ class QuickStats
28
27
 
29
28
  # Reset all state vars to initial values.
30
29
  #
31
- # - n = 0
30
+ # - sum_sqrs = n = 0
32
31
  # - sample_mean = sample_variance = min = max = NaN
33
32
  #
34
33
  # *Returns*::
35
34
  # - a reference to the QuickStats object.
36
35
  def reset
37
- @n = 0
38
- @sample_variance = @sample_mean = @max = @min = (1.0 / 0.0) / (1.0 / 0.0)
36
+ @sum_sqrs = @n = 0
37
+ @sample_mean = @max = @min = Float::NAN
39
38
  self
40
39
  end
41
40
 
42
- # Update the sample size, sample mean, sample variance, min, and max given
41
+ # Update the sample size, sample mean, sum of squares, min, and max given
43
42
  # a new observation. All but the sample size are maintained as floating point.
44
43
  #
45
44
  # *Arguments*::
46
45
  # - +datum+ -> the new observation. Must be numeric
47
46
  # *Returns*::
48
- # - the updated sample mean.
47
+ # - the number of observations
49
48
  # *Raises*::
50
49
  # - RuntimeError if datum is non-numeric
51
50
  #
52
51
  def new_obs(datum)
53
- raise "Observations must be numeric" unless datum.is_a? Numeric
52
+ fail 'Observations must be numeric' unless datum.is_a? Numeric
54
53
  x = datum.to_f
55
- @n += 1
56
- if (@n > 1)
57
- @max = x if x > @max
58
- @min = x if x < @min
59
- x -= @sample_mean
60
- @sample_variance *= (@n - 2).to_f / (@n - 1).to_f
61
- @sample_variance += x * x / @n
62
- @sample_mean += x / @n
54
+ @max = x unless x <= @max
55
+ @min = x unless x >= @min
56
+ if @n > 0
57
+ delta = x - @sample_mean
58
+ @n += 1
59
+ @sample_mean += delta / n
60
+ @sum_sqrs += delta * (x - @sample_mean)
63
61
  else
64
- @sample_variance = 0.0
65
- @sample_mean = @max = @min = x
62
+ @sample_mean = x
63
+ @n += 1
66
64
  end
65
+ @n
67
66
  end
68
67
 
69
68
  # Update the statistics with all elements of an enumerable set.
@@ -74,18 +73,29 @@ class QuickStats
74
73
  # *Returns*::
75
74
  # - the enumerable set reference.
76
75
  def add_set(enumerable_set)
77
- enumerable_set.each {|x| new_obs x}
76
+ enumerable_set.each { |x| new_obs x }
78
77
  end
79
78
 
80
79
  alias add_all add_set
81
80
 
81
+ # Calculates the sample variance on demand.
82
+ #
83
+ # *Returns*::
84
+ # - the sample variance of the data, or NaN if this is a
85
+ # new or just-reset QuickStats object.
86
+ def sample_variance
87
+ @n > 1 ? @sum_sqrs / (@n - 1) : Float::NAN
88
+ end
89
+
90
+ alias var sample_variance
91
+
82
92
  # Calculates the square root of the sample variance on demand.
83
93
  #
84
94
  # *Returns*::
85
95
  # - the sample standard deviation of the data, or NaN if this is a
86
96
  # new or just-reset QuickStats object.
87
97
  def standard_deviation
88
- Math::sqrt @sample_variance
98
+ Math.sqrt sample_variance
89
99
  end
90
100
 
91
101
  alias std_dev standard_deviation
@@ -96,9 +106,8 @@ class QuickStats
96
106
  # - the sample standard error of the data, or NaN if this is a
97
107
  # new or just-reset QuickStats object.
98
108
  def standard_error
99
- Math.sqrt(@sample_variance / @n)
109
+ Math.sqrt(sample_variance / @n)
100
110
  end
101
111
 
102
112
  alias std_err standard_error
103
-
104
113
  end
data/quickstats.gemspec CHANGED
@@ -1,13 +1,13 @@
1
1
  # -*- ruby -*-
2
- _VERSION = "1.0.0"
2
+ _VERSION = "2.0.0"
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "quickstats"
6
6
  s.version = _VERSION
7
- s.date = "2013-03-09"
7
+ s.date = "2016-07-12"
8
8
  s.summary = "Computationally stable and efficient basic descriptive statistics."
9
9
  s.email = "pjs@alum.mit.edu"
10
- s.description = "This class uses Kalman Filter updating to tally sample mean and sample variance, along with min, max, and sample size. Standard deviation and standard error are calculated on demand."
10
+ s.description = "This class uses Kalman Filter updating to tally sample mean and sum of squared deviations from the average, along with min, max, and sample size. Sample variance, standard deviation, and standard error are calculated on demand."
11
11
  s.author = "Paul J Sanchez"
12
12
  s.files = %w[
13
13
  quickstats.gemspec
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby -w
2
2
 
3
3
  require 'test/unit'
4
- require 'quickstats'
4
+ require_relative '../lib/quickstats.rb'
5
5
 
6
6
  class QuickStats_test < Test::Unit::TestCase
7
7
  def test_quickstats
@@ -20,6 +20,13 @@ class QuickStats_test < Test::Unit::TestCase
20
20
  assert_equal 41.0, qs.min
21
21
  assert_equal 43.0, qs.max
22
22
  assert_equal 1.0, qs.standard_error
23
+ qs.new_obs 42
24
+ assert_equal 3, qs.n
25
+ assert_equal 42.0, qs.sample_mean
26
+ assert_equal 1.0, qs.sample_variance
27
+ assert_equal 1.0, qs.standard_deviation
28
+ assert_equal 41.0, qs.min
29
+ assert_equal 43.0, qs.max
23
30
 
24
31
  qs.reset
25
32
  assert_equal 0, qs.n
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quickstats
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul J Sanchez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-03-09 00:00:00.000000000 Z
11
+ date: 2016-07-12 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: This class uses Kalman Filter updating to tally sample mean and sample
14
- variance, along with min, max, and sample size. Standard deviation and standard
15
- error are calculated on demand.
13
+ description: This class uses Kalman Filter updating to tally sample mean and sum of
14
+ squared deviations from the average, along with min, max, and sample size. Sample
15
+ variance, standard deviation, and standard error are calculated on demand.
16
16
  email: pjs@alum.mit.edu
17
17
  executables: []
18
18
  extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
- - quickstats.gemspec
21
+ - Rakefile
22
22
  - lgpl.txt
23
23
  - lib/quickstats.rb
24
- - Rakefile
24
+ - quickstats.gemspec
25
25
  - test/test_quickstats.rb
26
26
  homepage:
27
27
  licenses:
@@ -33,17 +33,17 @@ require_paths:
33
33
  - lib
34
34
  required_ruby_version: !ruby/object:Gem::Requirement
35
35
  requirements:
36
- - - '>='
36
+ - - ">="
37
37
  - !ruby/object:Gem::Version
38
38
  version: 1.8.1
39
39
  required_rubygems_version: !ruby/object:Gem::Requirement
40
40
  requirements:
41
- - - '>='
41
+ - - ">="
42
42
  - !ruby/object:Gem::Version
43
43
  version: '0'
44
44
  requirements: []
45
45
  rubyforge_project:
46
- rubygems_version: 2.0.2
46
+ rubygems_version: 2.4.5.1
47
47
  signing_key:
48
48
  specification_version: 4
49
49
  summary: Computationally stable and efficient basic descriptive statistics.