/usr/lib/python2.7/dist-packages/openturns/bayesian.py is in python-openturns 1.5-7build2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 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 | # This file was automatically generated by SWIG (http://www.swig.org).
# Version 2.0.12
#
# Do not make changes to this file unless you know what you are doing--modify
# the SWIG interface file instead.
"""
Bayesian algorithms.
"""
from sys import version_info
if version_info >= (2,6,0):
def swig_import_helper():
from os.path import dirname
import imp
fp = None
try:
fp, pathname, description = imp.find_module('_bayesian', [dirname(__file__)])
except ImportError:
import _bayesian
return _bayesian
if fp is not None:
try:
_mod = imp.load_module('_bayesian', fp, pathname, description)
finally:
fp.close()
return _mod
_bayesian = swig_import_helper()
del swig_import_helper
else:
import _bayesian
del version_info
try:
_swig_property = property
except NameError:
pass # Python < 2.2 doesn't have 'property'.
def _swig_setattr_nondynamic(self,class_type,name,value,static=1):
if (name == "thisown"): return self.this.own(value)
if (name == "this"):
if type(value).__name__ == 'SwigPyObject':
self.__dict__[name] = value
return
method = class_type.__swig_setmethods__.get(name,None)
if method: return method(self,value)
if (not static):
self.__dict__[name] = value
else:
raise AttributeError("You cannot add attributes to %s" % self)
def _swig_setattr(self,class_type,name,value):
return _swig_setattr_nondynamic(self,class_type,name,value,0)
def _swig_getattr(self,class_type,name):
if (name == "thisown"): return self.this.own()
method = class_type.__swig_getmethods__.get(name,None)
if method: return method(self)
raise AttributeError(name)
def _swig_repr(self):
try: strthis = "proxy of " + self.this.__repr__()
except: strthis = ""
return "<%s.%s; %s >" % (self.__class__.__module__, self.__class__.__name__, strthis,)
try:
_object = object
_newclass = 1
except AttributeError:
class _object : pass
_newclass = 0
class SwigPyIterator(_object):
__swig_setmethods__ = {}
__setattr__ = lambda self, name, value: _swig_setattr(self, SwigPyIterator, name, value)
__swig_getmethods__ = {}
__getattr__ = lambda self, name: _swig_getattr(self, SwigPyIterator, name)
def __init__(self, *args, **kwargs): raise AttributeError("No constructor defined - class is abstract")
__repr__ = _swig_repr
__swig_destroy__ = _bayesian.delete_SwigPyIterator
__del__ = lambda self : None;
def value(self): return _bayesian.SwigPyIterator_value(self)
def incr(self, n=1): return _bayesian.SwigPyIterator_incr(self, n)
def decr(self, n=1): return _bayesian.SwigPyIterator_decr(self, n)
def distance(self, *args): return _bayesian.SwigPyIterator_distance(self, *args)
def equal(self, *args): return _bayesian.SwigPyIterator_equal(self, *args)
def copy(self): return _bayesian.SwigPyIterator_copy(self)
def next(self): return _bayesian.SwigPyIterator_next(self)
def __next__(self): return _bayesian.SwigPyIterator___next__(self)
def previous(self): return _bayesian.SwigPyIterator_previous(self)
def advance(self, *args): return _bayesian.SwigPyIterator_advance(self, *args)
def __eq__(self, *args): return _bayesian.SwigPyIterator___eq__(self, *args)
def __ne__(self, *args): return _bayesian.SwigPyIterator___ne__(self, *args)
def __iadd__(self, *args): return _bayesian.SwigPyIterator___iadd__(self, *args)
def __isub__(self, *args): return _bayesian.SwigPyIterator___isub__(self, *args)
def __add__(self, *args): return _bayesian.SwigPyIterator___add__(self, *args)
def __sub__(self, *args): return _bayesian.SwigPyIterator___sub__(self, *args)
def __iter__(self): return self
SwigPyIterator_swigregister = _bayesian.SwigPyIterator_swigregister
SwigPyIterator_swigregister(SwigPyIterator)
GCC_VERSION = _bayesian.GCC_VERSION
class TestFailed:
"""TestFailed is used to raise an uniform exception in tests."""
__type = "TestFailed"
def __init__(self, reason=""):
self.reason = reason
def type(self):
return TestFailed.__type
def what(self):
return self.reason
def __str__(self):
return TestFailed.__type + ": " + self.reason
def __lshift__(self, ch):
self.reason += ch
return self
import openturns.base
import openturns.common
import openturns.wrapper
import openturns.typ
import openturns.statistics
import openturns.graph
import openturns.func
import openturns.geom
import openturns.diff
import openturns.optim
import openturns.solver
import openturns.algo
import openturns.experiment
import openturns.model_copula
import openturns.dist_bundle1
import openturns.dist_bundle2
import openturns.randomvector
class CalibrationStrategyImplementation(openturns.common.PersistentObject):
"""
Calibration strategy.
Available constructors:
CalibrationStrategy(*range*)
CalibrationStrategy(*range=[0.117, 0.468], expansionFactor=1.2, shrinkFactor=0.8, calibrationStep=100*)
Parameters
----------
range : :class:`~openturns.Interval` of dimension 1 :math:`[m,M]`
Acceptance rate values for which no update of the *calibration* coefficient
is performed.
expansionFactor : float, :math:`e > 1`
Expansion factor :math:`e` to use to rescale the *calibration* coefficient
if the latter is too high (greater than the upper bound of range).
shrinkFactor : float, :math:`0 < s < 1`
Shrink factor :math:`s` to use to rescale the *calibration* coefficient if
the latter is too low (smaller than the lower bound of range). If
*expansionFactor* is specified, *shrinkFactor* must be mentioned too.
calibrationStep : positive int
Calibration step corresponding for example to :math:`q` in the description
of the method
:meth:`~openturns.RandomWalkMetropolisHastings.getCalibrationStrategyPerComponent`
of the RandomWalkMetropolisHastings class.
Notes
-----
A CalibrationStrategy can be used by a
:class:`~openturns.RandomWalkMetropolisHastings` for example (see the
description of the method
:meth:`~openturns.RandomWalkMetropolisHastings.getCalibrationStrategyPerComponent`
).
"""
__swig_setmethods__ = {}
for _s in [openturns.common.PersistentObject]: __swig_setmethods__.update(getattr(_s,'__swig_setmethods__',{}))
__setattr__ = lambda self, name, value: _swig_setattr(self, CalibrationStrategyImplementation, name, value)
__swig_getmethods__ = {}
for _s in [openturns.common.PersistentObject]: __swig_getmethods__.update(getattr(_s,'__swig_getmethods__',{}))
__getattr__ = lambda self, name: _swig_getattr(self, CalibrationStrategyImplementation, name)
def getClassName(self):
"""
Accessor to the object's name.
Returns
-------
class_name : str
The object class name (`object.__class__.__name__`).
"""
return _bayesian.CalibrationStrategyImplementation_getClassName(self)
def __repr__(self): return _bayesian.CalibrationStrategyImplementation___repr__(self)
def setRange(self, *args):
"""
Set the range.
Parameters
----------
range : :class:`~openturns.Interval` of dimension 1
Range :math:`[m,M]` in the description of the method
:meth:`computeUpdateFactor`.
"""
return _bayesian.CalibrationStrategyImplementation_setRange(self, *args)
def getRange(self):
"""
Get the range.
Returns
-------
range : :class:`~openturns.Interval` of dimension 1
Range :math:`[m,M]` in the description of the method
:meth:`computeUpdateFactor`.
"""
return _bayesian.CalibrationStrategyImplementation_getRange(self)
def setExpansionFactor(self, *args):
"""
Set the expansion factor.
Parameters
----------
expansionFactor : float, :math:`e > 1`
Expansion factor :math:`e`. See the description of the method
:meth:`computeUpdateFactor`.
"""
return _bayesian.CalibrationStrategyImplementation_setExpansionFactor(self, *args)
def getExpansionFactor(self):
"""
Get the expansion factor.
Returns
-------
expansionFactor : float
Expansion factor :math:`e`. See the description of the method
:meth:`computeUpdateFactor`.
"""
return _bayesian.CalibrationStrategyImplementation_getExpansionFactor(self)
def setShrinkFactor(self, *args):
"""
Set the shrink factor.
Parameters
----------
shrinkFactor : float, :math:`0 < s < 1`
Shrink factor :math:`s` in the description of the method
:meth:`computeUpdateFactor`.
"""
return _bayesian.CalibrationStrategyImplementation_setShrinkFactor(self, *args)
def getShrinkFactor(self):
"""
Get the shrink factor.
Returns
-------
shrinkFactor : float
Shrink factor :math:`s` in the description of the method
:meth:`computeUpdateFactor`.
"""
return _bayesian.CalibrationStrategyImplementation_getShrinkFactor(self)
def setCalibrationStep(self, *args):
"""
Set the calibration step.
Parameters
----------
step : positive int
Calibration step corresponding for example to :math:`q` in the description
of the method
:meth:`~openturns.RandomWalkMetropolisHastings.getCalibrationStrategyPerComponent`
of the RandomWalkMetropolisHastings class.
"""
return _bayesian.CalibrationStrategyImplementation_setCalibrationStep(self, *args)
def getCalibrationStep(self):
"""
Get the calibration step.
Returns
-------
step : positive int
Calibration step corresponding for example to :math:`q` in the description
of the method
:meth:`~openturns.RandomWalkMetropolisHastings.getCalibrationStrategyPerComponent`
of the RandomWalkMetropolisHastings class.
"""
return _bayesian.CalibrationStrategyImplementation_getCalibrationStep(self)
def computeUpdateFactor(self, *args):
"""
Compute the update factor.
Parameters
----------
rho : float
Acceptance rate :math:`\\rho` to take into account.
Returns
-------
lambda : float
Let :math:`\\lambda` be the *calibration* coefficient to update, it gives a
factor :math:`\\phi(\\rho)` such that :math:`\\phi(\\rho) \\lambda` is the
updated *calibration* coefficient according to the strategy. The value is
computed as follows:
.. math::
\\phi(\\rho) = \\left\\{
\\begin{array}{l}
\\displaystyle s \\quad if \\; \\rho < m \\\\
\\displaystyle e \\quad if \\; \\rho > M \\\\
\\displaystyle 1 \\quad otherwise
\\end{array}
\\right.
with :math:`s \\in ]0, 1[, e > 1` and :math:`[m,M]` the values given,
respectively, by the methods :meth:`getShrinkFactor`,
:meth:`getExpansionFactor` and :meth:`getRange`.
Examples
--------
>>> import openturns as ot
>>> calibration = ot.CalibrationStrategy(ot.Interval(0.1, 0.4), 1.2, 0.8)
>>> print(calibration.computeUpdateFactor(0.09))
0.8
>>> print(calibration.computeUpdateFactor(0.6))
1.2
>>> print(calibration.computeUpdateFactor(0.18))
1.0
"""
return _bayesian.CalibrationStrategyImplementation_computeUpdateFactor(self, *args)
def __init__(self, *args):
this = _bayesian.new_CalibrationStrategyImplementation(*args)
try: self.this.append(this)
except: self.this = this
__swig_destroy__ = _bayesian.delete_CalibrationStrategyImplementation
__del__ = lambda self : None;
CalibrationStrategyImplementation_swigregister = _bayesian.CalibrationStrategyImplementation_swigregister
CalibrationStrategyImplementation_swigregister(CalibrationStrategyImplementation)
class CalibrationStrategyImplementationTypedInterfaceObject(openturns.common.InterfaceObject):
__swig_setmethods__ = {}
for _s in [openturns.common.InterfaceObject]: __swig_setmethods__.update(getattr(_s,'__swig_setmethods__',{}))
__setattr__ = lambda self, name, value: _swig_setattr(self, CalibrationStrategyImplementationTypedInterfaceObject, name, value)
__swig_getmethods__ = {}
for _s in [openturns.common.InterfaceObject]: __swig_getmethods__.update(getattr(_s,'__swig_getmethods__',{}))
__getattr__ = lambda self, name: _swig_getattr(self, CalibrationStrategyImplementationTypedInterfaceObject, name)
__repr__ = _swig_repr
def __init__(self, *args):
this = _bayesian.new_CalibrationStrategyImplementationTypedInterfaceObject(*args)
try: self.this.append(this)
except: self.this = this
def getImplementation(self, *args):
"""
Accessor to the underlying implementation.
Returns
-------
impl : Implementation
The implementation class.
"""
return _bayesian.CalibrationStrategyImplementationTypedInterfaceObject_getImplementation(self, *args)
def setName(self, *args):
"""
Accessor to the object's name.
Parameters
----------
name : string
The name of the object.
"""
return _bayesian.CalibrationStrategyImplementationTypedInterfaceObject_setName(self, *args)
def getName(self):
"""
Accessor to the object's name.
Returns
-------
name : string
The name of the object.
"""
return _bayesian.CalibrationStrategyImplementationTypedInterfaceObject_getName(self)
def __eq__(self, *args): return _bayesian.CalibrationStrategyImplementationTypedInterfaceObject___eq__(self, *args)
__swig_destroy__ = _bayesian.delete_CalibrationStrategyImplementationTypedInterfaceObject
__del__ = lambda self : None;
CalibrationStrategyImplementationTypedInterfaceObject_swigregister = _bayesian.CalibrationStrategyImplementationTypedInterfaceObject_swigregister
CalibrationStrategyImplementationTypedInterfaceObject_swigregister(CalibrationStrategyImplementationTypedInterfaceObject)
class CalibrationStrategyCollection(_object):
__swig_setmethods__ = {}
__setattr__ = lambda self, name, value: _swig_setattr(self, CalibrationStrategyCollection, name, value)
__swig_getmethods__ = {}
__getattr__ = lambda self, name: _swig_getattr(self, CalibrationStrategyCollection, name)
__swig_destroy__ = _bayesian.delete_CalibrationStrategyCollection
__del__ = lambda self : None;
def clear(self): return _bayesian.CalibrationStrategyCollection_clear(self)
def __len__(self): return _bayesian.CalibrationStrategyCollection___len__(self)
def __eq__(self, *args): return _bayesian.CalibrationStrategyCollection___eq__(self, *args)
def __contains__(self, *args): return _bayesian.CalibrationStrategyCollection___contains__(self, *args)
def __getitem__(self, *args): return _bayesian.CalibrationStrategyCollection___getitem__(self, *args)
def __setitem__(self, *args): return _bayesian.CalibrationStrategyCollection___setitem__(self, *args)
def __delitem__(self, *args): return _bayesian.CalibrationStrategyCollection___delitem__(self, *args)
def at(self, *args): return _bayesian.CalibrationStrategyCollection_at(self, *args)
def add(self, *args): return _bayesian.CalibrationStrategyCollection_add(self, *args)
def getSize(self): return _bayesian.CalibrationStrategyCollection_getSize(self)
def resize(self, *args): return _bayesian.CalibrationStrategyCollection_resize(self, *args)
def isEmpty(self): return _bayesian.CalibrationStrategyCollection_isEmpty(self)
def __repr__(self): return _bayesian.CalibrationStrategyCollection___repr__(self)
def __str__(self, offset=""): return _bayesian.CalibrationStrategyCollection___str__(self, offset)
def __init__(self, *args):
this = _bayesian.new_CalibrationStrategyCollection(*args)
try: self.this.append(this)
except: self.this = this
CalibrationStrategyCollection_swigregister = _bayesian.CalibrationStrategyCollection_swigregister
CalibrationStrategyCollection_swigregister(CalibrationStrategyCollection)
class CalibrationStrategy(CalibrationStrategyImplementationTypedInterfaceObject):
"""
Calibration strategy.
Available constructors:
CalibrationStrategy(*range*)
CalibrationStrategy(*range=[0.117, 0.468], expansionFactor=1.2, shrinkFactor=0.8, calibrationStep=100*)
Parameters
----------
range : :class:`~openturns.Interval` of dimension 1 :math:`[m,M]`
Acceptance rate values for which no update of the *calibration* coefficient
is performed.
expansionFactor : float, :math:`e > 1`
Expansion factor :math:`e` to use to rescale the *calibration* coefficient
if the latter is too high (greater than the upper bound of range).
shrinkFactor : float, :math:`0 < s < 1`
Shrink factor :math:`s` to use to rescale the *calibration* coefficient if
the latter is too low (smaller than the lower bound of range). If
*expansionFactor* is specified, *shrinkFactor* must be mentioned too.
calibrationStep : positive int
Calibration step corresponding for example to :math:`q` in the description
of the method
:meth:`~openturns.RandomWalkMetropolisHastings.getCalibrationStrategyPerComponent`
of the RandomWalkMetropolisHastings class.
Notes
-----
A CalibrationStrategy can be used by a
:class:`~openturns.RandomWalkMetropolisHastings` for example (see the
description of the method
:meth:`~openturns.RandomWalkMetropolisHastings.getCalibrationStrategyPerComponent`
).
"""
__swig_setmethods__ = {}
for _s in [CalibrationStrategyImplementationTypedInterfaceObject]: __swig_setmethods__.update(getattr(_s,'__swig_setmethods__',{}))
__setattr__ = lambda self, name, value: _swig_setattr(self, CalibrationStrategy, name, value)
__swig_getmethods__ = {}
for _s in [CalibrationStrategyImplementationTypedInterfaceObject]: __swig_getmethods__.update(getattr(_s,'__swig_getmethods__',{}))
__getattr__ = lambda self, name: _swig_getattr(self, CalibrationStrategy, name)
__repr__ = _swig_repr
def getClassName(self):
"""
Accessor to the object's name.
Returns
-------
class_name : str
The object class name (`object.__class__.__name__`).
"""
return _bayesian.CalibrationStrategy_getClassName(self)
def setRange(self, *args):
"""
Set the range.
Parameters
----------
range : :class:`~openturns.Interval` of dimension 1
Range :math:`[m,M]` in the description of the method
:meth:`computeUpdateFactor`.
"""
return _bayesian.CalibrationStrategy_setRange(self, *args)
def getRange(self):
"""
Get the range.
Returns
-------
range : :class:`~openturns.Interval` of dimension 1
Range :math:`[m,M]` in the description of the method
:meth:`computeUpdateFactor`.
"""
return _bayesian.CalibrationStrategy_getRange(self)
def setExpansionFactor(self, *args):
"""
Set the expansion factor.
Parameters
----------
expansionFactor : float, :math:`e > 1`
Expansion factor :math:`e`. See the description of the method
:meth:`computeUpdateFactor`.
"""
return _bayesian.CalibrationStrategy_setExpansionFactor(self, *args)
def getExpansionFactor(self):
"""
Get the expansion factor.
Returns
-------
expansionFactor : float
Expansion factor :math:`e`. See the description of the method
:meth:`computeUpdateFactor`.
"""
return _bayesian.CalibrationStrategy_getExpansionFactor(self)
def setShrinkFactor(self, *args):
"""
Set the shrink factor.
Parameters
----------
shrinkFactor : float, :math:`0 < s < 1`
Shrink factor :math:`s` in the description of the method
:meth:`computeUpdateFactor`.
"""
return _bayesian.CalibrationStrategy_setShrinkFactor(self, *args)
def getShrinkFactor(self):
"""
Get the shrink factor.
Returns
-------
shrinkFactor : float
Shrink factor :math:`s` in the description of the method
:meth:`computeUpdateFactor`.
"""
return _bayesian.CalibrationStrategy_getShrinkFactor(self)
def setCalibrationStep(self, *args):
"""
Set the calibration step.
Parameters
----------
step : positive int
Calibration step corresponding for example to :math:`q` in the description
of the method
:meth:`~openturns.RandomWalkMetropolisHastings.getCalibrationStrategyPerComponent`
of the RandomWalkMetropolisHastings class.
"""
return _bayesian.CalibrationStrategy_setCalibrationStep(self, *args)
def getCalibrationStep(self):
"""
Get the calibration step.
Returns
-------
step : positive int
Calibration step corresponding for example to :math:`q` in the description
of the method
:meth:`~openturns.RandomWalkMetropolisHastings.getCalibrationStrategyPerComponent`
of the RandomWalkMetropolisHastings class.
"""
return _bayesian.CalibrationStrategy_getCalibrationStep(self)
def computeUpdateFactor(self, *args):
"""
Compute the update factor.
Parameters
----------
rho : float
Acceptance rate :math:`\\rho` to take into account.
Returns
-------
lambda : float
Let :math:`\\lambda` be the *calibration* coefficient to update, it gives a
factor :math:`\\phi(\\rho)` such that :math:`\\phi(\\rho) \\lambda` is the
updated *calibration* coefficient according to the strategy. The value is
computed as follows:
.. math::
\\phi(\\rho) = \\left\\{
\\begin{array}{l}
\\displaystyle s \\quad if \\; \\rho < m \\\\
\\displaystyle e \\quad if \\; \\rho > M \\\\
\\displaystyle 1 \\quad otherwise
\\end{array}
\\right.
with :math:`s \\in ]0, 1[, e > 1` and :math:`[m,M]` the values given,
respectively, by the methods :meth:`getShrinkFactor`,
:meth:`getExpansionFactor` and :meth:`getRange`.
Examples
--------
>>> import openturns as ot
>>> calibration = ot.CalibrationStrategy(ot.Interval(0.1, 0.4), 1.2, 0.8)
>>> print(calibration.computeUpdateFactor(0.09))
0.8
>>> print(calibration.computeUpdateFactor(0.6))
1.2
>>> print(calibration.computeUpdateFactor(0.18))
1.0
"""
return _bayesian.CalibrationStrategy_computeUpdateFactor(self, *args)
def __str__(self): return _bayesian.CalibrationStrategy___str__(self)
def __init__(self, *args):
this = _bayesian.new_CalibrationStrategy(*args)
try: self.this.append(this)
except: self.this = this
__swig_destroy__ = _bayesian.delete_CalibrationStrategy
__del__ = lambda self : None;
CalibrationStrategy_swigregister = _bayesian.CalibrationStrategy_swigregister
CalibrationStrategy_swigregister(CalibrationStrategy)
class SamplerImplementation(openturns.common.PersistentObject):
"""
Sampler.
Available constructors:
Sampler(*aSampler*)
Parameters
----------
aSampler : :class:`~openturns.Sampler`
Particular sampler. By default it is a
:class:`~openturns.RandomWalkMetropolisHastings`.
Notes
-----
A Sampler is an object whose fundamental ability is to produce samples
according to a certain distribution.
See also
--------
MCMC, RandomWalkMetropolisHastings
Examples
--------
>>> import openturns as ot
>>> sampler = ot.Sampler()
"""
__swig_setmethods__ = {}
for _s in [openturns.common.PersistentObject]: __swig_setmethods__.update(getattr(_s,'__swig_setmethods__',{}))
__setattr__ = lambda self, name, value: _swig_setattr(self, SamplerImplementation, name, value)
__swig_getmethods__ = {}
for _s in [openturns.common.PersistentObject]: __swig_getmethods__.update(getattr(_s,'__swig_getmethods__',{}))
__getattr__ = lambda self, name: _swig_getattr(self, SamplerImplementation, name)
def getClassName(self):
"""
Accessor to the object's name.
Returns
-------
class_name : str
The object class name (`object.__class__.__name__`).
"""
return _bayesian.SamplerImplementation_getClassName(self)
def setVerbose(self, *args):
"""
Set the verbose mode.
Parameters
----------
isVerbose : Bool
The verbose mode is activated if it is *True*, desactivated otherwise.
"""
return _bayesian.SamplerImplementation_setVerbose(self, *args)
def getVerbose(self):
"""
Tell whether the verbose mode is activated or not.
Returns
-------
isVerbose : Bool
The verbose mode is activated if it is *True*, desactivated otherwise.
"""
return _bayesian.SamplerImplementation_getVerbose(self)
def __repr__(self): return _bayesian.SamplerImplementation___repr__(self)
def getDimension(self):
"""
Get the dimension of the samples generated.
Returns
-------
dimension : int
Dimension of the samples that the Sampler can generate.
"""
return _bayesian.SamplerImplementation_getDimension(self)
def getRealization(self):
"""
Return a realization.
Returns
-------
realization : float sequence
A new realization.
"""
return _bayesian.SamplerImplementation_getRealization(self)
def getSample(self, *args):
"""
Return several realizations.
Parameters
----------
size : int, :math:`size \\leq 0`
Number of realizations needed.
Returns
-------
realizations : 2D float sequence
Sequence composed of *size* new realizations.
"""
return _bayesian.SamplerImplementation_getSample(self, *args)
def __init__(self, *args):
this = _bayesian.new_SamplerImplementation(*args)
try: self.this.append(this)
except: self.this = this
__swig_destroy__ = _bayesian.delete_SamplerImplementation
__del__ = lambda self : None;
SamplerImplementation_swigregister = _bayesian.SamplerImplementation_swigregister
SamplerImplementation_swigregister(SamplerImplementation)
class SamplerImplementationTypedInterfaceObject(openturns.common.InterfaceObject):
__swig_setmethods__ = {}
for _s in [openturns.common.InterfaceObject]: __swig_setmethods__.update(getattr(_s,'__swig_setmethods__',{}))
__setattr__ = lambda self, name, value: _swig_setattr(self, SamplerImplementationTypedInterfaceObject, name, value)
__swig_getmethods__ = {}
for _s in [openturns.common.InterfaceObject]: __swig_getmethods__.update(getattr(_s,'__swig_getmethods__',{}))
__getattr__ = lambda self, name: _swig_getattr(self, SamplerImplementationTypedInterfaceObject, name)
__repr__ = _swig_repr
def __init__(self, *args):
this = _bayesian.new_SamplerImplementationTypedInterfaceObject(*args)
try: self.this.append(this)
except: self.this = this
def getImplementation(self, *args):
"""
Accessor to the underlying implementation.
Returns
-------
impl : Implementation
The implementation class.
"""
return _bayesian.SamplerImplementationTypedInterfaceObject_getImplementation(self, *args)
def setName(self, *args):
"""
Accessor to the object's name.
Parameters
----------
name : string
The name of the object.
"""
return _bayesian.SamplerImplementationTypedInterfaceObject_setName(self, *args)
def getName(self):
"""
Accessor to the object's name.
Returns
-------
name : string
The name of the object.
"""
return _bayesian.SamplerImplementationTypedInterfaceObject_getName(self)
def __eq__(self, *args): return _bayesian.SamplerImplementationTypedInterfaceObject___eq__(self, *args)
__swig_destroy__ = _bayesian.delete_SamplerImplementationTypedInterfaceObject
__del__ = lambda self : None;
SamplerImplementationTypedInterfaceObject_swigregister = _bayesian.SamplerImplementationTypedInterfaceObject_swigregister
SamplerImplementationTypedInterfaceObject_swigregister(SamplerImplementationTypedInterfaceObject)
class Sampler(SamplerImplementationTypedInterfaceObject):
"""
Sampler.
Available constructors:
Sampler(*aSampler*)
Parameters
----------
aSampler : :class:`~openturns.Sampler`
Particular sampler. By default it is a
:class:`~openturns.RandomWalkMetropolisHastings`.
Notes
-----
A Sampler is an object whose fundamental ability is to produce samples
according to a certain distribution.
See also
--------
MCMC, RandomWalkMetropolisHastings
Examples
--------
>>> import openturns as ot
>>> sampler = ot.Sampler()
"""
__swig_setmethods__ = {}
for _s in [SamplerImplementationTypedInterfaceObject]: __swig_setmethods__.update(getattr(_s,'__swig_setmethods__',{}))
__setattr__ = lambda self, name, value: _swig_setattr(self, Sampler, name, value)
__swig_getmethods__ = {}
for _s in [SamplerImplementationTypedInterfaceObject]: __swig_getmethods__.update(getattr(_s,'__swig_getmethods__',{}))
__getattr__ = lambda self, name: _swig_getattr(self, Sampler, name)
__repr__ = _swig_repr
def getClassName(self):
"""
Accessor to the object's name.
Returns
-------
class_name : str
The object class name (`object.__class__.__name__`).
"""
return _bayesian.Sampler_getClassName(self)
def setVerbose(self, *args):
"""
Set the verbose mode.
Parameters
----------
isVerbose : Bool
The verbose mode is activated if it is *True*, desactivated otherwise.
"""
return _bayesian.Sampler_setVerbose(self, *args)
def getVerbose(self):
"""
Tell whether the verbose mode is activated or not.
Returns
-------
isVerbose : Bool
The verbose mode is activated if it is *True*, desactivated otherwise.
"""
return _bayesian.Sampler_getVerbose(self)
def getDimension(self):
"""
Get the dimension of the samples generated.
Returns
-------
dimension : int
Dimension of the samples that the Sampler can generate.
"""
return _bayesian.Sampler_getDimension(self)
def getRealization(self):
"""
Return a realization.
Returns
-------
realization : float sequence
A new realization.
"""
return _bayesian.Sampler_getRealization(self)
def getSample(self, *args):
"""
Return several realizations.
Parameters
----------
size : int, :math:`size \\leq 0`
Number of realizations needed.
Returns
-------
realizations : 2D float sequence
Sequence composed of *size* new realizations.
"""
return _bayesian.Sampler_getSample(self, *args)
def __str__(self): return _bayesian.Sampler___str__(self)
def __init__(self, *args):
this = _bayesian.new_Sampler(*args)
try: self.this.append(this)
except: self.this = this
__swig_destroy__ = _bayesian.delete_Sampler
__del__ = lambda self : None;
Sampler_swigregister = _bayesian.Sampler_swigregister
Sampler_swigregister(Sampler)
class MCMC(SamplerImplementation):
"""
Monte-Carlo Markov Chain.
Available constructor:
MCMC(*prior, conditional, observations, initialState*)
MCMC(*prior, conditional, model, parameters, observations, initialState*)
Parameters
----------
prior : :class:`~openturns.Distribution`
Prior distribution of the parameters of the underlying Bayesian statistical
model.
conditional : :class:`~openturns.Distribution`
Required distribution to define the likelihood of the underlying Bayesian
statistical model.
model : :class:`~openturns.NumericalMathFunction`
Function required to define the likelihood.
observations : 2D float sequence
Observations required to define the likelihood.
initialState : float sequence
Initial state of the Monte-Carlo Markov chain on which the Sampler is
based.
parameters : 2D float sequence
Parameters of the model to be fixed.
Notes
-----
MCMC provides a implementation of the concept of sampler, using a Monte-Carlo
Markov Chain (MCMC) algorithm starting from *initialState*. More precisely,
let :math:`t(.)` be the PDF of its target distribution and :math:`d_{\\theta}`
its dimension, :math:`\\pi(.)` be the PDF of the *prior* distribution,
:math:`f(.|\\vect{w})` be the PDF of the *conditional* distribution
when its parameters are set to :math:`\\vect{w}`, :math:`d_w` be the number of
scalar parameters of *conditional* distribution (which corresponds to the
dimension of the above :math:`\\vect{w}`), :math:`g(.)` be the function
corresponding to model and :math:`(\\vect{y}^1, \\dots, \\vect{y}^n)` be the
sample *observations* (of size :math:`n`):
In the first usage, it creates a sampler based on a MCMC algorithm whose target
distribution is defined by:
.. _PDF_target_formula:
.. math::
t(\\vect{\\theta})
\\quad \\propto \\quad
\\underbrace{~\\pi(\\vect{\\theta})~}_{\\mbox{prior}} \\quad
\\underbrace{~\\prod_{i=1}^n f(\\vect{y}^i|\\vect{\\theta})~}_{\\mbox{likelihood}}
In the first usage, it creates a sampler based on a MCMC algorithm whose target
distribution is defined by:
.. _second_PDF_target_formula:
.. math::
t(\\vect{\\theta})
\\quad \\propto \\quad
\\underbrace{~\\pi(\\vect{\\theta})~}_{\\mbox{prior}} \\quad
\\underbrace{~\\prod_{i=1}^n f(\\vect{y}^i|g^i(\\vect{\\theta}))~}_{\\mbox{likelihood}}
where the :math:`g^i: \\Rset^{d_{\\theta}} \\rightarrow\\Rset^{d_w}`
(:math:`1\\leq{}i\\leq{}n`) are such that:
.. math::
\\begin{array}{rcl}
g:\\Rset^{d_\\theta} & \\longrightarrow & \\Rset^{n\\,d_w}\\\\
\\vect{\\theta} & \\longmapsto &
g(\\vect{\\theta}) = \\Tr{(\\Tr{g^1(\\vect{\\theta})}, \\cdots, \\Tr{g^n(\\vect{\\theta})})}
\\end{array}
In fact, the first usage is a particular case of the second.
The MCMC method implemented in OpenTURNS is the Random Walk Metropolis-Hastings
algorithm. A sample can be generated only through the MCMC's derived class:
:class:`~openturns.RandomWalkMetropolisHastings`.
"""
__swig_setmethods__ = {}
for _s in [SamplerImplementation]: __swig_setmethods__.update(getattr(_s,'__swig_setmethods__',{}))
__setattr__ = lambda self, name, value: _swig_setattr(self, MCMC, name, value)
__swig_getmethods__ = {}
for _s in [SamplerImplementation]: __swig_getmethods__.update(getattr(_s,'__swig_getmethods__',{}))
__getattr__ = lambda self, name: _swig_getattr(self, MCMC, name)
def getClassName(self):
"""
Accessor to the object's name.
Returns
-------
class_name : str
The object class name (`object.__class__.__name__`).
"""
return _bayesian.MCMC_getClassName(self)
def __repr__(self): return _bayesian.MCMC___repr__(self)
def computeLogLikelihood(self, *args):
"""
Compute the logarithm of the likelihood w.r.t. observations.
Parameters
----------
currentState : float sequence
Current state.
Returns
-------
logLikelihood : float
Logarithm of the likelihood w.r.t. observations
:math:`(\\vect{y}^1, \\dots, \\vect{y}^n)`.
"""
return _bayesian.MCMC_computeLogLikelihood(self, *args)
def setPrior(self, *args):
"""
Set the *prior* distribution.
Parameters
----------
prior : :class:`~openturns.Distribution`
The prior distribution of the parameter of the underlying Bayesian
statistical model, whose PDF corresponds to :math:`\\pi` in the equations of
the :ref:`target distribution's PDF <PDF_target_formula>`.
"""
return _bayesian.MCMC_setPrior(self, *args)
def getPrior(self):
"""
Get the *prior* distribution.
Returns
-------
prior : :class:`~openturns.Distribution`
The prior distribution of the parameter of the underlying Bayesian
statistical model, whose PDF corresponds to :math:`\\pi` in the equations of
the :ref:`target distribution's PDF <PDF_target_formula>`.
"""
return _bayesian.MCMC_getPrior(self)
def getConditional(self):
"""
Get the *conditional* distribution.
Returns
-------
conditional : :class:`~openturns.Distribution`
Distribution taken into account in the definition of the likelihood, whose
PDF with parameters :math:`\\vect{w}` corresponds to :math:`f(.|\\vect{w})`
in the equations of the
:ref:`target distribution's PDF <PDF_target_formula>`.
"""
return _bayesian.MCMC_getConditional(self)
def getModel(self):
"""
Get the model.
Returns
-------
model : :class:`~openturns.NumericalMathFunction`
Model take into account in the definition of the likelihood, which
corresponds to :math:`g`, that is the functions :math:`g^i`
(:math:`1\\leq i \\leq n`) in the equation of the
:ref:`target distribution's PDF <second_PDF_target_formula>`.
"""
return _bayesian.MCMC_getModel(self)
def setObservations(self, *args):
"""
Set the observations.
Parameters
----------
observations : 2D float sequence
Sample taken into account in the definition of the likelihood, which
corresponds to the :math:`n`-tuple of the :math:`\\vect{y}^i`
(:math:`1\\leq i \\leq n`) in the equations of the
:ref:`target distribution's PDF <PDF_target_formula>`.
"""
return _bayesian.MCMC_setObservations(self, *args)
def getObservations(self):
"""
Get the observations.
Returns
-------
observations : 2D float sequence
Sample taken into account in the definition of the likelihood, which
corresponds to the :math:`n`-tuple of the :math:`\\vect{y}^i`
(:math:`1\\leq i \\leq n`) in equations of the
:ref:`target distribution's PDF <PDF_target_formula>`.
"""
return _bayesian.MCMC_getObservations(self)
def setParameters(self, *args):
"""
Set the parameters.
Parameters
----------
parameters : float sequence
Fixed parameters of the model :math:`g` required to define the likelihood.
"""
return _bayesian.MCMC_setParameters(self, *args)
def getParameters(self):
"""
Get the parameters.
Returns
-------
parameters : float sequence
Fixed parameters of the model :math:`g` required to define the likelihood.
"""
return _bayesian.MCMC_getParameters(self)
def setBurnIn(self, *args):
"""
Set the length of the burn-in period.
Parameters
----------
lenght : int
Length of the burn-in period, that is the number of first iterates of the
MCMC chain which will be thrown away when generating the sample.
"""
return _bayesian.MCMC_setBurnIn(self, *args)
def getBurnIn(self):
"""
Get the length of the burn-in period.
Returns
-------
lenght : int
Length of the burn-in period, that is the number of first iterates of the
MCMC chain which will be thrown away when generating the sample.
"""
return _bayesian.MCMC_getBurnIn(self)
def setThinning(self, *args):
"""
Set the thinning parameter.
Parameters
----------
thinning : integer, :math:`k \\geq 0`
Thinning parameter: storing only every :math:`k^{th}` point after the
burn-in period.
Notes
-----
When generating a sample of size :math:`q`, the number of MCMC iterations
performed is :math:`l+1+(q-1)k` where :math:`l` is the burn-in period length
and :math:`k` the thinning parameter.
"""
return _bayesian.MCMC_setThinning(self, *args)
def getThinning(self):
"""
Get the thinning parameter.
Returns
-------
thinning : integer
Thinning parameter: storing only every :math:`k^{th}` point after the
burn-in period.
Notes
-----
When generating a sample of size :math:`q`, the number of MCMC iterations
performed is :math:`l+1+(q-1)k` where :math:`l` is the burn-in period length
and :math:`k` the thinning parameter.
"""
return _bayesian.MCMC_getThinning(self)
def getHistory(self): return _bayesian.MCMC_getHistory(self)
def setHistory(self, *args): return _bayesian.MCMC_setHistory(self, *args)
def getDimension(self):
"""
Get the dimension of the samples generated.
Returns
-------
dimension : int
Dimension of the samples that the Sampler can generate.
"""
return _bayesian.MCMC_getDimension(self)
def __init__(self, *args):
this = _bayesian.new_MCMC(*args)
try: self.this.append(this)
except: self.this = this
__swig_destroy__ = _bayesian.delete_MCMC
__del__ = lambda self : None;
MCMC_swigregister = _bayesian.MCMC_swigregister
MCMC_swigregister(MCMC)
class RandomWalkMetropolisHastings(MCMC):
"""
Random Walk Metropolis-Hastings method.
Available constructor:
RandomWalkMetropolisHastings(*prior, conditional, observations, initialState, proposal*)
RandomWalkMetropolisHastings(*prior, conditional, model, parameters, observations, initialState, proposal*)
Parameters
----------
prior : :class:`~openturns.Distribution`
Prior distribution of the parameters of the underlying Bayesian statistical
model.
conditional : :class:`~openturns.Distribution`
Required distribution to define the likelihood of the underlying Bayesian
statistical model.
model : :class:`~openturns.NumericalMathFunction`
Function required to define the likelihood.
observations : 2D float sequence
Observations required to define the likelihood.
initialState : float sequence
Initial state of the Monte-Carlo Markov chain on which the Sampler is
based.
parameters : 2D float sequence
Parameters of the model to be fixed.
proposal : list of :class:`~openturns.Distribution`
Distributions from which the transition kernels of the
:class:`~openturns.MCMC` are defined, as explained hereafter. In the
following of this paragraph, :math:`\\delta \\sim p_j` means that the
realization :math:`\\delta` is obtained according to the :math:`j^{th}`
Distribution of the list *proposal* of size :math:`d`. The underlying
MCMC algorithm is a Metropolis-Hastings one which draws candidates (for the
next state of the chain) using a random walk: from the current state
:math:`\\vect{\\theta}^k`, the candidate :math:`\\vect{c}^k` for
:math:`\\vect{\\theta}^{k+1}` can be expressed as
:math:`\\vect{c}^k = \\vect{\\theta}^k +\\vect{\\delta}^k` where the
distribution of :math:`\\vect{\\delta}^k` does not depend on
:math:`\\vect{\\theta}^k`. More precisely, here, during the :math:`k^{th}`
Metropolis-Hastings iteration, only the :math:`j^{th}` component
:math:`\\delta_j^k` of :math:`\\vect{\\delta}^k` , with :math:`j=k \\mod d`, is
not zero and :math:`\\delta_j^k = \\lambda_j^k \\delta^k` where
:math:`\\lambda_j^k` is a deterministic scalar *calibration* coefficient and
where :math:`\\delta^k \\sim p_j`. Moreover, :math:`\\lambda_j^k = 1` by default,
but adaptive strategy based on the acceptance rate of each component can be
defined using the method :meth:`setCalibrationStrategyPerComponent`.
Notes
-----
A RandomWalkMetropolisHastings enables to carry out :class:`~openturns.MCMC`
sampling according to the preceding statements. It is important to note that
sampling one new realization comes to carrying out :math:`d` Metropolis-
Hastings iterations (such as described above): all of the components of the new
realization can differ from the corresponding components of the previous
realization. Besides, the burn-in and thinning parameters do not take into
consideration the number of MCMC iterations indeed, but the number of sampled
realizations.
Examples
--------
>>> import openturns as ot
>>> ot.RandomGenerator.SetSeed(0)
>>> chainDim = 3
>>> # Observations
>>> obsDim = 1
>>> obsSize = 10
>>> y = [-9.50794871493506, -3.83296694500105, -2.44545713047953,
... 0.0803625289211318, 1.01898069723583, 0.661725805623086,
... -1.57581204592385, -2.95308465670895, -8.8878164296758,
... -13.0812290405651]
>>> y_obs = ot.NumericalSample(y, obsDim)
>>> # Parameters
>>> p = ot.NumericalSample(obsSize, chainDim)
>>> for i in range(obsSize):
... for j in range(chainDim):
... p[i, j] = (-2 + 5. * i / 9.) ** j
>>> # Model
>>> fullModel = ot.NumericalMathFunction(
... ['p1', 'p2', 'p3', 'x1', 'x2', 'x3'], ['z', 'sigma'],
... ['p1*x1+p2*x2+p3*x3', '1.0'])
>>> model = ot.NumericalMathFunction(fullModel, range(chainDim))
>>> # Calibration parameters
>>> calibrationColl = [ot.CalibrationStrategy()]*chainDim
>>> # Proposal distribution
>>> proposalColl = [ot.Uniform(-1., 1.)]*chainDim
>>> # Prior distribution
>>> sigma0 = [10.]*chainDim
>>> # Covariance matrix
>>> Q0_inv = ot.CorrelationMatrix(chainDim)
>>> for i in range(chainDim):
... Q0_inv[i, i] = sigma0[i] * sigma0[i]
>>> mu0 = [0.]*chainDim
>>> # x0 ~ N(mu0, sigma0)
>>> prior = ot.Normal(mu0, Q0_inv)
>>> # Conditional distribution y~N(z, 1.0)
>>> conditional = ot.Normal()
>>> # Create a metropolis-hastings sampler
>>> # prior =a distribution of dimension chainDim, the a priori distribution of the parameter
>>> # conditional =a distribution of dimension 1, the observation error on the output
>>> # model =the link between the parameters and the output
>>> # y_obs =noisy observations of the output
>>> # mu0 =starting point of the chain
>>> sampler = ot.RandomWalkMetropolisHastings(
... prior, conditional, model, p, y_obs, mu0, proposalColl)
>>> sampler.setCalibrationStrategyPerComponent(calibrationColl)
>>> # Get a realization
>>> print(sampler.getRealization())
[1.25054,1.32356,-2.15476]
"""
__swig_setmethods__ = {}
for _s in [MCMC]: __swig_setmethods__.update(getattr(_s,'__swig_setmethods__',{}))
__setattr__ = lambda self, name, value: _swig_setattr(self, RandomWalkMetropolisHastings, name, value)
__swig_getmethods__ = {}
for _s in [MCMC]: __swig_getmethods__.update(getattr(_s,'__swig_getmethods__',{}))
__getattr__ = lambda self, name: _swig_getattr(self, RandomWalkMetropolisHastings, name)
def getClassName(self):
"""
Accessor to the object's name.
Returns
-------
class_name : str
The object class name (`object.__class__.__name__`).
"""
return _bayesian.RandomWalkMetropolisHastings_getClassName(self)
def __repr__(self): return _bayesian.RandomWalkMetropolisHastings___repr__(self)
def getRealization(self):
"""
Return a realization.
Returns
-------
realization : float sequence
A new realization.
"""
return _bayesian.RandomWalkMetropolisHastings_getRealization(self)
def setCalibrationStrategy(self, *args):
"""
Set the calibration strategy.
Parameters
----------
strategy : :class:`~openturns.CalibrationStrategy`
Same strategy applied for each component :math:`\\lambda_j^k`.
See also
--------
setCalibrationStrategyPerComponent
"""
return _bayesian.RandomWalkMetropolisHastings_setCalibrationStrategy(self, *args)
def setCalibrationStrategyPerComponent(self, *args):
"""
Set the calibration strategy per component.
Parameters
----------
strategy : list of :class:`~openturns.CalibrationStrategy`
A list of CalibrationStrategy *strategy*, whose :math:`j^{th}` component
:math:`strategy[j]` defines whether and how the :math:`\\lambda_j^k` (see the
paragraph dedicated to the constructors of the class above) are rescaled,
on the basis of the last :math:`j^{th}` component acceptance rate
:math:`\\rho_j^k` . The *calibration* coefficients are rescaled every
:math:`q\\times d` MCMC iterations with
:math:`q = strategy[j].getCalibrationStep()`, thus on the basis of the
acceptances or refusals of the last :math:`q` candidates obtained by only
changing the :math:`j^{th}` component of the current state:
:math:`\\lambda_j^k = \\Phi_j (\\rho_j^k)\\lambda_j^{k-qd}` where
:math:`\\Phi_j(.)` is defined by :math:`strategy[j].computeUpdateFactor()`.
"""
return _bayesian.RandomWalkMetropolisHastings_setCalibrationStrategyPerComponent(self, *args)
def getCalibrationStrategyPerComponent(self):
"""
Get the calibration strategy per component.
Returns
-------
strategy : list of :class:`~openturns.CalibrationStrategy`
A list of CalibrationStrategy *strategy*, whose :math:`j^{th}` component
:math:`strategy[j]` defines whether and how the :math:`\\lambda_j^k` (see the
paragraph dedicated to the constructors of the class above) are rescaled,
on the basis of the last :math:`j^{th}` component acceptance rate
:math:`\\rho_j^k` . The *calibration* coefficients are rescaled every
:math:`q\\times d` MCMC iterations with
:math:`q = strategy[j].getCalibrationStep()`, thus on the basis of the
acceptances or refusals of the last :math:`q` candidates obtained by only
changing the :math:`j^{th}` component of the current state:
:math:`\\lambda_j^k = \\Phi_j (\\rho_j^k)\\lambda_j^{k-qd}` where
:math:`\\Phi_j(.)` is defined by :math:`strategy[j].computeUpdateFactor()`.
"""
return _bayesian.RandomWalkMetropolisHastings_getCalibrationStrategyPerComponent(self)
def setProposal(self, *args):
"""
Set the proposal.
Parameters
----------
proposal : list of :class:`~openturns.Distribution`
The :math:`d`-tuple of Distributions :math:`p_j (1 \\leq j \\leq d)` from
which the transition kernels of the random walk Metropolis-Hastings
algorithm are defined; look at the paragraph dedicated to the constructors
of the class above.
"""
return _bayesian.RandomWalkMetropolisHastings_setProposal(self, *args)
def getProposal(self):
"""
Get the proposal.
Returns
-------
proposal : list of :class:`~openturns.Distribution`
The :math:`d`-tuple of Distributions :math:`p_j (1 \\leq j \\leq d)` from
which the transition kernels of the random walk Metropolis-Hastings
algorithm are defined; look at the paragraph dedicated to the constructors
of the class above.
"""
return _bayesian.RandomWalkMetropolisHastings_getProposal(self)
def getAcceptanceRate(self):
"""
Get acceptance rate.
Returns
-------
acceptanceRate : float sequence of dimension :math:`d`
Sequence whose the :math:`j^{th}` component corresponds to the acceptance
rate of the candidates :math:`\\vect{c}^k` obtained from a state
:math:`\\vect{\\theta}^k` by only changing its :math:`j^{th}` component, that
is to the acceptance rate only relative to the :math:`k^{th}` MCMC
iterations such that :math:`k \\mod d=j` (see the paragraph dedicated to the
constructors of the class above). These are global acceptance rates over
all the MCMC iterations performed.
"""
return _bayesian.RandomWalkMetropolisHastings_getAcceptanceRate(self)
def __init__(self, *args):
this = _bayesian.new_RandomWalkMetropolisHastings(*args)
try: self.this.append(this)
except: self.this = this
__swig_destroy__ = _bayesian.delete_RandomWalkMetropolisHastings
__del__ = lambda self : None;
RandomWalkMetropolisHastings_swigregister = _bayesian.RandomWalkMetropolisHastings_swigregister
RandomWalkMetropolisHastings_swigregister(RandomWalkMetropolisHastings)
class PosteriorRandomVector(openturns.randomvector.RandomVectorImplementation):
__swig_setmethods__ = {}
for _s in [openturns.randomvector.RandomVectorImplementation]: __swig_setmethods__.update(getattr(_s,'__swig_setmethods__',{}))
__setattr__ = lambda self, name, value: _swig_setattr(self, PosteriorRandomVector, name, value)
__swig_getmethods__ = {}
for _s in [openturns.randomvector.RandomVectorImplementation]: __swig_getmethods__.update(getattr(_s,'__swig_getmethods__',{}))
__getattr__ = lambda self, name: _swig_getattr(self, PosteriorRandomVector, name)
def getClassName(self):
"""
Accessor to the object's name.
Returns
-------
class_name : str
The object class name (`object.__class__.__name__`).
"""
return _bayesian.PosteriorRandomVector_getClassName(self)
def __repr__(self): return _bayesian.PosteriorRandomVector___repr__(self)
def getSampler(self): return _bayesian.PosteriorRandomVector_getSampler(self)
def getDimension(self):
"""
Accessor to the dimension of the RandomVector.
Returns
-------
dimension : positive int
Dimension of the RandomVector.
"""
return _bayesian.PosteriorRandomVector_getDimension(self)
def getRealization(self):
"""
Compute one realization of the RandomVector.
Returns
-------
aRealization : float sequence
Sequence of values randomly determined from the RandomVector definition.
In the case of an event: one realization of the event (considered as a
Bernoulli variable) which is a boolean value (1 for the realization of the
event and 0 else).
See also
--------
getSample
Examples
--------
>>> import openturns as ot
>>> distribution = ot.Normal([0., 0.], [1., 1.], ot.CorrelationMatrix(2))
>>> randomVector = ot.RandomVector(distribution)
>>> ot.RandomGenerator.SetSeed(0)
>>> print(randomVector.getRealization())
[0.608202,-1.26617]
>>> print(randomVector.getRealization())
[-0.438266,1.20548]
"""
return _bayesian.PosteriorRandomVector_getRealization(self)
def getSample(self, *args):
"""
Compute realizations of the RandomVector.
Parameters
----------
n : int, :math:`n \\geq 0`
Number of realizations needed.
Returns
-------
realizations : 2D float sequence
n sequences of values randomly determined from the RandomVector definition.
In the case of an event: n realizations of the event (considered as a
Bernoulli variable) which are boolean values (1 for the realization of the
event and 0 else).
See also
--------
getRealization
Examples
--------
>>> import openturns as ot
>>> distribution = ot.Normal([0., 0.], [1., 1.], ot.CorrelationMatrix(2))
>>> randomVector = ot.RandomVector(distribution)
>>> ot.RandomGenerator.SetSeed(0)
>>> print(randomVector.getSample(3))
[ marginal 1 marginal 2 ]
0 : [ 0.608202 -1.26617 ]
1 : [ -0.438266 1.20548 ]
2 : [ -2.18139 0.350042 ]
"""
return _bayesian.PosteriorRandomVector_getSample(self, *args)
def __init__(self, *args):
this = _bayesian.new_PosteriorRandomVector(*args)
try: self.this.append(this)
except: self.this = this
__swig_destroy__ = _bayesian.delete_PosteriorRandomVector
__del__ = lambda self : None;
PosteriorRandomVector_swigregister = _bayesian.PosteriorRandomVector_swigregister
PosteriorRandomVector_swigregister(PosteriorRandomVector)
class CalibrationStrategyImplementationPointer(_object):
__swig_setmethods__ = {}
__setattr__ = lambda self, name, value: _swig_setattr(self, CalibrationStrategyImplementationPointer, name, value)
__swig_getmethods__ = {}
__getattr__ = lambda self, name: _swig_getattr(self, CalibrationStrategyImplementationPointer, name)
__swig_setmethods__["ptr_"] = _bayesian.CalibrationStrategyImplementationPointer_ptr__set
__swig_getmethods__["ptr_"] = _bayesian.CalibrationStrategyImplementationPointer_ptr__get
if _newclass:ptr_ = _swig_property(_bayesian.CalibrationStrategyImplementationPointer_ptr__get, _bayesian.CalibrationStrategyImplementationPointer_ptr__set)
def __init__(self, *args):
this = _bayesian.new_CalibrationStrategyImplementationPointer(*args)
try: self.this.append(this)
except: self.this = this
__swig_destroy__ = _bayesian.delete_CalibrationStrategyImplementationPointer
__del__ = lambda self : None;
def reset(self): return _bayesian.CalibrationStrategyImplementationPointer_reset(self)
def __ref__(self, *args): return _bayesian.CalibrationStrategyImplementationPointer___ref__(self, *args)
def __deref__(self): return _bayesian.CalibrationStrategyImplementationPointer___deref__(self)
def isNull(self): return _bayesian.CalibrationStrategyImplementationPointer_isNull(self)
def __nonzero__(self):
return _bayesian.CalibrationStrategyImplementationPointer___nonzero__(self)
__bool__ = __nonzero__
def get(self): return _bayesian.CalibrationStrategyImplementationPointer_get(self)
def getImplementation(self): return _bayesian.CalibrationStrategyImplementationPointer_getImplementation(self)
def unique(self): return _bayesian.CalibrationStrategyImplementationPointer_unique(self)
def use_count(self): return _bayesian.CalibrationStrategyImplementationPointer_use_count(self)
def swap(self, *args): return _bayesian.CalibrationStrategyImplementationPointer_swap(self, *args)
def getClassName(self):
"""
Accessor to the object's name.
Returns
-------
class_name : str
The object class name (`object.__class__.__name__`).
"""
return _bayesian.CalibrationStrategyImplementationPointer_getClassName(self)
def __repr__(self): return _bayesian.CalibrationStrategyImplementationPointer___repr__(self)
def setRange(self, *args):
"""
Set the range.
Parameters
----------
range : :class:`~openturns.Interval` of dimension 1
Range :math:`[m,M]` in the description of the method
:meth:`computeUpdateFactor`.
"""
return _bayesian.CalibrationStrategyImplementationPointer_setRange(self, *args)
def getRange(self):
"""
Get the range.
Returns
-------
range : :class:`~openturns.Interval` of dimension 1
Range :math:`[m,M]` in the description of the method
:meth:`computeUpdateFactor`.
"""
return _bayesian.CalibrationStrategyImplementationPointer_getRange(self)
def setExpansionFactor(self, *args):
"""
Set the expansion factor.
Parameters
----------
expansionFactor : float, :math:`e > 1`
Expansion factor :math:`e`. See the description of the method
:meth:`computeUpdateFactor`.
"""
return _bayesian.CalibrationStrategyImplementationPointer_setExpansionFactor(self, *args)
def getExpansionFactor(self):
"""
Get the expansion factor.
Returns
-------
expansionFactor : float
Expansion factor :math:`e`. See the description of the method
:meth:`computeUpdateFactor`.
"""
return _bayesian.CalibrationStrategyImplementationPointer_getExpansionFactor(self)
def setShrinkFactor(self, *args):
"""
Set the shrink factor.
Parameters
----------
shrinkFactor : float, :math:`0 < s < 1`
Shrink factor :math:`s` in the description of the method
:meth:`computeUpdateFactor`.
"""
return _bayesian.CalibrationStrategyImplementationPointer_setShrinkFactor(self, *args)
def getShrinkFactor(self):
"""
Get the shrink factor.
Returns
-------
shrinkFactor : float
Shrink factor :math:`s` in the description of the method
:meth:`computeUpdateFactor`.
"""
return _bayesian.CalibrationStrategyImplementationPointer_getShrinkFactor(self)
def setCalibrationStep(self, *args):
"""
Set the calibration step.
Parameters
----------
step : positive int
Calibration step corresponding for example to :math:`q` in the description
of the method
:meth:`~openturns.RandomWalkMetropolisHastings.getCalibrationStrategyPerComponent`
of the RandomWalkMetropolisHastings class.
"""
return _bayesian.CalibrationStrategyImplementationPointer_setCalibrationStep(self, *args)
def getCalibrationStep(self):
"""
Get the calibration step.
Returns
-------
step : positive int
Calibration step corresponding for example to :math:`q` in the description
of the method
:meth:`~openturns.RandomWalkMetropolisHastings.getCalibrationStrategyPerComponent`
of the RandomWalkMetropolisHastings class.
"""
return _bayesian.CalibrationStrategyImplementationPointer_getCalibrationStep(self)
def computeUpdateFactor(self, *args):
"""
Compute the update factor.
Parameters
----------
rho : float
Acceptance rate :math:`\\rho` to take into account.
Returns
-------
lambda : float
Let :math:`\\lambda` be the *calibration* coefficient to update, it gives a
factor :math:`\\phi(\\rho)` such that :math:`\\phi(\\rho) \\lambda` is the
updated *calibration* coefficient according to the strategy. The value is
computed as follows:
.. math::
\\phi(\\rho) = \\left\\{
\\begin{array}{l}
\\displaystyle s \\quad if \\; \\rho < m \\\\
\\displaystyle e \\quad if \\; \\rho > M \\\\
\\displaystyle 1 \\quad otherwise
\\end{array}
\\right.
with :math:`s \\in ]0, 1[, e > 1` and :math:`[m,M]` the values given,
respectively, by the methods :meth:`getShrinkFactor`,
:meth:`getExpansionFactor` and :meth:`getRange`.
Examples
--------
>>> import openturns as ot
>>> calibration = ot.CalibrationStrategy(ot.Interval(0.1, 0.4), 1.2, 0.8)
>>> print(calibration.computeUpdateFactor(0.09))
0.8
>>> print(calibration.computeUpdateFactor(0.6))
1.2
>>> print(calibration.computeUpdateFactor(0.18))
1.0
"""
return _bayesian.CalibrationStrategyImplementationPointer_computeUpdateFactor(self, *args)
def __eq__(self, *args): return _bayesian.CalibrationStrategyImplementationPointer___eq__(self, *args)
def __ne__(self, *args): return _bayesian.CalibrationStrategyImplementationPointer___ne__(self, *args)
def __str__(self, offset=""): return _bayesian.CalibrationStrategyImplementationPointer___str__(self, offset)
def getId(self):
"""
Accessor to the object's id.
Returns
-------
id : int
Internal unique identifier.
"""
return _bayesian.CalibrationStrategyImplementationPointer_getId(self)
def setShadowedId(self, *args):
"""
Accessor to the object's shadowed id.
Parameters
----------
id : int
Internal unique identifier.
"""
return _bayesian.CalibrationStrategyImplementationPointer_setShadowedId(self, *args)
def getShadowedId(self):
"""
Accessor to the object's shadowed id.
Returns
-------
id : int
Internal unique identifier.
"""
return _bayesian.CalibrationStrategyImplementationPointer_getShadowedId(self)
def setVisibility(self, *args):
"""
Accessor to the object's visibility state.
Parameters
----------
visible : bool
Visibility flag.
"""
return _bayesian.CalibrationStrategyImplementationPointer_setVisibility(self, *args)
def getVisibility(self):
"""
Accessor to the object's visibility state.
Returns
-------
visible : bool
Visibility flag.
"""
return _bayesian.CalibrationStrategyImplementationPointer_getVisibility(self)
def hasName(self):
"""
Test if the object is named.
Returns
-------
hasName : bool
True if the name is not empty.
"""
return _bayesian.CalibrationStrategyImplementationPointer_hasName(self)
def hasVisibleName(self):
"""
Test if the object has a distinghishable name.
Returns
-------
hasVisibleName : bool
True if the name is not empty and not the default one.
"""
return _bayesian.CalibrationStrategyImplementationPointer_hasVisibleName(self)
def getName(self):
"""
Accessor to the object's name.
Returns
-------
name : string
The name of the object.
"""
return _bayesian.CalibrationStrategyImplementationPointer_getName(self)
def setName(self, *args):
"""
Accessor to the object's name.
Parameters
----------
name : string
The name of the object.
"""
return _bayesian.CalibrationStrategyImplementationPointer_setName(self, *args)
CalibrationStrategyImplementationPointer_swigregister = _bayesian.CalibrationStrategyImplementationPointer_swigregister
CalibrationStrategyImplementationPointer_swigregister(CalibrationStrategyImplementationPointer)
# This file is compatible with both classic and new-style classes.
|