File size: 61,504 Bytes
7885a28 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 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 |
# Temporary file separated from _distribution_infrastructure.py
# to simplify the diff during PR review.
from abc import ABC, abstractmethod
class _ProbabilityDistribution(ABC):
@abstractmethod
def support(self):
r"""Support of the random variable
The support of a random variable is set of all possible outcomes;
i.e., the subset of the domain of argument :math:`x` for which
the probability density function :math:`f(x)` is nonzero.
This function returns lower and upper bounds of the support.
Returns
-------
out : tuple of Array
The lower and upper bounds of the support.
See Also
--------
pdf
References
----------
.. [1] Support (mathematics), *Wikipedia*,
https://en.wikipedia.org/wiki/Support_(mathematics)
Notes
-----
Suppose a continuous probability distribution has support ``(l, r)``.
The following table summarizes the value returned by methods
of ``ContinuousDistribution`` for arguments outside the support.
+----------------+---------------------+---------------------+
| Method | Value for ``x < l`` | Value for ``x > r`` |
+================+=====================+=====================+
| ``pdf(x)`` | 0 | 0 |
+----------------+---------------------+---------------------+
| ``logpdf(x)`` | -inf | -inf |
+----------------+---------------------+---------------------+
| ``cdf(x)`` | 0 | 1 |
+----------------+---------------------+---------------------+
| ``logcdf(x)`` | -inf | 0 |
+----------------+---------------------+---------------------+
| ``ccdf(x)`` | 1 | 0 |
+----------------+---------------------+---------------------+
| ``logccdf(x)`` | 0 | -inf |
+----------------+---------------------+---------------------+
For the ``cdf`` and related methods, the inequality need not be
strict; i.e. the tabulated value is returned when the method is
evaluated *at* the corresponding boundary.
The following table summarizes the value returned by the inverse
methods of ``ContinuousDistribution`` for arguments at the boundaries
of the domain ``0`` to ``1``.
+-------------+-----------+-----------+
| Method | ``x = 0`` | ``x = 1`` |
+=============+===========+===========+
| ``icdf(x)`` | ``l`` | ``r`` |
+-------------+-----------+-----------+
| ``icdf(x)`` | ``r`` | ``l`` |
+-------------+-----------+-----------+
For the inverse log-functions, the same values are returned for
for ``x = log(0)`` and ``x = log(1)``. All inverse functions return
``nan`` when evaluated at an argument outside the domain ``0`` to ``1``.
Examples
--------
Instantiate a distribution with the desired parameters:
>>> from scipy import stats
>>> X = stats.Uniform(a=-0.5, b=0.5)
Retrieve the support of the distribution:
>>> X.support()
(-0.5, 0.5)
For a distribution with infinite support,
>>> X = stats.Normal()
>>> X.support()
(-inf, inf)
Due to underflow, the numerical value returned by the PDF may be zero
even for arguments within the support, even if the true value is
nonzero. In such cases, the log-PDF may be useful.
>>> X.pdf([-100., 100.])
array([0., 0.])
>>> X.logpdf([-100., 100.])
array([-5000.91893853, -5000.91893853])
Use cases for the log-CDF and related methods are analogous.
"""
raise NotImplementedError()
@abstractmethod
def sample(self, shape, *, method, rng):
r"""Random sample from the distribution.
Parameters
----------
shape : tuple of ints, default: ()
The shape of the sample to draw. If the parameters of the distribution
underlying the random variable are arrays of shape ``param_shape``,
the output array will be of shape ``shape + param_shape``.
method : {None, 'formula', 'inverse_transform'}
The strategy used to produce the sample. By default (``None``),
the infrastructure chooses between the following options,
listed in order of precedence.
- ``'formula'``: an implementation specific to the distribution
- ``'inverse_transform'``: generate a uniformly distributed sample and
return the inverse CDF at these arguments.
Not all `method` options are available for all distributions.
If the selected `method` is not available, a `NotImplementedError``
will be raised.
rng : `numpy.random.Generator` or `scipy.stats.QMCEngine`, optional
Pseudo- or quasi-random number generator state. When `rng` is None,
a new `numpy.random.Generator` is created using entropy from the
operating system. Types other than `numpy.random.Generator` and
`scipy.stats.QMCEngine` are passed to `numpy.random.default_rng`
to instantiate a ``Generator``.
If `rng` is an instance of `scipy.stats.QMCEngine` configured to use
scrambling and `shape` is not empty, then each slice along the zeroth
axis of the result is a "quasi-independent", low-discrepancy sequence;
that is, they are distinct sequences that can be treated as statistically
independent for most practical purposes. Separate calls to `sample`
produce new quasi-independent, low-discrepancy sequences.
References
----------
.. [1] Sampling (statistics), *Wikipedia*,
https://en.wikipedia.org/wiki/Sampling_(statistics)
Examples
--------
Instantiate a distribution with the desired parameters:
>>> import numpy as np
>>> from scipy import stats
>>> X = stats.Uniform(a=0., b=1.)
Generate a pseudorandom sample:
>>> x = X.sample((1000, 1))
>>> octiles = (np.arange(8) + 1) / 8
>>> np.count_nonzero(x <= octiles, axis=0)
array([ 148, 263, 387, 516, 636, 751, 865, 1000]) # may vary
>>> X = stats.Uniform(a=np.zeros((3, 1)), b=np.ones(2))
>>> X.a.shape,
(3, 2)
>>> x = X.sample(shape=(5, 4))
>>> x.shape
(5, 4, 3, 2)
"""
raise NotImplementedError()
@abstractmethod
def moment(self, order, kind, *, method):
r"""Raw, central, or standard moment of positive integer order.
In terms of probability density function :math:`f(x)` and support
:math:`\chi`, the "raw" moment (about the origin) of order :math:`n` of
a random variable :math:`X` is:
.. math::
\mu'_n(X) = \int_{\chi} x^n f(x) dx
The "central" moment is the raw moment taken about the mean,
:math:`\mu = \mu'_1`:
.. math::
\mu_n(X) = \int_{\chi} (x - \mu) ^n f(x) dx
The "standardized" moment is the central moment normalized by the
:math:`n^\text{th}` power of the standard deviation
:math:`\sigma = \sqrt{\mu_2}` to produce a scale invariant quantity:
.. math::
\tilde{\mu}_n(X) = \frac{\mu_n(X)}
{\sigma^n}
Parameters
----------
order : int
The integer order of the moment; i.e. :math:`n` in the formulae above.
kind : {'raw', 'central', 'standardized'}
Whether to return the raw (default), central, or standardized moment
defined above.
method : {None, 'formula', 'general', 'transform', 'normalize', 'quadrature', 'cache'}
The strategy used to evaluate the moment. By default (``None``),
the infrastructure chooses between the following options,
listed in order of precedence.
- ``'cache'``: use the value of the moment most recently calculated
via another method
- ``'formula'``: use a formula for the moment itself
- ``'general'``: use a general result that is true for all distributions
with finite moments; for instance, the zeroth raw moment is
identically 1
- ``'transform'``: transform a raw moment to a central moment or
vice versa (see Notes)
- ``'normalize'``: normalize a central moment to get a standardized
or vice versa
- ``'quadrature'``: numerically integrate according to the definition
Not all `method` options are available for all orders, kinds, and
distributions. If the selected `method` is not available, a
``NotImplementedError`` will be raised.
Returns
-------
out : array
The moment of the random variable of the specified order and kind.
See Also
--------
pdf
mean
variance
standard_deviation
skewness
kurtosis
Notes
-----
Not all distributions have finite moments of all orders; moments of some
orders may be undefined or infinite. If a formula for the moment is not
specifically implemented for the chosen distribution, SciPy will attempt
to compute the moment via a generic method, which may yield a finite
result where none exists. This is not a critical bug, but an opportunity
for an enhancement.
The definition of a raw moment in the summary is specific to the raw moment
about the origin. The raw moment about any point :math:`a` is:
.. math::
E[(X-a)^n] = \int_{\chi} (x-a)^n f(x) dx
In this notation, a raw moment about the origin is :math:`\mu'_n = E[x^n]`,
and a central moment is :math:`\mu_n = E[(x-\mu)^n]`, where :math:`\mu`
is the first raw moment; i.e. the mean.
The ``'transform'`` method takes advantage of the following relationships
between moments taken about different points :math:`a` and :math:`b`.
.. math::
E[(X-b)^n] = \sum_{i=0}^n E[(X-a)^i] {n \choose i} (a - b)^{n-i}
For instance, to transform the raw moment to the central moment, we let
:math:`b = \mu` and :math:`a = 0`.
The distribution infrastructure provides flexibility for distribution
authors to implement separate formulas for raw moments, central moments,
and standardized moments of any order. By default, the moment of the
desired order and kind is evaluated from the formula if such a formula
is available; if not, the infrastructure uses any formulas that are
available rather than resorting directly to numerical integration.
For instance, if formulas for the first three raw moments are
available and the third standardized moments is desired, the
infrastructure will evaluate the raw moments and perform the transforms
and standardization required. The decision tree is somewhat complex,
but the strategy for obtaining a moment of a given order and kind
(possibly as an intermediate step due to the recursive nature of the
transform formula above) roughly follows this order of priority:
#. Use cache (if order of same moment and kind has been calculated)
#. Use formula (if available)
#. Transform between raw and central moment and/or normalize to convert
between central and standardized moments (if efficient)
#. Use a generic result true for most distributions (if available)
#. Use quadrature
References
----------
.. [1] Moment, *Wikipedia*,
https://en.wikipedia.org/wiki/Moment_(mathematics)
Examples
--------
Instantiate a distribution with the desired parameters:
>>> from scipy import stats
>>> X = stats.Normal(mu=1., sigma=2.)
Evaluate the first raw moment:
>>> X.moment(order=1, kind='raw')
1.0
>>> X.moment(order=1, kind='raw') == X.mean() == X.mu
True
Evaluate the second central moment:
>>> X.moment(order=2, kind='central')
4.0
>>> X.moment(order=2, kind='central') == X.variance() == X.sigma**2
True
Evaluate the fourth standardized moment:
>>> X.moment(order=4, kind='standardized')
3.0
>>> X.moment(order=4, kind='standardized') == X.kurtosis(convention='non-excess')
True
""" # noqa:E501
raise NotImplementedError()
@abstractmethod
def mean(self, *, method):
r"""Mean (raw first moment about the origin)
Parameters
----------
method : {None, 'formula', 'transform', 'quadrature', 'cache'}
Method used to calculate the raw first moment. Not
all methods are available for all distributions. See
`moment` for details.
See Also
--------
moment
median
mode
References
----------
.. [1] Mean, *Wikipedia*,
https://en.wikipedia.org/wiki/Mean#Mean_of_a_probability_distribution
Examples
--------
Instantiate a distribution with the desired parameters:
>>> from scipy import stats
>>> X = stats.Normal(mu=1., sigma=2.)
Evaluate the variance:
>>> X.mean()
1.0
>>> X.mean() == X.moment(order=1, kind='raw') == X.mu
True
"""
raise NotImplementedError()
@abstractmethod
def median(self, *, method):
r"""Median (50th percentil)
If a continuous random variable :math:`X` has probability :math:`0.5` of
taking on a value less than :math:`m`, then :math:`m` is the median.
That is, the median is the value :math:`m` for which:
.. math::
P(X ≤ m) = 0.5 = P(X ≥ m)
Parameters
----------
method : {None, 'formula', 'icdf'}
The strategy used to evaluate the median.
By default (``None``), the infrastructure chooses between the
following options, listed in order of precedence.
- ``'formula'``: use a formula for the median
- ``'icdf'``: evaluate the inverse CDF of 0.5
Not all `method` options are available for all distributions.
If the selected `method` is not available, a ``NotImplementedError``
will be raised.
Returns
-------
out : array
The median
See Also
--------
mean
mode
icdf
References
----------
.. [1] Median, *Wikipedia*,
https://en.wikipedia.org/wiki/Median#Probability_distributions
Examples
--------
Instantiate a distribution with the desired parameters:
>>> from scipy import stats
>>> X = stats.Uniform(a=0., b=10.)
Compute the median:
>>> X.median()
np.float64(5.0)
>>> X.median() == X.icdf(0.5) == X.iccdf(0.5)
True
"""
raise NotImplementedError()
@abstractmethod
def mode(self, *, method):
r"""Mode (most likely value)
Informally, the mode is a value that a random variable has the highest
probability (density) of assuming. That is, the mode is the element of
the support :math:`\chi` that maximizes the probability density
function :math:`f(x)`:
.. math::
\text{mode} = \arg\max_{x \in \chi} f(x)
Parameters
----------
method : {None, 'formula', 'optimization'}
The strategy used to evaluate the mode.
By default (``None``), the infrastructure chooses between the
following options, listed in order of precedence.
- ``'formula'``: use a formula for the median
- ``'optimization'``: numerically maximize the PDF
Not all `method` options are available for all distributions.
If the selected `method` is not available, a ``NotImplementedError``
will be raised.
Returns
-------
out : array
The mode
See Also
--------
mean
median
pdf
Notes
-----
For some distributions
#. the mode is not unique (e.g. the uniform distribution);
#. the PDF has one or more singularities, and it is debateable whether
a singularity is considered to be in the domain and called the mode
(e.g. the gamma distribution with shape parameter less than 1); and/or
#. the probability density function may have one or more local maxima
that are not a global maximum (e.g. mixture distributions).
In such cases, `mode` will
#. return a single value,
#. consider the mode to occur at a singularity, and/or
#. return a local maximum which may or may not be a global maximum.
If a formula for the mode is not specifically implemented for the
chosen distribution, SciPy will attempt to compute the mode
numerically, which may not meet the user's preferred definition of a
mode. In such cases, the user is encouraged to subclass the
distribution and override ``mode``.
References
----------
.. [1] Mode (statistics), *Wikipedia*,
https://en.wikipedia.org/wiki/Mode_(statistics)
Examples
--------
Instantiate a distribution with the desired parameters:
>>> from scipy import stats
>>> X = stats.Normal(mu=1., sigma=2.)
Evaluate the mode:
>>> X.mode()
1.0
If the mode is not uniquely defined, ``mode`` nonetheless returns a
single value.
>>> X = stats.Uniform(a=0., b=1.)
>>> X.mode()
0.5
If this choice does not satisfy your requirements, subclass the
distribution and override ``mode``:
>>> class BetterUniform(stats.Uniform):
... def mode(self):
... return self.b
>>> X = BetterUniform(a=0., b=1.)
>>> X.mode()
1.0
"""
raise NotImplementedError()
@abstractmethod
def variance(self, *, method):
r"""Variance (central second moment)
Parameters
----------
method : {None, 'formula', 'transform', 'normalize', 'quadrature', 'cache'}
Method used to calculate the central second moment. Not
all methods are available for all distributions. See
`moment` for details.
See Also
--------
moment
standard_deviation
mean
References
----------
.. [1] Variance, *Wikipedia*,
https://en.wikipedia.org/wiki/Variance#Absolutely_continuous_random_variable
Examples
--------
Instantiate a distribution with the desired parameters:
>>> from scipy import stats
>>> X = stats.Normal(mu=1., sigma=2.)
Evaluate the variance:
>>> X.variance()
4.0
>>> X.variance() == X.moment(order=2, kind='central') == X.sigma**2
True
"""
raise NotImplementedError()
@abstractmethod
def standard_deviation(self, *, method):
r"""Standard deviation (square root of the second central moment)
Parameters
----------
method : {None, 'formula', 'transform', 'normalize', 'quadrature', 'cache'}
Method used to calculate the central second moment. Not
all methods are available for all distributions. See
`moment` for details.
See Also
--------
variance
mean
moment
References
----------
.. [1] Standard deviation, *Wikipedia*,
https://en.wikipedia.org/wiki/Standard_deviation#Definition_of_population_values
Examples
--------
Instantiate a distribution with the desired parameters:
>>> from scipy import stats
>>> X = stats.Normal(mu=1., sigma=2.)
Evaluate the standard deviation:
>>> X.standard_deviation()
2.0
>>> X.standard_deviation() == X.moment(order=2, kind='central')**0.5 == X.sigma
True
"""
raise NotImplementedError()
@abstractmethod
def skewness(self, *, method):
r"""Skewness (standardized third moment)
Parameters
----------
method : {None, 'formula', 'general', 'transform', 'normalize', 'cache'}
Method used to calculate the standardized third moment. Not
all methods are available for all distributions. See
`moment` for details.
See Also
--------
moment
mean
variance
References
----------
.. [1] Skewness, *Wikipedia*,
https://en.wikipedia.org/wiki/Skewness
Examples
--------
Instantiate a distribution with the desired parameters:
>>> from scipy import stats
>>> X = stats.Normal(mu=1., sigma=2.)
Evaluate the skewness:
>>> X.skewness()
0.0
>>> X.skewness() == X.moment(order=3, kind='standardized')
True
"""
raise NotImplementedError()
@abstractmethod
def kurtosis(self, *, method):
r"""Kurtosis (standardized fourth moment)
By default, this is the standardized fourth moment, also known as the
"non-excess" or "Pearson" kurtosis (e.g. the kurtosis of the normal
distribution is 3). The "excess" or "Fisher" kurtosis (the standardized
fourth moment minus 3) is available via the `convention` parameter.
Parameters
----------
method : {None, 'formula', 'general', 'transform', 'normalize', 'cache'}
Method used to calculate the standardized fourth moment. Not
all methods are available for all distributions. See
`moment` for details.
convention : {'non-excess', 'excess'}
Two distinct conventions are available:
- ``'non-excess'``: the standardized fourth moment (Pearson's kurtosis)
- ``'excess'``: the standardized fourth moment minus 3 (Fisher's kurtosis)
The default is ``'non-excess'``.
See Also
--------
moment
mean
variance
References
----------
.. [1] Kurtosis, *Wikipedia*,
https://en.wikipedia.org/wiki/Kurtosis
Examples
--------
Instantiate a distribution with the desired parameters:
>>> from scipy import stats
>>> X = stats.Normal(mu=1., sigma=2.)
Evaluate the kurtosis:
>>> X.kurtosis()
3.0
>>> (X.kurtosis()
... == X.kurtosis(convention='excess') + 3.
... == X.moment(order=4, kind='standardized'))
True
"""
raise NotImplementedError()
@abstractmethod
def pdf(self, x, /, *, method):
r"""Probability density function
The probability density function ("PDF"), denoted :math:`f(x)`, is the
probability *per unit length* that the random variable will assume the
value :math:`x`. Mathematically, it can be defined as the derivative
of the cumulative distribution function :math:`F(x)`:
.. math::
f(x) = \frac{d}{dx} F(x)
`pdf` accepts `x` for :math:`x`.
Parameters
----------
x : array_like
The argument of the PDF.
method : {None, 'formula', 'logexp'}
The strategy used to evaluate the PDF. By default (``None``), the
infrastructure chooses between the following options, listed in
order of precedence.
- ``'formula'``: use a formula for the PDF itself
- ``'logexp'``: evaluate the log-PDF and exponentiate
Not all `method` options are available for all distributions.
If the selected `method` is not available, a ``NotImplementedError``
will be raised.
Returns
-------
out : array
The PDF evaluated at the argument `x`.
See Also
--------
cdf
logpdf
Notes
-----
Suppose a continuous probability distribution has support :math:`[l, r]`.
By definition of the support, the PDF evaluates to its minimum value
of :math:`0` outside the support; i.e. for :math:`x < l` or
:math:`x > r`. The maximum of the PDF may be less than or greater than
:math:`1`; since the valus is a probability *density*, only its integral
over the support must equal :math:`1`.
References
----------
.. [1] Probability density function, *Wikipedia*,
https://en.wikipedia.org/wiki/Probability_density_function
Examples
--------
Instantiate a distribution with the desired parameters:
>>> from scipy import stats
>>> X = stats.Uniform(a=-1., b=1.)
Evaluate the PDF at the desired argument:
>>> X.pdf(0.25)
0.5
"""
raise NotImplementedError()
@abstractmethod
def logpdf(self, x, /, *, method):
r"""Log of the probability density function
The probability density function ("PDF"), denoted :math:`f(x)`, is the
probability *per unit length* that the random variable will assume the
value :math:`x`. Mathematically, it can be defined as the derivative
of the cumulative distribution function :math:`F(x)`:
.. math::
f(x) = \frac{d}{dx} F(x)
`logpdf` computes the logarithm of the probability density function
("log-PDF"), :math:`\log(f(x))`, but it may be numerically favorable
compared to the naive implementation (computing :math:`f(x)` and
taking the logarithm).
`logpdf` accepts `x` for :math:`x`.
Parameters
----------
x : array_like
The argument of the log-PDF.
method : {None, 'formula', 'logexp'}
The strategy used to evaluate the log-PDF. By default (``None``), the
infrastructure chooses between the following options, listed in order
of precedence.
- ``'formula'``: use a formula for the log-PDF itself
- ``'logexp'``: evaluate the PDF and takes its logarithm
Not all `method` options are available for all distributions.
If the selected `method` is not available, a ``NotImplementedError``
will be raised.
Returns
-------
out : array
The log-PDF evaluated at the argument `x`.
See Also
--------
pdf
logcdf
Notes
-----
Suppose a continuous probability distribution has support :math:`[l, r]`.
By definition of the support, the log-PDF evaluates to its minimum value
of :math:`-\infty` (i.e. :math:`\log(0)`) outside the support; i.e. for
:math:`x < l` or :math:`x > r`. The maximum of the log-PDF may be less
than or greater than :math:`\log(1) = 0` because the maximum of the PDF
can be any positive real.
For distributions with infinite support, it is common for `pdf` to return
a value of ``0`` when the argument is theoretically within the support;
this can occur because the true value of the PDF is too small to be
represented by the chosen dtype. The log-PDF, however, will often be finite
(not ``-inf``) over a much larger domain. Consequently, it may be preferred
to work with the logarithms of probabilities and probability densities to
avoid underflow.
References
----------
.. [1] Probability density function, *Wikipedia*,
https://en.wikipedia.org/wiki/Probability_density_function
Examples
--------
Instantiate a distribution with the desired parameters:
>>> import numpy as np
>>> from scipy import stats
>>> X = stats.Uniform(a=-1.0, b=1.0)
Evaluate the log-PDF at the desired argument:
>>> X.logpdf(0.5)
-0.6931471805599453
>>> np.allclose(X.logpdf(0.5), np.log(X.pdf(0.5)))
True
"""
raise NotImplementedError()
@abstractmethod
def cdf(self, x, y, /, *, method):
r"""Cumulative distribution function
The cumulative distribution function ("CDF"), denoted :math:`F(x)`, is
the probability the random variable :math:`X` will assume a value
less than or equal to :math:`x`:
.. math::
F(x) = P(X ≤ x)
A two-argument variant of this function is also defined as the
probability the random variable :math:`X` will assume a value between
:math:`x` and :math:`y`.
.. math::
F(x, y) = P(x ≤ X ≤ y)
`cdf` accepts `x` for :math:`x` and `y` for :math:`y`.
Parameters
----------
x, y : array_like
The arguments of the CDF. `x` is required; `y` is optional.
method : {None, 'formula', 'logexp', 'complement', 'quadrature', 'subtraction'}
The strategy used to evaluate the CDF.
By default (``None``), the one-argument form of the function
chooses between the following options, listed in order of precedence.
- ``'formula'``: use a formula for the CDF itself
- ``'logexp'``: evaluate the log-CDF and exponentiate
- ``'complement'``: evaluate the CCDF and take the complement
- ``'quadrature'``: numerically integrate the PDF
In place of ``'complement'``, the two-argument form accepts:
- ``'subtraction'``: compute the CDF at each argument and take
the difference.
Not all `method` options are available for all distributions.
If the selected `method` is not available, a ``NotImplementedError``
will be raised.
Returns
-------
out : array
The CDF evaluated at the provided argument(s).
See Also
--------
logcdf
ccdf
Notes
-----
Suppose a continuous probability distribution has support :math:`[l, r]`.
The CDF :math:`F(x)` is related to the probability density function
:math:`f(x)` by:
.. math::
F(x) = \int_l^x f(u) du
The two argument version is:
.. math::
F(x, y) = \int_x^y f(u) du = F(y) - F(x)
The CDF evaluates to its minimum value of :math:`0` for :math:`x ≤ l`
and its maximum value of :math:`1` for :math:`x ≥ r`.
The CDF is also known simply as the "distribution function".
References
----------
.. [1] Cumulative distribution function, *Wikipedia*,
https://en.wikipedia.org/wiki/Cumulative_distribution_function
Examples
--------
Instantiate a distribution with the desired parameters:
>>> from scipy import stats
>>> X = stats.Uniform(a=-0.5, b=0.5)
Evaluate the CDF at the desired argument:
>>> X.cdf(0.25)
0.75
Evaluate the cumulative probability between two arguments:
>>> X.cdf(-0.25, 0.25) == X.cdf(0.25) - X.cdf(-0.25)
True
""" # noqa: E501
raise NotImplementedError()
@abstractmethod
def icdf(self, p, /, *, method):
r"""Inverse of the cumulative distribution function.
The inverse of the cumulative distribution function ("inverse CDF"),
denoted :math:`F^{-1}(p)`, is the argument :math:`x` for which the
cumulative distribution function :math:`F(x)` evaluates to :math:`p`.
.. math::
F^{-1}(p) = x \quad \text{s.t.} \quad F(x) = p
`icdf` accepts `p` for :math:`p \in [0, 1]`.
Parameters
----------
p : array_like
The argument of the inverse CDF.
method : {None, 'formula', 'complement', 'inversion'}
The strategy used to evaluate the inverse CDF.
By default (``None``), the infrastructure chooses between the
following options, listed in order of precedence.
- ``'formula'``: use a formula for the inverse CDF itself
- ``'complement'``: evaluate the inverse CCDF at the
complement of `p`
- ``'inversion'``: solve numerically for the argument at which the
CDF is equal to `p`
Not all `method` options are available for all distributions.
If the selected `method` is not available, a ``NotImplementedError``
will be raised.
Returns
-------
out : array
The inverse CDF evaluated at the provided argument.
See Also
--------
cdf
ilogcdf
Notes
-----
Suppose a continuous probability distribution has support :math:`[l, r]`. The
inverse CDF returns its minimum value of :math:`l` at :math:`p = 0`
and its maximum value of :math:`r` at :math:`p = 1`. Because the CDF
has range :math:`[0, 1]`, the inverse CDF is only defined on the
domain :math:`[0, 1]`; for :math:`p < 0` and :math:`p > 1`, `icdf`
returns ``nan``.
The inverse CDF is also known as the quantile function, percentile function,
and percent-point function.
References
----------
.. [1] Quantile function, *Wikipedia*,
https://en.wikipedia.org/wiki/Quantile_function
Examples
--------
Instantiate a distribution with the desired parameters:
>>> import numpy as np
>>> from scipy import stats
>>> X = stats.Uniform(a=-0.5, b=0.5)
Evaluate the inverse CDF at the desired argument:
>>> X.icdf(0.25)
-0.25
>>> np.allclose(X.cdf(X.icdf(0.25)), 0.25)
True
This function returns NaN when the argument is outside the domain.
>>> X.icdf([-0.1, 0, 1, 1.1])
array([ nan, -0.5, 0.5, nan])
"""
raise NotImplementedError()
@abstractmethod
def ccdf(self, x, y, /, *, method):
r"""Complementary cumulative distribution function
The complementary cumulative distribution function ("CCDF"), denoted
:math:`G(x)`, is the complement of the cumulative distribution function
:math:`F(x)`; i.e., probability the random variable :math:`X` will
assume a value greater than :math:`x`:
.. math::
G(x) = 1 - F(x) = P(X > x)
A two-argument variant of this function is:
.. math::
G(x, y) = 1 - F(x, y) = P(X < x \text{ or } X > y)
`ccdf` accepts `x` for :math:`x` and `y` for :math:`y`.
Parameters
----------
x, y : array_like
The arguments of the CCDF. `x` is required; `y` is optional.
method : {None, 'formula', 'logexp', 'complement', 'quadrature', 'addition'}
The strategy used to evaluate the CCDF.
By default (``None``), the infrastructure chooses between the
following options, listed in order of precedence.
- ``'formula'``: use a formula for the CCDF itself
- ``'logexp'``: evaluate the log-CCDF and exponentiate
- ``'complement'``: evaluate the CDF and take the complement
- ``'quadrature'``: numerically integrate the PDF
The two-argument form chooses between:
- ``'formula'``: use a formula for the CCDF itself
- ``'addition'``: compute the CDF at `x` and the CCDF at `y`, then add
Not all `method` options are available for all distributions.
If the selected `method` is not available, a ``NotImplementedError``
will be raised.
Returns
-------
out : array
The CCDF evaluated at the provided argument(s).
See Also
--------
cdf
logccdf
Notes
-----
Suppose a continuous probability distribution has support :math:`[l, r]`.
The CCDF :math:`G(x)` is related to the probability density function
:math:`f(x)` by:
.. math::
G(x) = \int_x^r f(u) du
The two argument version is:
.. math::
G(x, y) = \int_l^x f(u) du + \int_y^r f(u) du
The CCDF returns its minimum value of :math:`0` for :math:`x ≥ r`
and its maximum value of :math:`1` for :math:`x ≤ l`.
The CCDF is also known as the "survival function".
References
----------
.. [1] Cumulative distribution function, *Wikipedia*,
https://en.wikipedia.org/wiki/Cumulative_distribution_function#Derived_functions
Examples
--------
Instantiate a distribution with the desired parameters:
>>> import numpy as np
>>> from scipy import stats
>>> X = stats.Uniform(a=-0.5, b=0.5)
Evaluate the CCDF at the desired argument:
>>> X.ccdf(0.25)
0.25
>>> np.allclose(X.ccdf(0.25), 1-X.cdf(0.25))
True
Evaluate the complement of the cumulative probability between two arguments:
>>> X.ccdf(-0.25, 0.25) == X.cdf(-0.25) + X.ccdf(0.25)
True
""" # noqa: E501
raise NotImplementedError()
@abstractmethod
def iccdf(self, p, /, *, method):
r"""Inverse complementary cumulative distribution function.
The inverse complementary cumulative distribution function ("inverse CCDF"),
denoted :math:`G^{-1}(p)`, is the argument :math:`x` for which the
complementary cumulative distribution function :math:`G(x)` evaluates to
:math:`p`.
.. math::
G^{-1}(p) = x \quad \text{s.t.} \quad G(x) = p
`iccdf` accepts `p` for :math:`p \in [0, 1]`.
Parameters
----------
p : array_like
The argument of the inverse CCDF.
method : {None, 'formula', 'complement', 'inversion'}
The strategy used to evaluate the inverse CCDF.
By default (``None``), the infrastructure chooses between the
following options, listed in order of precedence.
- ``'formula'``: use a formula for the inverse CCDF itself
- ``'complement'``: evaluate the inverse CDF at the
complement of `p`
- ``'inversion'``: solve numerically for the argument at which the
CCDF is equal to `p`
Not all `method` options are available for all distributions.
If the selected `method` is not available, a ``NotImplementedError``
will be raised.
Returns
-------
out : array
The inverse CCDF evaluated at the provided argument.
Notes
-----
Suppose a continuous probability distribution has support :math:`[l, r]`. The
inverse CCDF returns its minimum value of :math:`l` at :math:`p = 1`
and its maximum value of :math:`r` at :math:`p = 0`. Because the CCDF
has range :math:`[0, 1]`, the inverse CCDF is only defined on the
domain :math:`[0, 1]`; for :math:`p < 0` and :math:`p > 1`, ``iccdf``
returns ``nan``.
See Also
--------
icdf
ilogccdf
Examples
--------
Instantiate a distribution with the desired parameters:
>>> import numpy as np
>>> from scipy import stats
>>> X = stats.Uniform(a=-0.5, b=0.5)
Evaluate the inverse CCDF at the desired argument:
>>> X.iccdf(0.25)
0.25
>>> np.allclose(X.iccdf(0.25), X.icdf(1-0.25))
True
This function returns NaN when the argument is outside the domain.
>>> X.iccdf([-0.1, 0, 1, 1.1])
array([ nan, 0.5, -0.5, nan])
"""
raise NotImplementedError()
@abstractmethod
def logcdf(self, x, y, /, *, method):
r"""Log of the cumulative distribution function
The cumulative distribution function ("CDF"), denoted :math:`F(x)`, is
the probability the random variable :math:`X` will assume a value
less than or equal to :math:`x`:
.. math::
F(x) = P(X ≤ x)
A two-argument variant of this function is also defined as the
probability the random variable :math:`X` will assume a value between
:math:`x` and :math:`y`.
.. math::
F(x, y) = P(x ≤ X ≤ y)
`logcdf` computes the logarithm of the cumulative distribution function
("log-CDF"), :math:`\log(F(x))`/:math:`\log(F(x, y))`, but it may be
numerically favorable compared to the naive implementation (computing
the CDF and taking the logarithm).
`logcdf` accepts `x` for :math:`x` and `y` for :math:`y`.
Parameters
----------
x, y : array_like
The arguments of the log-CDF. `x` is required; `y` is optional.
method : {None, 'formula', 'logexp', 'complement', 'quadrature', 'subtraction'}
The strategy used to evaluate the log-CDF.
By default (``None``), the one-argument form of the function
chooses between the following options, listed in order of precedence.
- ``'formula'``: use a formula for the log-CDF itself
- ``'logexp'``: evaluate the CDF and take the logarithm
- ``'complement'``: evaluate the log-CCDF and take the
logarithmic complement (see Notes)
- ``'quadrature'``: numerically log-integrate the log-PDF
In place of ``'complement'``, the two-argument form accepts:
- ``'subtraction'``: compute the log-CDF at each argument and take
the logarithmic difference (see Notes)
Not all `method` options are available for all distributions.
If the selected `method` is not available, a ``NotImplementedError``
will be raised.
Returns
-------
out : array
The log-CDF evaluated at the provided argument(s).
See Also
--------
cdf
logccdf
Notes
-----
Suppose a continuous probability distribution has support :math:`[l, r]`.
The log-CDF evaluates to its minimum value of :math:`\log(0) = -\infty`
for :math:`x ≤ l` and its maximum value of :math:`\log(1) = 0` for
:math:`x ≥ r`.
For distributions with infinite support, it is common for
`cdf` to return a value of ``0`` when the argument
is theoretically within the support; this can occur because the true value
of the CDF is too small to be represented by the chosen dtype. `logcdf`,
however, will often return a finite (not ``-inf``) result over a much larger
domain. Similarly, `logcdf` may provided a strictly negative result with
arguments for which `cdf` would return ``1.0``. Consequently, it may be
preferred to work with the logarithms of probabilities to avoid underflow
and related limitations of floating point numbers.
The "logarithmic complement" of a number :math:`z` is mathematically
equivalent to :math:`\log(1-\exp(z))`, but it is computed to avoid loss
of precision when :math:`\exp(z)` is nearly :math:`0` or :math:`1`.
Similarly, the term "logarithmic difference" of :math:`w` and :math:`z`
is used here to mean :math:`\log(\exp(w)-\exp(z))`.
If ``y < x``, the CDF is negative, and therefore the log-CCDF
is complex with imaginary part :math:`\pi`. For
consistency, the result of this function always has complex dtype
when `y` is provided, regardless of the value of the imaginary part.
References
----------
.. [1] Cumulative distribution function, *Wikipedia*,
https://en.wikipedia.org/wiki/Cumulative_distribution_function
Examples
--------
Instantiate a distribution with the desired parameters:
>>> import numpy as np
>>> from scipy import stats
>>> X = stats.Uniform(a=-0.5, b=0.5)
Evaluate the log-CDF at the desired argument:
>>> X.logcdf(0.25)
-0.287682072451781
>>> np.allclose(X.logcdf(0.), np.log(X.cdf(0.)))
True
""" # noqa: E501
raise NotImplementedError()
@abstractmethod
def ilogcdf(self, logp, /, *, method):
r"""Inverse of the logarithm of the cumulative distribution function.
The inverse of the logarithm of the cumulative distribution function
("inverse log-CDF") is the argument :math:`x` for which the logarithm
of the cumulative distribution function :math:`\log(F(x))` evaluates
to :math:`\log(p)`.
Mathematically, it is equivalent to :math:`F^{-1}(\exp(y))`, where
:math:`y = \log(p)`, but it may be numerically favorable compared to
the naive implementation (computing :math:`p = \exp(y)`, then
:math:`F^{-1}(p)`).
`ilogcdf` accepts `logp` for :math:`\log(p) ≤ 0`.
Parameters
----------
logp : array_like
The argument of the inverse log-CDF.
method : {None, 'formula', 'complement', 'inversion'}
The strategy used to evaluate the inverse log-CDF.
By default (``None``), the infrastructure chooses between the
following options, listed in order of precedence.
- ``'formula'``: use a formula for the inverse log-CDF itself
- ``'complement'``: evaluate the inverse log-CCDF at the
logarithmic complement of `logp` (see Notes)
- ``'inversion'``: solve numerically for the argument at which the
log-CDF is equal to `logp`
Not all `method` options are available for all distributions.
If the selected `method` is not available, a ``NotImplementedError``
will be raised.
Returns
-------
out : array
The inverse log-CDF evaluated at the provided argument.
See Also
--------
icdf
logcdf
Notes
-----
Suppose a continuous probability distribution has support :math:`[l, r]`.
The inverse log-CDF returns its minimum value of :math:`l` at
:math:`\log(p) = \log(0) = -\infty` and its maximum value of :math:`r` at
:math:`\log(p) = \log(1) = 0`. Because the log-CDF has range
:math:`[-\infty, 0]`, the inverse log-CDF is only defined on the
negative reals; for :math:`\log(p) > 0`, `ilogcdf` returns ``nan``.
Occasionally, it is needed to find the argument of the CDF for which
the resulting probability is very close to ``0`` or ``1`` - too close to
represent accurately with floating point arithmetic. In many cases,
however, the *logarithm* of this resulting probability may be
represented in floating point arithmetic, in which case this function
may be used to find the argument of the CDF for which the *logarithm*
of the resulting probability is :math:`y = \log(p)`.
The "logarithmic complement" of a number :math:`z` is mathematically
equivalent to :math:`\log(1-\exp(z))`, but it is computed to avoid loss
of precision when :math:`\exp(z)` is nearly :math:`0` or :math:`1`.
Examples
--------
Instantiate a distribution with the desired parameters:
>>> import numpy as np
>>> from scipy import stats
>>> X = stats.Uniform(a=-0.5, b=0.5)
Evaluate the inverse log-CDF at the desired argument:
>>> X.ilogcdf(-0.25)
0.2788007830714034
>>> np.allclose(X.ilogcdf(-0.25), X.icdf(np.exp(-0.25)))
True
"""
raise NotImplementedError()
@abstractmethod
def logccdf(self, x, y, /, *, method):
r"""Log of the complementary cumulative distribution function
The complementary cumulative distribution function ("CCDF"), denoted
:math:`G(x)` is the complement of the cumulative distribution function
:math:`F(x)`; i.e., probability the random variable :math:`X` will
assume a value greater than :math:`x`:
.. math::
G(x) = 1 - F(x) = P(X > x)
A two-argument variant of this function is:
.. math::
G(x, y) = 1 - F(x, y) = P(X < x \quad \text{or} \quad X > y)
`logccdf` computes the logarithm of the complementary cumulative
distribution function ("log-CCDF"), :math:`\log(G(x))`/:math:`\log(G(x, y))`,
but it may be numerically favorable compared to the naive implementation
(computing the CDF and taking the logarithm).
`logccdf` accepts `x` for :math:`x` and `y` for :math:`y`.
Parameters
----------
x, y : array_like
The arguments of the log-CCDF. `x` is required; `y` is optional.
method : {None, 'formula', 'logexp', 'complement', 'quadrature', 'addition'}
The strategy used to evaluate the log-CCDF.
By default (``None``), the one-argument form of the function
chooses between the following options, listed in order of precedence.
- ``'formula'``: use a formula for the log CCDF itself
- ``'logexp'``: evaluate the CCDF and take the logarithm
- ``'complement'``: evaluate the log-CDF and take the
logarithmic complement (see Notes)
- ``'quadrature'``: numerically log-integrate the log-PDF
The two-argument form chooses between:
- ``'formula'``: use a formula for the log CCDF itself
- ``'addition'``: compute the log-CDF at `x` and the log-CCDF at `y`,
then take the logarithmic sum (see Notes)
Not all `method` options are available for all distributions.
If the selected `method` is not available, a ``NotImplementedError``
will be raised.
Returns
-------
out : array
The log-CCDF evaluated at the provided argument(s).
See Also
--------
ccdf
logcdf
Notes
-----
Suppose a continuous probability distribution has support :math:`[l, r]`.
The log-CCDF returns its minimum value of :math:`\log(0)=-\infty` for
:math:`x ≥ r` and its maximum value of :math:`\log(1) = 0` for
:math:`x ≤ l`.
For distributions with infinite support, it is common for
`ccdf` to return a value of ``0`` when the argument
is theoretically within the support; this can occur because the true value
of the CCDF is too small to be represented by the chosen dtype. The log
of the CCDF, however, will often be finite (not ``-inf``) over a much larger
domain. Similarly, `logccdf` may provided a strictly negative result with
arguments for which `ccdf` would return ``1.0``. Consequently, it may be
preferred to work with the logarithms of probabilities to avoid underflow
and related limitations of floating point numbers.
The "logarithmic complement" of a number :math:`z` is mathematically
equivalent to :math:`\log(1-\exp(z))`, but it is computed to avoid loss
of precision when :math:`\exp(z)` is nearly :math:`0` or :math:`1`.
Similarly, the term "logarithmic sum" of :math:`w` and :math:`z`
is used here to mean the :math:`\log(\exp(w)+\exp(z))`, AKA
:math:`\text{LogSumExp}(w, z)`.
References
----------
.. [1] Cumulative distribution function, *Wikipedia*,
https://en.wikipedia.org/wiki/Cumulative_distribution_function#Derived_functions
Examples
--------
Instantiate a distribution with the desired parameters:
>>> import numpy as np
>>> from scipy import stats
>>> X = stats.Uniform(a=-0.5, b=0.5)
Evaluate the log-CCDF at the desired argument:
>>> X.logccdf(0.25)
-1.3862943611198906
>>> np.allclose(X.logccdf(0.), np.log(X.ccdf(0.)))
True
""" # noqa: E501
raise NotImplementedError()
@abstractmethod
def ilogccdf(self, logp, /, *, method):
r"""Inverse of the log of the complementary cumulative distribution function.
The inverse of the logarithm of the complementary cumulative distribution
function ("inverse log-CCDF") is the argument :math:`x` for which the logarithm
of the complementary cumulative distribution function :math:`\log(G(x))`
evaluates to :math:`\log(p)`.
Mathematically, it is equivalent to :math:`G^{-1}(\exp(y))`, where
:math:`y = \log(p)`, but it may be numerically favorable compared to the naive
implementation (computing :math:`p = \exp(y)`, then :math:`G^{-1}(p)`).
`ilogccdf` accepts `logp` for :math:`\log(p) ≤ 0`.
Parameters
----------
x : array_like
The argument of the inverse log-CCDF.
method : {None, 'formula', 'complement', 'inversion'}
The strategy used to evaluate the inverse log-CCDF.
By default (``None``), the infrastructure chooses between the
following options, listed in order of precedence.
- ``'formula'``: use a formula for the inverse log-CCDF itself
- ``'complement'``: evaluate the inverse log-CDF at the
logarithmic complement of `x` (see Notes)
- ``'inversion'``: solve numerically for the argument at which the
log-CCDF is equal to `x`
Not all `method` options are available for all distributions.
If the selected `method` is not available, a ``NotImplementedError``
will be raised.
Returns
-------
out : array
The inverse log-CCDF evaluated at the provided argument.
Notes
-----
Suppose a continuous probability distribution has support :math:`[l, r]`. The
inverse log-CCDF returns its minimum value of :math:`l` at
:math:`\log(p) = \log(1) = 0` and its maximum value of :math:`r` at
:math:`\log(p) = \log(0) = -\infty`. Because the log-CCDF has range
:math:`[-\infty, 0]`, the inverse log-CDF is only defined on the
negative reals; for :math:`\log(p) > 0`, `ilogccdf` returns ``nan``.
Occasionally, it is needed to find the argument of the CCDF for which
the resulting probability is very close to ``0`` or ``1`` - too close to
represent accurately with floating point arithmetic. In many cases,
however, the *logarithm* of this resulting probability may be
represented in floating point arithmetic, in which case this function
may be used to find the argument of the CCDF for which the *logarithm*
of the resulting probability is `y = \log(p)`.
The "logarithmic complement" of a number :math:`z` is mathematically
equivalent to :math:`\log(1-\exp(z))`, but it is computed to avoid loss
of precision when :math:`\exp(z)` is nearly :math:`0` or :math:`1`.
See Also
--------
iccdf
ilogccdf
Examples
--------
Instantiate a distribution with the desired parameters:
>>> import numpy as np
>>> from scipy import stats
>>> X = stats.Uniform(a=-0.5, b=0.5)
Evaluate the inverse log-CCDF at the desired argument:
>>> X.ilogccdf(-0.25)
-0.2788007830714034
>>> np.allclose(X.ilogccdf(-0.25), X.iccdf(np.exp(-0.25)))
True
"""
raise NotImplementedError()
@abstractmethod
def logentropy(self, *, method):
r"""Logarithm of the differential entropy
In terms of probability density function :math:`f(x)` and support
:math:`\chi`, the differential entropy (or simply "entropy") of a random
variable :math:`X` is:
.. math::
h(X) = - \int_{\chi} f(x) \log f(x) dx
`logentropy` computes the logarithm of the differential entropy
("log-entropy"), :math:`log(h(X))`, but it may be numerically favorable
compared to the naive implementation (computing :math:`h(X)` then
taking the logarithm).
Parameters
----------
method : {None, 'formula', 'logexp', 'quadrature}
The strategy used to evaluate the log-entropy. By default
(``None``), the infrastructure chooses between the following options,
listed in order of precedence.
- ``'formula'``: use a formula for the log-entropy itself
- ``'logexp'``: evaluate the entropy and take the logarithm
- ``'quadrature'``: numerically log-integrate the logarithm of the
entropy integrand
Not all `method` options are available for all distributions.
If the selected `method` is not available, a ``NotImplementedError``
will be raised.
Returns
-------
out : array
The log-entropy.
See Also
--------
entropy
logpdf
Notes
-----
If the entropy of a distribution is negative, then the log-entropy
is complex with imaginary part :math:`\pi`. For
consistency, the result of this function always has complex dtype,
regardless of the value of the imaginary part.
References
----------
.. [1] Differential entropy, *Wikipedia*,
https://en.wikipedia.org/wiki/Differential_entropy
Examples
--------
Instantiate a distribution with the desired parameters:
>>> import numpy as np
>>> from scipy import stats
>>> X = stats.Uniform(a=-1., b=1.)
Evaluate the log-entropy:
>>> X.logentropy()
(-0.3665129205816642+0j)
>>> np.allclose(np.exp(X.logentropy()), X.entropy())
True
For a random variable with negative entropy, the log-entropy has an
imaginary part equal to `np.pi`.
>>> X = stats.Uniform(a=-.1, b=.1)
>>> X.entropy(), X.logentropy()
(-1.6094379124341007, (0.4758849953271105+3.141592653589793j))
"""
raise NotImplementedError()
@abstractmethod
def entropy(self, *, method):
r"""Differential entropy
In terms of probability density function :math:`f(x)` and support
:math:`\chi`, the differential entropy (or simply "entropy") of a
continuous random variable :math:`X` is:
.. math::
h(X) = - \int_{\chi} f(x) \log f(x) dx
Parameters
----------
method : {None, 'formula', 'logexp', 'quadrature'}
The strategy used to evaluate the entropy. By default (``None``),
the infrastructure chooses between the following options, listed
in order of precedence.
- ``'formula'``: use a formula for the entropy itself
- ``'logexp'``: evaluate the log-entropy and exponentiate
- ``'quadrature'``: use numerical integration
Not all `method` options are available for all distributions.
If the selected `method` is not available, a ``NotImplementedError``
will be raised.
Returns
-------
out : array
The entropy of the random variable.
See Also
--------
logentropy
pdf
Notes
-----
This function calculates the entropy using the natural logarithm; i.e.
the logarithm with base :math:`e`. Consequently, the value is expressed
in (dimensionless) "units" of nats. To convert the entropy to different
units (i.e. corresponding with a different base), divide the result by
the natural logarithm of the desired base.
References
----------
.. [1] Differential entropy, *Wikipedia*,
https://en.wikipedia.org/wiki/Differential_entropy
Examples
--------
Instantiate a distribution with the desired parameters:
>>> from scipy import stats
>>> X = stats.Uniform(a=-1., b=1.)
Evaluate the entropy:
>>> X.entropy()
0.6931471805599454
"""
raise NotImplementedError()
|