def get_dataset_encoding_delta(sample_size,
                               examples,
                               current_rules,
                               candidate_rule_to_add,
                               candidate_rules_to_remove,
                               verbose=False):
  """Approximate increase in codelength to encode dataset."""
  # Make a copy of the ruleset and add/remove candidates.
  new_rules = current_rules.copy()
  for rule_to_remove in candidate_rules_to_remove:
    new_rules.remove(rule_to_remove)
  new_rules.add(candidate_rule_to_add)

  relevant_examples = _find_relevant_examples(examples, candidate_rule_to_add)
  num_relevant_examples = len(relevant_examples)
  sample = False

  if verbose:
    print("%s relevant rules." % num_relevant_examples)

  # If configured, sample rules for effeciency.
  if sample_size and num_relevant_examples > sample_size:
    random.shuffle(relevant_examples)

    relevant_examples = relevant_examples[:sample_size]
    sample = True

  # Compute the increase in target codelength summed across the sample.
  delta = 0
  for source, target in relevant_examples:
    new_codelength = _target_codelength(
        source, target, new_rules, verbose=verbose)
    original_codelength = _target_codelength(
        source, target, current_rules, verbose=verbose)
    delta += (new_codelength - original_codelength)

  # Estimate delta across entire set based on our sample.
  if sample:
    scale_factor = float(num_relevant_examples) / float(sample_size)
    delta *= scale_factor
    if verbose:
      print("Scaling delta by %s." % scale_factor)

  if verbose:
    print("Delta: %s." % delta)
  return delta
Approximate increase in codelength to encode dataset.
language/compgen/nqg/model/induction/codelength_utils.py