Search code examples
pythonprobabilitysympyarbitrary-precisionmpmath

Probability values of arbitrary precision in SymPy


I am trying to compute some simple probabilities in Sympy, but I keep getting numerical errors. How could I change this code to use arbitrary precision in mpmath?

from sympy import *
from sympy.stats import Binomial, density, P
from mpmath import mp
mp.dps = 1000
n = 128

X = Binomial('X', n, 1/2, succ=1, fail=0)

for t in range (n+1):
    print(t, P(X<t)) 

Solution

  • Why not use Rational?

    >>> from sympy import *
    >>> from sympy.stats import Binomial, density, P
    >>> n = 128
    >>> X = Binomial('X', n, Rational(1,2), succ=1, fail=0)
    >>> for t in range(n+1):
    ...     print(t, P(X<t))
    

    produces the following output:

    (0, 0)
    (1, 1/340282366920938463463374607431768211456)
    (2, 129/340282366920938463463374607431768211456)
    (3, 8257/340282366920938463463374607431768211456)
    (4, 349633/340282366920938463463374607431768211456)
    (5, 11017633/340282366920938463463374607431768211456)
    (6, 275584033/340282366920938463463374607431768211456)
    (7, 5699195233/340282366920938463463374607431768211456)
    (8, 100224990433/340282366920938463463374607431768211456)
    (9, 1529927642833/340282366920938463463374607431768211456)
    (10, 20592629674833/340282366920938463463374607431768211456)
    (11, 247438783855633/340282366920938463463374607431768211456)
    (12, 2680879346886033/340282366920938463463374607431768211456)
    (13, 26406924836432433/340282366920938463463374607431768211456)
    (14, 238116253820077233/340282366920938463463374607431768211456)
    (15, 1977157170471445233/340282366920938463463374607431768211456)
    (16, 15193868137021842033/340282366920938463463374607431768211456)
    (17, 108536889338284019433/340282366920938463463374607431768211456)
    (18, 723502676076011305833/340282366920938463463374607431768211456)
    (19, 4515791694291996238633/340282366920938463463374607431768211456)
    (20, 26471149168174014270633/340282366920938463463374607431768211456)
    (21, 146127847400831012545033/340282366920938463463374607431768211456)
    (22, 761505152597352717956233/340282366920938463463374607431768211456)
    (23, 3754476591507708285183433/340282366920938463463374607431768211456)
    (24, 17548171049094564377621833/340282366920938463463374607431768211456)
    (25, 77895584301037059782039833/340282366920938463463374607431768211456)
    (26, 328940823429117840664418713/340282366920938463463374607431768211456)
    (27, 1323466193821130164929227353/340282366920938463463374607431768211456)
    (28, 5080562037524287834374059993/340282366920938463463374607431768211456)
    (29, 18632943473739249427728634873/340282366920938463463374607431768211456)
    (30, 65365293253790841128951306873/340282366920938463463374607431768211456)
    (31, 219582047527961093742986124473/340282366920938463463374607431768211456)
    (32, 707105980394692860071225225273/340282366920938463463374607431768211456)
    (33, 2184912901896973526753699999573/340282366920938463463374607431768211456)
    (34, 6483987582630880920739081161173/340282366920938463463374607431768211456)
    (35, 18496108014093269227462940289173/340282366920938463463374607431768211456)
    (36, 50757231458592254965521304804373/340282366920938463463374607431768211456)
    (37, 134098467023547968122172079801973/340282366920938463463374607431768211456)
    (38, 341325323022897308944114547363573/340282366920938463463374607431768211456)
    (39, 837579109758181256701924140734773/340282366920938463463374607431768211456)
    (40, 1982780156070374982296869356206773/340282366920938463463374607431768211456)
    (41, 4530852484115006021745622460631973/340282366920938463463374607431768211456)
    (42, 9999885773576653130806360831105573/340282366920938463463374607431768211456)
    (43, 21328597587461493571003604598515173/340282366920938463463374607431768211456)
    (44, 43986021215231174451398092133334373/340282366920938463463374607431768211456)
    (45, 87756044132513512515796533961962373/340282366920938463463374607431768211456)
    (46, 169460086911440543569340292042067973/340282366920938463463374607431768211456)
    (47, 316882598882113230035517072925736773/340282366920938463463374607431768211456)
    (48, 574087832533074087274378690637669573/340282366920938463463374607431768211456)
    (49, 1008121664319070533864957670526556173/340282366920938463463374607431768211456)
    (50, 1716748328459472895645494780549228173/340282366920938463463374607431768211456)
    (51, 2836378457801308627258743414385049933/340282366920938463463374607431768211456)
    (52, 4548753949735880922667241324957483213/340282366920938463463374607431768211456)
    (53, 7084386889715920667791363230997432493/340282366920938463463374607431768211456)
    (54, 10720388841385411623063688983054718253/340282366920938463463374607431768211456)
    (55, 15770391552037482394275252527578726253/340282366920938463463374607431768211456)
    (56, 22564940653642086704632628932938300653/340282366920938463463374607431768211456)
    (57, 31422120732519517323491351747067745853/340282366920938463463374607431768211456)
    (58, 42610137674259429684155001617547045053/340282366920938463463374607431768211456)
    (59, 56305813585699667229105331631409635453/340282366920938463463374607431768211456)
    (60, 72554920599272830418029451986839827453/340282366920938463463374607431768211456)
    (61, 91241393664881968085292190395584548253/340282366920938463463374607431768211456)
    (62, 112072216098675760894699833211890138653/340282366920938463463374607431768211456)
    (63, 134582943567452924091962931094026825053/340282366920938463463374607431768211456)
    (64, 158165610439505190298619509827693829853/340282366920938463463374607431768211456)
    (65, 182116756481433273164755097604074381603/340282366920938463463374607431768211456)
    (66, 205699423353485539371411676337741386403/340282366920938463463374607431768211456)
    (67, 228210150822262702568674774219878072803/340282366920938463463374607431768211456)
    (68, 249040973256056495378082417036183663203/340282366920938463463374607431768211456)
    (69, 267727446321665633045345155444928384003/340282366920938463463374607431768211456)
    (70, 283976553335238796234269275800358576003/340282366920938463463374607431768211456)
    (71, 297672229246679033779219605814221166403/340282366920938463463374607431768211456)
    (72, 308860246188418946139883255684700465603/340282366920938463463374607431768211456)
    (73, 317717426267296376758741978498829910803/340282366920938463463374607431768211456)
    (74, 324511975368900981069099354904189485203/340282366920938463463374607431768211456)
    (75, 329561978079553051840310918448713493203/340282366920938463463374607431768211456)
    (76, 333197980031222542795583244200770778963/340282366920938463463374607431768211456)
    (77, 335733612971202582540707366106810728243/340282366920938463463374607431768211456)
    (78, 337445988463137154836115864017383161523/340282366920938463463374607431768211456)
    (79, 338565618592478990567729112651218983283/340282366920938463463374607431768211456)
    (80, 339274245256619392929509649761241655283/340282366920938463463374607431768211456)
    (81, 339708279088405389376100228741130541883/340282366920938463463374607431768211456)
    (82, 339965484322056350233339090358842474683/340282366920938463463374607431768211456)
    (83, 340112906834027022919805267139726143483/340282366920938463463374607431768211456)
    (84, 340194610876805949950858810897806249083/340282366920938463463374607431768211456)
    (85, 340238380899723232288923209339634877083/340282366920938463463374607431768211456)
    (86, 340261038323351001969803603827169696283/340282366920938463463374607431768211456)
    (87, 340272367035164886810243801070937105883/340282366920938463463374607431768211456)
    (88, 340277836068454348457352861809307579483/340282366920938463463374607431768211456)
    (89, 340280384140782393088392310562412004683/340282366920938463463374607431768211456)
    (90, 340281529341828705282117905507627476683/340282366920938463463374607431768211456)
    (91, 340282025595615440566065663317220847883/340282366920938463463374607431768211456)
    (92, 340282232822471439915406485259688409483/340282366920938463463374607431768211456)
    (93, 340282316163707004871119641910463407083/340282366920938463463374607431768211456)
    (94, 340282348424830449370105379968827922283/340282366920938463463374607431768211456)
    (95, 340282360436950880832493686692687050283/340282366920938463463374607431768211456)
    (96, 340282364736025561566401080678068211883/340282366920938463463374607431768211456)
    (97, 340282366213832483068681747360542986183/340282366920938463463374607431768211456)
    (98, 340282366701356415935413513688782086983/340282366920938463463374607431768211456)
    (99, 340282366855573170209583766302816904583/340282366920938463463374607431768211456)
    (100, 340282366902305519989635358004039576583/340282366920938463463374607431768211456)
    (101, 340282366915857901425850319597394151463/340282366920938463463374607431768211456)
    (102, 340282366919614997269553477266838984103/340282366920938463463374607431768211456)
    (103, 340282366920609522639945489591103792743/340282366920938463463374607431768211456)
    (104, 340282366920860567879073570371986171623/340282366920938463463374607431768211456)
    (105, 340282366920920915292325512867390589623/340282366920938463463374607431768211456)
    (106, 340282366920934708986783099723483028023/340282366920938463463374607431768211456)
    (107, 340282366920937701958222010079050255223/340282366920938463463374607431768211456)
    (108, 340282366920938317335527206600755666423/340282366920938463463374607431768211456)
    (109, 340282366920938436992225439257753940823/340282366920938463463374607431768211456)
    (110, 340282366920938458947582913139771972823/340282366920938463463374607431768211456)
    (111, 340282366920938462739871931355756905623/340282366920938463463374607431768211456)
    (112, 340282366920938463354837718093484192023/340282366920938463463374607431768211456)
    (113, 340282366920938463448180739294746369423/340282366920938463463374607431768211456)
    (114, 340282366920938463461397450261296766223/340282366920938463463374607431768211456)
    (115, 340282366920938463463136491177948134223/340282366920938463463374607431768211456)
    (116, 340282366920938463463348200506931779023/340282366920938463463374607431768211456)
    (117, 340282366920938463463371926552421325423/340282366920938463463374607431768211456)
    (118, 340282366920938463463374359992984355823/340282366920938463463374607431768211456)
    (119, 340282366920938463463374586839138536623/340282366920938463463374607431768211456)
    (120, 340282366920938463463374605901840568623/340282366920938463463374607431768211456)
    (121, 340282366920938463463374607331543221023/340282366920938463463374607431768211456)
    (122, 340282366920938463463374607426069016223/340282366920938463463374607431768211456)
    (123, 340282366920938463463374607431492627423/340282366920938463463374607431768211456)
    (124, 340282366920938463463374607431757193823/340282366920938463463374607431768211456)
    (125, 340282366920938463463374607431767861823/340282366920938463463374607431768211456)
    (126, 340282366920938463463374607431768203199/340282366920938463463374607431768211456)
    (127, 340282366920938463463374607431768211327/340282366920938463463374607431768211456)
    (128, 340282366920938463463374607431768211455/340282366920938463463374607431768211456)