Skip to content

Commit

Permalink
start work on CDF/SF thresholding
Browse files Browse the repository at this point in the history
  • Loading branch information
HighDiceRoller committed Oct 12, 2024
1 parent c81b6bf commit 567b7a9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/icepool/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@

from icepool.function import (d, z, __getattr__, coin, stochastic_round,
one_hot, iter_cartesian_product, from_cumulative,
from_rv, min_outcome, max_outcome, consecutive,
sorted_union, commonize_denominator, reduce,
accumulate, map, map_function, map_and_time,
map_to_pool)
from_rv, highest_threshold, lowest_threshold, min_outcome,
max_outcome, consecutive, sorted_union,
commonize_denominator, reduce, accumulate, map,
map_function, map_and_time, map_to_pool)

from icepool.population.base import Population
from icepool.population.die import implicit_convert_to_die, Die
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', 'lowest', 'highest', 'middle', 'min_outcome', 'max_outcome',
'from_rv', 'highest_threshold', 'lowest_threshold', '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
27 changes: 27 additions & 0 deletions src/icepool/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,33 @@ def _iter_outcomes(
else:
yield arg

def highest_threshold(*dice: 'icepool.Die[T]') -> 'icepool.Die[T]':
"""Description TBD
For each outcome, the chance of the result rolling >= to that outcome is the
same as the highest chance among the arguments.
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)

def lowest_threshold(*dice: 'icepool.Die[T]') -> 'icepool.Die[T]':
"""Description TBD
For each outcome, the chance of the result rolling <= to that outcome is the
same as the highest chance among the arguments.
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)

@overload
def min_outcome(arg: 'Iterable[T | icepool.Population[T]]', /) -> T:
Expand Down
16 changes: 15 additions & 1 deletion 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, min_outcome, max_outcome
from icepool import d4, d6, d8, d10, d12, d20, min_outcome, max_outcome, highest_threshold, lowest_threshold


def test_min_outcome_single_arg():
Expand All @@ -26,3 +26,17 @@ 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)
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)
for outcome in range(1, 21):
assert result.probability('<=', outcome) == max(
(3 @ d6).probability('<=', outcome),
d20.probability('<=', outcome))

0 comments on commit 567b7a9

Please sign in to comment.