continuance 0.0.2 → 0.0.3

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: 9b2ff83fc36a7f79a356a419b11a1cdfb3fdeb62
4
- data.tar.gz: 851972478e16c62e9a33e41cdb11c4750394df50
3
+ metadata.gz: 0f85dde2c72f37d014fbdbf60a9d865f2d5c4145
4
+ data.tar.gz: 5eb50ea0f31770157c7f869c1cec32f25c91c544
5
5
  SHA512:
6
- metadata.gz: 9ed917c455c20968392edc32465c8a53403cb513c43f38476c2095467d701381d670ca71a0ced2b2277251b4b86602b3ce6b74deb13ac5cfae2993eb89551f8c
7
- data.tar.gz: 041c3ce7a2242e60e226ee3362526a353e494c71c20c5754b40549d483a42b35df5110a61b2f80675118a09d12e5b8ffeb22b815e90dff14a0eec81f575b1968
6
+ metadata.gz: 63585a59e6d9dbbfd7d71e174ef2ab0d6cebc1019f7b1deb52217004eda3050a319496fef6ed0bde801e8189dec3400b7e8ae93fb3fdc2ad32bfd891227de88e
7
+ data.tar.gz: 6cddbdec9d506974f3d12b9703b3046dd6943bfebe9a719b619206efaa3893812cbfdedb3672306dbd2fde67eff627a189a58d80fea0558c9e54ec9c1525b73b
data/README.md CHANGED
@@ -56,6 +56,8 @@ Continuance::Duration.new(1, 0, 5, 0) + Continuance::Duration.new(0, 0, 6, 32)
56
56
 
57
57
  ## Collection Operations
58
58
 
59
+ ### Total & average
60
+
59
61
  At present Continuance provides `total` and `average` aggregate operations on a collection of duration objects. The Ruby code below demonstrates how to use these collection operations.
60
62
 
61
63
  ```ruby
@@ -66,6 +68,14 @@ average = durations.average
66
68
  total = durations.total
67
69
  ```
68
70
 
69
- The average method returns a Duration object, where as total returns the total number of seconds as a floating point value, this could change in the future. The order of complexity of both operations is `O(n)`.
71
+ The average method returns a Duration object, where as total returns the total number of seconds as a floating point value, this could change in the future. The order of complexity of both operations is `O(n)`. In mathematics average is also referred to as the arithmetic mean and median. The average is aliased as arithmetic mean and median.
72
+
73
+ ### Min & Max
74
+
75
+ The library also supports retrieving the minimum and maximum value for a collection of durations. Like the total and average operations, the order of complexity of the min and max operation is `O(n)`.
76
+
77
+ ### Variance & Standard Deviation
78
+
79
+ The library also supports retrieving the variance and standard deviation for a collection of durations.
70
80
 
71
81
  Report bugs on GitHub.
@@ -18,6 +18,11 @@ module Continuance
18
18
  Duration.create(time, '%H:%M:%S:%L')
19
19
  end
20
20
 
21
+ # In mathematics average is also known by the terms median and
22
+ # arithmetic mean
23
+ alias_method :arithmetic_mean, :average
24
+ alias_method :median, :average
25
+
21
26
  # Provides a sum of all the durations
22
27
  def total
23
28
  items.inject(0.0) do |sum, duration|
@@ -25,6 +30,33 @@ module Continuance
25
30
  end
26
31
  end
27
32
 
33
+ # Tells us what the smallest duration is of the given ones
34
+ def min
35
+ items.map(&:to_f).min
36
+ end
37
+
38
+ # Tells us what the largest duration is of the given ones
39
+ def max
40
+ items.map(&:to_f).max
41
+ end
42
+
43
+ # TODO: Add a variance and standard deviation methods to this class
44
+ def variance
45
+ if items.empty?
46
+ 0
47
+ else
48
+ mean = average # Cache the average for future use
49
+ diffs = items.map { |duration| duration.to_f - mean.to_f }
50
+ diff_squares = diffs.map { |diff| diff**2 }
51
+ diff_squares.inject(:+) / items.size
52
+ end
53
+ end
54
+
55
+ # The standard deviation is simply the square root of the variance
56
+ def standard_deviation
57
+ Math.sqrt(variance)
58
+ end
59
+
28
60
  private
29
61
 
30
62
  # Calculates the average of the time as a float
@@ -1,4 +1,4 @@
1
1
  # Major.Minor.Patch version numbering
2
2
  module Continuance
