Skip to content

Commit

Permalink
few review corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
traducha committed May 23, 2022
1 parent ab4ea1e commit dc5465c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 16 deletions.
11 changes: 5 additions & 6 deletions configuration/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class Config:
# and assigns all seats based on the results in the whole country;
# it can be, however, a first-past-the-post system with one district,
# or majoritarian voting, if a proper seat allocation rule is provided
'country-wide_system': es.single_district_voting,
'countrywide_system': es.single_district_voting,

# a system with electoral districts as specified by 'district_sizes', either PR or first-past-the-post
# what depends on the seat assignment method and number of seats per district,
Expand Down Expand Up @@ -240,11 +240,10 @@ def f(n, g):
# Determine the number of states
if self.num_parties < 2:
raise ValueError('The simulation needs at least two states')
else:
self.all_states = generate_state_labels(self.num_parties)
self.zealot_state = self.all_states[0]
self.not_zealot_state = self.all_states[-1]
self.suffix = ''.join(['_parties_', str(self.num_parties), self.suffix])
self.all_states = generate_state_labels(self.num_parties)
self.zealot_state = self.all_states[0]
self.not_zealot_state = self.all_states[-1]
self.suffix = ''.join(['_parties_', str(self.num_parties), self.suffix])

if self.mass_media is None:
self.mass_media = 1.0 / self.num_parties # the symmetric case with no propaganda
Expand Down
4 changes: 2 additions & 2 deletions configuration/tests/configuration_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def test_config_basic_class_attributes(self):
# might be silly to test whether some attributes exist, but it might help to think through future changes
# and all the parser actions should be defined as class attributes of Config, so instances know they have them
self.maxDiff = None
self.assertIn('country-wide_system', Config.voting_systems.keys())
self.assertIn('countrywide_system', Config.voting_systems.keys())
self.assertIn('main_district_system', Config.voting_systems.keys())
for action in parser._actions:
if action.dest != 'help':
Expand Down Expand Up @@ -153,7 +153,7 @@ def test_config_attributes_values_alternative_sys(self):
config = Config(input_parser, ArgumentDict())

self.assertListEqual(list(config.voting_systems.keys()),
['country-wide_system', 'main_district_system', 'one', 'two', 'three'])
['countrywide_system', 'main_district_system', 'one', 'two', 'three'])
self.assertDictEqual(config.alternative_systems[0],
{'name': 'one', 'type': 'basic', 'seat_rule': 'hare', 'threshold': 0.2,
'seat_alloc_function': seat_assignment_rules['hare'], 'seats': [2],
Expand Down
10 changes: 5 additions & 5 deletions electoral_sys/seat_assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def imperiali_method(total_seats, votes=None, **kwargs):
###########################################################


def largest_reminder_formula(quota, total_seats, votes):
def largest_remainder_formula(quota, total_seats, votes):
"""
General formula for the largest reminder seat assigning methods.
After providing the proper quota it can compute the seat assigment
Expand Down Expand Up @@ -194,7 +194,7 @@ def hare_quota(total_seats, votes=None, total_votes=None, **kwargs):
:return: seat assignment, a dict of a form {party_code: number_of_seats}
"""
quota = Decimal(total_votes) / total_seats
return largest_reminder_formula(quota, total_seats, votes)
return largest_remainder_formula(quota, total_seats, votes)


def droop_quota(total_seats, votes=None, total_votes=None, **kwargs):
Expand All @@ -206,7 +206,7 @@ def droop_quota(total_seats, votes=None, total_votes=None, **kwargs):
:return: seat assignment, a dict of a form {party_code: number_of_seats}
"""
quota = Decimal(1 + floor(Decimal(total_votes) / (total_seats + 1)))
return largest_reminder_formula(quota, total_seats, votes)
return largest_remainder_formula(quota, total_seats, votes)


def exact_droop_quota(total_seats, votes=None, total_votes=None, **kwargs):
Expand All @@ -219,7 +219,7 @@ def exact_droop_quota(total_seats, votes=None, total_votes=None, **kwargs):
:return: seat assignment, a dict of a form {party_code: number_of_seats}
"""
quota = Decimal(total_votes) / (total_seats + 1)
assignment = largest_reminder_formula(quota, total_seats, votes)
assignment = largest_remainder_formula(quota, total_seats, votes)
if sum(assignment.values()) > total_seats:
log.warning('The exact Droop quota assigned more seats than available! Increasing the quota to avoid it.')
return droop_quota(total_seats, votes=votes, total_votes=total_votes)
Expand All @@ -237,7 +237,7 @@ def imperiali_quota(total_seats, votes=None, total_votes=None, **kwargs):
:return: seat assignment, a dict of a form {party_code: number_of_seats}
"""
quota = Decimal(total_votes) / (total_seats + 2)
assignment = largest_reminder_formula(quota, total_seats, votes)
assignment = largest_remainder_formula(quota, total_seats, votes)
if sum(assignment.values()) > total_seats:
log.warning('The Imperiali quota assigned more seats than available! Using the Droop quota.')
return exact_droop_quota(total_seats, votes=votes, total_votes=total_votes)
Expand Down
6 changes: 3 additions & 3 deletions scripts/binom_approx.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def get_binom_hist(config_args, m, system):
@return: histogram bar sizes for a given setting in a binomial approximation. (numpy.array)
"""
p = 0.5 * (1.0 - config_args.epsilon) + config_args.epsilon * config_args.mass_media # effective state 'a' probability
if system == 'country-wide_system':
if system == 'countrywide_system':
eff_N = config_args.n - config_args.n_zealots # number of non-zealot voters
sub_density = st.binom(eff_N, p).pmf(np.arange(eff_N + 1)) # density for single voters
density = np.zeros(config_args.n + 1) # density including zealots
Expand All @@ -61,7 +61,7 @@ def get_binom_hist(config_args, m, system):
n_d = config_args.n / config_args.q # number of voters per district
n_z = config_args.n_zealots / config_args.q # average number of zealots per district

# In contrast to the 'country-wide_system' case, here we only approximate the effect of zealots.
# In contrast to the 'countrywide_system' case, here we only approximate the effect of zealots.
# Otherwise, we would need to sum over all possible combinations.
if n_d % 2:
p_d = 1.0 - st.binom(n_d - n_z, p).cdf(n_d // 2 - n_z) # probability of winning in a district
Expand Down Expand Up @@ -131,7 +131,7 @@ def plot_hist_with_binom_approx(distribution, m, hist, density, suffix, colors=(
except (OSError, IOError) as e:
raise IOError("Data for the given configuration was not found, "
"run main.py with the same configuration first.")
for system in ['country-wide_system', 'main_district_system']:
for system in ['countrywide_system', 'main_district_system']:
distribution = convert_to_distributions(res[system])
hist, density = get_binom_hist(cfg, m, system)

Expand Down

0 comments on commit dc5465c

Please sign in to comment.