Skip to content

Commit

Permalink
rename highest_threshold to pointwise_highest
Browse files Browse the repository at this point in the history
  • Loading branch information
HighDiceRoller committed Oct 12, 2024
1 parent e6154b1 commit 2b8a1ab
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/icepool/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

from icepool.function import (d, z, __getattr__, coin, stochastic_round,
one_hot, iter_cartesian_product, from_cumulative,
from_rv, highest_threshold, lowest_threshold, min_outcome,
from_rv, pointwise_highest, pointwise_lowest, min_outcome,
max_outcome, consecutive, sorted_union,
commonize_denominator, reduce, accumulate, map,
map_function, map_and_time, map_to_pool)
Expand Down Expand Up @@ -147,7 +147,7 @@
'd', 'z', 'coin', 'stochastic_round', 'one_hot', 'Outcome', 'Die',
'Population', 'tupleize', 'vectorize', 'Vector', 'Symbols', 'Again',
'CountsKeysView', 'CountsValuesView', 'CountsItemsView', 'from_cumulative',
'from_rv', 'highest_threshold', 'lowest_threshold', 'lowest', 'highest', 'middle', 'min_outcome', 'max_outcome',
'from_rv', 'pointwise_highest', 'pointwise_lowest', 'lowest', 'highest', 'middle', 'min_outcome', 'max_outcome',
'consecutive', 'sorted_union', 'commonize_denominator', 'reduce',
'accumulate', 'map', 'map_function', 'map_and_time', 'map_to_pool',
'Reroll', 'RerollType', 'Pool', 'standard_pool', 'MultisetGenerator',
Expand Down
26 changes: 16 additions & 10 deletions src/icepool/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,35 +235,41 @@ def _iter_outcomes(
else:
yield arg

def highest_threshold(*dice: 'icepool.Die[T]') -> 'icepool.Die[T]':
def pointwise_highest(*dice: 'icepool.Die[T]') -> 'icepool.Die[T]':
"""Selects the highest chance of rolling >= each outcome among the arguments.
Specifically, for each outcome, the chance of the result rolling >= to that
outcome is the same as the highest chance of rolling >= that outcome among
the arguments.
The result has the same mathematical structure as a probability
distribution, hence the result is a `Die`, even if it is difficult to
interpret as a physical process.
Equivalently, any quantile in the result is the highest of that quantile
among the arguments.
This is useful for selecting from several possible moves where you are
trying to get >= a threshold that is known but could change depending on the
situation.
Args:
dice: Any number of dice.
"""
dice = commonize_denominator(*dice)
outcomes = sorted_union(*dice)
cumulative = [max(die.quantity('>=', outcome) for die in dice) for outcome in outcomes]
return from_cumulative(outcomes, cumulative, reverse=True)
cumulative = [min(die.quantity('<=', outcome) for die in dice) for outcome in outcomes]
return from_cumulative(outcomes, cumulative)

def lowest_threshold(*dice: 'icepool.Die[T]') -> 'icepool.Die[T]':
def pointwise_lowest(*dice: 'icepool.Die[T]') -> 'icepool.Die[T]':
"""Selects the highest chance of rolling <= each outcome among the arguments.
Specifically, for each outcome, the chance of the result rolling <= to that
outcome is the same as the highest chance of rolling <= that outcome among
the arguments.
The result has the same mathematical structure as a probability
distribution, hence the result is a `Die`, even if it is difficult to
interpret as a physical process.
Equivalently, any quantile in the result is the lowest of that quantile
among the arguments.
This is useful for selecting from several possible moves where you are
trying to get <= a threshold that is known but could change depending on the
situation.
Args:
dice: Any number of dice.
Expand Down
10 changes: 5 additions & 5 deletions tests/function_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import icepool
import pytest

from icepool import d4, d6, d8, d10, d12, d20, min_outcome, max_outcome, highest_threshold, lowest_threshold
from icepool import d4, d6, d8, d10, d12, d20, min_outcome, max_outcome, pointwise_highest, pointwise_lowest


def test_min_outcome_single_arg():
Expand All @@ -27,15 +27,15 @@ def test_max_outcome_multiple_arg():
def test_max_outcome_bare_outcome():
assert max_outcome(d6, d8, 10, 2) == 10

def test_highest_threshold():
result = highest_threshold(3 @ d6, d20)
def test_pointwise_highest():
result = pointwise_highest(3 @ d6, d20)
for outcome in range(1, 21):
assert result.probability('>=', outcome) == max(
(3 @ d6).probability('>=', outcome),
d20.probability('>=', outcome))

def test_lowest_threshold():
result = lowest_threshold(3 @ d6, d20)
def test_pointwise_lowest():
result = pointwise_lowest(3 @ d6, d20)
for outcome in range(1, 21):
assert result.probability('<=', outcome) == max(
(3 @ d6).probability('<=', outcome),
Expand Down

0 comments on commit 2b8a1ab

Please sign in to comment.