125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877 | class H(_MappingT):
r"""
An immutable mapping for use as a histogram which supports arithmetic operations.
This is useful for modeling discrete outcomes, like individual dice. ``#!python H``
objects encode finite discrete probability distributions as integer counts without
any denominator.
!!! info
The lack of an explicit denominator is intentional and has two benefits. First,
a denominator is redundant. Without it, one never has to worry about
probabilities summing to one (e.g., via miscalculation, floating point error,
etc.). Second (and perhaps more importantly), sometimes one wants to have an
insight into non-reduced counts, not just probabilities. If needed,
probabilities can always be derived, as shown below.
The [initializer][dyce.h.H.__init__] takes a single parameter, *items*. In its most
explicit form, *items* maps outcome values to counts.
Modeling a single six-sided die (``1d6``) can be expressed as:
``` python
>>> from dyce import H
>>> d6 = H({1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1})
```
An iterable of pairs can also be used (similar to ``#!python dict``).
``` python
>>> d6 == H(((1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1)))
True
```
Two shorthands are provided. If *items* is an iterable of numbers, counts of 1 are
assumed.
``` python
>>> d6 == H((1, 2, 3, 4, 5, 6))
True
```
Repeated items are accumulated, as one would expect.
``` python
>>> H((2, 3, 3, 4, 4, 5))
H({2: 1, 3: 2, 4: 2, 5: 1})
```
If *items* is an integer, it is shorthand for creating a sequential range $[{1} ..
{items}]$ (or $[{items} .. {-1}]$ if *items* is negative).
``` python
>>> d6 == H(6)
True
```
Histograms are maps, so we can test equivalence against other maps.
``` python
>>> H(6) == {1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1}
True
```
Simple indexes can be used to look up an outcome’s count.
``` python
>>> H((2, 3, 3, 4, 4, 5))[3]
2
```
Most arithmetic operators are supported and do what one would expect. If the operand
is a number, the operator applies to the outcomes.
``` python
>>> d6 + 4
H({5: 1, 6: 1, 7: 1, 8: 1, 9: 1, 10: 1})
```
``` python
>>> d6 * -1
H({-6: 1, -5: 1, -4: 1, -3: 1, -2: 1, -1: 1})
>>> d6 * -1 == -d6
True
>>> d6 * -1 == H(-6)
True
```
If the operand is another histogram, combinations are computed. Modeling the sum of
two six-sided dice (``2d6``) can be expressed as:
``` python
>>> d6 + d6
H({2: 1, 3: 2, 4: 3, 5: 4, 6: 5, 7: 6, 8: 5, 9: 4, 10: 3, 11: 2, 12: 1})
>>> print((d6 + d6).format())
avg | 7.00
std | 2.42
var | 5.83
2 | 2.78% |#
3 | 5.56% |##
4 | 8.33% |####
5 | 11.11% |#####
6 | 13.89% |######
7 | 16.67% |########
8 | 13.89% |######
9 | 11.11% |#####
10 | 8.33% |####
11 | 5.56% |##
12 | 2.78% |#
```
To sum ${n}$ identical histograms, the matrix multiplication operator (``@``)
provides a shorthand.
``` python
>>> 3@d6 == d6 + d6 + d6
True
```
The ``#!python len`` built-in function can be used to show the number of distinct
outcomes.
``` python
>>> len(2@d6)
11
```
The [``total`` property][dyce.h.H.total] can be used to compute the total number of
combinations and each outcome’s probability.
``` python
>>> from fractions import Fraction
>>> (2@d6).total
36
>>> [(outcome, Fraction(count, (2@d6).total)) for outcome, count in (2@d6).items()]
[(2, Fraction(1, 36)), (3, Fraction(1, 18)), (4, Fraction(1, 12)), (5, Fraction(1, 9)), (6, Fraction(5, 36)), (7, Fraction(1, 6)), ..., (12, Fraction(1, 36))]
```
Histograms provide common comparators (e.g., [``eq``][dyce.h.H.eq]
[``ne``][dyce.h.H.ne], etc.). One way to count how often a first six-sided die
shows a different face than a second is:
``` python
>>> d6.ne(d6)
H({False: 6, True: 30})
>>> print(d6.ne(d6).format())
avg | 0.83
std | 0.37
var | 0.14
0 | 16.67% |########
1 | 83.33% |#########################################
```
Or, how often a first six-sided die shows a face less than a second is:
``` python
>>> d6.lt(d6)
H({False: 21, True: 15})
>>> print(d6.lt(d6).format())
avg | 0.42
std | 0.49
var | 0.24
0 | 58.33% |#############################
1 | 41.67% |####################
```
Or how often at least one ``#!python 2`` will show when rolling four six-sided dice:
``` python
>>> d6_eq2 = d6.eq(2) ; d6_eq2 # how often a 2 shows on a single six-sided die
H({False: 5, True: 1})
>>> 4@d6_eq2 # count of 2s showing on 4d6
H({0: 625, 1: 500, 2: 150, 3: 20, 4: 1})
>>> (4@d6_eq2).ge(1) # how often that count is at least one
H({False: 625, True: 671})
>>> print((4@d6_eq2).ge(1).format())
avg | 0.52
std | 0.50
var | 0.25
0 | 48.23% |########################
1 | 51.77% |#########################
```
!!! bug "Mind your parentheses"
Parentheses are often necessary to enforce the desired order of operations. This
is most often an issue with the ``#!python @`` operator, because it behaves
differently than the ``d`` operator in most dedicated grammars. More
specifically, in Python, ``#!python @`` has a [lower
precedence](https://docs.python.org/3/reference/expressions.html#operator-precedence)
than ``#!python .`` and ``#!python […]``.
``` python
>>> 2@d6[7] # type: ignore [operator]
Traceback (most recent call last):
...
KeyError: 7
>>> 2@d6.le(7) # probably not what was intended
H({2: 36})
>>> 2@d6.le(7) == 2@(d6.le(7))
True
```
``` python
>>> (2@d6)[7]
6
>>> (2@d6).le(7)
H({False: 15, True: 21})
>>> 2@d6.le(7) == (2@d6).le(7)
False
```
Counts are generally accumulated without reduction. To reduce, call the
[``lowest_terms`` method][dyce.h.H.lowest_terms].
``` python
>>> d6.ge(4)
H({False: 3, True: 3})
>>> d6.ge(4).lowest_terms()
H({False: 1, True: 1})
```
Testing equivalence implicitly performs reductions of operands.
``` python
>>> d6.ge(4) == d6.ge(4).lowest_terms()
True
```
"""
__slots__: Any = (
"_h",
"_hash",
"_lowest_terms",
"_order_stat_funcs_by_n",
"_total",
)
# ---- Initializer -----------------------------------------------------------------
@beartype
def __init__(self, items: _SourceT) -> None:
r"Initializer."
super().__init__()
self._h: _MappingT
if isinstance(items, H):
self._h = items._h
elif isinstance(items, SupportsInt):
if items == 0:
self._h = {}
else:
simple_init = as_int(items)
outcome_range = range(
simple_init if simple_init < 0 else 1,
0 if simple_init < 0 else simple_init + 1,
)
# if isinstance(items, RealLike):
# outcome_type = type(items)
# self._h = {outcome_type(i): 1 for i in outcome_range}
# else:
# self._h = {i: 1 for i in outcome_range}
assert isinstance(items, RealLike)
outcome_type = type(items)
self._h = {outcome_type(i): 1 for i in outcome_range}
elif isinstance(items, HableT):
self._h = items.h()._h
elif isinstance(items, IterableC):
if isinstance(items, Mapping):
items = items.items()
# items is either an Iterable[RealLike] or an Iterable[tuple[RealLike,
# SupportsInt]] (although this technically supports Iterable[RealLike |
# tuple[RealLike, SupportsInt]])
self._h = {}
sorted_items = list(items)
try:
sorted_items.sort()
except TypeError:
sorted_items.sort(key=natural_key)
# As of Python 3.7, insertion order of keys is preserved
for item in sorted_items:
if isinstance(item, tuple):
outcome, count = item
count = as_int(count)
else:
outcome = item
count = 1
if count < 0:
raise ValueError(f"count for {outcome} cannot be negative")
if outcome not in self._h:
self._h[outcome] = 0
self._h[outcome] += count
else:
raise TypeError(f"unrecognized initializer type {items!r}")
# We can't use something like functools.lru_cache for these values because those
# mechanisms call this object's __hash__ method which relies on both of these
# and we don't want a circular dependency when computing this object's hash.
self._hash: Optional[int] = None
self._total: int = sum(self._h.values())
self._lowest_terms: Optional[H] = None
# We don't use functools' caching mechanisms generally because they don't
# present a good mechanism for scoping the cache to object instances such that
# the cache will be purged when the object is deleted. functools.cached_property
# is an exception, but it requires that objects have proper __dict__ values,
# which Hs do not. So we basically do what functools.cached_property does, but
# without a __dict__.
self._order_stat_funcs_by_n: dict[int, Callable[[int], H]] = {}
# ---- Overrides -------------------------------------------------------------------
@beartype
def __repr__(self) -> str:
return f"{type(self).__name__}({dict.__repr__(self._h)})"
@beartype
def __eq__(self, other) -> bool:
if isinstance(other, HableT):
return __eq__(self, other.h())
elif isinstance(other, H):
return __eq__(self.lowest_terms()._h, other.lowest_terms()._h)
else:
return super().__eq__(other)
@beartype
def __ne__(self, other) -> bool:
if isinstance(other, HableT):
return __ne__(self, other.h())
elif isinstance(other, H):
return not __eq__(self, other)
else:
return super().__ne__(other)
@beartype
def __hash__(self) -> int:
if self._hash is None:
self._hash = hash(frozenset(self.lowest_terms().items()))
return self._hash
@beartype
def __bool__(self) -> int:
return bool(self.total)
@beartype
def __len__(self) -> int:
return len(self._h)
@beartype
def __getitem__(self, key: RealLike) -> int:
return __getitem__(self._h, key)
@beartype
def __iter__(self) -> Iterator[RealLike]:
return iter(self._h)
@beartype
def __reversed__(self) -> Iterator[RealLike]:
return reversed(self._h)
@beartype
def __contains__(self, key: RealLike) -> bool: # type: ignore [override]
return key in self._h
@beartype
def __add__(self, other: _OperandT) -> "H":
try:
return self.map(__add__, other)
except NotImplementedError:
return NotImplemented
@beartype
def __radd__(self, other: RealLike) -> "H":
try:
return self.rmap(other, __add__)
except NotImplementedError:
return NotImplemented
@beartype
def __sub__(self, other: _OperandT) -> "H":
try:
return self.map(__sub__, other)
except NotImplementedError:
return NotImplemented
@beartype
def __rsub__(self, other: RealLike) -> "H":
try:
return self.rmap(other, __sub__)
except NotImplementedError:
return NotImplemented
@beartype
def __mul__(self, other: _OperandT) -> "H":
try:
return self.map(__mul__, other)
except NotImplementedError:
return NotImplemented
@beartype
def __rmul__(self, other: RealLike) -> "H":
try:
return self.rmap(other, __mul__)
except NotImplementedError:
return NotImplemented
@beartype
def __matmul__(self, other: SupportsInt) -> "H":
try:
other = as_int(other)
except TypeError:
return NotImplemented
if other < 0:
raise ValueError("argument cannot be negative")
else:
return sum_h(repeat(self, other))
@beartype
def __rmatmul__(self, other: SupportsInt) -> "H":
return self.__matmul__(other)
@beartype
def __truediv__(self, other: _OperandT) -> "H":
try:
return self.map(__truediv__, other)
except NotImplementedError:
return NotImplemented
@beartype
def __rtruediv__(self, other: RealLike) -> "H":
try:
return self.rmap(other, __truediv__)
except NotImplementedError:
return NotImplemented
@beartype
def __floordiv__(self, other: _OperandT) -> "H":
try:
return self.map(__floordiv__, other)
except NotImplementedError:
return NotImplemented
@beartype
def __rfloordiv__(self, other: RealLike) -> "H":
try:
return self.rmap(other, __floordiv__)
except NotImplementedError:
return NotImplemented
@beartype
def __mod__(self, other: _OperandT) -> "H":
try:
return self.map(__mod__, other)
except NotImplementedError:
return NotImplemented
@beartype
def __rmod__(self, other: RealLike) -> "H":
try:
return self.rmap(other, __mod__)
except NotImplementedError:
return NotImplemented
@beartype
def __pow__(self, other: _OperandT) -> "H":
try:
return self.map(__pow__, other)
except NotImplementedError:
return NotImplemented
@beartype
def __rpow__(self, other: RealLike) -> "H":
try:
return self.rmap(other, __pow__)
except NotImplementedError:
return NotImplemented
@beartype
# TODO(posita): See <https://github.com/beartype/beartype/issues/152>
def __and__(self, other: Union[SupportsInt, "H", "HableT"]) -> "H":
try:
if isinstance(other, SupportsInt):
other = as_int(other)
return self.map(__and__, other)
except (NotImplementedError, TypeError):
return NotImplemented
@beartype
def __rand__(self, other: SupportsInt) -> "H":
try:
return self.rmap(as_int(other), __and__)
except (NotImplementedError, TypeError):
return NotImplemented
@beartype
# TODO(posita): See <https://github.com/beartype/beartype/issues/152>
def __xor__(self, other: Union[SupportsInt, "H", "HableT"]) -> "H":
try:
if isinstance(other, SupportsInt):
other = as_int(other)
return self.map(__xor__, other)
except NotImplementedError:
return NotImplemented
@beartype
def __rxor__(self, other: SupportsInt) -> "H":
try:
return self.rmap(as_int(other), __xor__)
except (NotImplementedError, TypeError):
return NotImplemented
@beartype
# TODO(posita): See <https://github.com/beartype/beartype/issues/152>
def __or__(self, other: Union[SupportsInt, "H", "HableT"]) -> "H":
try:
if isinstance(other, SupportsInt):
other = as_int(other)
return self.map(__or__, other)
except (NotImplementedError, TypeError):
return NotImplemented
@beartype
def __ror__(self, other: SupportsInt) -> "H":
try:
return self.rmap(as_int(other), __or__)
except (NotImplementedError, TypeError):
return NotImplemented
@beartype
def __neg__(self) -> "H":
return self.umap(__neg__)
@beartype
def __pos__(self) -> "H":
return self.umap(__pos__)
@beartype
def __abs__(self) -> "H":
return self.umap(__abs__)
@beartype
def __invert__(self) -> "H":
return self.umap(__invert__)
@beartype
def counts(self) -> ValuesView[int]:
r"""
More descriptive synonym for the [``values`` method][dyce.h.H.values].
"""
return self._h.values()
@beartype
def items(self) -> ItemsView[RealLike, int]:
return self._h.items()
@beartype
def keys(self) -> KeysView[RealLike]:
return self.outcomes()
@beartype
def outcomes(self) -> KeysView[RealLike]:
r"""
More descriptive synonym for the [``keys`` method][dyce.h.H.keys].
"""
return self._h.keys()
@beartype
def reversed(self) -> Iterator[RealLike]:
return reversed(self)
@beartype
def values(self) -> ValuesView[int]:
return self.counts()
# ---- Properties ------------------------------------------------------------------
@property
def total(self) -> int:
r"""
!!! warning "Experimental"
This property should be considered experimental and may change or disappear
in future versions.
Equivalent to ``#!python sum(self.counts())``.
"""
return self._total
# ---- Methods ---------------------------------------------------------------------
@classmethod
@deprecated
@beartype
def foreach(
cls,
dependent_term: Callable[..., HOrOutcomeT],
**independent_sources: _SourceT,
) -> "H":
r"""
!!! warning "Deprecated"
This method has been deprecated and will be removed in a future release. See
the [``expandable`` decorator][dyce.evaluation.expandable] and
[``foreach`` function][dyce.evaluation.foreach] for more flexible
alternatives.
Calls ``#!python dependent_term`` for each set of outcomes from the product of
``independent_sources`` and accumulates the results. This is useful for
resolving dependent probabilities. Returned histograms are always reduced to
their lowest terms.
For example rolling a d20, re-rolling a ``#!python 1`` if it comes up, and
keeping the result might be expressed as[^1]:
[^1]:
This is primarily for illustration. [``H.substitute``][dyce.h.H.substitute]
is often better suited to cases involving re-rolling a single independent
term such as this one.
``` python
>>> d20 = H(20)
>>> def reroll_one_dependent_term(d20_outcome):
... if d20_outcome == 1:
... return d20
... else:
... return d20_outcome
>>> H.foreach(reroll_one_dependent_term, d20_outcome=d20)
H({1: 1, 2: 21, 3: 21, ..., 19: 21, 20: 21})
```
The ``#!python foreach`` class method merely wraps *dependent_term* and calls
[``P.foreach``][dyce.p.P.foreach]. In doing so, it imposes a very modest
overhead that is negligible in most cases.
``` python
--8<-- "docs/assets/perf_foreach.txt"
```
<details>
<summary>Source: <a href="https://github.com/posita/dyce/blob/latest/docs/assets/perf_foreach.ipy"><code>perf_foreach.ipy</code></a></summary>
``` python
--8<-- "docs/assets/perf_foreach.ipy"
```
</details>
"""
from dyce import P
def _dependent_term(**roll_kw):
outcome_kw: dict[str, RealLike] = {}
for key, roll in roll_kw.items():
assert isinstance(roll, tuple)
assert len(roll) == 1
outcome_kw[key] = roll[0]
return dependent_term(**outcome_kw)
return P.foreach(_dependent_term, **independent_sources)
@beartype
def map(self, bin_op: _BinaryOperatorT, right_operand: _OperandT) -> "H":
r"""
Applies *bin_op* to each outcome of the histogram as the left operand and
*right_operand* as the right. Shorthands exist for many arithmetic operators and
comparators.
``` python
>>> import operator
>>> d6 = H(6)
>>> d6.map(operator.__add__, d6)
H({2: 1, 3: 2, 4: 3, 5: 4, 6: 5, 7: 6, 8: 5, 9: 4, 10: 3, 11: 2, 12: 1})
>>> d6.map(operator.__add__, d6) == d6 + d6
True
```
``` python
>>> d6.map(operator.__pow__, 2)
H({1: 1, 4: 1, 9: 1, 16: 1, 25: 1, 36: 1})
>>> d6.map(operator.__pow__, 2) == d6 ** 2
True
```
``` python
>>> d6.map(operator.__gt__, 3)
H({False: 3, True: 3})
>>> d6.map(operator.__gt__, 3) == d6.gt(3)
True
```
"""
if isinstance(right_operand, HableT):
right_operand = right_operand.h()
if isinstance(right_operand, H):
return type(self)(
(bin_op(s, o), self[s] * right_operand[o])
for s, o in product(self, right_operand)
)
else:
return type(self)(
(bin_op(outcome, right_operand), count)
for outcome, count in self.items()
)
@beartype
def rmap(self, left_operand: RealLike, bin_op: _BinaryOperatorT) -> "H":
r"""
Analogous to the [``map`` method][dyce.h.H.map], but where the caller supplies
*left_operand*.
``` python
>>> import operator
>>> d6 = H(6)
>>> d6.rmap(2, operator.__pow__)
H({2: 1, 4: 1, 8: 1, 16: 1, 32: 1, 64: 1})
>>> d6.rmap(2, operator.__pow__) == 2 ** d6
True
```
!!! note
The positions of *left_operand* and *bin_op* are different from
[``map`` method][dyce.h.H.map]. This is intentional and serves as a reminder
of operand ordering.
"""
return type(self)(
(bin_op(left_operand, outcome), count) for outcome, count in self.items()
)
@beartype
def umap(self, un_op: _UnaryOperatorT) -> "H":
r"""
Applies *un_op* to each outcome of the histogram.
``` python
>>> import operator
>>> H(6).umap(operator.__neg__)
H({-6: 1, -5: 1, -4: 1, -3: 1, -2: 1, -1: 1})
```
``` python
>>> H(4).umap(lambda outcome: (-outcome) ** outcome)
H({-27: 1, -1: 1, 4: 1, 256: 1})
```
"""
return type(self)((un_op(outcome), count) for outcome, count in self.items())
@beartype
def lt(self, other: _OperandT) -> "H":
r"""
Shorthand for ``#!python self.map(operator.__lt__, other).umap(bool)``.
``` python
>>> H(6).lt(3)
H({False: 4, True: 2})
```
See the [``map``][dyce.h.H.map] and [``umap``][dyce.h.H.umap] methods.
"""
return self.map(__lt__, other).umap(bool)
@beartype
def le(self, other: _OperandT) -> "H":
r"""
Shorthand for ``#!python self.map(operator.__le__, other).umap(bool)``.
``` python
>>> H(6).le(3)
H({False: 3, True: 3})
```
See the [``map``][dyce.h.H.map] and [``umap``][dyce.h.H.umap] methods.
"""
return self.map(__le__, other).umap(bool)
@beartype
def eq(self, other: _OperandT) -> "H":
r"""
Shorthand for ``#!python self.map(operator.__eq__, other).umap(bool)``.
``` python
>>> H(6).eq(3)
H({False: 5, True: 1})
```
See the [``map``][dyce.h.H.map] and [``umap``][dyce.h.H.umap] methods.
"""
return self.map(__eq__, other).umap(bool)
@beartype
def ne(self, other: _OperandT) -> "H":
r"""
Shorthand for ``#!python self.map(operator.__ne__, other).umap(bool)``.
``` python
>>> H(6).ne(3)
H({False: 1, True: 5})
```
See the [``map``][dyce.h.H.map] and [``umap``][dyce.h.H.umap] methods.
"""
return self.map(__ne__, other).umap(bool)
@beartype
def gt(self, other: _OperandT) -> "H":
r"""
Shorthand for ``#!python self.map(operator.__gt__, other).umap(bool)``.
``` python
>>> H(6).gt(3)
H({False: 3, True: 3})
```
See the [``map``][dyce.h.H.map] and [``umap``][dyce.h.H.umap] methods.
"""
return self.map(__gt__, other).umap(bool)
@beartype
def ge(self, other: _OperandT) -> "H":
r"""
Shorthand for ``#!python self.map(operator.__ge__, other).umap(bool)``.
``` python
>>> H(6).ge(3)
H({False: 2, True: 4})
```
See the [``map``][dyce.h.H.map] and [``umap``][dyce.h.H.umap] methods.
"""
return self.map(__ge__, other).umap(bool)
@beartype
def is_even(self) -> "H":
r"""
Equivalent to ``#!python self.umap(dyce.types.is_even)``.
``` python
>>> H((-4, -2, 0, 1, 2, 3)).is_even()
H({False: 2, True: 4})
```
See the [``umap`` method][dyce.h.H.umap].
"""
return self.umap(is_even)
@beartype
def is_odd(self) -> "H":
r"""
Equivalent to ``#!python self.umap(dyce.types.is_odd)``.
``` python
>>> H((-4, -2, 0, 1, 2, 3)).is_odd()
H({False: 4, True: 2})
```
See the [``umap`` method][dyce.h.H.umap].
"""
return self.umap(is_odd)
@beartype
def accumulate(self, other: _SourceT) -> "H":
r"""
Accumulates counts.
``` python
>>> H(4).accumulate(H(6))
H({1: 2, 2: 2, 3: 2, 4: 2, 5: 1, 6: 1})
```
"""
if not isinstance(other, H):
other = H(other)
return type(self)(cast(_SourceT, chain(self.items(), other.items())))
@beartype
def draw(
self,
outcomes: Optional[Union[RealLike, Iterable[RealLike], _MappingT]] = None,
) -> "H":
r"""
!!! warning "Experimental"
This property should be considered experimental and may change or disappear
in future versions.
Returns a new [``H`` object][dyce.h.H] where the counts associated with
*outcomes* has been updated. This may be useful for using histograms to model
decks of cards (rather than dice). If *outcome* is ``#!python None``, this is
equivalent to ``#!python self.draw(self.roll())``.
If *outcomes* is a single value, that value’s count is decremented by one. If
*outcomes* is an iterable of values, those values’ outcomes are decremented by
one for each time that outcome appears. If *outcomes* is a mapping of outcomes
to counts, those outcomes are decremented by the respective counts.
Counts are not reduced and zero counts are preserved. To reduce, call the
[``lowest_terms`` method][dyce.h.H.lowest_terms].
<!-- BEGIN MONKEY PATCH --
For deterministic outcomes.
>>> import random
>>> from dyce import rng
>>> rng.RNG = random.Random(1691413956)
-- END MONKEY PATCH -->
``` python
>>> H(6).draw()
H({1: 1, 2: 1, 3: 1, 4: 0, 5: 1, 6: 1})
>>> H(6).draw(2)
H({1: 1, 2: 0, 3: 1, 4: 1, 5: 1, 6: 1})
>>> H(6).draw((2, 3, 4, 5)).lowest_terms()
H({1: 1, 6: 1})
>>> h = H(6).accumulate(H(4)) ; h
H({1: 2, 2: 2, 3: 2, 4: 2, 5: 1, 6: 1})
>>> h.draw({1: 2, 4: 1, 6: 1})
H({1: 0, 2: 2, 3: 2, 4: 1, 5: 1, 6: 0})
```
!!! tip "Negative counts can be used to increase counts"
Where *outcomes* is a mapping of outcomes to counts, negative counts can be
used to *increase* or “add” outcomes’ counts.
``` python
>>> H(4).draw({5: -1})
H({1: 1, 2: 1, 3: 1, 4: 1, 5: 1})
```
"""
if outcomes is None:
return self.draw(self.roll())
if isinstance(outcomes, RealLike):
outcomes = (outcomes,)
to_draw_outcome_counts = Counter(outcomes)
self_outcome_counts = Counter(self)
# This approach is necessary because Counter.__sub__ does not preserve negative
# counts and Counter.subtract modifies the counter in-place
new_outcome_counts = Counter(self_outcome_counts)
new_outcome_counts.subtract(to_draw_outcome_counts)
would_go_negative = set(+to_draw_outcome_counts) - set(+self_outcome_counts)
if would_go_negative:
raise ValueError(f"outcomes to be drawn {would_go_negative} not in {self}")
zeros = set(self_outcome_counts) - set(new_outcome_counts)
for outcome in zeros:
new_outcome_counts[outcome] = 0
return type(self)(new_outcome_counts)
@experimental
@beartype
def exactly_k_times_in_n(
self,
outcome: RealLike,
n: SupportsInt,
k: SupportsInt,
) -> int:
r"""
!!! warning "Experimental"
This method should be considered experimental and may change or disappear in
future versions.
Computes (in constant time) and returns the number of times *outcome* appears
exactly *k* times among ``#!python n@self``. This is a more efficient
alternative to ``#!python (n@(self.eq(outcome)))[k]``.
``` python
>>> H(6).exactly_k_times_in_n(outcome=5, n=4, k=2)
150
>>> H((2, 3, 3, 4, 4, 5)).exactly_k_times_in_n(outcome=2, n=3, k=3)
1
>>> H((2, 3, 3, 4, 4, 5)).exactly_k_times_in_n(outcome=4, n=3, k=3)
8
```
"""
n = as_int(n)
k = as_int(k)
assert k <= n
c_outcome = self.get(outcome, 0)
return comb(n, k) * c_outcome**k * (self.total - c_outcome) ** (n - k)
@overload
def explode(
self,
max_depth: IntegralLike,
precision_limit: None = None,
) -> "H":
...
@overload
def explode(
self,
max_depth: None,
precision_limit: Union[RationalLikeMixedU, RealLike],
) -> "H":
...
@overload
def explode(
self,
max_depth: None = None,
*,
precision_limit: Union[RationalLikeMixedU, RealLike],
) -> "H":
...
@overload
def explode(
self,
max_depth: None = None,
precision_limit: None = None,
) -> "H":
...
@deprecated
@beartype
def explode(
self,
max_depth: Optional[IntegralLike] = None,
precision_limit: Optional[Union[RationalLikeMixedU, RealLike]] = None,
) -> "H":
r"""
!!! warning "Deprecated"
This method has been deprecated and will be removed in a future release. See
the [``explode`` function][dyce.evaluation.explode] for a more flexible
alternative.
Shorthand for ``#!python self.substitute(lambda h, outcome: outcome if len(h) == 1
else h if outcome == max(h) else outcome, operator.__add__, max_depth,
precision_limit)``.
``` python
>>> H(6).explode(max_depth=2)
H({1: 36, 2: 36, 3: 36, 4: 36, 5: 36, 7: 6, 8: 6, 9: 6, 10: 6, 11: 6, 13: 1, 14: 1, 15: 1, 16: 1, 17: 1, 18: 1})
```
This method guards against excessive recursion by returning ``#!python outcome``
if the passed histogram has only one face. See the [``substitute``
method][dyce.h.H.substitute].
"""
def _explode(h: H, outcome: RealLike) -> HOrOutcomeT:
return outcome if len(h) == 1 else h if outcome == max(h) else outcome
if max_depth is not None and precision_limit is not None:
raise ValueError("only one of max_depth and precision_limit is allowed")
elif max_depth is not None:
return self.substitute(_explode, __add__, max_depth)
elif precision_limit is not None:
return self.substitute(_explode, __add__, precision_limit=precision_limit)
else:
return self.substitute(_explode, __add__)
@beartype
def lowest_terms(self) -> "H":
r"""
Computes and returns a histogram whose nonzero counts share a greatest
common divisor of 1.
``` python
>>> df_obscured = H({-2: 0, -1: 2, 0: 2, 1: 2, 2: 0})
>>> df_obscured.lowest_terms()
H({-1: 1, 0: 1, 1: 1})
```
"""
if self._lowest_terms is None:
counts_gcd = gcd(*self.counts())
if counts_gcd in (0, 1) and 0 not in self.counts():
self._lowest_terms = self
else:
self._lowest_terms = type(self)(
(outcome, count // counts_gcd)
for outcome, count in self.items()
if count != 0
)
return self._lowest_terms
@experimental
@beartype
def order_stat_for_n_at_pos(self, n: SupportsInt, pos: SupportsInt) -> "H":
r"""
!!! warning "Experimental"
This method should be considered experimental and may change or disappear in
future versions.
Computes the probability distribution for each outcome appearing in at *pos* for
*n* histograms. *pos* is a zero-based index.
``` python
>>> d6avg = H((2, 3, 3, 4, 4, 5))
>>> d6avg.order_stat_for_n_at_pos(5, 3) # counts where outcome appears in the fourth of five positions
H({2: 26, 3: 1432, 4: 4792, 5: 1526})
```
The results show that, when rolling five six-sided “averaging” dice and sorting
each roll, there are 26 ways where ``#!python 2`` appears at the fourth (index
``#!python 3``) position, 1432 ways where ``#!python 3`` appears at the fourth
position, etc. This can be verified independently using the computationally
expensive method of enumerating rolls and counting those that meet the criteria.
``` python
>>> from dyce import P
>>> p_5d6avg = 5@P(d6avg)
>>> sum(count for roll, count in p_5d6avg.rolls_with_counts() if roll[3] == 5)
1526
```
Negative values for *pos* follow Python index semantics:
``` python
>>> d6 = H(6)
>>> d6.order_stat_for_n_at_pos(6, 0) == d6.order_stat_for_n_at_pos(6, -6)
True
>>> d6.order_stat_for_n_at_pos(6, 5) == d6.order_stat_for_n_at_pos(6, -1)
True
```
This method caches computing the betas for *n* so they can be reused for varying
values of *pos* in subsequent calls.
"""
# See <https://math.stackexchange.com/q/4173084/226394> for motivation
n = as_int(n)
pos = as_int(pos)
if n not in self._order_stat_funcs_by_n:
self._order_stat_funcs_by_n[n] = self._order_stat_func_for_n(n)
if pos < 0:
pos = n + pos
return self._order_stat_funcs_by_n[n](pos)
@beartype
def remove(self, outcome: RealLike) -> "H":
if outcome not in self:
return self
return type(self)(
(orig_outcome, count)
for orig_outcome, count in self.items()
if orig_outcome != outcome
)
@overload
def substitute(
self,
expand: _SubstituteExpandCallbackT,
coalesce: _SubstituteCoalesceCallbackT = coalesce_replace,
) -> "H":
...
@overload
def substitute(
self,
expand: _SubstituteExpandCallbackT,
coalesce: _SubstituteCoalesceCallbackT = coalesce_replace,
*,
max_depth: IntegralLike,
precision_limit: None = None,
) -> "H":
...
@overload
def substitute(
self,
expand: _SubstituteExpandCallbackT,
coalesce: _SubstituteCoalesceCallbackT,
max_depth: IntegralLike,
precision_limit: None = None,
) -> "H":
...
@overload
def substitute(
self,
expand: _SubstituteExpandCallbackT,
coalesce: _SubstituteCoalesceCallbackT = coalesce_replace,
*,
max_depth: None,
precision_limit: Union[RationalLikeMixedU, RealLike],
) -> "H":
...
@overload
def substitute(
self,
expand: _SubstituteExpandCallbackT,
coalesce: _SubstituteCoalesceCallbackT,
max_depth: None,
precision_limit: Union[RationalLikeMixedU, RealLike],
) -> "H":
...
@overload
def substitute(
self,
expand: _SubstituteExpandCallbackT,
coalesce: _SubstituteCoalesceCallbackT = coalesce_replace,
max_depth: None = None,
*,
precision_limit: Union[RationalLikeMixedU, RealLike],
) -> "H":
...
@overload
def substitute(
self,
expand: _SubstituteExpandCallbackT,
coalesce: _SubstituteCoalesceCallbackT = coalesce_replace,
max_depth: None = None,
precision_limit: None = None,
) -> "H":
...
@deprecated
@beartype
def substitute(
self,
expand: _SubstituteExpandCallbackT,
coalesce: _SubstituteCoalesceCallbackT = coalesce_replace,
max_depth: Optional[IntegralLike] = None,
precision_limit: Optional[Union[RationalLikeMixedU, RealLike]] = None,
) -> "H":
r"""
!!! warning "Deprecated"
This method has been deprecated and will be removed in a future release. See
the [``expandable`` decorator][dyce.evaluation.expandable] and
[``foreach`` function][dyce.evaluation.foreach] for more flexible
alternatives.
Calls *expand* on each outcome. If *expand* returns a single outcome, it
replaces the existing outcome. If it returns an [``H`` object][dyce.h.H],
evaluation is performed again (recursively) on that object until a limit (either
*max_depth* or *precision_limit*) is exhausted. *coalesce* is called on the
original outcome and the expanded histogram or outcome and the returned
histogram is “folded” into result. The default behavior for *coalesce* is to
replace the outcome with the expanded histogram. Returned histograms are always
reduced to their lowest terms.
!!! note "*coalesce* is not called unless *expand* returns a histogram"
If *expand* returns a single outcome, it *always* replaces the existing
outcome. This is intentional. To return a single outcome, but trigger
*coalesce*, characterize that outcome as a single-sided die (e.g.,
``#!python H({outcome: 1})``.
See the [``coalesce_replace``][dyce.h.coalesce_replace] and
[``lowest_terms``][dyce.h.H.lowest_terms] methods.
!!! tip "Precision limits"
The *max_depth* parameter is similar to an
[``expandable``][dyce.evaluation.expandable]-decorated function’s limit
argument when given a whole number. The *precision_limit* parameter is
similar to an [``expandable``][dyce.evaluation.expandable]-decorated
function’s limit argument being provided a fractional value. It is an error
to provide values for both *max_depth* and *precision_limit*.
"""
from .evaluation import HResult, LimitT, expandable
if max_depth is not None and precision_limit is not None:
raise ValueError("only one of max_depth and precision_limit is allowed")
limit: Optional[LimitT] = (
max_depth if precision_limit is None else precision_limit
)
@expandable(sentinel=self)
def _expand(result: HResult) -> HOrOutcomeT:
res = expand(result.h, result.outcome)
return coalesce(_expand(res), result.outcome) if isinstance(res, H) else res
return _expand(self, limit=limit)
@beartype
def vs(self, other: _OperandT) -> "H":
r"""
Compares the histogram with *other*. -1 represents where *other* is greater. 0
represents where they are equal. 1 represents where *other* is less.
Shorthand for ``#!python self.within(0, 0, other)``.
``` python
>>> H(6).vs(H(4))
H({-1: 6, 0: 4, 1: 14})
>>> H(6).vs(H(4)) == H(6).within(0, 0, H(4))
True
```
See the [``within`` method][dyce.h.H.within].
"""
return self.within(0, 0, other)
@beartype
def within(self, lo: RealLike, hi: RealLike, other: _OperandT = 0) -> "H":
r"""
Computes the difference between the histogram and *other*. -1 represents where that
difference is less than *lo*. 0 represents where that difference between *lo*
and *hi* (inclusive). 1 represents where that difference is greater than *hi*.
``` python
>>> d6_2 = 2@H(6)
>>> d6_2.within(7, 9)
H({-1: 15, 0: 15, 1: 6})
>>> print(d6_2.within(7, 9).format())
avg | -0.25
std | 0.72
var | 0.52
-1 | 41.67% |####################
0 | 41.67% |####################
1 | 16.67% |########
```
``` python
>>> d6_3, d8_2 = 3@H(6), 2@H(8)
>>> d6_3.within(-1, 1, d8_2) # 3d6 w/in 1 of 2d8
H({-1: 3500, 0: 3412, 1: 6912})
>>> print(d6_3.within(-1, 1, d8_2).format())
avg | 0.25
std | 0.83
var | 0.69
-1 | 25.32% |############
0 | 24.68% |############
1 | 50.00% |#########################
```
"""
return self.map(_within(lo, hi), other)
@beartype
def zero_fill(self, outcomes: Iterable[RealLike]) -> "H":
r"""
Shorthand for ``#!python self.accumulate({outcome: 0 for outcome in
outcomes})``.
``` python
>>> H(4).zero_fill(H(8).outcomes())
H({1: 1, 2: 1, 3: 1, 4: 1, 5: 0, 6: 0, 7: 0, 8: 0})
```
"""
return self.accumulate({outcome: 0 for outcome in outcomes})
@overload
def distribution(
self,
) -> Iterator[tuple[RealLike, Fraction]]:
...
@overload
def distribution(
self,
rational_t: Callable[[int, int], _T],
) -> Iterator[tuple[RealLike, _T]]:
...
@experimental
@beartype
def distribution(
self,
rational_t: Optional[Callable[[int, int], _T]] = None,
) -> Iterator[tuple[RealLike, _T]]:
r"""
Presentation helper function returning an iterator for each outcome/count or
outcome/probability pair.
``` python
>>> h = H((1, 2, 3, 3, 4, 4, 5, 6))
>>> list(h.distribution())
[(1, Fraction(1, 8)), (2, Fraction(1, 8)), (3, Fraction(1, 4)), (4, Fraction(1, 4)), (5, Fraction(1, 8)), (6, Fraction(1, 8))]
>>> list(h.ge(3).distribution())
[(False, Fraction(1, 4)), (True, Fraction(3, 4))]
```
!!! warning "Experimental"
The *rational_t* argument to this method should be considered experimental
and may change or disappear in future versions.
If provided, *rational_t* must be a callable that takes two ``#!python int``s (a
numerator and denominator) and returns an instance of a desired (but otherwise
arbitrary) type.
``` python
>>> list(h.distribution(rational_t=lambda n, d: f"{n}/{d}"))
[(1, '1/8'), (2, '1/8'), (3, '2/8'), (4, '2/8'), (5, '1/8'), (6, '1/8')]
```
``` python
>>> import sympy
>>> list(h.distribution(rational_t=sympy.Rational))
[(1, 1/8), (2, 1/8), (3, 1/4), (4, 1/4), (5, 1/8), (6, 1/8)]
```
``` python
>>> import sage.rings.rational # doctest: +SKIP
>>> list(h.distribution(rational_t=lambda n, d: sage.rings.rational.Rational((n, d)))) # doctest: +SKIP
[(1, 1/8), (2, 1/8), (3, 1/4), (4, 1/4), (5, 1/8), (6, 1/8)]
```
!!! note
The arguments passed to *rational_t* are not reduced to the lowest terms.
The *rational_t* argument is a convenience. Iteration or comprehension can be
used to accomplish something similar.
``` python
>>> [(outcome, f"{probability.numerator}/{probability.denominator}") for outcome, probability in (h).distribution()]
[(1, '1/8'), (2, '1/8'), (3, '1/4'), (4, '1/4'), (5, '1/8'), (6, '1/8')]
```
Many number implementations can convert directly from ``#!python
fractions.Fraction``s.
``` python
>>> import sympy.abc
>>> [(outcome, sympy.Rational(probability)) for outcome, probability in (h + sympy.abc.x).distribution()]
[(x + 1, 1/8), (x + 2, 1/8), (x + 3, 1/4), (x + 4, 1/4), (x + 5, 1/8), (x + 6, 1/8)]
```
``` python
>>> import sage.rings.rational # doctest: +SKIP
>>> [(outcome, sage.rings.rational.Rational(probability)) for outcome, probability in h.distribution()] # doctest: +SKIP
[(1, 1/6), (2, 1/6), (3, 1/3), (4, 1/3), (5, 1/6), (6, 1/6)]
```
"""
if rational_t is None:
# TODO(posita): See <https://github.com/python/mypy/issues/10854#issuecomment-1663057450>
rational_t = Fraction # type: ignore [assignment]
assert rational_t is not None
total = sum(self.values()) or 1
return (
(outcome, rational_t(self[outcome], total))
for outcome in sorted_outcomes(self)
)
@beartype
def distribution_xy(
self,
) -> tuple[tuple[RealLike, ...], tuple[float, ...]]:
r"""
Presentation helper function returning an iterator for a “zipped” arrangement of the
output from the [``distribution`` method][dyce.h.H.distribution] and ensures the
values are ``#!python float``s.
``` python
>>> list(H(6).distribution())
[(1, Fraction(1, 6)), (2, Fraction(1, 6)), (3, Fraction(1, 6)), (4, Fraction(1, 6)), (5, Fraction(1, 6)), (6, Fraction(1, 6))]
>>> H(6).distribution_xy()
((1, 2, 3, 4, 5, 6), (0.16666666, 0.16666666, 0.16666666, 0.16666666, 0.16666666, 0.16666666))
```
"""
# TODO(posita): See <https://github.com/python/typing/issues/193>
return tuple( # type: ignore [return-value]
zip(
*(
(outcome, float(probability))
for outcome, probability in self.distribution()
)
)
)
@beartype
def format(
self,
width: SupportsInt = _ROW_WIDTH,
scaled: bool = False,
tick: str = "#",
sep: str = os.linesep,
) -> str:
r"""
Returns a formatted string representation of the histogram. If *width* is
greater than zero, a horizontal bar ASCII graph is printed using *tick* and
*sep* (which are otherwise ignored if *width* is zero or less).
``` python
>>> print(H(6).format(width=0))
{avg: 3.50, 1: 16.67%, 2: 16.67%, 3: 16.67%, 4: 16.67%, 5: 16.67%, 6: 16.67%}
```
``` python
>>> print((2@H(6)).zero_fill(range(1, 21)).format(tick="@"))
avg | 7.00
std | 2.42
var | 5.83
1 | 0.00% |
2 | 2.78% |@
3 | 5.56% |@@
4 | 8.33% |@@@@
5 | 11.11% |@@@@@
6 | 13.89% |@@@@@@
7 | 16.67% |@@@@@@@@
8 | 13.89% |@@@@@@
9 | 11.11% |@@@@@
10 | 8.33% |@@@@
11 | 5.56% |@@
12 | 2.78% |@
13 | 0.00% |
14 | 0.00% |
15 | 0.00% |
16 | 0.00% |
17 | 0.00% |
18 | 0.00% |
19 | 0.00% |
20 | 0.00% |
```
If *scaled* is ``#!python True``, horizontal bars are scaled to *width*.
``` python
>>> h = (2@H(6)).ge(7)
>>> print(f"{' 65 chars wide -->|':->65}")
---------------------------------------------- 65 chars wide -->|
>>> print(H(1).format(scaled=False))
avg | 1.00
std | 0.00
var | 0.00
1 | 100.00% |##################################################
>>> print(h.format(scaled=False))
avg | 0.58
std | 0.49
var | 0.24
0 | 41.67% |####################
1 | 58.33% |#############################
>>> print(h.format(scaled=True))
avg | 0.58
std | 0.49
var | 0.24
0 | 41.67% |###################################
1 | 58.33% |##################################################
```
"""
width = as_int(width)
# We convert various values herein to native ints and floats because number
# tower implementations sometimes neglect to implement __format__ properly (or
# at all). (I'm looking at you, sage.rings.…!)
try:
mu: RealLike = float(self.mean())
except (OverflowError, TypeError):
mu = self.mean()
if width <= 0:
def _parts() -> Iterator[str]:
yield f"avg: {mu:.2f}"
for (
outcome,
probability,
) in self.distribution():
probability_f = float(probability)
yield f"{outcome}:{probability_f:7.2%}"
return "{" + ", ".join(_parts()) + "}"
else:
w = width - 15
def _lines() -> Iterator[str]:
try:
yield f"avg | {mu:7.2f}"
std = float(self.stdev(mu))
var = float(self.variance(mu))
yield f"std | {std:7.2f}"
yield f"var | {var:7.2f}"
except (OverflowError, TypeError) as exc:
warnings.warn(f"{str(exc)}; mu: {mu}")
if self:
outcomes, probabilities = self.distribution_xy()
tick_scale = max(probabilities) if scaled else 1.0
for outcome, probability in zip(outcomes, probabilities):
try:
outcome_str = f"{outcome: 3}"
except (TypeError, ValueError):
outcome_str = str(outcome)
outcome_str = f"{outcome_str: >3}"
ticks = tick * int(w * probability / tick_scale)
probability_f = float(probability)
yield f"{outcome_str} | {probability_f:7.2%} |{ticks}"
return sep.join(_lines())
@beartype
def mean(self) -> RealLike:
r"""
Returns the mean of the weighted outcomes (or 0.0 if there are no outcomes).
"""
numerator: float
denominator: float
numerator = denominator = 0
for outcome, count in self.items():
numerator += outcome * count
denominator += count
return numerator / (denominator or 1)
@beartype
def stdev(self, mu: Optional[RealLike] = None) -> RealLike:
r"""
Shorthand for ``#!python math.sqrt(self.variance(mu))``.
"""
return sqrt(self.variance(mu))
@beartype
def variance(self, mu: Optional[RealLike] = None) -> RealLike:
r"""
Returns the variance of the weighted outcomes. If provided, *mu* is used as the mean
(to avoid duplicate computation).
"""
mu = mu if mu else self.mean()
numerator: float
denominator: float
numerator = denominator = 0
for outcome, count in self.items():
numerator += outcome**2 * count
denominator += count
# While floating point overflow is impossible to eliminate, we avoid it under
# some circumstances by exploiting the equivalence of E[(X - E[X])**2] and the
# more efficient E[X**2] - E[X]**2. See
# <https://dlsun.github.io/probability/variance.html>.
return numerator / (denominator or 1) - mu**2
@beartype
def roll(self) -> RealLike:
r"""
Returns a (weighted) random outcome.
"""
return (
rng.RNG.choices(
population=tuple(self.outcomes()),
weights=tuple(self.counts()),
k=1,
)[0]
if self
else 0
)
def _order_stat_func_for_n(self, n: int) -> Callable[[int], "H"]:
betas_by_outcome: dict[RealLike, tuple[H, H]] = {}
for outcome in self.outcomes():
betas_by_outcome[outcome] = (
n @ self.le(outcome),
n @ self.lt(outcome),
)
def _gen_h_items_at_pos(pos: int) -> Iterator[_OutcomeCountT]:
for outcome, (h_le, h_lt) in betas_by_outcome.items():
yield (
outcome,
h_le.gt(pos).get(True, 0) - h_lt.gt(pos).get(True, 0),
)
@beartype
def order_stat_for_n_at_pos(pos: int) -> "H":
return type(self)(_gen_h_items_at_pos(pos))
return order_stat_for_n_at_pos
|