#| Generalized urn model in Racket/Gamble This is a generalization of the 2-ball model in beta_binomial_urn_model.wppl Some features: * arbitrary number of types (colors) of balls * when a ball of a certain type is draw there are three options - remove that ball - keep the ball add some arbitrary number of balls of the same type - remove the ball and remove some arbitrary number of of the same type * The returned values are - total number of observed balls - average number of observed balls - total number of balls left - average number of balls left - the number of observed balls for each type - the number of left balls for each type - number of draws * One can add probability for an event * One can add some observation For not too complex cases enumerate works quite nicely, but it takes long time for more complex combinations (use importance-sampler instead). * First a simple example: balls:(1 1) add_balls:(1 1) num_draws:10 Start with 1 white ball and 1 black ball and draw atmost 10 balls If we draw a white ball, add another white ball. If we draw a black ball, add another black ball. variable : num_observed_total 10: 1 (1.0) mean: 10 (10.0) variable : num_observed_mean 5.0: 1 (1.0) mean: 5.0 variable : num_balls_left_total 12: 1 (1.0) mean: 12 (12.0) variable : num_balls_left_mean 6.0: 1 (1.0) mean: 6.0 variable : num_observed (1 9): 1/11 (0.09090909090909091) (10 0): 1/11 (0.09090909090909091) (3 7): 1/11 (0.09090909090909091) (5 5): 1/11 (0.09090909090909091) (7 3): 1/11 (0.09090909090909091) (2 8): 1/11 (0.09090909090909091) (0 10): 1/11 (0.09090909090909091) (6 4): 1/11 (0.09090909090909091) (4 6): 1/11 (0.09090909090909091) (8 2): 1/11 (0.09090909090909091) (9 1): 1/11 (0.09090909090909091) variable : num_balls_left (6 6): 1/11 (0.09090909090909091) (4 8): 1/11 (0.09090909090909091) (11 1): 1/11 (0.09090909090909091) (2 10): 1/11 (0.09090909090909091) (5 7): 1/11 (0.09090909090909091) (1 11): 1/11 (0.09090909090909091) (8 4): 1/11 (0.09090909090909091) (3 9): 1/11 (0.09090909090909091) (9 3): 1/11 (0.09090909090909091) (10 2): 1/11 (0.09090909090909091) (7 5): 1/11 (0.09090909090909091) variable : num_runs 10: 1 (1.0) mean: 10 (10.0) variable : num_observed, num_balls_left ((3 7) (4 8)): 1/11 (0.09090909090909091) ((7 3) (8 4)): 1/11 (0.09090909090909091) ((0 10) (1 11)): 1/11 (0.09090909090909091) ((1 9) (2 10)): 1/11 (0.09090909090909091) ((2 8) (3 9)): 1/11 (0.09090909090909091) ((8 2) (9 3)): 1/11 (0.09090909090909091) ((10 0) (11 1)): 1/11 (0.09090909090909091) ((6 4) (7 5)): 1/11 (0.09090909090909091) ((9 1) (10 2)): 1/11 (0.09090909090909091) ((4 6) (5 7)): 1/11 (0.09090909090909091) ((5 5) (6 6)): 1/11 (0.09090909090909091) This is not especially interesting since everything is uniform * Perhaps a little more interesting case: balls:(10 5) add_balls:(0 1) num_draws:10 10 white balls and 5 black balls and (max 10) draws. If we draw a white ball, this is removed (and no white is added). If we draw a black ball, this is kept and an extra black is added. variable : num_observed_total 10: 1 (1.0) mean: 10 (10.0) variable : num_observed_mean 5.0: 1 (1.0) mean: 5.0 variable : num_balls_left_total 19: 10224511211423353774902866047/48409605004758070091158388736 (0.21120831724238218) 18: 109354237071065969036747/559768986419627047256064 (0.1953560124338316) 20: 777941852391527779409069/4394481209582250371383296 (0.177027006212977) 17: 836635826473329227521/6142869535469158268928 (0.1361962551284156) 21: 14913627226992445750381/129627369701299611058176 (0.11504998721610969) 16: 522861237151/7925422620672 (0.06597266318482665) 22: 114664107650292659215/2023337426968169883648 (0.05667077874505037) 23: 22002843494763037/1095821148936640896 (0.020078863705189555) 15: 1024/59049 (0.017341529915832612) 24: 2807735411/611947174608 (0.004588198994134704) 25: 91/178296 (0.0005103872212500561) mean: 87645316915483549606914390393093230307791/4625467013569710163880746507043302539264 (18.948425458090806) variable : num_balls_left_mean 9.5: 10224511211423353774902866047/48409605004758070091158388736 (0.21120831724238218) 9.0: 109354237071065969036747/559768986419627047256064 (0.1953560124338316) 10.0: 777941852391527779409069/4394481209582250371383296 (0.177027006212977) 8.5: 836635826473329227521/6142869535469158268928 (0.1361962551284156) 10.5: 14913627226992445750381/129627369701299611058176 (0.11504998721610969) 8.0: 522861237151/7925422620672 (0.06597266318482665) 11.0: 114664107650292659215/2023337426968169883648 (0.05667077874505037) 11.5: 22002843494763037/1095821148936640896 (0.020078863705189555) 7.5: 1024/59049 (0.017341529915832612) 12.0: 2807735411/611947174608 (0.004588198994134704) 12.5: 91/178296 (0.0005103872212500561) mean: 9.474212729045401 variable : num_observed (6 4): 10224511211423353774902866047/48409605004758070091158388736 (0.21120831724238218) (7 3): 109354237071065969036747/559768986419627047256064 (0.1953560124338316) (5 5): 777941852391527779409069/4394481209582250371383296 (0.177027006212977) (8 2): 836635826473329227521/6142869535469158268928 (0.1361962551284156) (4 6): 14913627226992445750381/129627369701299611058176 (0.11504998721610969) (9 1): 522861237151/7925422620672 (0.06597266318482665) (3 7): 114664107650292659215/2023337426968169883648 (0.05667077874505037) (2 8): 22002843494763037/1095821148936640896 (0.020078863705189555) (10 0): 1024/59049 (0.017341529915832612) (1 9): 2807735411/611947174608 (0.004588198994134704) (0 10): 91/178296 (0.0005103872212500561) variable : num_balls_left (10 9): 10224511211423353774902866047/48409605004758070091158388736 (0.21120831724238218) (10 8): 109354237071065969036747/559768986419627047256064 (0.1953560124338316) (10 10): 777941852391527779409069/4394481209582250371383296 (0.177027006212977) (10 7): 836635826473329227521/6142869535469158268928 (0.1361962551284156) (10 11): 14913627226992445750381/129627369701299611058176 (0.11504998721610969) (10 6): 522861237151/7925422620672 (0.06597266318482665) (10 12): 114664107650292659215/2023337426968169883648 (0.05667077874505037) (10 13): 22002843494763037/1095821148936640896 (0.020078863705189555) (10 5): 1024/59049 (0.017341529915832612) (10 14): 2807735411/611947174608 (0.004588198994134704) (10 15): 91/178296 (0.0005103872212500561) variable : num_runs 10: 1 (1.0) mean: 10 (10.0) variable : num_observed, num_balls_left ((6 4) (10 9)): 10224511211423353774902866047/48409605004758070091158388736 (0.21120831724238218) ((7 3) (10 8)): 109354237071065969036747/559768986419627047256064 (0.1953560124338316) ((5 5) (10 10)): 777941852391527779409069/4394481209582250371383296 (0.177027006212977) ((8 2) (10 7)): 836635826473329227521/6142869535469158268928 (0.1361962551284156) ((4 6) (10 11)): 14913627226992445750381/129627369701299611058176 (0.11504998721610969) ((9 1) (10 6)): 522861237151/7925422620672 (0.06597266318482665) ((3 7) (10 12)): 114664107650292659215/2023337426968169883648 (0.05667077874505037) ((2 8) (10 13)): 22002843494763037/1095821148936640896 (0.020078863705189555) ((10 0) (10 5)): 1024/59049 (0.017341529915832612) ((1 9) (10 14)): 2807735411/611947174608 (0.004588198994134704) ((0 10) (10 15)): 91/178296 (0.0005103872212500561) * balls:(10 20) add_balls:(-1 -2) num_draws:20 We have 10 white balls and 20 black balls and we draw max 20 time. If we draw a white ball, then this is removed as well as another white ball. If we draw a black ball, then this is removed as well as 2 black balls. variable : num_observed_total 19: 961133/3459729 (0.27780586282914066) 18: 442558/2003001 (0.22094746832378018) 17: 358904354/1940907969 (0.18491569911216124) 16: 38474008/277272567 (0.13875879758418366) 15: 42012392/462120945 (0.09091211392723175) 14: 14174675/277272567 (0.051121808238605876) 13: 46528687/1940907969 (0.023972639477580507) 12: 273869/30808063 (0.00888952349909178) 11: 4547651/1940907969 (0.002343053391832408) 10: 10006/30045015 (0.0003330336163919372) mean: 16630975/969969 (17.145883012756077) variable : num_observed_mean 9.5: 961133/3459729 (0.27780586282914066) 9.0: 442558/2003001 (0.22094746832378018) 8.5: 358904354/1940907969 (0.18491569911216124) 8.0: 38474008/277272567 (0.13875879758418366) 7.5: 42012392/462120945 (0.09091211392723175) 7.0: 14174675/277272567 (0.051121808238605876) 6.5: 46528687/1940907969 (0.023972639477580507) 6.0: 273869/30808063 (0.00888952349909178) 5.5: 4547651/1940907969 (0.002343053391832408) 5.0: 10006/30045015 (0.0003330336163919372) mean: 8.572941506378038 variable : num_balls_left_total 2: 1770232/6009003 (0.29459662443170687) 1: 1922266/10379187 (0.18520390855276045) 3: 349859408/1940907969 (0.18025553688681878) 4: 43392733/277272567 (0.1564984717727232) 5: 107078/1181895 (0.0905985726312405) 6: 8316839/149300613 (0.055705323862267064) 7: 46495984/1940907969 (0.02395579014699795) 8: 2178030/215656441 (0.010099536048635803) 9: 505202/215656441 (0.0023426242112564587) 10: 10007/15477735 (0.0006465416289915804) 12: 28/365313 (7.664660168129795e-5) 14: 991/58815393 (1.6849330582556847e-5) 16: 61/19605131 (3.1114303699373393e-6) 18: 7/16310151 (4.2918057594929683e-7) 20: 1/30045015 (3.328339160423119e-8) mean: 971790/323323 (3.005632138759074) variable : num_balls_left_mean 1.0: 1770232/6009003 (0.29459662443170687) 0.5: 1922266/10379187 (0.18520390855276045) 1.5: 349859408/1940907969 (0.18025553688681878) 2.0: 43392733/277272567 (0.1564984717727232) 2.5: 107078/1181895 (0.0905985726312405) 3.0: 8316839/149300613 (0.055705323862267064) 3.5: 46495984/1940907969 (0.02395579014699795) 4.0: 2178030/215656441 (0.010099536048635803) 4.5: 505202/215656441 (0.0023426242112564587) 5.0: 10007/15477735 (0.0006465416289915804) 6.0: 28/365313 (7.664660168129795e-5) 7.0: 991/58815393 (1.6849330582556847e-5) 8.0: 61/19605131 (3.1114303699373393e-6) 9.0: 7/16310151 (4.2918057594929683e-7) 10.0: 1/30045015 (3.328339160423119e-8) mean: 1.5028160693795365 variable : num_observed (8 10): 7687315/38057019 (0.20199467015532666) (9 10): 1922266/10379187 (0.18520390855276045) (7 10): 349859408/1940907969 (0.18025553688681878) (6 10): 38137642/277272567 (0.1375456736042697) (10 9): 961133/10379187 (0.09260195427638022) (5 10): 107078/1181895 (0.0905985726312405) (4 10): 744917/14593293 (0.05104516163692458) (3 10): 46495984/1940907969 (0.02395579014699795) (10 8): 34347/1812239 (0.018952798168453498) (2 10): 1916412/215656441 (0.008886412068721842) (10 7): 1004994/215656441 (0.004660162225342483) (1 10): 505202/215656441 (0.0023426242112564587) (10 6): 37374/30808063 (0.0012131239799139596) (0 10): 1/3003 (0.000333000333000333) (10 5): 2542/8107385 (0.0003135412959912475) (10 4): 28/365313 (7.664660168129795e-5) (10 3): 991/58815393 (1.6849330582556847e-5) (10 2): 61/19605131 (3.1114303699373393e-6) (10 1): 7/16310151 (4.2918057594929683e-7) (10 0): 1/30045015 (3.328339160423119e-8) variable : num_balls_left (2 0): 7687315/38057019 (0.20199467015532666) (1 0): 1922266/10379187 (0.18520390855276045) (3 0): 349859408/1940907969 (0.18025553688681878) (4 0): 38137642/277272567 (0.1375456736042697) (0 2): 961133/10379187 (0.09260195427638022) (5 0): 107078/1181895 (0.0905985726312405) (6 0): 744917/14593293 (0.05104516163692458) (7 0): 46495984/1940907969 (0.02395579014699795) (0 4): 34347/1812239 (0.018952798168453498) (8 0): 1916412/215656441 (0.008886412068721842) (0 6): 1004994/215656441 (0.004660162225342483) (9 0): 505202/215656441 (0.0023426242112564587) (0 8): 37374/30808063 (0.0012131239799139596) (10 0): 1/3003 (0.000333000333000333) (0 10): 2542/8107385 (0.0003135412959912475) (0 12): 28/365313 (7.664660168129795e-5) (0 14): 991/58815393 (1.6849330582556847e-5) (0 16): 61/19605131 (3.1114303699373393e-6) (0 18): 7/16310151 (4.2918057594929683e-7) (0 20): 1/30045015 (3.328339160423119e-8) variable : num_runs 19: 961133/3459729 (0.27780586282914066) 18: 442558/2003001 (0.22094746832378018) 17: 358904354/1940907969 (0.18491569911216124) 16: 38474008/277272567 (0.13875879758418366) 15: 42012392/462120945 (0.09091211392723175) 14: 14174675/277272567 (0.051121808238605876) 13: 46528687/1940907969 (0.023972639477580507) 12: 273869/30808063 (0.00888952349909178) 11: 4547651/1940907969 (0.002343053391832408) 10: 10006/30045015 (0.0003330336163919372) mean: 16630975/969969 (17.145883012756077) variable : num_observed, num_balls_left ((8 10) (2 0)): 7687315/38057019 (0.20199467015532666) ((9 10) (1 0)): 1922266/10379187 (0.18520390855276045) ((7 10) (3 0)): 349859408/1940907969 (0.18025553688681878) ((6 10) (4 0)): 38137642/277272567 (0.1375456736042697) ((10 9) (0 2)): 961133/10379187 (0.09260195427638022) ((5 10) (5 0)): 107078/1181895 (0.0905985726312405) ((4 10) (6 0)): 744917/14593293 (0.05104516163692458) ((3 10) (7 0)): 46495984/1940907969 (0.02395579014699795) ((10 8) (0 4)): 34347/1812239 (0.018952798168453498) ((2 10) (8 0)): 1916412/215656441 (0.008886412068721842) ((10 7) (0 6)): 1004994/215656441 (0.004660162225342483) ((1 10) (9 0)): 505202/215656441 (0.0023426242112564587) ((10 6) (0 8)): 37374/30808063 (0.0012131239799139596) ((0 10) (10 0)): 1/3003 (0.000333000333000333) ((10 5) (0 10)): 2542/8107385 (0.0003135412959912475) ((10 4) (0 12)): 28/365313 (7.664660168129795e-5) ((10 3) (0 14)): 991/58815393 (1.6849330582556847e-5) ((10 2) (0 16)): 61/19605131 (3.1114303699373393e-6) ((10 1) (0 18)): 7/16310151 (4.2918057594929683e-7) ((10 0) (0 20)): 1/30045015 (3.328339160423119e-8) * balls:(3 2 1) add_balls:(-1 0 1) num_draws:10 Three type of balls: white, black, and red. Start with 3 white, 2 black, and 1 red ball and draw atmost 10 balls. White ball is drawn: Remove the ball and remove another white ball Black ball is drawn: Remove the ball but does not remove or add another ball Red ball is drawn: Put the ball back, and add another red ball. variable : num_observed_total 10: 2332229260093376411/4981296307680000000 (0.46819725550106733) 5: 83191/882000 (0.09432086167800453) 6: 4239511/46305000 (0.09155622502969442) 4: 349/4200 (0.0830952380952381) 7: 6393829517/77792400000 (0.08219092760989505) 8: 25455803670541/359400888000000 (0.07082843843875256) 9: 297936580691984929/4981296307680000000 (0.05981105364734799) 3: 1/20 (0.05) mean: 39180678258919781351/4981296307680000000 (7.865558649565233) variable : num_observed_mean 3.3333333333333335: 2332229260093376411/4981296307680000000 (0.46819725550106733) 1.6666666666666667: 83191/882000 (0.09432086167800453) 2.0: 4239511/46305000 (0.09155622502969442) 1.3333333333333333: 349/4200 (0.0830952380952381) 2.3333333333333335: 6393829517/77792400000 (0.08219092760989505) 2.6666666666666665: 25455803670541/359400888000000 (0.07082843843875256) 3.0: 297936580691984929/4981296307680000000 (0.05981105364734799) 1.0: 1/20 (0.05) mean: 2.621852883188411 variable : num_balls_left_total 3: 107310418303/437400000000 (0.2453370331572931) 4: 1595005519977463/10553253169921875 (0.15113875260032927) 5: 192922293872795403521/1815497249328000000000 (0.10626416203285624) 6: 6607550654218821734171/76250884471776000000000 (0.08665539685201401) 8: 36602346451552020942030611/438436485641954257920000000 (0.08348380586519673) 9: 68399077374153102502195019/826765944353399457792000000 (0.0827308839234385) 7: 29912725872642949983119/366004245464524800000000 (0.08172781120251311) 10: 75367323063231653969339207/1055602946808358236288000000 (0.07139741632126609) 11: 1069385586997365931300913/21782283029378820748800000 (0.04909428389829632) 12: 956098918452045510007/37456306796550882240000 (0.02552571249603516) 13: 50262355920026951/4632814693450944000 (0.010849204910155159) 14: 26434277473/5968891328400 (0.0044286746095084095) 15: 74587/72144072 (0.0010338617980975623) 16: 1/3003 (0.000333000333000333) mean: 3365763026544614896883988457018127/546340500157500909668808000000000 (6.160559258510619) variable : num_balls_left_mean 1.0: 107310418303/437400000000 (0.2453370331572931) 1.3333333333333333: 1595005519977463/10553253169921875 (0.15113875260032927) 1.6666666666666667: 192922293872795403521/1815497249328000000000 (0.10626416203285624) 2.0: 6607550654218821734171/76250884471776000000000 (0.08665539685201401) 2.6666666666666665: 36602346451552020942030611/438436485641954257920000000 (0.08348380586519673) 3.0: 68399077374153102502195019/826765944353399457792000000 (0.0827308839234385) 2.3333333333333335: 29912725872642949983119/366004245464524800000000 (0.08172781120251311) 3.3333333333333335: 75367323063231653969339207/1055602946808358236288000000 (0.07139741632126609) 3.6666666666666665: 1069385586997365931300913/21782283029378820748800000 (0.04909428389829632) 4.0: 956098918452045510007/37456306796550882240000 (0.02552571249603516) 4.333333333333333: 50262355920026951/4632814693450944000 (0.010849204910155159) 4.666666666666667: 26434277473/5968891328400 (0.0044286746095084095) 5.0: 74587/72144072 (0.0010338617980975623) 5.333333333333333: 1/3003 (0.000333000333000333) mean: 2.053519752836873 variable : num_observed (2 3 5): 1735225134619/26033337792000 (0.06665396302552613) (2 4 4): 5479227875671/88216581600000 (0.062111088145712055) (3 1 0): 37/600 (0.06166666666666667) (2 2 6): 697880173/12678573600 (0.055044060555834134) (3 2 0): 919/18000 (0.051055555555555555) (3 0 0): 1/20 (0.05) (2 5 3): 3619589955939529/76219126502400000 (0.04748926053128613) (3 3 0): 19153/540000 (0.03546851851851852) (3 1 1): 319/9800 (0.03255102040816327) (3 2 1): 64171/2058000 (0.031181243926141886) (2 1 7): 485333/15615600 (0.031080009733855888) (2 6 2): 2644835876174899/85384938240000000 (0.030975438182560884) (3 3 1): 10419739/432180000 (0.02410972048683419) (3 4 0): 361711/16200000 (0.02232783950617284) (3 0 1): 3/140 (0.02142857142857143) (3 2 2): 334699/16464000 (0.020329142371234208) (3 1 2): 743/39200 (0.01895408163265306) (2 7 1): 2966585468214379/168101597160000000 (0.017647574552136867) (3 3 2): 118501307/6914880000 (0.017137145836225647) (1 3 6): 1090383854767/63900010944000 (0.017063907167755868) (3 4 1): 1493652451/90757800000 (0.016457565641740984) (1 2 7): 87671530027/5803088791500 (0.015107735410737769) (1 4 5): 41959661109350161/2841473736652320000 (0.014766865717641615) (3 2 3): 3714811/266716800 (0.013927922800513503) (3 5 0): 6418657/486000000 (0.01320711316872428) (3 3 3): 4239682369/336063168000 (0.012615730531350582) (3 4 2): 36341076451/2904249600000 (0.012513069279926905) (3 1 3): 2509/211680 (0.011852796674225246) (3 0 2): 3/280 (0.010714285714285714) (1 5 4): 1548441818935891/148203857088000000 (0.010448053440447655) (3 5 1): 197497515259/19059138000000 (0.010362352969950687) (3 2 4): 4410583/444528000 (0.009921946424072275) (3 4 3): 4130738255851/423439591680000 (0.00975520082914841) (3 3 4): 1070229857/112021056000 (0.009553827603624804) (1 1 8): 420983/46246200 (0.009103083064122025) (2 0 8): 9/1001 (0.008991008991008992) (2 8 0): 29123798671/3280500000000 (0.008877853580551745) (3 5 2): 10135733515643/1219784832000000 (0.00830944380495707) (3 1 4): 2761/352800 (0.007825963718820862) (3 6 0): 109195759/14580000000 (0.007489421056241427) (3 2 5): 616565863/84523824000 (0.007294580791801375) (1 6 3): 661228869152666221/105039483711120000000 (0.006295050639921084) (3 6 1): 24696622766131/4002418980000000 (0.006170424158375093) (3 0 3): 1/168 (0.005952380952380952) (3 1 5): 32891/6098400 (0.005393381870654598) (3 7 0): 1802927233/437400000000 (0.004121918685413809) (3 1 6): 35201/9147600 (0.0038481131662949845) (3 0 4): 1/280 (0.0035714285714285713) (1 7 2): 102015172933076117/30738577766400000000 (0.003318799383248882) (1 0 9): 2/715 (0.002797202797202797) (3 0 5): 1/440 (0.0022727272727272726) (0 3 7): 8089458944857991/4632814693450944000 (0.0017461218460331342) (0 2 8): 9738077953/5968891328400 (0.001631471812305612) (1 8 1): 268723106505722/172369801775390625 (0.0015589917940259986) (3 0 6): 1/660 (0.0015151515151515152) (0 4 6): 24328154177767771/17048842419913920000 (0.0014269680942883983) (3 0 7): 3/2860 (0.001048951048951049) (0 1 9): 74587/72144072 (0.0010338617980975623) (0 5 5): 122492088186769294577/128889248694549235200000 (0.0009503669966845692) (1 9 0): 50700551/76886718750 (0.0006594188414367728) (0 6 4): 271021530780821689/504189521813376000000 (0.0005375389988392884) (0 0 10): 1/3003 (0.000333000333000333) (0 7 3): 959896531384835/3614030492358279168 (0.0002656027760182149) (0 8 2): 6089120919541/52053937132732416 (0.0001169771443803442) (0 9 1): 222009073/4765680279486 (4.65849700315911e-5) (0 10 0): 1/59049 (1.6935087808430286e-5) variable : num_balls_left (0 2 1): 107310418303/437400000000 (0.2453370331572931) (0 2 2): 569387722348621/4002418980000000 (0.14226089901977754) (0 2 3): 107288820171863/1219784832000000 (0.0879571686392826) (1 2 6): 1735225134619/26033337792000 (0.06665396302552613) (1 2 5): 5479227875671/88216581600000 (0.062111088145712055) (1 2 7): 697880173/12678573600 (0.055044060555834134) (0 2 4): 22909789128391/423439591680000 (0.054104031787618696) (1 2 4): 3619589955939529/76219126502400000 (0.04748926053128613) (1 2 8): 485333/15615600 (0.031080009733855888) (1 2 3): 2644835876174899/85384938240000000 (0.030975438182560884) (0 2 5): 3458444693/112021056000 (0.030873166317946513) (1 2 2): 2966585468214379/168101597160000000 (0.017647574552136867) (2 2 7): 1090383854767/63900010944000 (0.017063907167755868) (2 2 8): 87671530027/5803088791500 (0.015107735410737769) (0 2 6): 1264534723/84523824000 (0.014960689935183244) (2 2 6): 41959661109350161/2841473736652320000 (0.014766865717641615) (2 2 5): 1548441818935891/148203857088000000 (0.010448053440447655) (2 2 9): 420983/46246200 (0.009103083064122025) (1 2 9): 9/1001 (0.008991008991008992) (1 2 1): 29123798671/3280500000000 (0.008877853580551745) (2 2 4): 661228869152666221/105039483711120000000 (0.006295050639921084) (0 2 7): 49061/9147600 (0.0053632646814464995) (2 2 3): 102015172933076117/30738577766400000000 (0.003318799383248882) (2 2 10): 2/715 (0.002797202797202797) (3 2 8): 8089458944857991/4632814693450944000 (0.0017461218460331342) (3 2 9): 9738077953/5968891328400 (0.001631471812305612) (2 2 2): 268723106505722/172369801775390625 (0.0015589917940259986) (3 2 7): 24328154177767771/17048842419913920000 (0.0014269680942883983) (0 2 8): 3/2860 (0.001048951048951049) (3 2 10): 74587/72144072 (0.0010338617980975623) (3 2 6): 122492088186769294577/128889248694549235200000 (0.0009503669966845692) (2 2 1): 50700551/76886718750 (0.0006594188414367728) (3 2 5): 271021530780821689/504189521813376000000 (0.0005375389988392884) (3 2 11): 1/3003 (0.000333000333000333) (3 2 4): 959896531384835/3614030492358279168 (0.0002656027760182149) (3 2 3): 6089120919541/52053937132732416 (0.0001169771443803442) (3 2 2): 222009073/4765680279486 (4.65849700315911e-5) (3 2 1): 1/59049 (1.6935087808430286e-5) variable : num_runs 10: 2332229260093376411/4981296307680000000 (0.46819725550106733) 5: 83191/882000 (0.09432086167800453) 6: 4239511/46305000 (0.09155622502969442) 4: 349/4200 (0.0830952380952381) 7: 6393829517/77792400000 (0.08219092760989505) 8: 25455803670541/359400888000000 (0.07082843843875256) 9: 297936580691984929/4981296307680000000 (0.05981105364734799) 3: 1/20 (0.05) mean: 39180678258919781351/4981296307680000000 (7.865558649565233) variable : num_observed, num_balls_left ((2 3 5) (1 2 6)): 1735225134619/26033337792000 (0.06665396302552613) ((2 4 4) (1 2 5)): 5479227875671/88216581600000 (0.062111088145712055) ((3 1 0) (0 2 1)): 37/600 (0.06166666666666667) ((2 2 6) (1 2 7)): 697880173/12678573600 (0.055044060555834134) ((3 2 0) (0 2 1)): 919/18000 (0.051055555555555555) ((3 0 0) (0 2 1)): 1/20 (0.05) ((2 5 3) (1 2 4)): 3619589955939529/76219126502400000 (0.04748926053128613) ((3 3 0) (0 2 1)): 19153/540000 (0.03546851851851852) ((3 1 1) (0 2 2)): 319/9800 (0.03255102040816327) ((3 2 1) (0 2 2)): 64171/2058000 (0.031181243926141886) ((2 1 7) (1 2 8)): 485333/15615600 (0.031080009733855888) ((2 6 2) (1 2 3)): 2644835876174899/85384938240000000 (0.030975438182560884) ((3 3 1) (0 2 2)): 10419739/432180000 (0.02410972048683419) ((3 4 0) (0 2 1)): 361711/16200000 (0.02232783950617284) ((3 0 1) (0 2 2)): 3/140 (0.02142857142857143) ((3 2 2) (0 2 3)): 334699/16464000 (0.020329142371234208) ((3 1 2) (0 2 3)): 743/39200 (0.01895408163265306) ((2 7 1) (1 2 2)): 2966585468214379/168101597160000000 (0.017647574552136867) ((3 3 2) (0 2 3)): 118501307/6914880000 (0.017137145836225647) ((1 3 6) (2 2 7)): 1090383854767/63900010944000 (0.017063907167755868) ((3 4 1) (0 2 2)): 1493652451/90757800000 (0.016457565641740984) ((1 2 7) (2 2 8)): 87671530027/5803088791500 (0.015107735410737769) ((1 4 5) (2 2 6)): 41959661109350161/2841473736652320000 (0.014766865717641615) ((3 2 3) (0 2 4)): 3714811/266716800 (0.013927922800513503) ((3 5 0) (0 2 1)): 6418657/486000000 (0.01320711316872428) ((3 3 3) (0 2 4)): 4239682369/336063168000 (0.012615730531350582) ((3 4 2) (0 2 3)): 36341076451/2904249600000 (0.012513069279926905) ((3 1 3) (0 2 4)): 2509/211680 (0.011852796674225246) ((3 0 2) (0 2 3)): 3/280 (0.010714285714285714) ((1 5 4) (2 2 5)): 1548441818935891/148203857088000000 (0.010448053440447655) ((3 5 1) (0 2 2)): 197497515259/19059138000000 (0.010362352969950687) ((3 2 4) (0 2 5)): 4410583/444528000 (0.009921946424072275) ((3 4 3) (0 2 4)): 4130738255851/423439591680000 (0.00975520082914841) ((3 3 4) (0 2 5)): 1070229857/112021056000 (0.009553827603624804) ((1 1 8) (2 2 9)): 420983/46246200 (0.009103083064122025) ((2 0 8) (1 2 9)): 9/1001 (0.008991008991008992) ((2 8 0) (1 2 1)): 29123798671/3280500000000 (0.008877853580551745) ((3 5 2) (0 2 3)): 10135733515643/1219784832000000 (0.00830944380495707) ((3 1 4) (0 2 5)): 2761/352800 (0.007825963718820862) ((3 6 0) (0 2 1)): 109195759/14580000000 (0.007489421056241427) ((3 2 5) (0 2 6)): 616565863/84523824000 (0.007294580791801375) ((1 6 3) (2 2 4)): 661228869152666221/105039483711120000000 (0.006295050639921084) ((3 6 1) (0 2 2)): 24696622766131/4002418980000000 (0.006170424158375093) ((3 0 3) (0 2 4)): 1/168 (0.005952380952380952) ((3 1 5) (0 2 6)): 32891/6098400 (0.005393381870654598) ((3 7 0) (0 2 1)): 1802927233/437400000000 (0.004121918685413809) ((3 1 6) (0 2 7)): 35201/9147600 (0.0038481131662949845) ((3 0 4) (0 2 5)): 1/280 (0.0035714285714285713) ((1 7 2) (2 2 3)): 102015172933076117/30738577766400000000 (0.003318799383248882) ((1 0 9) (2 2 10)): 2/715 (0.002797202797202797) ((3 0 5) (0 2 6)): 1/440 (0.0022727272727272726) ((0 3 7) (3 2 8)): 8089458944857991/4632814693450944000 (0.0017461218460331342) ((0 2 8) (3 2 9)): 9738077953/5968891328400 (0.001631471812305612) ((1 8 1) (2 2 2)): 268723106505722/172369801775390625 (0.0015589917940259986) ((3 0 6) (0 2 7)): 1/660 (0.0015151515151515152) ((0 4 6) (3 2 7)): 24328154177767771/17048842419913920000 (0.0014269680942883983) ((3 0 7) (0 2 8)): 3/2860 (0.001048951048951049) ((0 1 9) (3 2 10)): 74587/72144072 (0.0010338617980975623) ((0 5 5) (3 2 6)): 122492088186769294577/128889248694549235200000 (0.0009503669966845692) ((1 9 0) (2 2 1)): 50700551/76886718750 (0.0006594188414367728) ((0 6 4) (3 2 5)): 271021530780821689/504189521813376000000 (0.0005375389988392884) ((0 0 10) (3 2 11)): 1/3003 (0.000333000333000333) ((0 7 3) (3 2 4)): 959896531384835/3614030492358279168 (0.0002656027760182149) ((0 8 2) (3 2 3)): 6089120919541/52053937132732416 (0.0001169771443803442) ((0 9 1) (3 2 2)): 222009073/4765680279486 (4.65849700315911e-5) ((0 10 0) (3 2 1)): 1/59049 (1.6935087808430286e-5) * Same as the previous but with atmost 100 drawn balls. For this, the importance-sampler is used with 100000 samples variable : num_observed_total 5: 0.09374000000000095 6: 0.09232000000000094 4: 0.08257000000000084 7: 0.08135000000000081 8: 0.07077000000000072 ... 87: 0.000390000000000004 95: 0.0003900000000000039 90: 0.0003800000000000039 96: 0.00035000000000000347 94: 0.0003400000000000034 mean: 16.38439000000016 variable : num_observed_mean 1.6666666666666667: 0.09374000000000095 2.0: 0.09232000000000094 1.3333333333333333: 0.08257000000000084 2.3333333333333335: 0.08135000000000081 2.6666666666666665: 0.07077000000000072 ... 29.0: 0.000390000000000004 31.666666666666668: 0.0003900000000000039 30.0: 0.0003800000000000039 32.0: 0.00035000000000000347 31.333333333333332: 0.0003400000000000034 mean: 5.461463333333389 variable : num_balls_left_total 3: 0.24941000000000255 4: 0.14971000000000154 5: 0.099980000000001 6: 0.0704400000000007 7: 0.05375000000000054 ... 77: 0.0004000000000000041 100: 0.00040000000000000403 101: 0.00016000000000000164 102: 1.0000000000000102e-5 103: 1.0000000000000102e-5 mean: 12.852800000000128 variable : num_balls_left_mean 1.0: 0.24941000000000255 1.3333333333333333: 0.14971000000000154 1.6666666666666667: 0.099980000000001 2.0: 0.0704400000000007 2.3333333333333335: 0.05375000000000054 ... 25.666666666666668: 0.0004000000000000041 33.333333333333336: 0.00040000000000000403 33.666666666666664: 0.00016000000000000164 34.0: 1.0000000000000102e-5 34.333333333333336: 1.0000000000000102e-5 mean: 4.284266666666709 variable : num_observed (3 1 0): 0.061040000000000615 (3 2 0): 0.05092000000000051 (3 0 0): 0.0491200000000005 (3 3 0): 0.03585000000000036 (3 1 1): 0.03267000000000033 ... (3 16 37): 1.0000000000000102e-5 (3 17 57): 1.0000000000000102e-5 (3 1 56): 1.0000000000000102e-5 (3 16 20): 1.0000000000000102e-5 (3 14 63): 1.0000000000000102e-5 variable : num_balls_left (0 2 1): 0.24941000000000255 (0 2 2): 0.14971000000000154 (0 2 3): 0.099980000000001 (0 2 4): 0.0704400000000007 (0 2 5): 0.05375000000000054 ... (2 2 99): 1.0000000000000102e-5 (1 2 75): 1.0000000000000102e-5 (1 2 79): 1.0000000000000102e-5 (1 2 78): 1.0000000000000102e-5 (1 2 74): 1.0000000000000102e-5 variable : num_runs 5: 0.09374000000000095 6: 0.09232000000000094 4: 0.08257000000000084 7: 0.08135000000000081 8: 0.07077000000000072 ... 87: 0.000390000000000004 95: 0.0003900000000000039 90: 0.0003800000000000039 96: 0.00035000000000000347 94: 0.0003400000000000034 mean: 16.38439000000016 variable : num_observed, num_balls_left ((3 1 0) (0 2 1)): 0.061040000000000615 ((3 2 0) (0 2 1)): 0.05092000000000051 ((3 0 0) (0 2 1)): 0.0491200000000005 ((3 3 0) (0 2 1)): 0.03585000000000036 ((3 1 1) (0 2 2)): 0.03267000000000033 ... ((3 13 54) (0 2 55)): 1.0000000000000102e-5 ((3 16 20) (0 2 21)): 1.0000000000000102e-5 ((3 0 26) (0 2 27)): 1.0000000000000102e-5 ((3 16 70) (0 2 71)): 1.0000000000000102e-5 ((3 1 56) (0 2 57)): 1.0000000000000102e-5 TODOs: - In the current urn model we cannot answer how many of the white (or black, ...) balls are drawn in average, just the combination of (white,black,...) balls that was drawn. Todo: "explode" the lists num_observed and num_balls_left into separate entities in the returned list Compare with the WebPPL model urn_model_generalized.wppl, which was an easier case since the returned entity of the model are hashes. - add the option of adding balls of another (opposite) colors when a ball is drawn? This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Racket page: http://www.hakank.org/racket/ |# #lang gamble ; (require gamble/viz) (require racket) (require "gamble_utils.rkt") ; (require "gamble_distributions.rkt") (require racket/hash) #| A general Urn model, N balls Given - array of (N) initial balls - array of (N) indicating how many balls to add if the i'th ball type is observed If negative: remove that many balls of the color of the drawn ball. * > 1: Add some more balls of the same color * 1: Add the draw ball back (draw with replacement) * 0: Do not add any ball (draw without replacement) * < 0: Draw without replacement and remove extra balls of the same color - number of draws - obs: optional function for observation/fail - prob: optional function for probability Returns - array of N observed balls - array of N left balls - number of runs (might be < N if the add number are negative) - the progression of the draws (a list) The runs are terminated (before the N number of runs) if any of the number of balls <= 0. |# (define (urn_model balls add_balls num_draws #:obs [obs #f] #:prob [prob #f]) (displayln (format "balls:~a add_balls:~a num_draws:~a~n" balls add_balls num_draws)) (define len (length balls)) (enumerate ; rejection-sampler ; importance-sampler ; mh-sampler (define (f num_balls num_observed progress i) (if (or (>= i num_draws) (memf (lambda (v) (< v 0)) num_balls)) ; Return the number of observed balls and number of balls that are left. (values num_observed num_balls i progress) ; else (let* ([ball (categorical-vw2 (list->vector num_balls) (list->vector (range len)))]) (f (for/list ((i len)) (if (= ball i) (+ (list-ref num_balls i) (list-ref add_balls i)) (list-ref num_balls i))) (for/list ((i len)) (if (= ball i) (add1 (list-ref num_observed i)) (list-ref num_observed i))) (append progress (list ball)) (add1 i) )))) (let-values (((num_observed num_balls_left num_runs progress) (f balls (rep len 0) '() 0))) (let* ([num_observed_total (sum num_observed)] [num_observed_mean (* 1.0 (avg num_observed))] [num_balls_left_total (sum num_balls_left)] [num_balls_left_mean (* 1.0 (avg num_balls_left))] ) ; Observation (when obs (let ([what (first obs)] [o (second obs)]) (cond [(eq? what "num_observed") (observe/fail (o num_observed))] [(eq? what "num_balls_left") (observe/fail (o num_balls_left))] [(eq? what "num_runs") (observe/fail (o num_runs))] [else 0])) ) ; Probability: experimental (define p (if prob (let ([what (first prob)] [o (second prob)]) (cond [(eq? what "num_observed") (o num_observed)] [(eq? what "num_balls_left") (o num_balls_left)] [(eq? what "num_runs") (o num_runs)] [else 0])) ; No observation 1) ) (list num_observed_total num_observed_mean num_balls_left_total num_balls_left_mean num_observed num_balls_left num_runs (list num_observed num_balls_left) p ; progress ) )) ) ) (define num_balls '(12 23 9)) (define add_balls '(-1 -1 -1)) (define num_draws 5) ; Experimental: Probability ; (define prob (list "num_observed" (lambda (v) (equal? v '(6 4))))) ; (define prob '(1 0 4)) ; (define prob (list "num_observed" (lambda (v) (equal? v '(1 0 4)))) ; (define prob (list "num_observed" (lambda (v) (>= (first v) 1)))) ; (define prob (list "num_observed" (lambda (v) (for/list ([b v]) (> b 0))))) ; (define prob (list "num_observed" (lambda (v) (sum (for/list ([b v]) (b2i (> b 0))))))) ; Banach's match box problem: ; A person has, in each of his two pockets, a box with n matches. ; Now and then he talkes a match from a randomly chosen box until ; he finds the selected box empty. Find the expectation of the ; number, R, of remaining matches in the other box. ; Cf gamble_banachs_match_box_problem.rkt ; (define prob (list "num_balls_left" (lambda (v) (if (= 0 (first v)) (second v) (first v) )) )) ; (define prob (list "num_balls_left" (lambda (v) (abs (- (first v) (second v)))))) (define prob (list "num_observed" (lambda (v) (equal? v (list 2 0 3))))) ; (define prob #f) ; Experimental: Observation ; (define obs (list "num_observed" (lambda (v) (> (first v) 0)))) ; (define obs (list "num_runs" (lambda (v) (<= v 8)))) (define obs #f) (show-marginals (urn_model num_balls add_balls num_draws #:prob prob #:obs obs) (list "num_observed_total" "num_observed_mean" "num_balls_left_total" "num_balls_left_mean" "num_observed" "num_balls_left" "num_runs" "num_observed, num_balls_left" "p" "progress" ) #:num-samples 10000 ; #:truncate-output 5 ; #:skip-marginals? #t ; #:show-stats? #t #:credible-interval 0.84 ; #:show-histogram? #t ; #:show-percentiles? #t )