3
- VERSION = '0.0.2'
3
+ VERSION = '0.0.3'
4
4
  end
@@ -15,6 +15,22 @@ module Continuance
15
15
  it 'should be able to calculate an average' do
16
16
  expect(durations.average).to eq(Duration.new)
17
17
  end
18
+
19
+ it 'should be able to extract the minimum' do
20
+ expect(durations.min).to be_nil
21
+ end
22
+
23
+ it 'should be able to extract the maximum' do
24
+ expect(durations.max).to be_nil
25
+ end
26
+
27
+ it 'should be able to calculate the variance' do
28
+ expect(durations.variance).to eq(0.0)
29
+ end
30
+
31
+ it 'should be able to calculate the standard deviation' do
32
+ expect(durations.standard_deviation).to eq(0.0)
33
+ end
18
34
  end
19
35
 
20
36
  context 'with a single duration' do
@@ -28,6 +44,14 @@ module Continuance
28
44
  it 'should be able to calculate an average' do
29
45
  expect(durations.average).to eq(Duration.new(1, 1, 1, 0))
30
46
  end
47
+
48
+ it 'should be able to extract the minimum' do
49
+ expect(durations.min).to eq(Duration.new(1, 1, 1, 0))
50
+ end
51
+
52
+ it 'should be able to extract the maximum' do
53
+ expect(durations.max).to eq(Duration.new(1, 1, 1, 0))
54
+ end
31
55
  end
32
56
 
33
57
  context 'with multiple durations' do
@@ -35,7 +59,7 @@ module Continuance
35
59
  let(:duration2) { Duration.new(0, 1, 0, 0) }
36
60
  let(:duration3) { Duration.new(0, 0, 1, 0) }
37
61
  let(:duration4) { Duration.new(0, 0, 0, 1_000_000) }
38
- let(:durations) { Durations.new([duration1, duration2, duration3, duration4]) }
62
+ let(:durations) { Durations.new([duration1, duration2, duration3, duration4]) }
39
63
 
40
64
  it 'should be able to calculate the total' do
41
65
  expect(durations.total).to eq(3661.001)
@@ -44,6 +68,40 @@ module Continuance
44
68
  it 'should be able to calculate an average' do
45
69
  expect(durations.average).to eq(Duration.new(0, 15, 15, 250_250_000))
46
70
  end
71
+
72
+ it 'should be able to extract the minimum' do
73
+ expect(durations.min).to eq(Duration.new(0, 0, 0, 1_000_000))
74
+ end
75
+
76
+ it 'should be able to extract the maximum' do
77
+ expect(durations.max).to eq(Duration.new(1, 0, 0, 0))
78
+ end
79
+
80
+ it 'should alias arithmetic mean to average' do
81
+ expect(durations.arithmetic_mean).to eq(durations.average)
82
+ end
83
+
84
+ it 'should alias median to average' do
85
+ expect(durations.median).to eq(durations.average)
86
+ end
87
+ end
88
+
89
+ context 'like the six side of a dice' do
90
+ let(:side1) { Duration.new(0, 0, 1, 0) }
91
+ let(:side2) { Duration.new(0, 0, 2, 0) }
92
+ let(:side3) { Duration.new(0, 0, 3, 0) }
93
+ let(:side4) { Duration.new(0, 0, 4, 0) }
94
+ let(:side5) { Duration.new(0, 0, 5, 0) }
95
+ let(:side6) { Duration.new(0, 0, 6, 0) }
96
+ let(:sides) { Durations.new([side1, side2, side3, side4, side5, side6]) }
97
+
98
+ it 'should be able to calculate the variance' do
99
+ expect(sides.variance).to eq(2.9166666666666665)
100
+ end
101
+
102
+ it 'should be able to calculate the standard deviation' do
103
+ expect(sides.standard_deviation).to eq(1.707825127659933)
104
+ end
47
105
  end
48
106
  end
49
107
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: continuance
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shirren Premaratne
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-17 00:00:00.000000000 Z
11
+ date: 2015-06-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Continuance is a library for handling durations as opposed to time. A
14
14
  duration represents a finite period of time meaurable in hours, minutes, seconds
@@ -51,7 +51,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
51
51
  version: '0'
52
52
  requirements: []
53
53
  rubyforge_project:
54
- rubygems_version: 2.4.8
54
+ rubygems_version: 2.4.6
55
55
  signing_key:
56
56
  specification_version: 4
57
57
  summary: Continuance is a library for handling durations as opposed to time.