├── .gitignore ├── LICENSE ├── README.md ├── output ├── oakland-examples │ ├── oakland-good-top-2-1000-days-strong-prior.png │ ├── oakland-good-top-2-1000-days-weak-prior.png │ ├── oakland-good-top-vs-random-1000-days-strong-prior.png │ ├── oakland-good-top-vs-random-1000-days-weak-prior.png │ ├── oakland-top-2-1000-days-strong-prior.png │ ├── oakland-top-2-1000-days-weak-prior.png │ ├── oakland-top-2-poisson-1000-days-strong-prior.png │ ├── oakland-top-2-poisson-1000-days-weak-prior.png │ ├── oakland-top-vs-random-1000-days-strong-prior.png │ ├── oakland-top-vs-random-1000-days-weak-prior.png │ ├── oakland-top-vs-random-poisson-1000-days-strong-prior.png │ └── oakland-top-vs-random-poisson-1000-days-weak-prior.png ├── paper-examples │ ├── oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-partialsurprise_wd0.5.png │ ├── oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-partialsurprise_wd0.6.png │ ├── oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-partialsurprise_wd0.7.png │ ├── oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-partialsurprise_wd0.8.png │ ├── oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-partialsurprise_wd0.9.png │ ├── oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-partialsurprise_wd1.png │ ├── oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-surprise.png │ ├── oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-weightedsurprise.png │ ├── oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-weightedsurprise_wd0.5.png │ ├── oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-weightedsurprise_wd0.6.png │ ├── oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-weightedsurprise_wd0.7.png │ ├── oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-weightedsurprise_wd0.8.png │ ├── oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-weightedsurprise_wd0.9.png │ ├── oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed.png │ ├── oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed_wd0.5.png │ ├── oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed_wd0.6.png │ ├── oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed_wd0.7.png │ ├── oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed_wd0.8.png │ ├── oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed_wd0.9.png │ ├── oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed_wd1.png │ ├── oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-with-surprise.png │ ├── oakland-top-2-1000-days-strong-prior-poisson-binomial-decay.png │ ├── oakland-top-vs-random-1000-days-strong-prior-poisson-binomial-decay-mixed-with-surprise.png │ ├── oakland-top-vs-random-1000-days-strong-prior-poisson-binomial-decay-mixed-with-weightedsurprise.png │ ├── oakland-top-vs-random-1000-days-strong-prior-poisson-binomial-decay-mixed.png │ ├── oakland-top-vs-random-1000-days-strong-prior-poisson-binomial-decay-with-surprise.png │ └── oakland-top-vs-random-1000-days-strong-prior-poisson-binomial-decay.png ├── polya-urn-decay.png └── polya-urn-nodecay.png └── src ├── Makefile ├── main.py └── polya.py /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | __pycache__ 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Source code for Polya Urn experiments in "Runaway Feedback Loops in Predictive Policing" 2 | 3 | This repository contains code for the generalized Polya Urn experiments reported in 4 | [Runaway Feedback Loops in Predictive Policing](https://arxiv.org/abs/1706.09847). 5 | 6 | You'll need: 7 | 8 | * python 3 9 | * numpy 10 | * matplotlib 11 | 12 | To regenerate the examples: 13 | 14 | $ git clone https://github.com/algofairness/runaway-feedback-loops-src.git 15 | $ cd runaway-feedback-loops-src 16 | $ make -f src/Makefile 17 | -------------------------------------------------------------------------------- /output/oakland-examples/oakland-good-top-2-1000-days-strong-prior.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/oakland-examples/oakland-good-top-2-1000-days-strong-prior.png -------------------------------------------------------------------------------- /output/oakland-examples/oakland-good-top-2-1000-days-weak-prior.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/oakland-examples/oakland-good-top-2-1000-days-weak-prior.png -------------------------------------------------------------------------------- /output/oakland-examples/oakland-good-top-vs-random-1000-days-strong-prior.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/oakland-examples/oakland-good-top-vs-random-1000-days-strong-prior.png -------------------------------------------------------------------------------- /output/oakland-examples/oakland-good-top-vs-random-1000-days-weak-prior.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/oakland-examples/oakland-good-top-vs-random-1000-days-weak-prior.png -------------------------------------------------------------------------------- /output/oakland-examples/oakland-top-2-1000-days-strong-prior.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/oakland-examples/oakland-top-2-1000-days-strong-prior.png -------------------------------------------------------------------------------- /output/oakland-examples/oakland-top-2-1000-days-weak-prior.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/oakland-examples/oakland-top-2-1000-days-weak-prior.png -------------------------------------------------------------------------------- /output/oakland-examples/oakland-top-2-poisson-1000-days-strong-prior.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/oakland-examples/oakland-top-2-poisson-1000-days-strong-prior.png -------------------------------------------------------------------------------- /output/oakland-examples/oakland-top-2-poisson-1000-days-weak-prior.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/oakland-examples/oakland-top-2-poisson-1000-days-weak-prior.png -------------------------------------------------------------------------------- /output/oakland-examples/oakland-top-vs-random-1000-days-strong-prior.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/oakland-examples/oakland-top-vs-random-1000-days-strong-prior.png -------------------------------------------------------------------------------- /output/oakland-examples/oakland-top-vs-random-1000-days-weak-prior.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/oakland-examples/oakland-top-vs-random-1000-days-weak-prior.png -------------------------------------------------------------------------------- /output/oakland-examples/oakland-top-vs-random-poisson-1000-days-strong-prior.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/oakland-examples/oakland-top-vs-random-poisson-1000-days-strong-prior.png -------------------------------------------------------------------------------- /output/oakland-examples/oakland-top-vs-random-poisson-1000-days-weak-prior.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/oakland-examples/oakland-top-vs-random-poisson-1000-days-weak-prior.png -------------------------------------------------------------------------------- /output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-partialsurprise_wd0.5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-partialsurprise_wd0.5.png -------------------------------------------------------------------------------- /output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-partialsurprise_wd0.6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-partialsurprise_wd0.6.png -------------------------------------------------------------------------------- /output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-partialsurprise_wd0.7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-partialsurprise_wd0.7.png -------------------------------------------------------------------------------- /output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-partialsurprise_wd0.8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-partialsurprise_wd0.8.png -------------------------------------------------------------------------------- /output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-partialsurprise_wd0.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-partialsurprise_wd0.9.png -------------------------------------------------------------------------------- /output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-partialsurprise_wd1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-partialsurprise_wd1.png -------------------------------------------------------------------------------- /output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-surprise.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-surprise.png -------------------------------------------------------------------------------- /output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-weightedsurprise.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-weightedsurprise.png -------------------------------------------------------------------------------- /output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-weightedsurprise_wd0.5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-weightedsurprise_wd0.5.png -------------------------------------------------------------------------------- /output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-weightedsurprise_wd0.6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-weightedsurprise_wd0.6.png -------------------------------------------------------------------------------- /output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-weightedsurprise_wd0.7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-weightedsurprise_wd0.7.png -------------------------------------------------------------------------------- /output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-weightedsurprise_wd0.8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-weightedsurprise_wd0.8.png -------------------------------------------------------------------------------- /output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-weightedsurprise_wd0.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-weightedsurprise_wd0.9.png -------------------------------------------------------------------------------- /output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed.png -------------------------------------------------------------------------------- /output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed_wd0.5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed_wd0.5.png -------------------------------------------------------------------------------- /output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed_wd0.6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed_wd0.6.png -------------------------------------------------------------------------------- /output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed_wd0.7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed_wd0.7.png -------------------------------------------------------------------------------- /output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed_wd0.8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed_wd0.8.png -------------------------------------------------------------------------------- /output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed_wd0.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed_wd0.9.png -------------------------------------------------------------------------------- /output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed_wd1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed_wd1.png -------------------------------------------------------------------------------- /output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-with-surprise.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-with-surprise.png -------------------------------------------------------------------------------- /output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay.png -------------------------------------------------------------------------------- /output/paper-examples/oakland-top-vs-random-1000-days-strong-prior-poisson-binomial-decay-mixed-with-surprise.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/paper-examples/oakland-top-vs-random-1000-days-strong-prior-poisson-binomial-decay-mixed-with-surprise.png -------------------------------------------------------------------------------- /output/paper-examples/oakland-top-vs-random-1000-days-strong-prior-poisson-binomial-decay-mixed-with-weightedsurprise.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/paper-examples/oakland-top-vs-random-1000-days-strong-prior-poisson-binomial-decay-mixed-with-weightedsurprise.png -------------------------------------------------------------------------------- /output/paper-examples/oakland-top-vs-random-1000-days-strong-prior-poisson-binomial-decay-mixed.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/paper-examples/oakland-top-vs-random-1000-days-strong-prior-poisson-binomial-decay-mixed.png -------------------------------------------------------------------------------- /output/paper-examples/oakland-top-vs-random-1000-days-strong-prior-poisson-binomial-decay-with-surprise.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/paper-examples/oakland-top-vs-random-1000-days-strong-prior-poisson-binomial-decay-with-surprise.png -------------------------------------------------------------------------------- /output/paper-examples/oakland-top-vs-random-1000-days-strong-prior-poisson-binomial-decay.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/paper-examples/oakland-top-vs-random-1000-days-strong-prior-poisson-binomial-decay.png -------------------------------------------------------------------------------- /output/polya-urn-decay.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/polya-urn-decay.png -------------------------------------------------------------------------------- /output/polya-urn-nodecay.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algofairness/runaway-feedback-loops-src/4110c52e4404ab95d641cfc9d9a7b7b8ff0f52e6/output/polya-urn-nodecay.png -------------------------------------------------------------------------------- /src/Makefile: -------------------------------------------------------------------------------- 1 | all: paper-examples 2 | 3 | distribution: 4 | src/main.py --a 1 --b 0 --c 0 --d 1 --output output/distribution/decay.png --command histogram --nr 1 --nb 1 --exponential_decay 0.01 5 | src/main.py --a 1 --b 0 --c 0 --d 1 --output output/distribution/nodecay.png --command histogram --nr 1 --nb 1 6 | src/main.py --a 2 --b 0 --c 0 --d 1 --output output/distribution/decay-diffreplace.png --command histogram --nr 1 --nb 1 --exponential_decay 0.01 7 | src/main.py --a 2 --b 0 --c 0 --d 1 --output output/distribution/nodecay-diffreplace.png --command histogram --nr 1 --nb 1 8 | src/main.py --a 1 --b 0 --c 0 --d 1 --output output/distribution/decay-redstart.png --command histogram --nr 5 --nb 1 --exponential_decay 0.01 9 | src/main.py --a 1 --b 0 --c 0 --d 1 --output output/distribution/nodecay-redstart.png --command histogram --nr 5 --nb 1 10 | src/main.py --a 2 --b 0 --c 0 --d 1 --output output/distribution/decay-diffreplace-redstart.png --command histogram --nr 5 --nb 1 --exponential_decay 0.01 11 | src/main.py --a 2 --b 0 --c 0 --d 1 --output output/distribution/nodecay-diffreplace-redstart.png --command histogram --nr 5 --nb 1 12 | src/main.py --a 1 --b 0 --c 0 --d 1 --output output/distribution/decay-blackstart.png --command histogram --nr 1 --nb 5 --exponential_decay 0.01 13 | src/main.py --a 1 --b 0 --c 0 --d 1 --output output/distribution/nodecay-blackstart.png --command histogram --nr 1 --nb 5 14 | src/main.py --a 2 --b 0 --c 0 --d 1 --output output/distribution/decay-diffreplace-blackstart.png --command histogram --nr 1 --nb 5 --exponential_decay 0.01 15 | src/main.py --a 2 --b 0 --c 0 --d 1 --output output/distribution/nodecay-diffreplace-blackstart.png --command histogram --nr 1 --nb 5 16 | 17 | probplot: 18 | src/main.py --a 1 --b 0 --c 0 --d 1 --output output/probplot/decay.png --command probplot --nr 1 --nb 1 --exponential_decay 0.01 19 | src/main.py --a 1 --b 0 --c 0 --d 1 --output output/probplot/nodecay.png --command probplot --nr 1 --nb 1 20 | src/main.py --a 2 --b 0 --c 0 --d 1 --output output/probplot/decay-diffreplace.png --command probplot --nr 1 --nb 1 --exponential_decay 0.01 21 | src/main.py --a 2 --b 0 --c 0 --d 1 --output output/probplot/nodecay-diffreplace.png --command probplot --nr 1 --nb 1 22 | src/main.py --a 1 --b 0 --c 0 --d 1 --output output/probplot/decay-redstart.png --command probplot --nr 5 --nb 1 --exponential_decay 0.01 23 | src/main.py --a 1 --b 0 --c 0 --d 1 --output output/probplot/nodecay-redstart.png --command probplot --nr 5 --nb 1 24 | src/main.py --a 2 --b 0 --c 0 --d 1 --output output/probplot/decay-diffreplace-redstart.png --command probplot --nr 5 --nb 1 --exponential_decay 0.01 25 | src/main.py --a 2 --b 0 --c 0 --d 1 --output output/probplot/nodecay-diffreplace-redstart.png --command probplot --nr 5 --nb 1 26 | src/main.py --a 1 --b 0 --c 0 --d 1 --output output/probplot/decay-blackstart.png --command probplot --nr 1 --nb 5 --exponential_decay 0.01 27 | src/main.py --a 1 --b 0 --c 0 --d 1 --output output/probplot/nodecay-blackstart.png --command probplot --nr 1 --nb 5 28 | src/main.py --a 2 --b 0 --c 0 --d 1 --output output/probplot/decay-diffreplace-blackstart.png --command probplot --nr 1 --nb 5 --exponential_decay 0.01 29 | src/main.py --a 2 --b 0 --c 0 --d 1 --output output/probplot/nodecay-diffreplace-blackstart.png --command probplot --nr 1 --nb 5 30 | 31 | good: 32 | src/main.py --a 0.1 --b 0 --c 0 --d 0.1 --output output/good/decay.png --command probplot --nr 1 --nb 1 --linear_surprise --exponential_decay 0.01 33 | src/main.py --a 0.1 --b 0 --c 0 --d 0.1 --output output/good/nodecay.png --command probplot --nr 1 --nb 1 --linear_surprise 34 | src/main.py --a 0.2 --b 0 --c 0 --d 0.1 --output output/good/decay-diffreplace.png --command probplot --nr 1 --nb 1 --linear_surprise --exponential_decay 0.01 35 | src/main.py --a 0.2 --b 0 --c 0 --d 0.1 --output output/good/nodecay-diffreplace.png --command probplot --nr 1 --nb 1 --linear_surprise 36 | src/main.py --a 0.1 --b 0 --c 0 --d 0.1 --output output/good/decay-redstart.png --command probplot --nr 5 --nb 1 --linear_surprise --exponential_decay 0.01 37 | src/main.py --a 0.1 --b 0 --c 0 --d 0.1 --output output/good/nodecay-redstart.png --command probplot --nr 5 --nb 1 --linear_surprise 38 | src/main.py --a 0.2 --b 0 --c 0 --d 0.1 --output output/good/decay-diffreplace-redstart.png --command probplot --nr 5 --nb 1 --linear_surprise --exponential_decay 0.01 39 | src/main.py --a 0.2 --b 0 --c 0 --d 0.1 --output output/good/nodecay-diffreplace-redstart.png --command probplot --nr 5 --nb 1 --linear_surprise 40 | src/main.py --a 0.1 --b 0 --c 0 --d 0.1 --output output/good/decay-blackstart.png --command probplot --nr 1 --nb 5 --linear_surprise --exponential_decay 0.01 41 | src/main.py --a 0.1 --b 0 --c 0 --d 0.1 --output output/good/nodecay-blackstart.png --command probplot --nr 1 --nb 5 --linear_surprise 42 | src/main.py --a 0.2 --b 0 --c 0 --d 0.1 --output output/good/decay-diffreplace-blackstart.png --command probplot --nr 1 --nb 5 --linear_surprise --exponential_decay 0.01 43 | src/main.py --a 0.2 --b 0 --c 0 --d 0.1 --output output/good/nodecay-diffreplace-blackstart.png --command probplot --nr 1 --nb 5 --linear_surprise 44 | 45 | simple: 46 | src/main.py --a 1 --b 0 --c 0 --d 1 --output output/polya-urn-decay.png --command singlerun --nr 1 --nb 1 --exponential_decay 0.01 47 | src/main.py --a 1 --b 0 --c 0 --d 1 --output output/polya-urn-nodecay.png --command singlerun --nr 1 --nb 1 48 | 49 | # oakland-examples-new: 50 | # src/main.py --a 3.68511 --b 0 --c 0 --d 2.816466 --command probplot --nr 609 --nb 379 --ndraws 1000 --output output/paper-examples/oakland-top-2-1000-days-strong-prior.png 51 | # src/main.py --a 3.68511 --b 0 --c 0 --d 2.816466 --command probplot --nr 6.09 --nb 3.79 --ndraws 1000 --output output/paper-examples/oakland-top-2-1000-days-weak-prior.png 52 | # src/main.py --a 0.368511 --b 0 --c 0 --d 0.2816466 --command probplot --nr 609 --nb 379 --ndraws 1000 --linear_surprise --output output/paper-examples/oakland-good-top-2-1000-days-strong-prior.png 53 | # src/main.py --a 0.368511 --b 0 --c 0 --d 0.2816466 --command probplot --nr 6.09 --nb 3.79 --ndraws 1000 --linear_surprise --output output/paper-examples/oakland-good-top-2-1000-days-weak-prior.png 54 | # src/main.py --a 3.68511 --b 0 --c 0 --d 2.816466 --command probplot --nr 609 --nb 379 --ndraws 1000 --nruns 1000 --poisson --output output/paper-examples/oakland-top-2-poisson-1000-days-strong-prior.png 55 | # src/main.py --a 3.68511 --b 0 --c 0 --d 2.816466 --command probplot --nr 6 --nb 3 --ndraws 1000 --nruns 1000 --poisson --output output/paper-examples/oakland-top-2-poisson-1000-days-weak-prior.png 56 | 57 | oakland-examples: 58 | src/main.py --a 3.68511 --b 0 --c 0 --d 2.816466 --command probplot --nr 609 --nb 379 --ndraws 1000 --output output/paper-examples/oakland-top-2-1000-days-strong-prior.png 59 | src/main.py --a 3.68511 --b 0 --c 0 --d 2.816466 --command probplot --nr 6.09 --nb 3.79 --ndraws 1000 --output output/paper-examples/oakland-top-2-1000-days-weak-prior.png 60 | src/main.py --a 0.368511 --b 0 --c 0 --d 0.2816466 --command probplot --nr 609 --nb 379 --ndraws 1000 --linear_surprise --output output/paper-examples/oakland-good-top-2-1000-days-strong-prior.png 61 | src/main.py --a 0.368511 --b 0 --c 0 --d 0.2816466 --command probplot --nr 6.09 --nb 3.79 --ndraws 1000 --linear_surprise --output output/paper-examples/oakland-good-top-2-1000-days-weak-prior.png 62 | src/main.py --a 3.68511 --b 0 --c 0 --d 2.816466 --command probplot --nr 609 --nb 379 --ndraws 1000 --nruns 1000 --poisson --output output/paper-examples/oakland-top-2-poisson-1000-days-strong-prior.png 63 | src/main.py --a 3.68511 --b 0 --c 0 --d 2.816466 --command probplot --nr 6 --nb 3 --ndraws 1000 --nruns 1000 --poisson --output output/paper-examples/oakland-top-2-poisson-1000-days-weak-prior.png 64 | src/main.py --a 3.68511 --b 0 --c 0 --d 2.35592 --command probplot --nr 609 --nb 7 --ndraws 1000 --output output/paper-examples/oakland-top-vs-random-1000-days-strong-prior.png 65 | src/main.py --a 3.68511 --b 0 --c 0 --d 2.35592 --command probplot --nr 6.09 --nb 0.07 --ndraws 1000 --output output/paper-examples/oakland-top-vs-random-1000-days-weak-prior.png 66 | src/main.py --a 0.368511 --b 0 --c 0 --d 0.235592 --command probplot --nr 609 --nb 7 --ndraws 1000 --linear_surprise --output output/paper-examples/oakland-good-top-vs-random-1000-days-strong-prior.png 67 | src/main.py --a 0.368511 --b 0 --c 0 --d 0.235592 --command probplot --nr 6.09 --nb 1 --ndraws 1000 --linear_surprise --output output/paper-examples/oakland-good-top-vs-random-1000-days-weak-prior.png 68 | src/main.py --a 3.68511 --b 0 --c 0 --d 2.35592 --command probplot --nr 609 --nb 7 --ndraws 1000 --poisson --output output/paper-examples/oakland-top-vs-random-poisson-1000-days-strong-prior.png 69 | src/main.py --a 3.68511 --b 0 --c 0 --d 2.35592 --command probplot --nr 6 --nb 1 --ndraws 1000 --poisson --output output/paper-examples/oakland-top-vs-random-poisson-1000-days-weak-prior.png 70 | src/main.py --a 3.68511 --b 0 --c 0 --d 2.816466 --nr 609 --nb 379 --ndraws 1000 --command probplot --nruns 1000 --output output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-nodecay.png 71 | src/main.py --a 3.68511 --b 0 --c 0 --d 2.816466 --nr 6.09 --nb 3.79 --ndraws 1000 --command probplot --nruns 1000 --output output/paper-examples/oakland-top-2-1000-days-weak-prior-poisson-binomial-nodecay.png 72 | src/main.py --a 3.68511 --b 0 --c 0 --d 2.816466 --poisson --linear_surprise --nr 609 --nb 379 --ndraws 1000 --command probplot --nruns 1000 --output output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-nodecay-with-surprise.png 73 | src/main.py --a 3.68511 --b 0 --c 0 --d 2.816466 --poisson --linear_surprise --nr 6.09 --nb 3.79 --ndraws 1000 --command probplot --nruns 1000 --output output/paper-examples/oakland-top-2-1000-days-weak-prior-poisson-binomial-nodecay-with-surprise.png 74 | src/main.py --a 3.68511 --b 0 --c 0 --d 2.35592 --command probplot --nr 609 --nb 7 --ndraws 1000 --nruns 1000 --poisson --output output/paper-examples/oakland-top-vs-random-1000-days-strong-prior-poisson-binomial-nodecay.png 75 | src/main.py --a 3.68511 --b 0 --c 0 --d 2.35592 --command probplot --nr 6 --nb 1 --ndraws 1000 --nruns 1000 --poisson --output output/paper-examples/oakland-top-vs-random-1000-days-weak-prior-poisson-binomial-nodecay.png 76 | src/main.py --a 3.68511 --b 0 --c 0 --d 2.35592 --command probplot --nr 609 --nb 7 --ndraws 1000 --nruns 1000 --poisson --linear_surprise --output output/paper-examples/oakland-top-vs-random-1000-days-strong-prior-poisson-binomial-nodecay-with-surprise.png 77 | src/main.py --a 3.68511 --b 0 --c 0 --d 2.35592 --command probplot --nr 6 --nb 1 --ndraws 1000 --nruns 1000 --poisson --linear_surprise --output output/paper-examples/oakland-top-vs-random-1000-days-weak-prior-poisson-binomial-nodecay-with-surprise.png 78 | src/main.py --a 3.68511 --b 0 --c 0 --d 2.816466 --poisson --nr 6.09 --nb 3.79 --ndraws 1000 --command probplot --nruns 1000 --exponential_decay 0.01 --output output/paper-examples/oakland-top-2-1000-days-weak-prior-poisson-binomial-decay.png 79 | src/main.py --a 3.68511 --b 0 --c 0 --d 2.816466 --poisson --linear_surprise --nr 6.09 --nb 3.79 --ndraws 1000 --command probplot --nruns 1000 --exponential_decay 0.01 --output output/paper-examples/oakland-top-2-1000-days-weak-prior-poisson-binomial-decay-with-surprise.png 80 | src/main.py --a 3.68511 --b 0 --c 0 --d 2.35592 --command probplot --nr 6 --nb 1 --ndraws 1000 --nruns 1000 --poisson --exponential_decay 0.01 --output output/paper-examples/oakland-top-vs-random-1000-days-weak-prior-poisson-binomial-decay.png 81 | src/main.py --a 3.68511 --b 0 --c 0 --d 2.35592 --command probplot --nr 6 --nb 1 --ndraws 1000 --nruns 1000 --poisson --linear_surprise --exponential_decay 0.01 --output output/paper-examples/oakland-top-vs-random-1000-days-weak-prior-poisson-binomial-decay-with-surprise.png 82 | 83 | paper-examples: 84 | src/main.py --lambdaa 3.68511 --lambdab 2.816466 --a 3.68511 --b 0 --c 0 --d 2.816466 --poisson --nr 609 --nb 379 --ndraws 1000 --command probplot --nruns 1000 --exponential_decay 0.01 --output output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay.png 85 | src/main.py --lambdaa 3.68511 --lambdab 2.816466 --a 3.68511 --b 0 --c 0 --d 2.816466 --poisson --linear_surprise --nr 609 --nb 379 --ndraws 1000 --command probplot --nruns 1000 --exponential_decay 0.01 --output output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-with-surprise.png 86 | src/main.py --lambdaa 3.68511 --lambdab 2.35592 --a 3.68511 --b 0 --c 0 --d 2.35592 --command probplot --nr 609 --nb 7 --ndraws 1000 --nruns 1000 --poisson --exponential_decay 0.01 --output output/paper-examples/oakland-top-vs-random-1000-days-strong-prior-poisson-binomial-decay.png 87 | src/main.py --lambdaa 3.68511 --lambdab 2.35592 --a 3.68511 --b 0 --c 0 --d 2.35592 --command probplot --nr 609 --nb 7 --ndraws 1000 --nruns 1000 --poisson --linear_surprise --exponential_decay 0.01 --output output/paper-examples/oakland-top-vs-random-1000-days-strong-prior-poisson-binomial-decay-with-surprise.png 88 | src/main.py --lambdaa 3.68511 --lambdab 2.816466 --a 7.37022 --b 2.816466 --c 3.68511 --d 5.632932 --poisson --nr 609 --nb 379 --ndraws 1000 --command probplot --nruns 1000 --exponential_decay 0.01 --output output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed.png 89 | src/main.py --lambdaa 3.68511 --lambdab 2.816466 --a 7.37022 --b 2.816466 --c 3.68511 --d 5.632932 --poisson --linear_surprise --nr 609 --nb 379 --ndraws 1000 --command probplot --nruns 1000 --exponential_decay 0.01 --output output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-surprise.png 90 | src/main.py --lambdaa 3.68511 --lambdab 2.35592 --a 7.37022 --b 2.35592 --c 3.68511 --d 4.71184 --command probplot --nr 609 --nb 7 --ndraws 1000 --nruns 1000 --poisson --exponential_decay 0.01 --output output/paper-examples/oakland-top-vs-random-1000-days-strong-prior-poisson-binomial-decay-mixed.png 91 | src/main.py --lambdaa 3.68511 --lambdab 2.35592 --a 7.37022 --b 2.35592 --c 3.68511 --d 4.71184 --command probplot --nr 609 --nb 7 --ndraws 1000 --nruns 1000 --poisson --linear_surprise --exponential_decay 0.01 --output output/paper-examples/oakland-top-vs-random-1000-days-strong-prior-poisson-binomial-decay-mixed-with-surprise.png 92 | src/main.py --lambdaa 3.68511 --lambdab 2.816466 --a 7.37022 --b 2.816466 --c 3.68511 --d 5.632932 --poisson --weighted_surprise 3.68511 2.816466 0.5 0.5 --nr 609 --nb 379 --ndraws 1000 --command probplot --nruns 1000 --exponential_decay 0.01 --output output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-weightedsurprise.png 93 | ### varying wd with NO fix 94 | src/main.py --lambdaa 3.68511 --lambdab 2.816466 --a 3.68511 --b 0 --c 0 --d 2.816466 --poisson --nr 609 --nb 379 --ndraws 1000 --command probplot --nruns 1000 --exponential_decay 0.01 --output output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed_wd1.png 95 | src/main.py --lambdaa 3.68511 --lambdab 2.816466 --a 3.68511 --b 0.2816466 --c 0.368511 --d 2.816466 --poisson --nr 609 --nb 379 --ndraws 1000 --command probplot --nruns 1000 --exponential_decay 0.01 --output output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed_wd0.9.png 96 | src/main.py --lambdaa 3.68511 --lambdab 2.816466 --a 3.68511 --b 0.5632932 --c 0.737022 --d 2.816466 --poisson --nr 609 --nb 379 --ndraws 1000 --command probplot --nruns 1000 --exponential_decay 0.01 --output output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed_wd0.8.png 97 | src/main.py --lambdaa 3.68511 --lambdab 2.816466 --a 3.68511 --b 0.8449398 --c 1.10553 --d 2.816466 --poisson --nr 609 --nb 379 --ndraws 1000 --command probplot --nruns 1000 --exponential_decay 0.01 --output output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed_wd0.7.png 98 | src/main.py --lambdaa 3.68511 --lambdab 2.816466 --a 3.68511 --b 1.1265864 --c 1.474044 --d 2.816466 --poisson --nr 609 --nb 379 --ndraws 1000 --command probplot --nruns 1000 --exponential_decay 0.01 --output output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed_wd0.6.png 99 | src/main.py --lambdaa 3.68511 --lambdab 2.816466 --a 3.68511 --b 1.408233 --c 1.842555 --d 2.816466 --poisson --nr 609 --nb 379 --ndraws 1000 --command probplot --nruns 1000 --exponential_decay 0.01 --output output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed_wd0.5.png 100 | ### varying wd with the fix 101 | src/main.py --lambdaa 3.68511 --lambdab 2.816466 --a 3.68511 --b 0 --c 0 --d 2.816466 --poisson --weighted_surprise 3.68511 2.816466 1 0 --nr 609 --nb 379 --ndraws 1000 --command probplot --nruns 1000 --exponential_decay 0.01 --output output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-weightedsurprise_wd1.png 102 | src/main.py --lambdaa 3.68511 --lambdab 2.816466 --a 3.68511 --b 0.2816466 --c 0.368511 --d 2.816466 --poisson --weighted_surprise 0.368511 0.2816466 0.9 0.1 --nr 609 --nb 379 --ndraws 1000 --command probplot --nruns 1000 --exponential_decay 0.01 --output output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-weightedsurprise_wd0.9.png 103 | src/main.py --lambdaa 3.68511 --lambdab 2.816466 --a 3.68511 --b 0.5632932 --c 0.737022 --d 2.816466 --poisson --weighted_surprise 0.737022 0.5632932 0.8 0.2 --nr 609 --nb 379 --ndraws 1000 --command probplot --nruns 1000 --exponential_decay 0.01 --output output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-weightedsurprise_wd0.8.png 104 | src/main.py --lambdaa 3.68511 --lambdab 2.816466 --a 3.68511 --b 0.8449398 --c 1.10553 --d 2.816466 --poisson --weighted_surprise 1.10553 0.8449398 0.7 0.3 --nr 609 --nb 379 --ndraws 1000 --command probplot --nruns 1000 --exponential_decay 0.01 --output output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-weightedsurprise_wd0.7.png 105 | src/main.py --lambdaa 3.68511 --lambdab 2.816466 --a 3.68511 --b 1.1265864 --c 1.474044 --d 2.816466 --poisson --weighted_surprise 1.474044 1.1265864 0.6 0.4 --nr 609 --nb 379 --ndraws 1000 --command probplot --nruns 1000 --exponential_decay 0.01 --output output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-weightedsurprise_wd0.6.png 106 | src/main.py --lambdaa 3.68511 --lambdab 2.816466 --a 3.68511 --b 1.408233 --c 1.842555 --d 2.816466 --poisson --weighted_surprise 1.842555 1.408233 0.5 0.5 --nr 609 --nb 379 --ndraws 1000 --command probplot --nruns 1000 --exponential_decay 0.01 --output output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-weightedsurprise_wd0.5.png 107 | #### varying wd with partial fix 108 | src/main.py --lambdaa 3.68511 --lambdab 2.816466 --a 3.68511 --b 0 --c 0 --d 2.816466 --poisson --partial_surprise 3.68511 2.816466 --nr 609 --nb 379 --ndraws 1000 --command probplot --nruns 1000 --exponential_decay 0.01 --output output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-partialsurprise_wd1.png 109 | src/main.py --lambdaa 3.68511 --lambdab 2.816466 --a 3.68511 --b 0.2816466 --c 0.368511 --d 2.816466 --poisson --partial_surprise 0.368511 0.2816466 --nr 609 --nb 379 --ndraws 1000 --command probplot --nruns 1000 --exponential_decay 0.01 --output output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-partialsurprise_wd0.9.png 110 | src/main.py --lambdaa 3.68511 --lambdab 2.816466 --a 3.68511 --b 0.5632932 --c 0.737022 --d 2.816466 --poisson --partial_surprise 0.737022 0.5632932 --nr 609 --nb 379 --ndraws 1000 --command probplot --nruns 1000 --exponential_decay 0.01 --output output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-partialsurprise_wd0.8.png 111 | src/main.py --lambdaa 3.68511 --lambdab 2.816466 --a 3.68511 --b 0.8449398 --c 1.10553 --d 2.816466 --poisson --partial_surprise 1.10553 0.8449398 --nr 609 --nb 379 --ndraws 1000 --command probplot --nruns 1000 --exponential_decay 0.01 --output output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-partialsurprise_wd0.7.png 112 | src/main.py --lambdaa 3.68511 --lambdab 2.816466 --a 3.68511 --b 1.1265864 --c 1.474044 --d 2.816466 --poisson --partial_surprise 1.474044 1.1265864 --nr 609 --nb 379 --ndraws 1000 --command probplot --nruns 1000 --exponential_decay 0.01 --output output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-partialsurprise_wd0.6.png 113 | src/main.py --lambdaa 3.68511 --lambdab 2.816466 --a 3.68511 --b 1.408233 --c 1.842555 --d 2.816466 --poisson --partial_surprise 1.842555 1.408233 --nr 609 --nb 379 --ndraws 1000 --command probplot --nruns 1000 --exponential_decay 0.01 --output output/paper-examples/oakland-top-2-1000-days-strong-prior-poisson-binomial-decay-mixed-with-partialsurprise_wd0.5.png 114 | ## src/main.py --lambdaa 3.68511 --lambdab 2.35592 --a 7.37022 --b 2.35592 --c 3.68511 --d 4.71184 --command probplot --nr 609 --nb 7 --ndraws 1000 --nruns 1000 --poisson --weighted_surprise 3.68511 2.35592 0.5 0.5 --exponential_decay 0.01 --output output/paper-examples/oakland-top-vs-random-1000-days-strong-prior-poisson-binomial-decay-mixed-with-weightedsurprise.png 115 | 116 | clean: 117 | rm output/paper-examples/* 118 | rm output/good/* 119 | rm output/probplot/* 120 | rm output/distribution/* 121 | 122 | 123 | -------------------------------------------------------------------------------- /src/main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | import math 3 | import pylab 4 | import sys 5 | import click 6 | from polya import * 7 | 8 | from mpl_toolkits.mplot3d import Axes3D 9 | import matplotlib.pyplot as plt 10 | from matplotlib import cm 11 | from matplotlib.ticker import LinearLocator, FormatStrFormatter 12 | import numpy as np 13 | 14 | def histogram(urn, ndraws, nrunsm, lambdaa, lambdab): 15 | x = [] 16 | for i in range(nruns): 17 | urn.reset() 18 | for _ in urn.draw(ndraws): pass 19 | x.append(urn.state[0] / (urn.state[0] + urn.state[1])) 20 | if i % 100 == 0: 21 | print(".", file=sys.stderr, end='') 22 | sys.stderr.flush() 23 | print("", file=sys.stderr) 24 | pylab.hist(x) 25 | 26 | def singlerun(urn, ndraws, nrunsm, lambdaa, lambdab): 27 | x = [] 28 | y = [] 29 | for state in urn.draw(ndraws): 30 | x.append(state[0]) 31 | y.append(state[1]) 32 | pylab.plot(x, 'r') 33 | pylab.plot(y, 'k') 34 | pylab.plot(list(a + b for a,b in zip(x,y)), 'b--') 35 | 36 | def singleprob(urn, ndraws, nrunsm, lambdaa, lambdab): 37 | x = [] 38 | y = [] 39 | s = 0 40 | c = 0 41 | for state in urn.draw(ndraws): 42 | x.append(state[0]) 43 | y.append(state[1]) 44 | s += (x[-1] / (x[-1]+y[-1])) 45 | c += 1 46 | pylab.plot(list((a / (a + b)) for (a,b) in zip(x, y)), 'r') 47 | print(s/c) 48 | print(urn.state) 49 | 50 | def probplot(urn, ndraws, nruns, lambdaa, lambdab): 51 | burn_in = 0 52 | histo_resolution = 100 53 | counts = numpy.zeros((histo_resolution+1, ndraws-burn_in)) 54 | for i in range(nruns): 55 | urn.reset() 56 | for _ in urn.draw(burn_in): pass 57 | for j, state in enumerate(urn.draw(ndraws-burn_in)): 58 | u = state[0] / (state[0] + state[1]) 59 | counts[100-int(u*histo_resolution), j] += 1 60 | if i % 100 == 0: 61 | print(".", file=sys.stderr, end='') 62 | sys.stderr.flush() 63 | v = (counts * numpy.linspace(1, 0, 101)[:,numpy.newaxis]).sum(axis=0) / counts.sum(axis=0) 64 | counts_max = counts.max(axis=0) 65 | counts = counts / counts_max 66 | pylab.imshow(counts, extent=[0, counts.shape[1], 67 | 0, 1.01], 68 | aspect=ndraws, cmap=pylab.cm.gray_r) 69 | ((a, b), (c, d)) = urn.update_matrix 70 | result = numpy.roots([c+d-a-b, a - 2 * c - d, c]) 71 | for r in result: 72 | if r >= 0 and r <= 1: 73 | pylab.plot([0, counts.shape[1]], [r, r], 'r--') 74 | # pylab.plot([0, counts.shape[1]], [(a + b) / (a + b + c + d), 75 | # (a + b) / (a + b + c + d)], 'g--') 76 | pylab.plot([0, counts.shape[1]], [ lambdaa / (lambdab + lambdaa), lambdaa / (lambdab + lambdaa)], 'g--') 77 | pylab.plot(numpy.arange(counts.shape[1]), v, 'y-') 78 | pylab.xlim([0, counts.shape[1]]) 79 | pylab.ylim([1, 0]) 80 | print(result) 81 | pylab.colorbar() 82 | 83 | commands = { 84 | "singlerun": singlerun, 85 | "histogram": histogram, 86 | "probplot": probplot, 87 | "singleprob": singleprob 88 | } 89 | 90 | urn_class = PolyaUrn 91 | 92 | def exponential_decay_option(click, param, exponential_decay): 93 | global urn_class 94 | if exponential_decay == 0.0: 95 | return 96 | print("Setting exponential decay to %f" % exponential_decay) 97 | urn_class = add_exponential_decay(urn_class, exponential_decay) 98 | 99 | def linear_surprise_option(click, param, linear_surprise): 100 | global urn_class 101 | if not linear_surprise: 102 | return 103 | print("Setting urn to behave with linear surprise") 104 | urn_class = add_linear_surprise(urn_class) 105 | 106 | def partial_surprise_option(click, param, partial_surprise): 107 | global urn_class 108 | if partial_surprise == (None, None): 109 | return 110 | print("Setting urn to behave with partial surprise") 111 | urn_class = add_partial_surprise(urn_class, partial_surprise) 112 | 113 | def weighted_surprise_option(click, param, weighted_surprise): 114 | global urn_class 115 | if weighted_surprise == (None, None, None, None): 116 | return 117 | print("Setting urn to behave with weighted surprise") 118 | urn_class = add_weighted_surprise(urn_class, weighted_surprise) 119 | 120 | def sqrt_surprise_option(click, param, sqrt_surprise): 121 | global urn_class 122 | if not sqrt_surprise: 123 | return 124 | print("Setting urn to behave with sqrt surprise") 125 | urn_class = add_sqrt_surprise(urn_class) 126 | 127 | def poisson_option(click, param, poisson): 128 | global urn_class 129 | if not poisson: 130 | return 131 | print("Setting urn to be Poisson") 132 | urn_class = add_poisson_update(urn_class) 133 | 134 | def truncation_option(click, param, truncation): 135 | global urn_class 136 | if truncation is None: 137 | return 138 | print("Truncating urn update to %d" % truncation) 139 | urn_class = add_truncation(urn_class, truncation) 140 | 141 | urn_params = [[1.0,0.0],[0.0,1.0]] 142 | 143 | def mixed_option(click, param, mixed): 144 | if mixed == (None, None, None, None): 145 | return 146 | (d_a, d_b, r_a, r_b) = mixed 147 | urn_params[0][0] = d_a + r_a 148 | urn_params[0][1] = r_b 149 | urn_params[1][0] = r_a 150 | urn_params[1][1] = d_b + r_b 151 | 152 | def set_a(click, param, a): 153 | if a is None: return 154 | urn_params[0][0] = a 155 | def set_b(click, param, b): 156 | if b is None: return 157 | urn_params[0][1] = b 158 | def set_c(click, param, c): 159 | if c is None: return 160 | urn_params[1][0] = c 161 | def set_d(click, param, d): 162 | if d is None: return 163 | urn_params[1][1] = d 164 | 165 | command_docstring = """Commands: 166 | - singlerun 167 | - histogram 168 | - probplot 169 | - singleprob 170 | """ 171 | 172 | @click.command() 173 | @click.option('--a', callback=set_a, type=float, help="Set the urn's a parameter") 174 | @click.option('--b', callback=set_b, type=float, help="Set the urn's b parameter") 175 | @click.option('--c', callback=set_c, type=float, help="Set the urn's c parameter") 176 | @click.option('--d', callback=set_d, type=float, help="Set the urn's d parameter") 177 | @click.option('--command', default="singlerun", help=command_docstring) 178 | @click.option('--ndraws', type=int, default=2000, help="How many draws from an urn") 179 | @click.option('--nruns', type=int, default=1000, help="How many urn runs to run") 180 | @click.option('--nr', type=float, default=1, help="Number of red balls in urn's starting configuration") 181 | @click.option('--nb', type=float, default=1, help="Number of black balls in urn's starting configuration") 182 | @click.option('--lambdaa', type=float, default=0.5, help="Underlying true rate for neighborhood A (red balls)") 183 | @click.option('--lambdab', type=float, default=0.5, help="Underlying true rate for neighborhood B (black balls)") 184 | @click.option('--exponential_decay', callback=exponential_decay_option, type=float, default=0, help="Add exponential decay to the urn") 185 | @click.option('--truncation', callback=truncation_option, type=int, help="Truncate the maximum number of new balls to add to urn") 186 | @click.option('--linear_surprise', callback=linear_surprise_option, is_flag=True, help="Incorporate a linear surprise factor in urn update") 187 | @click.option('--partial_surprise', callback=partial_surprise_option, nargs=2, type=(float, float), default=(None, None), help="Incorporate a partial surprise factor in urn update, adding only reported crimes") 188 | @click.option('--weighted_surprise', callback=weighted_surprise_option, nargs=4, type=(float, float, float, float), default=(None, None, None, None), help="Incorporate a weighted surprise factor in urn update, adding only reported crimes") 189 | @click.option('--sqrt_surprise', callback=sqrt_surprise_option, is_flag=True, help="Incorporate a sqrt surprise factor in urn update") 190 | @click.option('--poisson', callback=poisson_option, is_flag=True, help="urn updates are draws from a poisson instead of deterministic") 191 | @click.option('--mixed', callback=mixed_option, nargs=4, type=(float, float, float, float), default=(None, None, None, None), help="set parameters of a mixed urn d_A, d_B, r_B, r_B") 192 | @click.option('--interactive', is_flag=True, help='if set, show image interactively instead of saving to file') 193 | @click.option('--output', type=str, default="fig_out.png", help='name of output file if noninteractive') 194 | def main(a, b, c, d, output, command, ndraws, nruns, nr, nb, lambdaa, lambdab, exponential_decay, truncation, interactive, linear_surprise, sqrt_surprise, partial_surprise, weighted_surprise, poisson, mixed): 195 | print("Urn starting state: %s" % ((nr, nb),)) 196 | print("Urn parameters: %s" % urn_params) 197 | urn = urn_class((nr, nb), urn_params) 198 | commands[command](urn, ndraws, nruns, lambdaa, lambdab) 199 | if interactive: 200 | pylab.show() 201 | else: 202 | pylab.savefig(output) 203 | 204 | if __name__ == "__main__": 205 | main() 206 | -------------------------------------------------------------------------------- /src/polya.py: -------------------------------------------------------------------------------- 1 | import numpy.random 2 | import random 3 | 4 | ############################################################################## 5 | 6 | class PolyaUrn: 7 | """A basic Polya Urn with a given update matrix""" 8 | 9 | def __init__(self, initial_state, update_matrix): 10 | self.initial_state = initial_state 11 | self.update_matrix = update_matrix 12 | self.reset() 13 | 14 | def reset(self): 15 | self.state = self.initial_state 16 | 17 | def after_draw(self, choice, state): 18 | return state 19 | 20 | def update_row(self, choice, state): 21 | return self.update_matrix[choice] 22 | 23 | def draw(self, draws=1): 24 | rr = random.random 25 | binom = numpy.random.binomial 26 | ss = self.state 27 | ad = self.after_draw 28 | ur = self.update_row 29 | choices = { False: 1, True: 0 } 30 | for i in range(draws): 31 | v = rr() * (ss[0] + ss[1]) 32 | choice = choices[v= v[choice]: 88 | return (0,0) 89 | return cls.update_row(self, choice, ss) 90 | return GoodPolyaUrn 91 | 92 | def add_partial_surprise(cls, param): 93 | rr = random.random 94 | class GoodPolyaUrn(cls): 95 | def __init__(self, *args, **kwargs): 96 | cls.__init__(self, *args, **kwargs) 97 | def update_row(self, choice, ss): 98 | r = rr() 99 | t = sum(ss) 100 | v = ((t - ss[0]) / t, 101 | (t - ss[1]) / t) 102 | mx = max(v[0], v[1]) 103 | v = (v[0] / mx, v[1] / mx) 104 | if r >= v[choice]: 105 | return param 106 | return cls.update_row(self, choice, ss) 107 | return GoodPolyaUrn 108 | 109 | def add_weighted_surprise(cls, param): 110 | rr = random.random 111 | class GoodPolyaUrn(cls): 112 | def __init__(self, *args, **kwargs): 113 | cls.__init__(self, *args, **kwargs) 114 | def update_row(self, choice, ss): 115 | r = rr() 116 | t = sum(ss) 117 | v = ((t - ss[0]) / t, 118 | (t - ss[1]) / t) 119 | mx = max(v[0], v[1]) 120 | v = (v[0] / mx, v[1] / mx) 121 | if r >= v[choice]: 122 | return param[0:2] 123 | da = self.update_matrix[0][0] - param[0] 124 | db = self.update_matrix[1][1] - param[1] 125 | ra = param[0] 126 | rb = param[1] 127 | wd = param[2] 128 | wr = param[3] 129 | if choice == 0: 130 | return (da + ra, rb / wr) 131 | return (ra / wr, db + rb) 132 | return GoodPolyaUrn 133 | 134 | def add_sqrt_surprise(cls): 135 | rr = random.random 136 | class BetterPolyaUrn(cls): 137 | """FIXME: I don't remember what this does. I remember 138 | that we worked out the family of all possible compensations 139 | and that we believed this converged faster. But I don't remember 140 | the details. 141 | 142 | We're not going to use this for now, but let's not forget it exists. 143 | """ 144 | def __init__(self, *args, **kwargs): 145 | cls.__init__(self, *args, **kwargs) 146 | 147 | def update_row(self, choice, ss): 148 | r = rr() 149 | t = sum(ss) 150 | ps = (ss[0] / t, ss[1] / t) 151 | v = ((ps[1] / ps[0]) ** 0.5, 152 | (ps[0] / ps[1]) ** 0.5) 153 | mx = max(v[0], v[1]) 154 | v = (v[0] / mx, v[1] / mx) 155 | if r >= v[choice]: 156 | return (0,0) 157 | return cls.update_row(self, choice, ss) 158 | return BetterPolyaUrn 159 | 160 | ############################################################################## 161 | 162 | class BatchedPoissonUrn: 163 | 164 | def __init__(self, initial_state, update_parameters, truncation): 165 | self.initial_state = initial_state 166 | self.update_parameters = update_parameters 167 | self.truncation = truncation 168 | self.reset() 169 | 170 | def reset(self): 171 | self.state = self.initial_state 172 | 173 | def draw(self, draws=1): 174 | rr = random.random 175 | ss = self.state 176 | poisson = numpy.random.poisson 177 | binom = numpy.random.binomial 178 | lamb = self.update_parameters 179 | truncation = self.truncation 180 | 181 | for i in range(draws): 182 | v = ss[0] / (ss[0] + ss[1]) 183 | t = binom(truncation, v) 184 | 185 | cops_0 = t 186 | cops_1 = truncation - t 187 | crimes_0 = poisson(lamb[0]) 188 | crimes_1 = poisson(lamb[1]) 189 | 190 | arrests_0 = min(cops_0, crimes_0) 191 | arrests_1 = min(cops_1, crimes_1) 192 | 193 | updates_0 = 0 194 | updates_1 = 0 195 | 196 | r = rr() 197 | if cops_0 > 0 and r > ss[0] / ((ss[0] + ss[1]) * cops_0): 198 | updates_0 = arrests_0 199 | r = rr() 200 | if cops_1 > 0 and r > ss[1] / ((ss[0] + ss[1]) * cops_1): 201 | updates_1 = arrests_1 202 | ss = (ss[0] + updates_0, ss[1] + updates_1) 203 | yield ss 204 | self.state = ss 205 | 206 | 207 | 208 | class MixedUrn: 209 | 210 | def __init__(self, initial_state, d_a, d_b, r_a, r_b): 211 | self.update_matrix = ((d_a + r_a, r_b), 212 | ( r_a, d_b + r_b)) 213 | self.initial_state = initial_state 214 | self.state = initial_state 215 | 216 | def reset(self): 217 | self.state = self.initial_state 218 | 219 | def draw(self, draws=1): 220 | rr = random.random 221 | ss = self.state 222 | um = self.update_matrix 223 | for i in range(draws): 224 | v = rr() * (ss[0] + ss[1]) 225 | if v < ss[0]: 226 | update_row = um[0] 227 | else: 228 | update_row = um[1] 229 | ss = (ss[0] + update_row[0], ss[1] + update_row[1]) 230 | yield ss 231 | self.state = ss 232 | --------------------------------------------------------------------------------