sapor 0.1b1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +7 -0
  2. data/Area Class Diagram.dia +0 -0
  3. data/Area Class Diagram.png +0 -0
  4. data/Class Diagram.dia +0 -0
  5. data/Class Diagram.png +0 -0
  6. data/Examples.md +361 -0
  7. data/LICENSE +674 -0
  8. data/README.md +70 -0
  9. data/Rakefile +18 -0
  10. data/Technical Documentation.md +14 -0
  11. data/bin/create_installation_package.sh +49 -0
  12. data/bin/install.sh +45 -0
  13. data/bin/sapor.rb +22 -0
  14. data/bin/sapor.sh +105 -0
  15. data/lib/sapor.rb +44 -0
  16. data/lib/sapor/binomials_cache.rb +45 -0
  17. data/lib/sapor/combinations_distribution.rb +180 -0
  18. data/lib/sapor/dichotomies.rb +98 -0
  19. data/lib/sapor/dichotomy.rb +138 -0
  20. data/lib/sapor/first_past_the_post.rb +78 -0
  21. data/lib/sapor/leveled_proportional.rb +64 -0
  22. data/lib/sapor/log4r_logger.rb +49 -0
  23. data/lib/sapor/log_facade.rb +40 -0
  24. data/lib/sapor/number_formatter.rb +45 -0
  25. data/lib/sapor/poll.rb +137 -0
  26. data/lib/sapor/polychotomy.rb +359 -0
  27. data/lib/sapor/proportional.rb +128 -0
  28. data/lib/sapor/pseudorandom_multirange_enumerator.rb +87 -0
  29. data/lib/sapor/regional_data/area.rb +80 -0
  30. data/lib/sapor/regional_data/catalonia-2012-2015.psv +100 -0
  31. data/lib/sapor/regional_data/catalonia-2012.psv +87 -0
  32. data/lib/sapor/regional_data/catalonia.rb +90 -0
  33. data/lib/sapor/regional_data/norway.rb +408 -0
  34. data/lib/sapor/regional_data/united_kingdom.rb +1075 -0
  35. data/lib/sapor/regional_data/utopia.rb +66 -0
  36. data/sapor.gemspec +35 -0
  37. data/spec/integration/area_spec.rb +28 -0
  38. data/spec/integration/poll_spec.rb +107 -0
  39. data/spec/integration/sample.poll +7 -0
  40. data/spec/spec_helper.rb +31 -0
  41. data/spec/unit/area_spec.rb +115 -0
  42. data/spec/unit/binomials_cache_spec.rb +34 -0
  43. data/spec/unit/catalonia_spec.rb +82 -0
  44. data/spec/unit/combinations_distribution_spec.rb +241 -0
  45. data/spec/unit/denominators_spec.rb +34 -0
  46. data/spec/unit/dichotomies_spec.rb +154 -0
  47. data/spec/unit/dichotomy_spec.rb +320 -0
  48. data/spec/unit/first_past_the_post_spec.rb +53 -0
  49. data/spec/unit/leveled_proportional_spec.rb +51 -0
  50. data/spec/unit/norway_spec.rb +47 -0
  51. data/spec/unit/number_formatter_spec.rb +173 -0
  52. data/spec/unit/poll_spec.rb +105 -0
  53. data/spec/unit/polychotomy_spec.rb +332 -0
  54. data/spec/unit/proportional_spec.rb +86 -0
  55. data/spec/unit/pseudorandom_multirange_enumerator_spec.rb +82 -0
  56. metadata +119 -0
