Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the epsilon selectors so they correctly handle the rounds. #21

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions sciope/inference/smc_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ def infer(self, num_samples, batch_size,
abc_history.append(abc_results)

# SMC iterations
round = 1
abc_round = 2 # Indicates the round getting ready to start
while not terminate:

tol, relative, terminate = eps_selector.get_epsilon(round, abc_history)
tol, relative, terminate = eps_selector.get_epsilon(abc_round, abc_history)

print("Starting epsilon = {}".format(tol))
if self.use_logger:
Expand Down Expand Up @@ -228,7 +228,7 @@ def infer(self, num_samples, batch_size,
if self.parameters is not None:
abc_results = InferenceRound.build_from_inference_round(abc_results, list(self.parameters.keys()))
abc_history.append(abc_results)
round += 1
abc_round += 1

except KeyboardInterrupt:
if self.parameters is None:
Expand Down
10 changes: 5 additions & 5 deletions sciope/utilities/epsilonselectors/absolute_epsilon_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(self, epsilon_sequence):

assert (len(epsilon_sequence) > 0)
self.epsilon_sequence = epsilon_sequence
self.last_round = len(self.epsilon_sequence) - 1
self.last_round = len(self.epsilon_sequence)

def get_initial_epsilon(self):
"""Gets the first epsilon in the sequence.
Expand All @@ -48,14 +48,14 @@ def get_initial_epsilon(self):
has_more : bool
Whether there are more epsilons after this one
"""
return self.epsilon_sequence[0], False, len(self.epsilon_sequence) == 1
return self.epsilon_sequence[0], False, self.last_round == 1

def get_epsilon(self, round, abc_history):
def get_epsilon(self, abc_round, abc_history):
"""Returns the n-th epsilon in the seqeunce.

Parameters
----------
round : int
abc_round : int
the round to get the epsilon for
abc_history : type
A list of dictionaries with keys `accepted_samples` and `distances`
Expand All @@ -70,4 +70,4 @@ def get_epsilon(self, round, abc_history):
terminate : bool
Whether to stop after this epsilon
"""
return self.epsilon_sequence[round], False, round == self.last_round
return self.epsilon_sequence[abc_round - 1], False, abc_round >= self.last_round
14 changes: 8 additions & 6 deletions sciope/utilities/epsilonselectors/relative_epsilon_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def __init__(self, epsilon_percentile, max_rounds=None):
max_rounds : int
The maximum number of rounds before stopping. If None, doesn't end.
"""
if max_rounds == 0:
raise ValueError("max_rounds must be greater than 0.")

self.epsilon_percentile = epsilon_percentile
self.max_rounds = max_rounds
Expand All @@ -52,14 +54,14 @@ def get_initial_epsilon(self):
has_more : bool
Whether there are more epsilons after this one
"""
return self.epsilon_percentile, True, self.max_rounds == 0
return self.epsilon_percentile, True, self.max_rounds == 1

def get_epsilon(self, round, abc_history):
def get_epsilon(self, abc_round, abc_history):
"""Returns the new epsilon based on the n-th round.

Parameters
----------
round : int
abc_round : int
the n-th round of the sequence
abc_history : type
A list of dictionaries with keys `accepted_samples` and `distances`
Expand All @@ -74,8 +76,8 @@ def get_epsilon(self, round, abc_history):
terminate : bool
Whether to stop after this epsilon
"""
if round > len(abc_history):
if abc_round > len(abc_history):
epsilon = np.percentile(abc_history[-1]['distances'], self.epsilon_percentile)
else:
epsilon = np.percentile(abc_history[round - 1]['distances'], self.epsilon_percentile)
return epsilon, False, self.max_rounds and round + 1 == self.max_rounds
epsilon = np.percentile(abc_history[abc_round - 1]['distances'], self.epsilon_percentile)
return epsilon, False, self.max_rounds and abc_round >= self.max_rounds