@@ -0,0 +1,86 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Statistical Analysis of Polling Results (SAPoR)
4
+ # Copyright (C) 2014 Filip van Laenen <f.a.vanlaenen@ieee.org>
5
+ #
6
+ # This file is part of SAPoR.
7
+ #
8
+ # SAPoR is free software: you can redistribute it and/or modify it under the
9
+ # terms of the GNU General Public License as published by the Free Software
10
+ # Foundation, either version 3 of the License, or (at your option) any later
11
+ # version.
12
+ #
13
+ # SAPoR is distributed in the hope that it will be useful, but WITHOUT ANY
14
+ # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15
+ # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16
+ #
17
+ # You can find a copy of the GNU General Public License in /doc/gpl.txt
18
+ #
19
+
20
+ SAMPLE_RGB_ELECTION_RESULT = { 'Red' => 120, 'Green' => 120, 'Blue' => 100 }
21
+
22
+ SAMPLE_DETAILED_ELECTION_RESULT = { 'North' => { 'Red' => 50, 'Green' => 70
23
+ },
24
+ 'South' => { 'Red' => 70, 'Green' => 50,
25
+ 'Blue' => 100 } }
26
+
27
+ SAMPLE_SEAT_DISTRIBUTION = { 'North' => 3, 'South' => 5 }
28
+
29
+ PROPORTIONAL = Sapor::Proportional.new(SAMPLE_RGB_ELECTION_RESULT,
30
+ SAMPLE_DETAILED_ELECTION_RESULT,
31
+ SAMPLE_SEAT_DISTRIBUTION,
32
+ Sapor::DhondtDenominators)
33
+
34
+ describe Sapor::Proportional, '#project' do
35
+ # Seat distribution:
36
+ # North: Green 1 70, Red 1 50, Green 2 35, (Red 2 25)
37
+ # South: Blue 1 100, Red 1 70, Green 1 50, Blue 2 50, Red 2 35, (Blue 3 33)
38
+ it 'projects same result as last result if fed with last election result' do
39
+ projection = PROPORTIONAL.project(SAMPLE_RGB_ELECTION_RESULT)
40
+ expect(projection['Red']).to eq(3)
41
+ expect(projection['Green']).to eq(3)
42
+ expect(projection['Blue']).to eq(2)
43
+ end
44
+
45
+ # Seat distribution:
46
+ # North: Green 1 62, Red 1 44, Green 2 31, (Red 2 22)
47
+ # South: Blue 1 128, Red 1 62, Green 1 44, Blue 2 64, Blue 3 42, (Red 2 31)
48
+ it 'extrapolates the seat distribution according to the new result' do
49
+ new_result = { 'Red' => 100, 'Green' => 100, 'Blue' => 120 }
50
+ projection = PROPORTIONAL.project(new_result)
51
+ expect(projection['Red']).to eq(2)
52
+ expect(projection['Green']).to eq(3)
53
+ expect(projection['Blue']).to eq(3)
54
+ end
55
+
56
+ # Seat distribution:
57
+ # North: Green 1 70, Red 1 50, Green 2 35 (Red 2 25, Green 3 23)
58
+ # South: Blue 1 100, Red 1 70, Blue 2 50, Red 2 35, Blue 3 33, (Blue 4 25,
59
+ # Red 3 23, Green below threshold)
60
+ it 'excludes parties below the threshold' do
61
+ proportional = Sapor::Proportional.new(SAMPLE_RGB_ELECTION_RESULT,
62
+ SAMPLE_DETAILED_ELECTION_RESULT,
63
+ SAMPLE_SEAT_DISTRIBUTION,
64
+ Sapor::DhondtDenominators,
65
+ 51.to_f / 220)
66
+ projection = proportional.project(SAMPLE_RGB_ELECTION_RESULT)
67
+ expect(projection['Red']).to eq(3)
68
+ expect(projection['Green']).to eq(2)
69
+ expect(projection['Blue']).to eq(3)
70
+ end
71
+
72
+ # Seat distribution:
73
+ # North: Green 1 70, Red 1 50, Green 2 35, (Red 2 25)
74
+ # South: Blue 1 100, Red 1 70, Green 1 50, Blue 2 50, Red 2 35, (Blue 3 33)
75
+ it 'includes parties at the threshold' do
76
+ proportional = Sapor::Proportional.new(SAMPLE_RGB_ELECTION_RESULT,
77
+ SAMPLE_DETAILED_ELECTION_RESULT,
78
+ SAMPLE_SEAT_DISTRIBUTION,
79
+ Sapor::DhondtDenominators,
80
+ 50.to_f / 220)
81
+ projection = proportional.project(SAMPLE_RGB_ELECTION_RESULT)
82
+ expect(projection['Red']).to eq(3)
83
+ expect(projection['Green']).to eq(3)
84
+ expect(projection['Blue']).to eq(2)
85
+ end
86
+ end
@@ -0,0 +1,82 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Statistical Analysis of Polling Results (SAPoR)
4
+ # Copyright (C) 2014 Filip van Laenen <f.a.vanlaenen@ieee.org>
5
+ #
6
+ # This file is part of SAPoR.
7
+ #
8
+ # SAPoR is free software: you can redistribute it and/or modify it under the
9
+ # terms of the GNU General Public License as published by the Free Software
10
+ # Foundation, either version 3 of the License, or (at your option) any later
11
+ # version.
12
+ #
13
+ # SAPoR is distributed in the hope that it will be useful, but WITHOUT ANY
14
+ # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15
+ # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16
+ #
17
+ # You can find a copy of the GNU General Public License in /doc/gpl.txt
18
+ #
19
+
20
+ require 'spec_helper'
21
+
22
+ describe Sapor::PseudoRandomMultiRangeEnumerator, '#each' do
23
+ it 'sets the size correctly for one dimension' do
24
+ enum = Sapor::PseudoRandomMultiRangeEnumerator.new([5]).each
25
+ expect(enum.size).to eq(5)
26
+ end
27
+
28
+ it 'sets the size correctly for two dimensions' do
29
+ enum = Sapor::PseudoRandomMultiRangeEnumerator.new([5, 3]).each
30
+ expect(enum.size).to eq(15)
31
+ end
32
+
33
+ it 'returns a simple enumeration for one dimension' do
34
+ enum = Sapor::PseudoRandomMultiRangeEnumerator.new([5]).each
35
+ expect(enum.to_a).to eq([[0], [1], [2], [3], [4]])
36
+ end
37
+
38
+ it 'returns [0] as the first element for one dimension' do
39
+ enum = Sapor::PseudoRandomMultiRangeEnumerator.new([5]).each
40
+ expect(enum.next).to eq([0])
41
+ end
42
+
43
+ it 'returns [0, 0] as the first element for two dimensions' do
44
+ enum = Sapor::PseudoRandomMultiRangeEnumerator.new([5, 3]).each
45
+ expect(enum.next).to eq([0, 0])
46
+ end
47
+
48
+ it 'returns [1] as the second element for one dimension' do
49
+ enum = Sapor::PseudoRandomMultiRangeEnumerator.new([5]).each
50
+ enum.next
51
+ expect(enum.next).to eq([1])
52
+ end
53
+
54
+ it 'returns [1, 2] as the second element of a 3×5 hyper-rectangle' do
55
+ enum = Sapor::PseudoRandomMultiRangeEnumerator.new([3, 5]).each
56
+ enum.next
57
+ expect(enum.next).to eq([1, 2])
58
+ end
59
+
60
+ it 'returns [2, 1] as the second element of a 5×3 hyper-rectangle' do
61
+ enum = Sapor::PseudoRandomMultiRangeEnumerator.new([5, 3]).each
62
+ enum.next
63
+ expect(enum.next).to eq([2, 1])
64
+ end
65
+
66
+ it 'returns [1, 3] as the second element of a 2×5 hyper-rectangle' do
67
+ enum = Sapor::PseudoRandomMultiRangeEnumerator.new([2, 5]).each
68
+ enum.next
69
+ expect(enum.next).to eq([1, 3])
70
+ end
71
+
72
+ it 'returns [1, 2, 5] as the second element of a 9×9×9 hyper-rectangle' do
73
+ enum = Sapor::PseudoRandomMultiRangeEnumerator.new([9, 9, 9]).each
74
+ enum.next
75
+ expect(enum.next).to eq([1, 2, 5])
76
+ end
77
+
78
+ it "raises an ArgumentError when it can't construct incrementers" do
79
+ expect { Sapor::PseudoRandomMultiRangeEnumerator.new([3, 2]) }.to \
80
+ raise_error(ArgumentError, 'Could not construct suitable incrementers.')
81
+ end
82
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sapor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1b1
5
+ platform: ruby
6
+ authors:
7
+ - Filip van Laenen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-01 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: SAPoR
14
+ email:
15
+ - f.a.vanlaenen@ieee.org
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files:
19
+ - LICENSE
20
+ - README.md
21
+ files:
22
+ - Area Class Diagram.dia
23
+ - Area Class Diagram.png
24
+ - Class Diagram.dia
25
+ - Class Diagram.png
26
+ - Examples.md
27
+ - LICENSE
28
+ - README.md
29
+ - Rakefile
30
+ - Technical Documentation.md
31
+ - bin/create_installation_package.sh
32
+ - bin/install.sh
33
+ - bin/sapor.rb
34
+ - bin/sapor.sh
35
+ - lib/sapor.rb
36
+ - lib/sapor/binomials_cache.rb
37
+ - lib/sapor/combinations_distribution.rb
38
+ - lib/sapor/dichotomies.rb
39
+ - lib/sapor/dichotomy.rb
40
+ - lib/sapor/first_past_the_post.rb
41
+ - lib/sapor/leveled_proportional.rb
42
+ - lib/sapor/log4r_logger.rb
43
+ - lib/sapor/log_facade.rb
44
+ - lib/sapor/number_formatter.rb
45
+ - lib/sapor/poll.rb
46
+ - lib/sapor/polychotomy.rb
47
+ - lib/sapor/proportional.rb
48
+ - lib/sapor/pseudorandom_multirange_enumerator.rb
49
+ - lib/sapor/regional_data/area.rb
50
+ - lib/sapor/regional_data/catalonia-2012-2015.psv
51
+ - lib/sapor/regional_data/catalonia-2012.psv
52
+ - lib/sapor/regional_data/catalonia.rb
53
+ - lib/sapor/regional_data/norway.rb
54
+ - lib/sapor/regional_data/united_kingdom.rb
55
+ - lib/sapor/regional_data/utopia.rb
56
+ - sapor.gemspec
57
+ - spec/integration/area_spec.rb
58
+ - spec/integration/poll_spec.rb
59
+ - spec/integration/sample.poll
60
+ - spec/spec_helper.rb
61
+ - spec/unit/area_spec.rb
62
+ - spec/unit/binomials_cache_spec.rb
63
+ - spec/unit/catalonia_spec.rb
64
+ - spec/unit/combinations_distribution_spec.rb
65
+ - spec/unit/denominators_spec.rb
66
+ - spec/unit/dichotomies_spec.rb
67
+ - spec/unit/dichotomy_spec.rb
68
+ - spec/unit/first_past_the_post_spec.rb
69
+ - spec/unit/leveled_proportional_spec.rb
70
+ - spec/unit/norway_spec.rb
71
+ - spec/unit/number_formatter_spec.rb
72
+ - spec/unit/poll_spec.rb
73
+ - spec/unit/polychotomy_spec.rb
74
+ - spec/unit/proportional_spec.rb
75
+ - spec/unit/pseudorandom_multirange_enumerator_spec.rb
76
+ homepage: https://github.com/filipvanlaenen/sapor
77
+ licenses:
78
+ - GPL
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">"
92
+ - !ruby/object:Gem::Version
93
+ version: 1.3.1
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.2.2
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Statistical Analysis of Polling Results
100
+ test_files:
101
+ - spec/integration/area_spec.rb
102
+ - spec/integration/poll_spec.rb
103
+ - spec/integration/sample.poll
104
+ - spec/spec_helper.rb
105
+ - spec/unit/area_spec.rb
106
+ - spec/unit/binomials_cache_spec.rb
107
+ - spec/unit/catalonia_spec.rb
108
+ - spec/unit/combinations_distribution_spec.rb
109
+ - spec/unit/denominators_spec.rb
110
+ - spec/unit/dichotomies_spec.rb
111
+ - spec/unit/dichotomy_spec.rb
112
+ - spec/unit/first_past_the_post_spec.rb
113
+ - spec/unit/leveled_proportional_spec.rb
114
+ - spec/unit/norway_spec.rb
115
+ - spec/unit/number_formatter_spec.rb
116
+ - spec/unit/poll_spec.rb
117
+ - spec/unit/polychotomy_spec.rb
118
+ - spec/unit/proportional_spec.rb
119
+ - spec/unit/pseudorandom_multirange_enumerator_spec.rb