├── LICENSE.md ├── Prior_Predictive_Sampling_Toggle_Switch ├── Makefile ├── run_bench.sh ├── toggle_switch_ABC_pps_naive.R ├── toggle_switch_ABC_pps_opt.R └── toggle_switch_ABC_pps_vec_par.c ├── Prior_Predictive_Sampling_Tuberculosis ├── Makefile ├── run_bench.sh └── tuberculosis_ABC_pps_vec_par.c ├── README.md ├── SMC_Inference_BEGE_model ├── Makefile ├── MonthlyReturns2018.csv ├── SMC_RW_LikeAnneal_BEGE_novec_nopar.c ├── SMC_RW_LikeAnneal_BEGE_novec_par.c ├── SMC_RW_LikeAnneal_BEGE_vec_nopar.c ├── SMC_RW_LikeAnneal_BEGE_vec_par.c └── run_bench.sh └── Weakly_Informative_Priors ├── Makefile ├── run_bench.sh └── weak_info_test_vec_par.c /LICENSE.md: -------------------------------------------------------------------------------- 1 | ### GNU GENERAL PUBLIC LICENSE 2 | 3 | Version 3, 29 June 2007 4 | 5 | Copyright (C) 2007 Free Software Foundation, Inc. 6 | 7 | 8 | Everyone is permitted to copy and distribute verbatim copies of this 9 | license document, but changing it is not allowed. 10 | 11 | ### Preamble 12 | 13 | The GNU General Public License is a free, copyleft license for 14 | software and other kinds of works. 15 | 16 | The licenses for most software and other practical works are designed 17 | to take away your freedom to share and change the works. By contrast, 18 | the GNU General Public License is intended to guarantee your freedom 19 | to share and change all versions of a program--to make sure it remains 20 | free software for all its users. We, the Free Software Foundation, use 21 | the GNU General Public License for most of our software; it applies 22 | also to any other work released this way by its authors. You can apply 23 | it to your programs, too. 24 | 25 | When we speak of free software, we are referring to freedom, not 26 | price. Our General Public Licenses are designed to make sure that you 27 | have the freedom to distribute copies of free software (and charge for 28 | them if you wish), that you receive source code or can get it if you 29 | want it, that you can change the software or use pieces of it in new 30 | free programs, and that you know you can do these things. 31 | 32 | To protect your rights, we need to prevent others from denying you 33 | these rights or asking you to surrender the rights. Therefore, you 34 | have certain responsibilities if you distribute copies of the 35 | software, or if you modify it: responsibilities to respect the freedom 36 | of others. 37 | 38 | For example, if you distribute copies of such a program, whether 39 | gratis or for a fee, you must pass on to the recipients the same 40 | freedoms that you received. You must make sure that they, too, receive 41 | or can get the source code. And you must show them these terms so they 42 | know their rights. 43 | 44 | Developers that use the GNU GPL protect your rights with two steps: 45 | (1) assert copyright on the software, and (2) offer you this License 46 | giving you legal permission to copy, distribute and/or modify it. 47 | 48 | For the developers' and authors' protection, the GPL clearly explains 49 | that there is no warranty for this free software. For both users' and 50 | authors' sake, the GPL requires that modified versions be marked as 51 | changed, so that their problems will not be attributed erroneously to 52 | authors of previous versions. 53 | 54 | Some devices are designed to deny users access to install or run 55 | modified versions of the software inside them, although the 56 | manufacturer can do so. This is fundamentally incompatible with the 57 | aim of protecting users' freedom to change the software. The 58 | systematic pattern of such abuse occurs in the area of products for 59 | individuals to use, which is precisely where it is most unacceptable. 60 | Therefore, we have designed this version of the GPL to prohibit the 61 | practice for those products. If such problems arise substantially in 62 | other domains, we stand ready to extend this provision to those 63 | domains in future versions of the GPL, as needed to protect the 64 | freedom of users. 65 | 66 | Finally, every program is threatened constantly by software patents. 67 | States should not allow patents to restrict development and use of 68 | software on general-purpose computers, but in those that do, we wish 69 | to avoid the special danger that patents applied to a free program 70 | could make it effectively proprietary. To prevent this, the GPL 71 | assures that patents cannot be used to render the program non-free. 72 | 73 | The precise terms and conditions for copying, distribution and 74 | modification follow. 75 | 76 | ### TERMS AND CONDITIONS 77 | 78 | #### 0. Definitions. 79 | 80 | "This License" refers to version 3 of the GNU General Public License. 81 | 82 | "Copyright" also means copyright-like laws that apply to other kinds 83 | of works, such as semiconductor masks. 84 | 85 | "The Program" refers to any copyrightable work licensed under this 86 | License. Each licensee is addressed as "you". "Licensees" and 87 | "recipients" may be individuals or organizations. 88 | 89 | To "modify" a work means to copy from or adapt all or part of the work 90 | in a fashion requiring copyright permission, other than the making of 91 | an exact copy. The resulting work is called a "modified version" of 92 | the earlier work or a work "based on" the earlier work. 93 | 94 | A "covered work" means either the unmodified Program or a work based 95 | on the Program. 96 | 97 | To "propagate" a work means to do anything with it that, without 98 | permission, would make you directly or secondarily liable for 99 | infringement under applicable copyright law, except executing it on a 100 | computer or modifying a private copy. Propagation includes copying, 101 | distribution (with or without modification), making available to the 102 | public, and in some countries other activities as well. 103 | 104 | To "convey" a work means any kind of propagation that enables other 105 | parties to make or receive copies. Mere interaction with a user 106 | through a computer network, with no transfer of a copy, is not 107 | conveying. 108 | 109 | An interactive user interface displays "Appropriate Legal Notices" to 110 | the extent that it includes a convenient and prominently visible 111 | feature that (1) displays an appropriate copyright notice, and (2) 112 | tells the user that there is no warranty for the work (except to the 113 | extent that warranties are provided), that licensees may convey the 114 | work under this License, and how to view a copy of this License. If 115 | the interface presents a list of user commands or options, such as a 116 | menu, a prominent item in the list meets this criterion. 117 | 118 | #### 1. Source Code. 119 | 120 | The "source code" for a work means the preferred form of the work for 121 | making modifications to it. "Object code" means any non-source form of 122 | a work. 123 | 124 | A "Standard Interface" means an interface that either is an official 125 | standard defined by a recognized standards body, or, in the case of 126 | interfaces specified for a particular programming language, one that 127 | is widely used among developers working in that language. 128 | 129 | The "System Libraries" of an executable work include anything, other 130 | than the work as a whole, that (a) is included in the normal form of 131 | packaging a Major Component, but which is not part of that Major 132 | Component, and (b) serves only to enable use of the work with that 133 | Major Component, or to implement a Standard Interface for which an 134 | implementation is available to the public in source code form. A 135 | "Major Component", in this context, means a major essential component 136 | (kernel, window system, and so on) of the specific operating system 137 | (if any) on which the executable work runs, or a compiler used to 138 | produce the work, or an object code interpreter used to run it. 139 | 140 | The "Corresponding Source" for a work in object code form means all 141 | the source code needed to generate, install, and (for an executable 142 | work) run the object code and to modify the work, including scripts to 143 | control those activities. However, it does not include the work's 144 | System Libraries, or general-purpose tools or generally available free 145 | programs which are used unmodified in performing those activities but 146 | which are not part of the work. For example, Corresponding Source 147 | includes interface definition files associated with source files for 148 | the work, and the source code for shared libraries and dynamically 149 | linked subprograms that the work is specifically designed to require, 150 | such as by intimate data communication or control flow between those 151 | subprograms and other parts of the work. 152 | 153 | The Corresponding Source need not include anything that users can 154 | regenerate automatically from other parts of the Corresponding Source. 155 | 156 | The Corresponding Source for a work in source code form is that same 157 | work. 158 | 159 | #### 2. Basic Permissions. 160 | 161 | All rights granted under this License are granted for the term of 162 | copyright on the Program, and are irrevocable provided the stated 163 | conditions are met. This License explicitly affirms your unlimited 164 | permission to run the unmodified Program. The output from running a 165 | covered work is covered by this License only if the output, given its 166 | content, constitutes a covered work. This License acknowledges your 167 | rights of fair use or other equivalent, as provided by copyright law. 168 | 169 | You may make, run and propagate covered works that you do not convey, 170 | without conditions so long as your license otherwise remains in force. 171 | You may convey covered works to others for the sole purpose of having 172 | them make modifications exclusively for you, or provide you with 173 | facilities for running those works, provided that you comply with the 174 | terms of this License in conveying all material for which you do not 175 | control copyright. Those thus making or running the covered works for 176 | you must do so exclusively on your behalf, under your direction and 177 | control, on terms that prohibit them from making any copies of your 178 | copyrighted material outside their relationship with you. 179 | 180 | Conveying under any other circumstances is permitted solely under the 181 | conditions stated below. Sublicensing is not allowed; section 10 makes 182 | it unnecessary. 183 | 184 | #### 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 185 | 186 | No covered work shall be deemed part of an effective technological 187 | measure under any applicable law fulfilling obligations under article 188 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 189 | similar laws prohibiting or restricting circumvention of such 190 | measures. 191 | 192 | When you convey a covered work, you waive any legal power to forbid 193 | circumvention of technological measures to the extent such 194 | circumvention is effected by exercising rights under this License with 195 | respect to the covered work, and you disclaim any intention to limit 196 | operation or modification of the work as a means of enforcing, against 197 | the work's users, your or third parties' legal rights to forbid 198 | circumvention of technological measures. 199 | 200 | #### 4. Conveying Verbatim Copies. 201 | 202 | You may convey verbatim copies of the Program's source code as you 203 | receive it, in any medium, provided that you conspicuously and 204 | appropriately publish on each copy an appropriate copyright notice; 205 | keep intact all notices stating that this License and any 206 | non-permissive terms added in accord with section 7 apply to the code; 207 | keep intact all notices of the absence of any warranty; and give all 208 | recipients a copy of this License along with the Program. 209 | 210 | You may charge any price or no price for each copy that you convey, 211 | and you may offer support or warranty protection for a fee. 212 | 213 | #### 5. Conveying Modified Source Versions. 214 | 215 | You may convey a work based on the Program, or the modifications to 216 | produce it from the Program, in the form of source code under the 217 | terms of section 4, provided that you also meet all of these 218 | conditions: 219 | 220 | - a) The work must carry prominent notices stating that you modified 221 | it, and giving a relevant date. 222 | - b) The work must carry prominent notices stating that it is 223 | released under this License and any conditions added under 224 | section 7. This requirement modifies the requirement in section 4 225 | to "keep intact all notices". 226 | - c) You must license the entire work, as a whole, under this 227 | License to anyone who comes into possession of a copy. This 228 | License will therefore apply, along with any applicable section 7 229 | additional terms, to the whole of the work, and all its parts, 230 | regardless of how they are packaged. This License gives no 231 | permission to license the work in any other way, but it does not 232 | invalidate such permission if you have separately received it. 233 | - d) If the work has interactive user interfaces, each must display 234 | Appropriate Legal Notices; however, if the Program has interactive 235 | interfaces that do not display Appropriate Legal Notices, your 236 | work need not make them do so. 237 | 238 | A compilation of a covered work with other separate and independent 239 | works, which are not by their nature extensions of the covered work, 240 | and which are not combined with it such as to form a larger program, 241 | in or on a volume of a storage or distribution medium, is called an 242 | "aggregate" if the compilation and its resulting copyright are not 243 | used to limit the access or legal rights of the compilation's users 244 | beyond what the individual works permit. Inclusion of a covered work 245 | in an aggregate does not cause this License to apply to the other 246 | parts of the aggregate. 247 | 248 | #### 6. Conveying Non-Source Forms. 249 | 250 | You may convey a covered work in object code form under the terms of 251 | sections 4 and 5, provided that you also convey the machine-readable 252 | Corresponding Source under the terms of this License, in one of these 253 | ways: 254 | 255 | - a) Convey the object code in, or embodied in, a physical product 256 | (including a physical distribution medium), accompanied by the 257 | Corresponding Source fixed on a durable physical medium 258 | customarily used for software interchange. 259 | - b) Convey the object code in, or embodied in, a physical product 260 | (including a physical distribution medium), accompanied by a 261 | written offer, valid for at least three years and valid for as 262 | long as you offer spare parts or customer support for that product 263 | model, to give anyone who possesses the object code either (1) a 264 | copy of the Corresponding Source for all the software in the 265 | product that is covered by this License, on a durable physical 266 | medium customarily used for software interchange, for a price no 267 | more than your reasonable cost of physically performing this 268 | conveying of source, or (2) access to copy the Corresponding 269 | Source from a network server at no charge. 270 | - c) Convey individual copies of the object code with a copy of the 271 | written offer to provide the Corresponding Source. This 272 | alternative is allowed only occasionally and noncommercially, and 273 | only if you received the object code with such an offer, in accord 274 | with subsection 6b. 275 | - d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | - e) Convey the object code using peer-to-peer transmission, 288 | provided you inform other peers where the object code and 289 | Corresponding Source of the work are being offered to the general 290 | public at no charge under subsection 6d. 291 | 292 | A separable portion of the object code, whose source code is excluded 293 | from the Corresponding Source as a System Library, need not be 294 | included in conveying the object code work. 295 | 296 | A "User Product" is either (1) a "consumer product", which means any 297 | tangible personal property which is normally used for personal, 298 | family, or household purposes, or (2) anything designed or sold for 299 | incorporation into a dwelling. In determining whether a product is a 300 | consumer product, doubtful cases shall be resolved in favor of 301 | coverage. For a particular product received by a particular user, 302 | "normally used" refers to a typical or common use of that class of 303 | product, regardless of the status of the particular user or of the way 304 | in which the particular user actually uses, or expects or is expected 305 | to use, the product. A product is a consumer product regardless of 306 | whether the product has substantial commercial, industrial or 307 | non-consumer uses, unless such uses represent the only significant 308 | mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to 312 | install and execute modified versions of a covered work in that User 313 | Product from a modified version of its Corresponding Source. The 314 | information must suffice to ensure that the continued functioning of 315 | the modified object code is in no case prevented or interfered with 316 | solely because modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or 331 | updates for a work that has been modified or installed by the 332 | recipient, or for the User Product in which it has been modified or 333 | installed. Access to a network may be denied when the modification 334 | itself materially and adversely affects the operation of the network 335 | or violates the rules and protocols for communication across the 336 | network. 337 | 338 | Corresponding Source conveyed, and Installation Information provided, 339 | in accord with this section must be in a format that is publicly 340 | documented (and with an implementation available to the public in 341 | source code form), and must require no special password or key for 342 | unpacking, reading or copying. 343 | 344 | #### 7. Additional Terms. 345 | 346 | "Additional permissions" are terms that supplement the terms of this 347 | License by making exceptions from one or more of its conditions. 348 | Additional permissions that are applicable to the entire Program shall 349 | be treated as though they were included in this License, to the extent 350 | that they are valid under applicable law. If additional permissions 351 | apply only to part of the Program, that part may be used separately 352 | under those permissions, but the entire Program remains governed by 353 | this License without regard to the additional permissions. 354 | 355 | When you convey a copy of a covered work, you may at your option 356 | remove any additional permissions from that copy, or from any part of 357 | it. (Additional permissions may be written to require their own 358 | removal in certain cases when you modify the work.) You may place 359 | additional permissions on material, added by you to a covered work, 360 | for which you have or can give appropriate copyright permission. 361 | 362 | Notwithstanding any other provision of this License, for material you 363 | add to a covered work, you may (if authorized by the copyright holders 364 | of that material) supplement the terms of this License with terms: 365 | 366 | - a) Disclaiming warranty or limiting liability differently from the 367 | terms of sections 15 and 16 of this License; or 368 | - b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | - c) Prohibiting misrepresentation of the origin of that material, 372 | or requiring that modified versions of such material be marked in 373 | reasonable ways as different from the original version; or 374 | - d) Limiting the use for publicity purposes of names of licensors 375 | or authors of the material; or 376 | - e) Declining to grant rights under trademark law for use of some 377 | trade names, trademarks, or service marks; or 378 | - f) Requiring indemnification of licensors and authors of that 379 | material by anyone who conveys the material (or modified versions 380 | of it) with contractual assumptions of liability to the recipient, 381 | for any liability that these contractual assumptions directly 382 | impose on those licensors and authors. 383 | 384 | All other non-permissive additional terms are considered "further 385 | restrictions" within the meaning of section 10. If the Program as you 386 | received it, or any part of it, contains a notice stating that it is 387 | governed by this License along with a term that is a further 388 | restriction, you may remove that term. If a license document contains 389 | a further restriction but permits relicensing or conveying under this 390 | License, you may add to a covered work material governed by the terms 391 | of that license document, provided that the further restriction does 392 | not survive such relicensing or conveying. 393 | 394 | If you add terms to a covered work in accord with this section, you 395 | must place, in the relevant source files, a statement of the 396 | additional terms that apply to those files, or a notice indicating 397 | where to find the applicable terms. 398 | 399 | Additional terms, permissive or non-permissive, may be stated in the 400 | form of a separately written license, or stated as exceptions; the 401 | above requirements apply either way. 402 | 403 | #### 8. Termination. 404 | 405 | You may not propagate or modify a covered work except as expressly 406 | provided under this License. Any attempt otherwise to propagate or 407 | modify it is void, and will automatically terminate your rights under 408 | this License (including any patent licenses granted under the third 409 | paragraph of section 11). 410 | 411 | However, if you cease all violation of this License, then your license 412 | from a particular copyright holder is reinstated (a) provisionally, 413 | unless and until the copyright holder explicitly and finally 414 | terminates your license, and (b) permanently, if the copyright holder 415 | fails to notify you of the violation by some reasonable means prior to 416 | 60 days after the cessation. 417 | 418 | Moreover, your license from a particular copyright holder is 419 | reinstated permanently if the copyright holder notifies you of the 420 | violation by some reasonable means, this is the first time you have 421 | received notice of violation of this License (for any work) from that 422 | copyright holder, and you cure the violation prior to 30 days after 423 | your receipt of the notice. 424 | 425 | Termination of your rights under this section does not terminate the 426 | licenses of parties who have received copies or rights from you under 427 | this License. If your rights have been terminated and not permanently 428 | reinstated, you do not qualify to receive new licenses for the same 429 | material under section 10. 430 | 431 | #### 9. Acceptance Not Required for Having Copies. 432 | 433 | You are not required to accept this License in order to receive or run 434 | a copy of the Program. Ancillary propagation of a covered work 435 | occurring solely as a consequence of using peer-to-peer transmission 436 | to receive a copy likewise does not require acceptance. However, 437 | nothing other than this License grants you permission to propagate or 438 | modify any covered work. These actions infringe copyright if you do 439 | not accept this License. Therefore, by modifying or propagating a 440 | covered work, you indicate your acceptance of this License to do so. 441 | 442 | #### 10. Automatic Licensing of Downstream Recipients. 443 | 444 | Each time you convey a covered work, the recipient automatically 445 | receives a license from the original licensors, to run, modify and 446 | propagate that work, subject to this License. You are not responsible 447 | for enforcing compliance by third parties with this License. 448 | 449 | An "entity transaction" is a transaction transferring control of an 450 | organization, or substantially all assets of one, or subdividing an 451 | organization, or merging organizations. If propagation of a covered 452 | work results from an entity transaction, each party to that 453 | transaction who receives a copy of the work also receives whatever 454 | licenses to the work the party's predecessor in interest had or could 455 | give under the previous paragraph, plus a right to possession of the 456 | Corresponding Source of the work from the predecessor in interest, if 457 | the predecessor has it or can get it with reasonable efforts. 458 | 459 | You may not impose any further restrictions on the exercise of the 460 | rights granted or affirmed under this License. For example, you may 461 | not impose a license fee, royalty, or other charge for exercise of 462 | rights granted under this License, and you may not initiate litigation 463 | (including a cross-claim or counterclaim in a lawsuit) alleging that 464 | any patent claim is infringed by making, using, selling, offering for 465 | sale, or importing the Program or any portion of it. 466 | 467 | #### 11. Patents. 468 | 469 | A "contributor" is a copyright holder who authorizes use under this 470 | License of the Program or a work on which the Program is based. The 471 | work thus licensed is called the contributor's "contributor version". 472 | 473 | A contributor's "essential patent claims" are all patent claims owned 474 | or controlled by the contributor, whether already acquired or 475 | hereafter acquired, that would be infringed by some manner, permitted 476 | by this License, of making, using, or selling its contributor version, 477 | but do not include claims that would be infringed only as a 478 | consequence of further modification of the contributor version. For 479 | purposes of this definition, "control" includes the right to grant 480 | patent sublicenses in a manner consistent with the requirements of 481 | this License. 482 | 483 | Each contributor grants you a non-exclusive, worldwide, royalty-free 484 | patent license under the contributor's essential patent claims, to 485 | make, use, sell, offer for sale, import and otherwise run, modify and 486 | propagate the contents of its contributor version. 487 | 488 | In the following three paragraphs, a "patent license" is any express 489 | agreement or commitment, however denominated, not to enforce a patent 490 | (such as an express permission to practice a patent or covenant not to 491 | sue for patent infringement). To "grant" such a patent license to a 492 | party means to make such an agreement or commitment not to enforce a 493 | patent against the party. 494 | 495 | If you convey a covered work, knowingly relying on a patent license, 496 | and the Corresponding Source of the work is not available for anyone 497 | to copy, free of charge and under the terms of this License, through a 498 | publicly available network server or other readily accessible means, 499 | then you must either (1) cause the Corresponding Source to be so 500 | available, or (2) arrange to deprive yourself of the benefit of the 501 | patent license for this particular work, or (3) arrange, in a manner 502 | consistent with the requirements of this License, to extend the patent 503 | license to downstream recipients. "Knowingly relying" means you have 504 | actual knowledge that, but for the patent license, your conveying the 505 | covered work in a country, or your recipient's use of the covered work 506 | in a country, would infringe one or more identifiable patents in that 507 | country that you have reason to believe are valid. 508 | 509 | If, pursuant to or in connection with a single transaction or 510 | arrangement, you convey, or propagate by procuring conveyance of, a 511 | covered work, and grant a patent license to some of the parties 512 | receiving the covered work authorizing them to use, propagate, modify 513 | or convey a specific copy of the covered work, then the patent license 514 | you grant is automatically extended to all recipients of the covered 515 | work and works based on it. 516 | 517 | A patent license is "discriminatory" if it does not include within the 518 | scope of its coverage, prohibits the exercise of, or is conditioned on 519 | the non-exercise of one or more of the rights that are specifically 520 | granted under this License. You may not convey a covered work if you 521 | are a party to an arrangement with a third party that is in the 522 | business of distributing software, under which you make payment to the 523 | third party based on the extent of your activity of conveying the 524 | work, and under which the third party grants, to any of the parties 525 | who would receive the covered work from you, a discriminatory patent 526 | license (a) in connection with copies of the covered work conveyed by 527 | you (or copies made from those copies), or (b) primarily for and in 528 | connection with specific products or compilations that contain the 529 | covered work, unless you entered into that arrangement, or that patent 530 | license was granted, prior to 28 March 2007. 531 | 532 | Nothing in this License shall be construed as excluding or limiting 533 | any implied license or other defenses to infringement that may 534 | otherwise be available to you under applicable patent law. 535 | 536 | #### 12. No Surrender of Others' Freedom. 537 | 538 | If conditions are imposed on you (whether by court order, agreement or 539 | otherwise) that contradict the conditions of this License, they do not 540 | excuse you from the conditions of this License. If you cannot convey a 541 | covered work so as to satisfy simultaneously your obligations under 542 | this License and any other pertinent obligations, then as a 543 | consequence you may not convey it at all. For example, if you agree to 544 | terms that obligate you to collect a royalty for further conveying 545 | from those to whom you convey the Program, the only way you could 546 | satisfy both those terms and this License would be to refrain entirely 547 | from conveying the Program. 548 | 549 | #### 13. Use with the GNU Affero General Public License. 550 | 551 | Notwithstanding any other provision of this License, you have 552 | permission to link or combine any covered work with a work licensed 553 | under version 3 of the GNU Affero General Public License into a single 554 | combined work, and to convey the resulting work. The terms of this 555 | License will continue to apply to the part which is the covered work, 556 | but the special requirements of the GNU Affero General Public License, 557 | section 13, concerning interaction through a network will apply to the 558 | combination as such. 559 | 560 | #### 14. Revised Versions of this License. 561 | 562 | The Free Software Foundation may publish revised and/or new versions 563 | of the GNU General Public License from time to time. Such new versions 564 | will be similar in spirit to the present version, but may differ in 565 | detail to address new problems or concerns. 566 | 567 | Each version is given a distinguishing version number. If the Program 568 | specifies that a certain numbered version of the GNU General Public 569 | License "or any later version" applies to it, you have the option of 570 | following the terms and conditions either of that numbered version or 571 | of any later version published by the Free Software Foundation. If the 572 | Program does not specify a version number of the GNU General Public 573 | License, you may choose any version ever published by the Free 574 | Software Foundation. 575 | 576 | If the Program specifies that a proxy can decide which future versions 577 | of the GNU General Public License can be used, that proxy's public 578 | statement of acceptance of a version permanently authorizes you to 579 | choose that version for the Program. 580 | 581 | Later license versions may give you additional or different 582 | permissions. However, no additional obligations are imposed on any 583 | author or copyright holder as a result of your choosing to follow a 584 | later version. 585 | 586 | #### 15. Disclaimer of Warranty. 587 | 588 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 589 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 590 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT 591 | WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT 592 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 593 | A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND 594 | PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE 595 | DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR 596 | CORRECTION. 597 | 598 | #### 16. Limitation of Liability. 599 | 600 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 601 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR 602 | CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 603 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES 604 | ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT 605 | NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR 606 | LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM 607 | TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER 608 | PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 609 | 610 | #### 17. Interpretation of Sections 15 and 16. 611 | 612 | If the disclaimer of warranty and limitation of liability provided 613 | above cannot be given local legal effect according to their terms, 614 | reviewing courts shall apply local law that most closely approximates 615 | an absolute waiver of all civil liability in connection with the 616 | Program, unless a warranty or assumption of liability accompanies a 617 | copy of the Program in return for a fee. 618 | 619 | END OF TERMS AND CONDITIONS 620 | 621 | ### How to Apply These Terms to Your New Programs 622 | 623 | If you develop a new program, and you want it to be of the greatest 624 | possible use to the public, the best way to achieve this is to make it 625 | free software which everyone can redistribute and change under these 626 | terms. 627 | 628 | To do so, attach the following notices to the program. It is safest to 629 | attach them to the start of each source file to most effectively state 630 | the exclusion of warranty; and each file should have at least the 631 | "copyright" line and a pointer to where the full notice is found. 632 | 633 | 634 | Copyright (C) 635 | 636 | This program is free software: you can redistribute it and/or modify 637 | it under the terms of the GNU General Public License as published by 638 | the Free Software Foundation, either version 3 of the License, or 639 | (at your option) any later version. 640 | 641 | This program is distributed in the hope that it will be useful, 642 | but WITHOUT ANY WARRANTY; without even the implied warranty of 643 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 644 | GNU General Public License for more details. 645 | 646 | You should have received a copy of the GNU General Public License 647 | along with this program. If not, see . 648 | 649 | Also add information on how to contact you by electronic and paper 650 | mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands \`show w' and \`show c' should show the 661 | appropriate parts of the General Public License. Of course, your 662 | program's commands might be different; for a GUI interface, you would 663 | use an "about box". 664 | 665 | You should also get your employer (if you work as a programmer) or 666 | school, if any, to sign a "copyright disclaimer" for the program, if 667 | necessary. For more information on this, and how to apply and follow 668 | the GNU GPL, see . 669 | 670 | The GNU General Public License does not permit incorporating your 671 | program into proprietary programs. If your program is a subroutine 672 | library, you may consider it more useful to permit linking proprietary 673 | applications with the library. If this is what you want to do, use the 674 | GNU Lesser General Public License instead of this License. But first, 675 | please read . 676 | -------------------------------------------------------------------------------- /Prior_Predictive_Sampling_Toggle_Switch/Makefile: -------------------------------------------------------------------------------- 1 | 2 | all: 3 | make toggle_switch_ABC_pps_vec_par 4 | make toggle_switch_ABC_pps_novec_nopar 5 | make toggle_switch_ABC_pps_novec_par 6 | 7 | toggle_switch_ABC_pps_vec_par: toggle_switch_ABC_pps_vec_par.c Makefile 8 | icc -mkl=sequential -qopenmp -O2 -xHost -fcf-protection=none toggle_switch_ABC_pps_vec_par.c -o toggle_switch_ABC_pps_vec_par 9 | 10 | toggle_switch_ABC_pps_novec_par: toggle_switch_ABC_pps_vec_par.c Makefile 11 | icc -mkl=sequential -qopenmp -O2 -xHost -qno-openmp-simd -no-vec toggle_switch_ABC_pps_vec_par.c -o toggle_switch_ABC_pps_novec_par 12 | 13 | toggle_switch_ABC_pps_novec_nopar: toggle_switch_ABC_pps_vec_par.c Makefile 14 | icc -mkl=sequential -qno-openmp -O2 -xHost -no-vec toggle_switch_ABC_pps_novec_nopar.c -o toggle_switch_ABC_pps_novec_nopar 15 | 16 | clean: 17 | rm -f toggle_switch_ABC_pps_vec_par toggle_switch_ABC_pps_novec_nopar toggle_switch_ABC_pps_novec_par 18 | -------------------------------------------------------------------------------- /Prior_Predictive_Sampling_Toggle_Switch/run_bench.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | N=4000 4 | C=8000 5 | T=600 6 | SEED=1337 7 | 8 | echo "Toggle switch prior predicive sampling benchmark" 9 | echo "N = $N C = $C T = $T" 10 | 11 | #run and time vectorised parallel version 12 | echo 'timing parallel+SIMD' 13 | time ./toggle_switch_ABC_pps_vec_par $N $SEED $C $T > /dev/null 14 | echo 'done' 15 | 16 | #run and time scalar parallel version 17 | # (still uses OpenMP and MKL/VSL, but SIMD and auto-vectorisation disabled) 18 | echo 'timing parallel+scalar' 19 | time ./toggle_switch_ABC_pps_novec_par $N $SEED $C $T > /dev/null 20 | echo 'done' 21 | 22 | #run and time scalar sequential version 23 | # (still uses MKL/VSL, but OpenMP and auto-vectorisation disabled ) 24 | echo 'timing sequential+scalar' 25 | time ./toggle_switch_ABC_pps_novec_nopar $N $SEED $C $T > /dev/null 26 | echo 'done' 27 | 28 | 29 | -------------------------------------------------------------------------------- /Prior_Predictive_Sampling_Toggle_Switch/toggle_switch_ABC_pps_naive.R: -------------------------------------------------------------------------------- 1 | library(tictoc) 2 | library(doParallel) 3 | 4 | # Naive implementation of toggle switch simulation 5 | simulate_toggle_switch <- function(mu,sigma,gam, alpha, beta, T, C) { 6 | 7 | # create ouput array 8 | y <- numeric(C) 9 | # evolve each cell one-by-one 10 | for (i in 1:C) { 11 | # initialise 12 | alpha_u <- alpha[1] 13 | alpha_v <- alpha[2] 14 | beta_u <- beta[1] 15 | beta_v <- beta[2] 16 | u_t <- 10 17 | v_t <- 10 18 | for (j in 2:T){ 19 | p_u <- v_t^beta_u 20 | p_v <- u_t^beta_v 21 | u_t <- 0.97*u_t + alpha_u/(1+p_u) - 1.0 + 0.5*rnorm(1,0,1) 22 | v_t <- 0.97*v_t + alpha_v/(1+p_v) - 1.0 + 0.5*rnorm(1,0,1) 23 | if (u_t < 1.0 ){ 24 | u_t <- 1.0 25 | } 26 | if (v_t < 1.0) { 27 | v_t <- 1.0 28 | } 29 | } 30 | y[i] <- u_t + sigma*mu*rnorm(1,0,1)/(u_t^gam) 31 | if (y[i] < 1.0) { 32 | y <- 1.0 33 | } 34 | } 35 | return(y) 36 | } 37 | 38 | # prior predictive sampling 39 | T <- 600 40 | C <- 8000 41 | N <- 240 42 | 43 | for (P in cores) { 44 | # set up level of parallelism 45 | cl <- makeCluster(P) 46 | registerDoParallel(cl) 47 | # run optimised R simulations 48 | tic() 49 | obs_vals2 <- foreach(k = 1:N) %dopar% { 50 | theta <- runif(7,c(250.0,0.05,0.05,0.0,0.0,0.0,0.0), 51 | c(400.0,0.5,0.35,50.0,50.0,7.0,7.0)) 52 | mu <- theta[1] 53 | sigma <- theta[2] 54 | gam <- theta[3] 55 | alpha <- theta[4:5] 56 | beta <- theta[6:7] 57 | 58 | c(theta,simulate_toggle_switch(mu,sigma,gam,alpha,beta,T,C)) 59 | } 60 | print(c(P,N,C,T)) 61 | toc() 62 | stopCluster(cl) 63 | } 64 | -------------------------------------------------------------------------------- /Prior_Predictive_Sampling_Toggle_Switch/toggle_switch_ABC_pps_opt.R: -------------------------------------------------------------------------------- 1 | library(tictoc) 2 | library(doParallel) 3 | 4 | # optimized R-base implementation of the toggle switch model 5 | simulate_toggle_switch_vec <- function(mu,sigma,gam, alpha, beta, T, C) { 6 | 7 | # create ouput array 8 | u_t <- numeric(C) 9 | v_t <- numeric(C) 10 | 11 | # initialise 12 | alpha_u <- alpha[1] 13 | alpha_v <- alpha[2] 14 | beta_u <- beta[1] 15 | beta_v <- beta[2] 16 | u_t[1:C] <- 10 17 | v_t[1:C] <- 10 18 | # generate random variates 19 | zeta <- matrix(nrow=C,ncol=2*(T-1)+1) 20 | zeta[,] <- rnorm(C*(2*(T-1)+1),0,1) 21 | # evolve all cells together 22 | for (j in 2:T) { 23 | p_u <- v_t^beta_u 24 | p_v <- u_t^beta_v 25 | u_t <- 0.97*u_t + alpha_u/(1+p_u) - 1.0 + 0.5*zeta[1:C,2*(j-1)] 26 | v_t <- 0.97*v_t + alpha_v/(1+p_v) - 1.0 + 0.5*zeta[1:C,2*(j-1) + 1] 27 | u_t[u_t < 1.0] <- 1.0; 28 | v_t[v_t < 1.0] <- 1.0; 29 | 30 | } 31 | y <- u_t + sigma*mu*zeta[1:C,1]/(u_t^gam) 32 | y[y < 1.0] <- 1.0 33 | return(y) 34 | } 35 | 36 | # prior predictive sampling 37 | T <- 600 38 | C <- 8000 39 | N <- 8064 40 | 41 | for (P in cores) { 42 | # set up level of parallelism 43 | cl <- makeCluster(P) 44 | registerDoParallel(cl) 45 | # run optimised R simulations 46 | tic() 47 | obs_vals2 <- foreach(k = 1:N) %dopar% { 48 | theta <- runif(7,c(250.0,0.05,0.05,0.0,0.0,0.0,0.0), 49 | c(400.0,0.5,0.35,50.0,50.0,7.0,7.0)) 50 | mu <- theta[1] 51 | sigma <- theta[2] 52 | gam <- theta[3] 53 | alpha <- theta[4:5] 54 | beta <- theta[6:7] 55 | 56 | c(theta,simulate_toggle_switch_vec(mu,sigma,gam,alpha,beta,T,C)) 57 | } 58 | print(c(P,N,C,T)) 59 | toc() 60 | stopCluster(cl) 61 | } 62 | -------------------------------------------------------------------------------- /Prior_Predictive_Sampling_Toggle_Switch/toggle_switch_ABC_pps_vec_par.c: -------------------------------------------------------------------------------- 1 | /* Bayesian computations using SIMD operations 2 | * Copyright (C) 2019 David J. Warne, Christopher C. Drovandi 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | /** 18 | * @file toggle_switch_ABC_pps.c 19 | * 20 | * @brief Demonstration of vectorisation for approximate Bayesian computation 21 | * 22 | * @details Efficient prior predictive sampling for the genetic toggle switch 23 | * model. Cells are evoluted in groups of equal to the vector processing unit 24 | * capacity for double precision floating point. Standard Gaussian random 25 | * variates are generated for the entire evolution of the group to efficiently 26 | * utilise the MKL VSL generator. 27 | * 28 | * @author David J. Warne (david.warne@qut.edu.au) 29 | * School of Mathematical Sciences 30 | * Queensland University of Technology 31 | * 32 | * @author Chris C. Drovandi (c.drovandi@qut.edu.au) 33 | * School of Mathematical Sciences 34 | * Queensland University of Technology 35 | * 36 | * @date 12 Oct 2018 37 | * 38 | */ 39 | 40 | /* standard C headers*/ 41 | #include 42 | #include 43 | #include 44 | 45 | /* Intel headers */ 46 | #include 47 | #include 48 | 49 | /* OpenMP header */ 50 | #include 51 | 52 | /* length of vector processing units and ideal memory alignement*/ 53 | #if defined(__AVX512BW__) 54 | #define VECLEN 8 55 | #define ALIGN 64 56 | #elif defined(__AVX2__) 57 | #define VECLEN 4 58 | #define ALIGN 64 59 | #elif defined(__AVX__) 60 | #define VECLEN 4 61 | #define ALIGN 32 62 | #elif defined(__SSE4_2__) 63 | #define VECLEN 2 64 | #define ALIGN 16 65 | #endif 66 | 67 | /** 68 | * @brief vectorised toggle switch stochastic simulation 69 | * 70 | * @details processes cells in blocks of VECLEN to exploit vectorisation and 71 | * cache. Utilises MKL VSL routine to generate all Gaussian random variates for 72 | * the entire evolution of the block. 73 | * 74 | * @param stream Pointer to RNG state 75 | * @param mu basal observation noise level 76 | * @param sigma,gamma increase the observation noise at low expression levels 77 | * @param alpha, beta logistic-like repression of gene expression 78 | * @param T simulation time and observation time 79 | * @param C number of cells 80 | * @param zeta memory location to store gaussian random variaates 81 | * @param y vector of C observations of u-gene expression levels 82 | * 83 | * @note assumes argument data arrays are aligned on ALIGN-byte boundary. 84 | * The restrict keyword is used to ensure the compile knows there is no pointer 85 | * aliasing. 86 | * 87 | * @warning For optimal performance, the number of cells, C, should be a 88 | * multiple of VECLEN 89 | */ 90 | void 91 | simulate_toggle_switch(VSLStreamStatePtr stream, 92 | double mu, double sigma, double gamma, 93 | double * restrict alpha, double * restrict beta, 94 | int T, int C,double * restrict zeta, double * restrict y) 95 | { 96 | 97 | /* process cells in blocks of VECLEN*/ 98 | for (int c=0;c= 1.0) ? u_t : 1.0; 136 | v_t += 0.5*zeta_v; 137 | v_t = (v_t >= 1.0) ? v_t : 1.0; 138 | } 139 | 140 | /* make noisy observation */ 141 | y[c+c2] = u_t + _mu + _sigma*_mu*zeta[(T-1)*VECLEN*2 + c2]/pow(u_t,_gamma); 142 | y[c+c2] = (y[c+c2] >= 1.0) ? y[c+c2]: 1.0; 143 | } 144 | } 145 | } 146 | 147 | /** 148 | * @brief program entry point 149 | * @details Generates prior predictive samples for the genetic toggle switch 150 | * model. Distributes simulations across availablle cores and utilises fine grain 151 | * SIMD operations for groups of cells within each simulation. 152 | * 153 | * @param argc number of command line arguments 154 | * @param argv vector of argument strings 155 | */ 156 | int 157 | main(int argc,char **argv) 158 | { 159 | int T, C, K; 160 | double *theta, *obs_vals; 161 | int seed, sims; 162 | 163 | /* default number of simulation timesteps*/ 164 | T = 300; 165 | 166 | /* default number of cells*/ 167 | C = 2000; 168 | 169 | /* number of parameters*/ 170 | K = 7; 171 | 172 | /* get command line arguments*/ 173 | if (argc < 2) 174 | { 175 | fprintf(stderr,"Usage : [%s] sims seed [C] [T] [VEC %d ALIGN %d\n",argv[0],VECLEN,ALIGN); 176 | exit(1); 177 | } 178 | sims = (int)atoi(argv[1]); 179 | seed = (int)atoi(argv[2]); 180 | 181 | /* check if C was specified*/ 182 | if (argc > 3) 183 | { 184 | C = (int)atoi(argv[3]); 185 | if (C%VECLEN != 0) 186 | { 187 | fprintf(stderr,"Warning: For optimal performance C must be a multiple of %d\n",VECLEN); 188 | } 189 | } 190 | /* check if T was specified*/ 191 | if (argc > 4) 192 | { 193 | T = (int)atoi(argv[4]); 194 | } 195 | 196 | /*allocate aligned memory for noisy observations*/ 197 | obs_vals = (double *)_mm_malloc(C*sims*sizeof(double),ALIGN); 198 | 199 | /* allocate aligned memory prior samples that generated the data*/ 200 | theta = (double *)_mm_malloc(K*sims*sizeof(double),ALIGN); 201 | 202 | /* compute simulations in parallel*/ 203 | #pragma omp parallel shared(seed,sims,C,obs_vals,theta) 204 | { 205 | VSLStreamStatePtr stream; 206 | int thread_id, num_threads, sims_per_thread; 207 | double *zeta; 208 | 209 | /* get thread information and assign workload*/ 210 | thread_id = omp_get_thread_num(); 211 | num_threads = omp_get_num_threads(); 212 | sims_per_thread = sims/num_threads; 213 | 214 | fprintf(stderr,"ID: %d, total: %d sims : %d\n",thread_id,num_threads, sims_per_thread); 215 | /* initialise RNG stream for this thread*/ 216 | vslNewStream(&stream,VSL_BRNG_MT2203+thread_id,seed); 217 | 218 | /*allocate aligned memory for Gaussian random variates*/ 219 | zeta = (double *)_mm_malloc(2*VECLEN*T*sizeof(double),ALIGN); 220 | 221 | /* compute simulations in this threads workload*/ 222 | for (int k=thread_id*sims_per_thread;k<(thread_id+1)*sims_per_thread;k++) 223 | { 224 | /* model parameters */ 225 | double mu,sigma,gamma; /* measurement error parameters */ 226 | double alpha[2], beta[2]; /* parameters for gene expression*/ 227 | 228 | /*sample prior */ 229 | vdRngUniform(VSL_RNG_METHOD_UNIFORM_STD,stream,1,&mu,250.0,400.0); 230 | vdRngUniform(VSL_RNG_METHOD_UNIFORM_STD,stream,1,&sigma,0.05,0.5); 231 | vdRngUniform(VSL_RNG_METHOD_UNIFORM_STD,stream,1,&gamma,0.05,0.35); 232 | vdRngUniform(VSL_RNG_METHOD_UNIFORM_STD,stream,2,alpha,0.0,50.0); 233 | vdRngUniform(VSL_RNG_METHOD_UNIFORM_STD,stream,2,beta,0.0,7.0); 234 | 235 | /*run simulation and store observations*/ 236 | simulate_toggle_switch(stream,mu,sigma,gamma,alpha,beta,T,C,zeta, 237 | obs_vals +k*C); 238 | /*store prior sample*/ 239 | theta[k*7] = mu; 240 | theta[k*7 + 1] = sigma; 241 | theta[k*7 + 2] = gamma; 242 | theta[k*7 + 3] = alpha[0]; 243 | theta[k*7 + 4] = beta[0]; 244 | theta[k*7 + 5] = alpha[1]; 245 | theta[k*7 + 6] = beta[1]; 246 | } 247 | /*clean up memory*/ 248 | vslDeleteStream(&stream); 249 | _mm_free(zeta); 250 | } 251 | 252 | /*output prior predicitive samples for postprocessing for ABC*/ 253 | fprintf(stdout,"\"Sample\",\"mu\",\"sigma\",\"gamma\",\"alpha_u\",\"beta_u\",\"alpha_v\",\"beta_v\""); 254 | for (int j=0;j /dev/null 12 | echo 'done' 13 | 14 | #run and time scalar parallel version 15 | # (still uses OpenMP and MKL/VSL, but SIMD and auto-vectorisation disabled) 16 | echo 'timing parallel+scalar' 17 | time ./tuberculosis_ABC_pps_novec_par $N $SEED > /dev/null 18 | echo 'done' 19 | 20 | #run and time scalar sequential version 21 | # (still uses MKL/VSL, but OpenMP and auto-vectorisation disabled ) 22 | echo 'timing sequential+scalar' 23 | time ./tuberculosis_ABC_pps_novec_nopar $N $SEED > /dev/null 24 | echo 'done' 25 | 26 | -------------------------------------------------------------------------------- /Prior_Predictive_Sampling_Tuberculosis/tuberculosis_ABC_pps_vec_par.c: -------------------------------------------------------------------------------- 1 | /* Bayesian computations using SIMD operations 2 | * Copyright (C) 2019 David J. Warne, Christopher C. Drovandi, Scott A. Sisson 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | /** 18 | * @file tuderculosis_ABC_pps.c 19 | * @brief Demonstration of vectorisation for approximate Bayesian computation 20 | * @details Uses the Disease transmission and mutation model 21 | * 22 | * @author David J. Warne (david.warne@qut.edu.au) 23 | * School of Mathematical Sciences 24 | * Queensland University of Technology 25 | * 26 | * @author Chris C. Drovandi (c.drovandi@qut.edu.au) 27 | * School of Mathematical Sciences 28 | * Queensland University of Technology 29 | * 30 | * @author Scott A. Sisson (scott.sisson@unsw.edu.au) 31 | * School of Mathematics and Statistics 32 | * University of New South Whales 33 | * 34 | * @date 1 Nov 2018 35 | * 36 | */ 37 | 38 | /* standard C headers */ 39 | #include 40 | #include 41 | #include 42 | 43 | /* Intel headers */ 44 | #include 45 | #include 46 | 47 | /* OpenMP header */ 48 | #include 49 | 50 | /* length of vector processing units and ideal memory alignment*/ 51 | #if defined(__AVX512BW__) 52 | #define VECLEN 8 53 | #define ALIGN 64 54 | #elif defined(__AVX2__) 55 | #define VECLEN 4 56 | #define ALIGN 64 57 | #elif defined(__AVX__) 58 | #define VECLEN 4 59 | #define ALIGN 32 60 | #elif defined(__SSE4_2__) 61 | #define VECLEN 2 62 | #define ALIGN 16 63 | #endif 64 | 65 | #define BIRTH 0 66 | #define DEATH 1 67 | #define MUTATION 2 68 | 69 | #define MAXN 10000 70 | #define MAXG 1000 71 | 72 | /** 73 | * @brief discrete random variable sampler using lookup method 74 | * 75 | * @param stream Pointer to RNG state 76 | * @param p array of probabilities 77 | * @param n number of possible outcomes 78 | * @param ix pointer to store the selected outcome 79 | */ 80 | void 81 | sample(VSLStreamStatePtr stream,double * restrict p,int n, int *ix) 82 | { 83 | double sum,u; 84 | int k; 85 | 86 | k = 0; 87 | sum = p[k]; 88 | /* generate a uniform random variable and see where it lies in the cumulative prob */ 89 | vdRngUniform(VSL_RNG_METHOD_UNIFORM_STD,stream,1,&u, 0.0,1.0); 90 | while (u > sum && k < n-1) 91 | { 92 | k++; 93 | sum += p[k]; 94 | } 95 | *ix = k; 96 | return; 97 | } 98 | 99 | /** 100 | * @brief partly vectorised TB dynamics stochastic simulation 101 | * 102 | * @details uses Gillespies direct method with some update operations being 103 | * vectorised. Example of an algorithm that is diffisult to vectorise without 104 | * introduction of approximations. 105 | * 106 | * @param stream Pointer to RNG state 107 | * @param alpha birth rate 108 | * @param delta death rate 109 | * @param mutation rate 110 | * @param g_ret pointer to store distinct genotype count 111 | * @param H_ret pointer to store genetic diveristy 112 | 113 | * @note assumes argument data arrays are aligned on ALIGN-byte boundary. 114 | * The restrict keyword is used to ensure the compile knows there is no pointer 115 | * aliasing. 116 | */ 117 | void 118 | simulate(VSLStreamStatePtr stream, double alpha, double delta, double mu, 119 | double * restrict g_ret, double * restrict H_ret) 120 | { 121 | 122 | /* the current number of genotypes and population*/ 123 | int G=1, N=1; 124 | 125 | /*maximum population*/ 126 | int Nstop = MAXN; 127 | 128 | /*maximum genotypes*/ 129 | int maxG = MAXN; 130 | 131 | /*birth/death/mutation probabilities*/ 132 | __declspec(align(ALIGN)) double probs[3]; 133 | double sumProbs; 134 | 135 | /*genotype probabilities*/ 136 | __declspec(align(ALIGN)) double probs_G[MAXN]; 137 | 138 | /* number of individuals in each genotype in population */ 139 | __declspec(align(ALIGN)) double X[MAXN]; 140 | 141 | /* number of individuals in each genotype in sample */ 142 | __declspec(align(ALIGN)) double x[MAXN]; 143 | double g,H; 144 | int event_val = 0, event_val_G = 0; 145 | 146 | /* initialise the numbers in each genotype */ 147 | #pragma omp simd 148 | for (int i=0; i 0 && N < Nstop && G < maxG) 165 | { 166 | float Nf; 167 | Nf = (double)N; 168 | 169 | /* which event occurs?*/ 170 | sample(stream,probs,3,&event_val); 171 | 172 | #pragma omp simd 173 | for (int i=0; i 0) 230 | { 231 | int numSamples = 473; 232 | int index; 233 | /* take a sample from the population */ 234 | for (int i = 0; i 0) 240 | { 241 | /* remove sample from population (sampling without replacement) */ 242 | X[index] = X[index]-1; 243 | /* add invidual into the sample */ 244 | x[index] = x[index]+1; 245 | break; 246 | } 247 | } 248 | } 249 | 250 | 251 | /* compute the distinct genotypes */ 252 | g = 0; 253 | #pragma omp simd reduction(+:g) 254 | for (int i=0; i 0); 257 | } 258 | 259 | /* compute the genetic diversity */ 260 | H = 0; 261 | #pragma omp simd reduction(+:H) 262 | for (int i=0;i 0) 343 | { 344 | break; 345 | } 346 | 347 | } 348 | /* simulate pseudo-data*/ 349 | simulate(stream,alpha, delta, mu, &g_ret, &H_ret); 350 | 351 | /*store prior samples*/ 352 | theta[k*3] = alpha; 353 | theta[k*3 + 1] = delta; 354 | theta[k*3 + 2] = mu; 355 | 356 | /*store summary stats*/ 357 | S[k*2] = g_ret; 358 | S[k*2 +1] = H_ret; 359 | } 360 | /*clean up memory*/ 361 | vslDeleteStream(&stream); 362 | } 363 | 364 | /*output prior predictive samples for postprocessing for ABC*/ 365 | fprintf(stdout,"\"Sample\",\"alpha\",\"delta\",\"mu\",\"G\",\"H\"\n"); 366 | for (int j=0;j. 41 | 42 | 43 | 44 | ## Requirements 45 | 46 | 1. Intel Math Kernel Library (MKL) version >= 18 Update 1; 47 | 2. Intel C Compiler version >= 18.0.1 (Also compatible with GNU C Compiler version >= 7.0.0, but not tested); 48 | 3. CPU supporting Advance Vector Extensions instruction sets (either AVX, AVX2 or AVX512). 49 | 50 | ## Compile and run Benchmarks 51 | 52 | The build scripts and benchmarks assume a Linux Operating system using the Bourne-again shell 53 | 54 | 1. `cd path/to/example/` 55 | 2. `make` 56 | 3. `./run_bench.sh` 57 | 58 | Please note, the benchmark results presented in the Paper were performed using 4 cores of the Intel Xeon E5-2680v3 and 4 cores of the Intel Xeon Gold 6140. Speed-up factors may vary across CPU models, core counts and vector widths. 59 | -------------------------------------------------------------------------------- /SMC_Inference_BEGE_model/Makefile: -------------------------------------------------------------------------------- 1 | 2 | all: 3 | make SMC_RW_LikeAnneal_BEGE_vec_par 4 | make SMC_RW_LikeAnneal_BEGE_novec_par 5 | make SMC_RW_LikeAnneal_BEGE_novec_nopar 6 | make SMC_RW_LikeAnneal_BEGE_vec_nopar 7 | 8 | SMC_RW_LikeAnneal_BEGE_vec_par: SMC_RW_LikeAnneal_BEGE_vec_par.c Makefile 9 | icc -mkl -qopenmp -O2 -xhost SMC_RW_LikeAnneal_BEGE_vec_par.c -o SMC_RW_LikeAnneal_BEGE_vec_par 10 | 11 | SMC_RW_LikeAnneal_BEGE_novec_par: SMC_RW_LikeAnneal_BEGE_novec_par.c Makefile 12 | icc -mkl -qopenmp -O2 -xhost -no-vec SMC_RW_LikeAnneal_BEGE_novec_par.c -o SMC_RW_LikeAnneal_BEGE_novec_par 13 | 14 | SMC_RW_LikeAnneal_BEGE_vec_nopar: SMC_RW_LikeAnneal_BEGE_vec_nopar.c Makefile 15 | icc -mkl -qopenmp -O2 -xhost SMC_RW_LikeAnneal_BEGE_vec_nopar.c -o SMC_RW_LikeAnneal_BEGE_vec_nopar 16 | 17 | SMC_RW_LikeAnneal_BEGE_novec_nopar: SMC_RW_LikeAnneal_BEGE_novec_nopar.c Makefile 18 | icc -mkl -O2 -xhost -no-vec SMC_RW_LikeAnneal_BEGE_novec_nopar.c -o SMC_RW_LikeAnneal_BEGE_novec_nopar 19 | 20 | clean: 21 | rm -f SMC_RW_LikeAnneal_BEGE_vec_par SMC_RW_LikeAnneal_BEGE_novec_par SMC_RW_LikeAnneal_BEGE_novec_nopar SMC_RW_LikeAnneal_BEGE_vec_nopar 22 | -------------------------------------------------------------------------------- /SMC_Inference_BEGE_model/MonthlyReturns2018.csv: -------------------------------------------------------------------------------- 1 | 1099 2 | 0.0296 3 | 0.0264 4 | 0.0036 5 | -0.0324 6 | 0.0253 7 | 0.0262 8 | -0.0006 9 | 0.0418 10 | 0.0013 11 | 0.0046 12 | 0.0544 13 | -0.0234 14 | 0.0726 15 | 0.0197 16 | 0.0476 17 | -0.0431 18 | 0.0658 19 | 0.0209 20 | -0.0068 21 | -0.017 22 | 0.0881 23 | 0.0423 24 | 0.0152 25 | -0.0485 26 | 0.0062 27 | 0.0668 28 | 0.0288 29 | 0.0133 30 | 0.1181 31 | 0.0036 32 | 0.0466 33 | -0.0034 34 | -0.0089 35 | 0.0143 36 | -0.0639 37 | 0.097 38 | 0.0446 39 | 0.0818 40 | -0.0547 41 | -0.2012 42 | -0.1274 43 | 0.0133 44 | 0.0561 45 | 0.025 46 | 0.071 47 | -0.0206 48 | -0.0166 49 | -0.1627 50 | 0.0412 51 | 0.003 52 | -0.1275 53 | -0.0878 54 | -0.0304 55 | -0.0783 56 | 0.0624 57 | 0.1088 58 | -0.0643 59 | -0.0998 60 | -0.1324 61 | 0.139 62 | -0.0662 63 | 0.0041 64 | -0.2913 65 | 0.0804 66 | -0.0908 67 | -0.1353 68 | -0.0158 69 | 0.0546 70 | -0.1121 71 | -0.1796 72 | -0.2051 73 | -0.007 74 | 0.3384 75 | 0.3706 76 | -0.0294 77 | -0.1317 78 | -0.0588 79 | 0.044 80 | 0.0125 81 | -0.1524 82 | 0.0329 83 | 0.3885 84 | 0.2143 85 | 0.1311 86 | -0.0963 87 | 0.1205 88 | -0.1065 89 | -0.0836 90 | 0.0997 91 | 0.0183 92 | 0.126 93 | -0.025 94 | 0.0009 95 | -0.0179 96 | -0.0725 97 | 0.0264 98 | -0.1096 99 | 0.0558 100 | -0.0023 101 | -0.0166 102 | 0.0833 103 | 0.0036 104 | -0.0345 105 | -0.0194 106 | -0.0368 107 | 0.0906 108 | 0.0347 109 | 0.0593 110 | 0.0751 111 | 0.0265 112 | 0.0263 113 | 0.0703 114 | 0.0488 115 | 0.0456 116 | 0.0689 117 | 0.0249 118 | 0.0099 119 | -0.0814 120 | 0.0519 121 | 0.024 122 | 0.0667 123 | 0.0099 124 | 0.0098 125 | 0.0712 126 | 0.0327 127 | 0.0021 128 | 0.0335 129 | 0.0109 130 | -0.0027 131 | -0.0736 132 | -0.0083 133 | -0.0421 134 | 0.0891 135 | -0.0486 136 | -0.1361 137 | -0.0961 138 | -0.0831 139 | -0.0424 140 | 0.0049 141 | 0.0584 142 | -0.2382 143 | 0.1451 144 | -0.0383 145 | 0.2387 146 | 0.0734 147 | -0.0267 148 | 0.0081 149 | 0.078 150 | -0.0172 151 | 0.0419 152 | -0.0596 153 | 0.0351 154 | -0.1199 155 | -0.0018 156 | 0.068 157 | -0.0531 158 | 0.1024 159 | -0.0668 160 | 0.1688 161 | -0.0053 162 | -0.0362 163 | 0.0303 164 | -0.0241 165 | 0.0144 166 | 0.0205 167 | 0.0022 168 | -0.2195 169 | 0.0667 170 | 0.0316 171 | 0.0219 172 | 0.0239 173 | 0.0302 174 | -0.0161 175 | 0.0069 176 | -0.0417 177 | -0.0143 178 | 0.0084 179 | -0.0546 180 | 0.0139 181 | 0.0583 182 | 0.0587 183 | -0.0017 184 | -0.0087 185 | -0.0525 186 | -0.0192 187 | -0.0487 188 | 0.0079 189 | -0.0246 190 | -0.0658 191 | -0.0437 192 | 0.0594 193 | 0.0269 194 | 0.0351 195 | 0.018 196 | 0.0261 197 | 0.0682 198 | 0.0015 199 | 0.0512 200 | 0.0713 201 | 0.0615 202 | 0.0601 203 | 0.0081 204 | 0.0574 205 | 0.0182 206 | -0.0477 207 | 0.013 208 | 0.024 209 | -0.0115 210 | -0.0591 211 | 0.0636 212 | 0.0174 213 | 0.0037 214 | 0.0246 215 | -0.0169 216 | 0.0507 217 | 0.0549 218 | -0.0149 219 | 0.0157 220 | 0.0001 221 | 0.0016 222 | 0.0171 223 | 0.0403 224 | 0.0201 225 | 0.0623 226 | -0.0389 227 | 0.078 228 | 0.0173 229 | 0.0039 230 | -0.0217 231 | 0.062 232 | 0.0477 233 | 0.0389 234 | 0.0539 235 | 0.012 236 | 0.0624 237 | -0.0583 238 | 0.0587 239 | 0.0423 240 | 0.0393 241 | -0.0389 242 | -0.0269 243 | -0.0644 244 | -0.1017 245 | -0.0144 246 | -0.0001 247 | 0.0496 248 | 0.0125 249 | -0.0108 250 | -0.0167 251 | -0.048 252 | -0.0097 253 | 0.0529 254 | 0.0414 255 | -0.0174 256 | -0.0054 257 | 0.0247 258 | -0.0197 259 | 0.03 260 | -0.0393 261 | -0.0438 262 | 0.0807 263 | 0.0365 264 | 0.073 265 | -0.001 266 | -0.0509 267 | 0.0025 268 | -0.0297 269 | 0.0596 270 | -0.093 271 | 0.0326 272 | 0.0023 273 | -0.0293 274 | 0.0404 275 | -0.0187 276 | -0.0294 277 | 0.001 278 | 0.0554 279 | 0.026 280 | 0.0309 281 | 0.0314 282 | 0.0182 283 | 0.0513 284 | 0.017 285 | 0.0148 286 | 0.0126 287 | 0.0394 288 | 0.0431 289 | -0.0594 290 | 0.0136 291 | 0.0485 292 | 0.0481 293 | -0.0018 294 | 0.0276 295 | 0.0554 296 | 0.057 297 | 0.0141 298 | -0.0215 299 | 0.0486 300 | -0.0234 301 | -0.0262 302 | 0.0694 303 | 0.0427 304 | 0.007 305 | -0.0253 306 | 0.0057 307 | 0.0333 308 | 0.0145 309 | -0.0262 310 | 0.0444 311 | -0.0497 312 | 0.032 313 | 0.0383 314 | 0.0091 315 | -0.0076 316 | -0.0203 317 | -0.0066 318 | 0.0594 319 | 0.0293 320 | -0.0034 321 | -0.0027 322 | -0.0143 323 | -0.0283 324 | 0.0052 325 | -0.0189 326 | 0.024 327 | -0.0452 328 | 0.002 329 | 0.046 330 | 0.0283 331 | 0.0003 332 | 0.0513 333 | 0.0167 334 | 0.0365 335 | 0.0427 336 | 0.0309 337 | 0.0107 338 | 0.0499 339 | -0.0234 340 | 0.0639 341 | -0.0167 342 | 0.0938 343 | 0.0548 344 | 0.006 345 | 0.0302 346 | -0.0016 347 | 0.0311 348 | 0.0093 349 | 0.0655 350 | 0.019 351 | 0.0021 352 | -0.0036 353 | -0.0268 354 | 0.0703 355 | 0.0149 356 | -0.0303 357 | 0.0377 358 | 0.0664 359 | 0.0028 360 | -0.052 361 | 0.0348 362 | 0.0484 363 | -0.0318 364 | -0.0514 365 | 0.0052 366 | 0.0036 367 | 0.0316 368 | -0.0358 369 | -0.0206 370 | 0.0213 371 | 0.0426 372 | 0.0345 373 | -0.0074 374 | 0.0066 375 | -0.0511 376 | -0.0598 377 | -0.0432 378 | 0.023 379 | -0.0391 380 | 0.0466 381 | -0.0152 382 | 0.0327 383 | 0.0309 384 | 0.0231 385 | 0.0293 386 | 0.0439 387 | 0.0191 388 | 0.0466 389 | 0.0253 390 | 0.0301 391 | 0.0515 392 | 0.0071 393 | 0.0095 394 | 0.0028 395 | 0.0366 396 | 0.0173 397 | -0.0025 398 | 0.0317 399 | -0.0139 400 | -0.048 401 | 0.0128 402 | 0.016 403 | 0.0245 404 | -0.0698 405 | 0.0117 406 | -0.0163 407 | -0.0171 408 | 0.0312 409 | 0.0208 410 | -0.0237 411 | 0.0301 412 | -0.0599 413 | -0.0071 414 | 0.0469 415 | 0.0471 416 | 0.062 417 | 0.0357 418 | 0.0289 419 | 0.0029 420 | 0.024 421 | -0.0308 422 | 0.0283 423 | 0.0257 424 | -0.0215 425 | 0.0257 426 | 0.0445 427 | -0.0018 428 | -0.0387 429 | 0.0181 430 | -0.0068 431 | -0.0659 432 | -0.0865 433 | -0.0847 434 | 0.0628 435 | 0.0213 436 | -0.0522 437 | -0.0005 438 | 0.1087 439 | 0.0101 440 | 0.0493 441 | -0.0238 442 | 0.0308 443 | 0.0451 444 | 0.0176 445 | -0.02 446 | -0.0039 447 | 0.0507 448 | -0.0157 449 | 0.0253 450 | -0.0085 451 | 0.0183 452 | 0.0224 453 | 0.0154 454 | 0.0141 455 | 0.001 456 | 0.0142 457 | 0.0127 458 | 0.0174 459 | -0.0144 460 | 0.0269 461 | 0.0059 462 | 0 463 | 0.0003 464 | 0.0354 465 | 0.0044 466 | -0.0134 467 | 0.0311 468 | -0.0077 469 | -0.0551 470 | 0.0143 471 | 0.0273 472 | 0.0286 473 | 0.026 474 | -0.0003 475 | 0.0101 476 | 0.0072 477 | -0.0121 478 | -0.0251 479 | 0.0214 480 | -0.0566 481 | -0.0144 482 | -0.0163 483 | -0.0791 484 | -0.0106 485 | 0.0386 486 | 0.014 487 | 0.0013 488 | 0.0815 489 | 0.0078 490 | 0.0399 491 | 0.0389 492 | -0.0433 493 | 0.0241 494 | 0.0458 495 | -0.0089 496 | 0.0311 497 | -0.0309 498 | 0.0037 499 | 0.0305 500 | -0.0406 501 | -0.0375 502 | 0.002 503 | 0.0905 504 | 0.0228 505 | 0.0069 506 | -0.0272 507 | 0.0134 508 | 0.0403 509 | 0.0042 510 | 0.0543 511 | -0.0394 512 | -0.0125 513 | -0.0584 514 | 0.0264 515 | 0.0146 516 | -0.001 517 | -0.0718 518 | -0.07 519 | 0.0468 520 | -0.0298 521 | 0.0506 522 | -0.0379 523 | -0.0263 524 | -0.081 525 | 0.0513 526 | -0.0106 527 | -0.11 528 | -0.0692 529 | -0.0579 530 | 0.0693 531 | 0.0449 532 | 0.0418 533 | -0.0228 534 | 0.0459 535 | 0.0572 536 | 0.0484 537 | 0.0141 538 | 0.0413 539 | 0.0315 540 | -0.0398 541 | -0.001 542 | -0.045 543 | 0.0379 544 | -0.0085 545 | -0.0442 546 | -0.0046 547 | 0.0871 548 | 0.0249 549 | 0.0287 550 | 0.0063 551 | 0.0029 552 | 0.0125 553 | -0.0243 554 | -0.008 555 | 0.0326 556 | -0.0114 557 | 0.0052 558 | 0.046 559 | 0.0062 560 | -0.0329 561 | -0.0485 562 | -0.013 563 | -0.0568 564 | -0.0294 565 | -0.0156 566 | 0.0505 567 | -0.0382 568 | 0.0475 569 | -0.0083 570 | -0.1275 571 | 0.0061 572 | -0.0017 573 | -0.0047 574 | -0.0281 575 | -0.0529 576 | -0.0468 577 | -0.0283 578 | -0.0805 579 | -0.0935 580 | -0.1177 581 | 0.161 582 | -0.0451 583 | -0.0345 584 | 0.1366 585 | 0.0556 586 | 0.0266 587 | 0.0423 588 | 0.0519 589 | 0.0483 590 | -0.0659 591 | -0.0285 592 | -0.0426 593 | 0.0531 594 | 0.0265 595 | -0.016 596 | 0.1216 597 | 0.0032 598 | 0.0233 599 | -0.0149 600 | -0.0134 601 | 0.0405 602 | -0.0107 603 | -0.0056 604 | 0.0206 605 | -0.0242 606 | 0.0036 607 | 0.0565 608 | -0.0405 609 | -0.0195 610 | -0.0137 611 | 0.0015 612 | -0.0146 613 | 0.0471 614 | -0.0169 615 | -0.0175 616 | -0.0027 617 | -0.0438 618 | 0.04 619 | 0.0027 620 | -0.0601 621 | -0.0138 622 | 0.0285 623 | 0.0788 624 | 0.0176 625 | -0.0169 626 | 0.0511 627 | 0.0375 628 | -0.0143 629 | -0.1191 630 | 0.0271 631 | 0.0088 632 | 0.0423 633 | -0.0356 634 | 0.0568 635 | -0.0006 636 | -0.0221 637 | 0.0385 638 | 0.0082 639 | 0.0553 640 | -0.0082 641 | -0.081 642 | 0.0521 643 | 0.0179 644 | 0.0551 645 | -0.0122 646 | -0.129 647 | 0.0397 648 | 0.0526 649 | 0.0306 650 | 0.0649 651 | 0.018 652 | 0.0219 653 | 0.0106 654 | 0.0959 655 | -0.0452 656 | -0.0504 657 | 0.0056 658 | 0.0356 659 | -0.021 660 | 0.0011 661 | -0.0236 662 | -0.0154 663 | -0.0703 664 | -0.0717 665 | 0.0492 666 | 0.0336 667 | -0.0365 668 | -0.0324 669 | -0.0586 670 | -0.0187 671 | 0.0327 672 | -0.0399 673 | -0.0309 674 | -0.0319 675 | 0.1114 676 | 0.0129 677 | 0.113 678 | 0.0467 679 | 0.0055 680 | 0.036 681 | 0.0259 682 | 0.0282 683 | 0.0667 684 | 0.0052 685 | 0.0307 686 | -0.0407 687 | -0.005 688 | 0.0091 689 | -0.0344 690 | 0.0216 691 | -0.0178 692 | -0.0192 693 | -0.0482 694 | 0.0063 695 | -0.0052 696 | -0.0597 697 | 0.0182 698 | -0.0274 699 | 0.1028 700 | -0.008 701 | -0.0084 702 | -0.0176 703 | 0.0184 704 | 0.0799 705 | 0.0122 706 | -0.0084 707 | -0.0096 708 | 0.0509 709 | 0.0127 710 | -0.0074 711 | -0.0102 712 | -0.0454 713 | 0.0402 714 | 0.0648 715 | 0.0388 716 | 0.0065 717 | 0.0713 718 | 0.0488 719 | -0.0131 720 | 0.0462 721 | 0.0103 722 | -0.0645 723 | 0.0607 724 | -0.086 725 | 0.0466 726 | 0.0117 727 | -0.0327 728 | 0.1247 729 | 0.0439 730 | 0.0164 731 | -0.0211 732 | 0.0011 733 | 0.0394 734 | 0.0385 735 | 0.0352 736 | -0.0259 737 | -0.2324 738 | -0.0777 739 | 0.0681 740 | 0.0421 741 | 0.0475 742 | -0.0227 743 | 0.0056 744 | -0.0029 745 | 0.0479 746 | -0.0125 747 | -0.0331 748 | 0.033 749 | 0.0115 750 | -0.0229 751 | 0.0149 752 | 0.061 753 | -0.0225 754 | 0.0157 755 | 0.0433 756 | 0.0335 757 | -0.0135 758 | 0.072 759 | 0.0144 760 | -0.0076 761 | -0.0367 762 | 0.0103 763 | 0.0116 764 | -0.0785 765 | 0.0111 766 | 0.0183 767 | -0.0336 768 | 0.0842 769 | -0.0109 770 | -0.019 771 | -0.1015 772 | -0.0612 773 | -0.0192 774 | 0.0635 775 | 0.0246 776 | 0.0469 777 | 0.0719 778 | 0.0265 779 | -0.0028 780 | 0.0365 781 | -0.0494 782 | 0.0424 783 | 0.0232 784 | -0.0159 785 | 0.0129 786 | -0.0419 787 | 0.1084 788 | -0.0059 789 | 0.0109 790 | -0.0266 791 | 0.0107 792 | 0.003 793 | -0.0234 794 | 0.0377 795 | -0.0238 796 | 0.0119 797 | 0.0102 798 | 0.0413 799 | 0.0153 800 | 0.0093 801 | 0.0012 802 | 0.023 803 | -0.0305 804 | 0.0289 805 | 0.0031 806 | -0.0034 807 | 0.0371 808 | -0.0012 809 | 0.0141 810 | -0.0189 811 | 0.0165 812 | 0.0287 813 | -0.0255 814 | -0.0478 815 | 0.0068 816 | 0.0058 817 | -0.0303 818 | 0.0282 819 | 0.0401 820 | -0.0231 821 | 0.0134 822 | -0.0404 823 | 0.0086 824 | 0.018 825 | 0.0363 826 | 0.0219 827 | 0.0211 828 | 0.029 829 | 0.0272 830 | 0.0372 831 | 0.0055 832 | 0.0335 833 | -0.0152 834 | 0.0396 835 | 0.0103 836 | 0.0226 837 | 0.0133 838 | 0.0073 839 | 0.0206 840 | 0.0236 841 | -0.0114 842 | -0.0597 843 | 0.0277 844 | 0.0501 845 | 0.0086 846 | 0.0625 847 | -0.017 848 | 0.0498 849 | -0.0049 850 | -0.0502 851 | 0.0404 852 | 0.0674 853 | 0.041 854 | 0.0733 855 | -0.0415 856 | 0.0535 857 | -0.038 858 | 0.0298 859 | 0.0132 860 | 0.0015 861 | 0.0704 862 | 0.0476 863 | 0.0073 864 | -0.0307 865 | 0.0318 866 | -0.0246 867 | -0.1608 868 | 0.0615 869 | 0.0713 870 | 0.061 871 | 0.0616 872 | 0.035 873 | -0.0408 874 | 0.0345 875 | 0.0433 876 | -0.0246 877 | 0.0477 878 | -0.0347 879 | -0.0138 880 | -0.0281 881 | 0.0613 882 | 0.0337 883 | 0.0772 884 | -0.0474 885 | 0.0245 886 | 0.052 887 | -0.064 888 | -0.0442 889 | 0.0464 890 | -0.0251 891 | 0.0703 892 | -0.0545 893 | -0.0276 894 | -0.1072 895 | 0.0119 896 | 0.0313 897 | -0.1005 898 | -0.0726 899 | 0.0794 900 | 0.0072 901 | -0.0194 902 | -0.0213 903 | -0.0646 904 | -0.0925 905 | 0.0246 906 | 0.0754 907 | 0.0161 908 | -0.0144 909 | -0.0229 910 | 0.0424 911 | -0.052 912 | -0.0138 913 | -0.0721 914 | -0.0818 915 | 0.005 916 | -0.1035 917 | 0.0784 918 | 0.0596 919 | -0.0576 920 | -0.0257 921 | -0.0188 922 | 0.0109 923 | 0.0822 924 | 0.0605 925 | 0.0142 926 | 0.0235 927 | 0.0234 928 | -0.0124 929 | 0.0608 930 | 0.0135 931 | 0.0429 932 | 0.0215 933 | 0.014 934 | -0.0132 935 | -0.0183 936 | 0.0117 937 | 0.0186 938 | -0.0406 939 | 0.0008 940 | 0.016 941 | 0.0143 942 | 0.0454 943 | 0.0343 944 | -0.0276 945 | 0.0189 946 | -0.0197 947 | -0.0261 948 | 0.0365 949 | 0.0057 950 | 0.0392 951 | -0.0122 952 | 0.0049 953 | -0.0202 954 | 0.0361 955 | -0.0025 956 | 0.0304 957 | -0.003 958 | 0.0146 959 | 0.0073 960 | -0.0357 961 | -0.0035 962 | -0.0078 963 | 0.0203 964 | 0.0184 965 | 0.0323 966 | 0.0171 967 | 0.0087 968 | 0.014 969 | -0.0196 970 | 0.0068 971 | 0.0349 972 | 0.0324 973 | -0.0196 974 | -0.0373 975 | 0.0092 976 | 0.0322 977 | 0.018 978 | -0.0483 979 | -0.0087 980 | -0.0636 981 | -0.0309 982 | -0.0093 983 | 0.046 984 | 0.0186 985 | -0.0844 986 | -0.0077 987 | 0.0153 988 | -0.0924 989 | -0.1723 990 | -0.0786 991 | 0.0174 992 | -0.0812 993 | -0.101 994 | 0.0895 995 | 0.1019 996 | 0.0521 997 | 0.0043 998 | 0.0772 999 | 0.0333 1000 | 0.0408 1001 | -0.0259 1002 | 0.0556 1003 | 0.0275 1004 | -0.0336 1005 | 0.034 1006 | 0.0631 1007 | 0.02 1008 | -0.0789 1009 | -0.0556 1010 | 0.0693 1011 | -0.0477 1012 | 0.0954 1013 | 0.0388 1014 | 0.006 1015 | 0.0682 1016 | 0.0199 1017 | 0.0349 1018 | 0.0045 1019 | 0.029 1020 | -0.0127 1021 | -0.0175 1022 | -0.0236 1023 | -0.0599 1024 | -0.0759 1025 | 0.1135 1026 | -0.0028 1027 | 0.0074 1028 | 0.0505 1029 | 0.0442 1030 | 0.0311 1031 | -0.0085 1032 | -0.0619 1033 | 0.0389 1034 | 0.0079 1035 | 0.0255 1036 | 0.0273 1037 | -0.0176 1038 | 0.0078 1039 | 0.0118 1040 | 0.0557 1041 | 0.0129 1042 | 0.0403 1043 | 0.0155 1044 | 0.028 1045 | -0.012 1046 | 0.0565 1047 | -0.0271 1048 | 0.0377 1049 | 0.0418 1050 | 0.0312 1051 | 0.0281 1052 | -0.0332 1053 | 0.0465 1054 | 0.0043 1055 | -0.0019 1056 | 0.0206 1057 | 0.0261 1058 | -0.0204 1059 | 0.0424 1060 | -0.0197 1061 | 0.0252 1062 | 0.0255 1063 | -0.0006 1064 | -0.0311 1065 | 0.0613 1066 | -0.0112 1067 | 0.0059 1068 | 0.0136 1069 | -0.0153 1070 | 0.0154 1071 | -0.0604 1072 | -0.0308 1073 | 0.0775 1074 | 0.0056 1075 | -0.0217 1076 | -0.0577 1077 | -0.0007 1078 | 0.0696 1079 | 0.0092 1080 | 0.0178 1081 | -0.0005 1082 | 0.0395 1083 | 0.005 1084 | 0.0025 1085 | -0.0202 1086 | 0.0486 1087 | 0.0182 1088 | 0.0194 1089 | 0.0357 1090 | 0.0017 1091 | 0.0109 1092 | 0.0106 1093 | 0.0078 1094 | 0.0187 1095 | 0.0016 1096 | 0.0251 1097 | 0.0225 1098 | 0.0312 1099 | 0.0106 1100 | 0.0557 1101 | -------------------------------------------------------------------------------- /SMC_Inference_BEGE_model/SMC_RW_LikeAnneal_BEGE_novec_nopar.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file SMC_RW_LikeAnneal_BEGE.c 3 | * @brief Demonstration of vectorisationn and parallelisation for inference on 4 | * the Bad Environment, Good Evironment (BEGE) model. 5 | * 6 | * @details Random walk adaptive SMC with likelihood annealing with an expensive 7 | * likelihood. 8 | * 9 | * @author Chris C. Drovandi (c.drovandi@qut.edu.au) 10 | * School of Mathematical Sciences 11 | * Queensland University of Technology 12 | * @author David J. Warne (david.warne@qut.edu.au) 13 | * School of Mathematical Sciences 14 | * Queensland University of Technology 15 | * 16 | * @date 4 Feb 2019 17 | */ 18 | 19 | #include 20 | #include 21 | #include 22 | 23 | /* Intel headers */ 24 | #include 25 | #include 26 | #include 27 | 28 | /* OpenMP header */ 29 | #include 30 | 31 | /* length of vector processing units and ideal memory alignement*/ 32 | #if defined(__AVX512BW__) 33 | #define VECLEN 8 34 | #define ALIGN 64 35 | #elif defined(__AVX2__) 36 | #define VECLEN 4 37 | #define ALIGN 64 38 | #elif defined(__AVX__) 39 | #define VECLEN 4 40 | #define ALIGN 32 41 | #elif defined(__SSE4_2__) 42 | #define VECLEN 2 43 | #define ALIGN 16 44 | #endif 45 | 46 | 47 | #define ITMAX 100 /*maximum allowed number of iterations*/ 48 | #define EPS 3.0e-7 /*relative accuracy*/ 49 | 50 | #define FPMIN 1.0e-30 /*number near the smallest representable 51 | floating point number*/ 52 | 53 | #define NUM_PARAM 11 54 | 55 | /*function prototypes for gamma function and incomplete gamma functions*/ 56 | 57 | /* error message function*/ 58 | void 59 | nrerror(char *); 60 | 61 | /* series approximation @note could be a challenge to vectorise */ 62 | void 63 | gser_vec(double * restrict, double * restrict, double * restrict, double * restrict); 64 | 65 | /* compute P(a,x) = \gamma(a,x)/Gamma(a)*/ 66 | void 67 | gammp_vec(double, double, int, double * restrict, double * restrict); 68 | 69 | /* compute Q(a,x) = 1 - P(a,x) = \Gamma(a,x)/Gamma(a)*/ 70 | void 71 | gammq_vec(double, double, int, double * restrict, double * restrict); 72 | 73 | /* compute ln Gamma(a) @note possibly use the intel imf lgamma*/ 74 | void 75 | gammln_vec(double * restrict, double * restrict); 76 | 77 | double* 78 | loadData(char *, int *); 79 | 80 | double 81 | compute_ESS_diff(double, double, double * restrict, double * restrict, 82 | unsigned int); 83 | 84 | double 85 | SMC_RW_LikeAnneal(VSLStreamStatePtr, unsigned int, int,double* restrict, 86 | double * restrict, double * restrict, double* restrict); 87 | 88 | double 89 | log_prior(double * restrict, double * restrict, double * restrict); 90 | 91 | double 92 | bege_gjrgarch_likelihood(double * restrict, int, double * restrict, 93 | double * restrict, double * restrict); 94 | double 95 | loglikedgam_vec(double, double, double, double, double, double); 96 | 97 | double 98 | quantile(unsigned int, double *,double); 99 | 100 | /** 101 | * @brief main entry point of program 102 | */ 103 | int 104 | main(int argc, char ** argv) 105 | { 106 | char *filename; 107 | double *rate_return, *theta; 108 | int ndat; 109 | unsigned int seed; 110 | int N, NUM_PARTICLES; 111 | 112 | /* Intel MKL VSL random stream */ 113 | VSLStreamStatePtr stream; 114 | 115 | /*limits for uniform priors*/ 116 | __declspec(align(ALIGN)) double prior_l[NUM_PARAM] = {1e-4,1e-4,1e-4,1e-4, 117 | 1e-4,1e-4,1e-4,1e-4,-0.2,1e-4,-0.9}; 118 | __declspec(align(ALIGN)) double prior_u[NUM_PARAM] = { 0.5, 0.3,0.99, 0.5, 119 | 0.5, 1.0, 0.3,0.99, 0.1,0.75, 0.9}; 120 | 121 | if (argc < 4) 122 | { 123 | fprintf(stderr,"Usage: [%s] N datafile seed\n",argv[0]); 124 | exit(1); 125 | } 126 | NUM_PARTICLES = (int)atoi(argv[1]); 127 | filename = argv[2]; 128 | seed = (unsigned int)atoi(argv[3]); 129 | 130 | /* load finance data */ 131 | rate_return = loadData(filename, &ndat); 132 | 133 | /* to ensure that particles pack neatly into vectors with no leftovers*/ 134 | if (NUM_PARTICLES%VECLEN == 0) 135 | { 136 | N = NUM_PARTICLES; 137 | } 138 | else 139 | { 140 | N = ((NUM_PARTICLES / VECLEN) + 1)*VECLEN; 141 | } 142 | fprintf(stderr,"Particles: %d, Vector length: %d\n",N,VECLEN); 143 | 144 | /* allocate memory for particles */ /**@note theta[N][NUM_PARAM]*/ 145 | theta = (double*)_mm_malloc(NUM_PARAM*N*sizeof(double),ALIGN); 146 | 147 | /*initialise RNG */ 148 | vslNewStream(&stream,VSL_BRNG_MT2203,seed); 149 | 150 | /* perform Random Walk SMC with Likelihood Annealling*/ 151 | SMC_RW_LikeAnneal(stream, N, ndat, rate_return, prior_l, prior_u, theta); 152 | 153 | /*output particles*/ 154 | for (int i=0;i w_max) ? logw[i] : w_max; 481 | } 482 | w_sum = 0; 483 | w_sum2 = 0; 484 | for (int i=0;i= Nf/2.0) 500 | { 501 | gamma_tp1 = 1.0; 502 | } 503 | else 504 | { 505 | double a = 0; 506 | double b = 0; 507 | double p = 0; 508 | double fa = 0; 509 | double fp = 0; 510 | double err = 0; 511 | 512 | /* find optimum temperature stepp using bisection method */ 513 | a = gamma_t + 1e-6; 514 | b = 1.0; 515 | p = (a + b)/2.0; 516 | fp = compute_ESS_diff(p,gamma_t,w_temp,loglike,N); 517 | err = fabs(fp); 518 | while (err > 1e-5) 519 | { 520 | fa = compute_ESS_diff(a,gamma_t,w_temp,loglike,N); 521 | if (fa*fp < 0) 522 | { 523 | b = p; 524 | } 525 | else 526 | { 527 | a = p; 528 | } 529 | p = (a + b)/2.0; 530 | fp = compute_ESS_diff(p,gamma_t,w_temp,loglike,N); 531 | err = fabs(fp); 532 | } 533 | gamma_tp1 = p; 534 | } 535 | 536 | fprintf(stderr,"gamma(t) = %g next gamma(t+1) = %g\n",gamma_t,gamma_tp1); 537 | /* substitute the value of just calculated gamma */ 538 | for (int i=0;i w_max) ? logw[i] : w_max; 547 | } 548 | w_sum = 0; 549 | w_sum2 = 0; 550 | for (int i=0;i w_sum3 && k < N-1) 573 | { 574 | k++; 575 | w_sum3 += w[k]; 576 | } 577 | r[i] = k; 578 | } 579 | 580 | /* re-assign particles*/ 581 | for (int i=0;i median_ESJD[ind]) 740 | { 741 | ind = j; 742 | } 743 | } 744 | 745 | h_opt = h[ind]; 746 | fprintf(stderr,"The scale is %f\n",h_opt); 747 | 748 | memset(dist_move,0,N*sizeof(double)); 749 | belowThreshold = 1; 750 | R_move = 0; 751 | 752 | 753 | fprintf(stderr,"MCMC proposals for particle mutation...\n"); 754 | /* Performing remaining repeats */ 755 | while (belowThreshold) 756 | { 757 | int sum_cond = 0;; 758 | R_move++; 759 | /*generate N MCMC proposals*/ 760 | memset(mu,0,NUM_PARAM*sizeof(double)); 761 | vdRngGaussianMV(VSL_RNG_METHOD_GAUSSIANMV_BOXMULLER2,stream,N, 762 | theta_particle_prop, NUM_PARAM, VSL_MATRIX_STORAGE_FULL,mu,T); 763 | /* N uniforms for accept/reject step*/ 764 | vdRngUniform(VSL_RNG_METHOD_UNIFORM_STD,stream,N,u,0,1); 765 | for (int i=0;i median_dist); 820 | } 821 | belowThreshold = (sum_cond < (int)ceil(Nf*0.5)); 822 | } 823 | fprintf(stderr,"The value of R_move was %d\n",R_move); 824 | 825 | gamma_t = gamma_tp1; 826 | } 827 | 828 | /* transform theta*/ 829 | for (int i=0;i 0) ? sum[i]*exp(-x[i] + a[i]*log(x[i]) - (gln[i])) : 0.0; 1156 | } 1157 | return; 1158 | } 1159 | } 1160 | 1161 | 1162 | /** 1163 | * @brief computes P(a,x[1:N]/b) = \gamma(a,x[1:N]/b)/\Gamma(a) 1164 | * @note assumes the array x contains a monotonically increasing sequence. 1165 | */ 1166 | void 1167 | gammp_vec(double a, double b, int N, double * restrict x, double * restrict P) 1168 | { 1169 | __declspec(align(ALIGN)) double _a[VECLEN]; 1170 | for(int i=0;i= 0 && x[j] > A) 1239 | { 1240 | x[j+1] = x[j]; 1241 | j--; 1242 | } 1243 | x[j+1] = A; 1244 | } 1245 | } 1246 | 1247 | /** 1248 | * @brief compute the q-quantile 1249 | * 1250 | * @param n number of samples 1251 | * @param x samples 1252 | * @param q the q-th quantile level 1253 | * @return the value of quantile 1254 | */ 1255 | double 1256 | quantile(unsigned int n, double *x,double q) 1257 | { 1258 | double u,l; 1259 | unsigned int i; 1260 | /*sort samples */ 1261 | insertionSort(n,x); 1262 | 1263 | /* pick the samples closes to the q quantile*/ 1264 | 1265 | /* upper and lower bound of the quantile value*/ 1266 | u = ceil(((double)n)*q); 1267 | l = floor(((double)n)*q); 1268 | 1269 | /* nearest neighbour interpolation */ 1270 | i = (unsigned int)((u - ((double)n)*q < ((double)n)*q - l) ? u : l); 1271 | return x[i]; 1272 | } 1273 | 1274 | 1275 | -------------------------------------------------------------------------------- /SMC_Inference_BEGE_model/SMC_RW_LikeAnneal_BEGE_vec_nopar.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file SMC_RW_LikeAnneal_BEGE.c 3 | * @brief Demonstration of vectorisationn and parallelisation for inference on 4 | * the Bad Environment, Good Evironment (BEGE) model. 5 | * 6 | * @details Random walk adaptive SMC with likelihood annealing with an expensive 7 | * likelihood. 8 | * 9 | * @author Chris C. Drovandi (c.drovandi@qut.edu.au) 10 | * School of Mathematical Sciences 11 | * Queensland University of Technology 12 | * @author David J. Warne (david.warne@qut.edu.au) 13 | * School of Mathematical Sciences 14 | * Queensland University of Technology 15 | * 16 | * @date 4 Feb 2019 17 | */ 18 | 19 | #include 20 | #include 21 | #include 22 | 23 | /* Intel headers */ 24 | #include 25 | #include 26 | #include 27 | 28 | /* OpenMP header */ 29 | #include 30 | 31 | /* length of vector processing units and ideal memory alignement*/ 32 | #if defined(__AVX512BW__) 33 | #define VECLEN 8 34 | #define ALIGN 64 35 | #elif defined(__AVX2__) 36 | #define VECLEN 4 37 | #define ALIGN 64 38 | #elif defined(__AVX__) 39 | #define VECLEN 4 40 | #define ALIGN 32 41 | #elif defined(__SSE4_2__) 42 | #define VECLEN 2 43 | #define ALIGN 16 44 | #endif 45 | 46 | 47 | #define ITMAX 100 /*maximum allowed number of iterations*/ 48 | #define EPS 3.0e-7 /*relative accuracy*/ 49 | 50 | #define FPMIN 1.0e-30 /*number near the smallest representable 51 | floating point number*/ 52 | 53 | #define NUM_PARAM 11 54 | 55 | /*function prototypes for gamma function and incomplete gamma functions*/ 56 | 57 | /* error message function*/ 58 | void 59 | nrerror(char *); 60 | 61 | /* series approximation @note could be a challenge to vectorise */ 62 | void 63 | gser_vec(double * restrict, double * restrict, double * restrict, double * restrict); 64 | 65 | /* compute P(a,x) = \gamma(a,x)/Gamma(a)*/ 66 | void 67 | gammp_vec(double, double, int, double * restrict, double * restrict); 68 | 69 | /* compute Q(a,x) = 1 - P(a,x) = \Gamma(a,x)/Gamma(a)*/ 70 | void 71 | gammq_vec(double, double, int, double * restrict, double * restrict); 72 | 73 | /* compute ln Gamma(a) @note possibly use the intel imf lgamma*/ 74 | void 75 | gammln_vec(double * restrict, double * restrict); 76 | 77 | double* 78 | loadData(char *, int *); 79 | 80 | double 81 | compute_ESS_diff(double, double, double * restrict, double * restrict, 82 | unsigned int); 83 | 84 | double 85 | SMC_RW_LikeAnneal(VSLStreamStatePtr, unsigned int, int,double* restrict, 86 | double * restrict, double * restrict, double* restrict); 87 | 88 | double 89 | log_prior(double * restrict, double * restrict, double * restrict); 90 | 91 | double 92 | bege_gjrgarch_likelihood(double * restrict, int, double * restrict, 93 | double * restrict, double * restrict); 94 | double 95 | loglikedgam_vec(double, double, double, double, double, double); 96 | 97 | double 98 | quantile(unsigned int, double *,double); 99 | 100 | /** 101 | * @brief main entry point of program 102 | */ 103 | int 104 | main(int argc, char ** argv) 105 | { 106 | char *filename; 107 | double *rate_return, *theta; 108 | int ndat; 109 | unsigned int seed; 110 | int N, NUM_PARTICLES; 111 | 112 | /* Intel MKL VSL random stream */ 113 | VSLStreamStatePtr stream; 114 | 115 | /*limits for uniform priors*/ 116 | __declspec(align(ALIGN)) double prior_l[NUM_PARAM] = {1e-4,1e-4,1e-4,1e-4, 117 | 1e-4,1e-4,1e-4,1e-4,-0.2,1e-4,-0.9}; 118 | __declspec(align(ALIGN)) double prior_u[NUM_PARAM] = { 0.5, 0.3,0.99, 0.5, 119 | 0.5, 1.0, 0.3,0.99, 0.1,0.75, 0.9}; 120 | 121 | if (argc < 4) 122 | { 123 | fprintf(stderr,"Usage: [%s] N datafile seed\n",argv[0]); 124 | exit(1); 125 | } 126 | NUM_PARTICLES = (int)atoi(argv[1]); 127 | filename = argv[2]; 128 | seed = (unsigned int)atoi(argv[3]); 129 | 130 | /* load finance data */ 131 | rate_return = loadData(filename, &ndat); 132 | 133 | /* to ensure that particles pack neatly into vectors with no leftovers*/ 134 | if (NUM_PARTICLES%VECLEN == 0) 135 | { 136 | N = NUM_PARTICLES; 137 | } 138 | else 139 | { 140 | N = ((NUM_PARTICLES / VECLEN) + 1)*VECLEN; 141 | } 142 | fprintf(stderr,"Particles: %d, Vector length: %d\n",N,VECLEN); 143 | 144 | /* allocate memory for particles */ /**@note theta[N][NUM_PARAM]*/ 145 | theta = (double*)_mm_malloc(NUM_PARAM*N*sizeof(double),ALIGN); 146 | 147 | /*initialise RNG */ 148 | vslNewStream(&stream,VSL_BRNG_MT2203,seed); 149 | 150 | /* perform Random Walk SMC with Likelihood Annealling*/ 151 | SMC_RW_LikeAnneal(stream, N, ndat, rate_return, prior_l, prior_u, theta); 152 | 153 | /*output particles*/ 154 | for (int i=0;i w_max) ? logw[i] : w_max; 487 | } 488 | w_sum = 0; 489 | w_sum2 = 0; 490 | #pragma omp simd reduction (+:w_sum) 491 | for (int i=0;i= Nf/2.0) 508 | { 509 | gamma_tp1 = 1.0; 510 | } 511 | else 512 | { 513 | double a = 0; 514 | double b = 0; 515 | double p = 0; 516 | double fa = 0; 517 | double fp = 0; 518 | double err = 0; 519 | 520 | /* find optimum temperature stepp using bisection method */ 521 | a = gamma_t + 1e-6; 522 | b = 1.0; 523 | p = (a + b)/2.0; 524 | fp = compute_ESS_diff(p,gamma_t,w_temp,loglike,N); 525 | err = fabs(fp); 526 | while (err > 1e-5) 527 | { 528 | fa = compute_ESS_diff(a,gamma_t,w_temp,loglike,N); 529 | if (fa*fp < 0) 530 | { 531 | b = p; 532 | } 533 | else 534 | { 535 | a = p; 536 | } 537 | p = (a + b)/2.0; 538 | fp = compute_ESS_diff(p,gamma_t,w_temp,loglike,N); 539 | err = fabs(fp); 540 | } 541 | gamma_tp1 = p; 542 | } 543 | 544 | fprintf(stderr,"gamma(t) = %g next gamma(t+1) = %g\n",gamma_t,gamma_tp1); 545 | /* substitute the value of just calculated gamma */ 546 | #pragma omp simd 547 | for (int i=0;i w_max) ? logw[i] : w_max; 556 | } 557 | w_sum = 0; 558 | w_sum2 = 0; 559 | #pragma omp simd reduction (+:w_sum) 560 | for (int i=0;i w_sum3 && k < N-1) 583 | { 584 | k++; 585 | w_sum3 += w[k]; 586 | } 587 | r[i] = k; 588 | } 589 | 590 | /* re-assign particles*/ 591 | for (int i=0;i median_ESJD[ind]) 754 | { 755 | ind = j; 756 | } 757 | } 758 | 759 | h_opt = h[ind]; 760 | fprintf(stderr,"The scale is %f\n",h_opt); 761 | 762 | memset(dist_move,0,N*sizeof(double)); 763 | belowThreshold = 1; 764 | R_move = 0; 765 | 766 | 767 | fprintf(stderr,"MCMC proposals for particle mutation...\n"); 768 | /* Performing remaining repeats */ 769 | while (belowThreshold) 770 | { 771 | int sum_cond = 0;; 772 | R_move++; 773 | /*generate N MCMC proposals*/ 774 | memset(mu,0,NUM_PARAM*sizeof(double)); 775 | vdRngGaussianMV(VSL_RNG_METHOD_GAUSSIANMV_BOXMULLER2,stream,N, 776 | theta_particle_prop, NUM_PARAM, VSL_MATRIX_STORAGE_FULL,mu,T); 777 | /* N uniforms for accept/reject step*/ 778 | vdRngUniform(VSL_RNG_METHOD_UNIFORM_STD,stream,N,u,0,1); 779 | for (int i=0;i median_dist); 837 | } 838 | belowThreshold = (sum_cond < (int)ceil(Nf*0.5)); 839 | } 840 | fprintf(stderr,"The value of R_move was %d\n",R_move); 841 | 842 | gamma_t = gamma_tp1; 843 | } 844 | 845 | /* transform theta*/ 846 | for (int i=0;i 0) ? sum[i]*exp(-x[i] + a[i]*log(x[i]) - (gln[i])) : 0.0; 1192 | } 1193 | return; 1194 | } 1195 | } 1196 | 1197 | 1198 | /** 1199 | * @brief computes P(a,x[1:N]/b) = \gamma(a,x[1:N]/b)/\Gamma(a) 1200 | * @note assumes the array x contains a monotonically increasing sequence. 1201 | */ 1202 | void 1203 | gammp_vec(double a, double b, int N, double * restrict x, double * restrict P) 1204 | { 1205 | __declspec(align(ALIGN)) double _a[VECLEN]; 1206 | #pragma omp simd 1207 | for(int i=0;i= 0 && x[j] > A) 1281 | { 1282 | x[j+1] = x[j]; 1283 | j--; 1284 | } 1285 | x[j+1] = A; 1286 | } 1287 | } 1288 | 1289 | /** 1290 | * @brief compute the q-quantile 1291 | * 1292 | * @param n number of samples 1293 | * @param x samples 1294 | * @param q the q-th quantile level 1295 | * @return the value of quantile 1296 | */ 1297 | double 1298 | quantile(unsigned int n, double *x,double q) 1299 | { 1300 | double u,l; 1301 | unsigned int i; 1302 | /*sort samples */ 1303 | insertionSort(n,x); 1304 | 1305 | /* pick the samples closes to the q quantile*/ 1306 | 1307 | /* upper and lower bound of the quantile value*/ 1308 | u = ceil(((double)n)*q); 1309 | l = floor(((double)n)*q); 1310 | 1311 | /* nearest neighbour interpolation */ 1312 | i = (unsigned int)((u - ((double)n)*q < ((double)n)*q - l) ? u : l); 1313 | return x[i]; 1314 | } 1315 | 1316 | 1317 | -------------------------------------------------------------------------------- /SMC_Inference_BEGE_model/run_bench.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | DAT=MonthlyReturns2018.csv 4 | SEED=1337 5 | 6 | echo "Prior weak informativity test benchmark" 7 | 8 | #run and time vectorised parallel version 9 | echo 'timing parallel+SIMD' 10 | time ./weak_info_test_vec_par $N $K $SEED > /dev/null 11 | echo 'done' 12 | 13 | #run and time scalar parallel version 14 | # (still uses OpenMP and MKL/VSL, but SIMD and auto-vectorisation disabled) 15 | echo 'timing parallel+scalar' 16 | time ./weak_info_test_novec_par $N $K $SEED > /dev/null 17 | echo 'done' 18 | 19 | #run and time scalar sequential version 20 | # (still uses MKL/VSL, but OpenMP and auto-vectorisation disabled ) 21 | echo 'timing sequential+scalar' 22 | time ./weak_info_test_novec_nopar $N $K $SEED > /dev/null 23 | echo 'done' 24 | -------------------------------------------------------------------------------- /Weakly_Informative_Priors/Makefile: -------------------------------------------------------------------------------- 1 | 2 | all: 3 | make weak_info_test_vec_par 4 | make weak_info_test_novec_nopar 5 | make weak_info_test_novec_par 6 | 7 | weak_info_test_vec_par: weak_info_test_vec_par.c Makefile 8 | icc -mkl -qopenmp -O2 -xhost weak_info_test_vec_par.c -o weak_info_test_vec_par 9 | 10 | weak_info_test_novec_par: weak_info_test_vec_par.c Makefile 11 | icc -mkl -qopenmp -O2 -xhost -qno-openmp-simd -no-vec weak_info_test_vec_par.c -o weak_info_test_novec_par 12 | 13 | weak_info_test_novec_nopar: weak_info_test_vec_par.c Makefile 14 | icc -mkl -O2 -xhost -no-vec weak_info_test_vec_par.c -o weak_info_test_novec_nopar 15 | 16 | clean: 17 | rm -f weak_info_test_vec_par weak_info_test_novec_nopar weak_info_test_novec_par 18 | -------------------------------------------------------------------------------- /Weakly_Informative_Priors/run_bench.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | N=400 4 | K=400 5 | SEED=1337 6 | 7 | echo "Prior weak informativity test benchmark" 8 | echo "N = $N K = $K" 9 | 10 | #run and time vectorised parallel version 11 | echo 'timing parallel+SIMD' 12 | time ./weak_info_test_vec_par $N $K $SEED > /dev/null 13 | echo 'done' 14 | 15 | #run and time scalar parallel version 16 | # (still uses OpenMP and MKL/VSL, but SIMD and auto-vectorisation disabled) 17 | echo 'timing parallel+scalar' 18 | time ./weak_info_test_novec_par $N $K $SEED > /dev/null 19 | echo 'done' 20 | 21 | #run and time scalar sequential version 22 | # (still uses MKL/VSL, but OpenMP and auto-vectorisation disabled ) 23 | echo 'timing sequential+scalar' 24 | time ./weak_info_test_novec_nopar $N $K $SEED > /dev/null 25 | echo 'done' 26 | -------------------------------------------------------------------------------- /Weakly_Informative_Priors/weak_info_test_vec_par.c: -------------------------------------------------------------------------------- 1 | /* Bayesian computations using SIMD operations 2 | * Copyright (C) 2019 David J. Warne, Christopher C. Drovandi, Scott A. Sisson 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | /** 18 | * @file weakInfo_bioassy.c 19 | * @brief Demonstration of Intel Xeon Phi weakly informative prior selection. 20 | * @details The approach is based on the prior predictive p-value aproach 21 | * applied to the bioassy application. 22 | * 23 | * @author Chris C. Drovandi (c.drovandi@qut.edu.au) 24 | * School of Mathematical Sciences 25 | * Queensland University of Technology 26 | * 27 | * @author David J. Warne (david.warne@qut.edu.au) 28 | * School of Mathematical Sciences 29 | * Queensland University of Technology 30 | * 31 | * @date 13 Nov 2018 32 | */ 33 | 34 | /* standard C headers */ 35 | #include 36 | #include 37 | #include 38 | #include 39 | 40 | /* Intel headers */ 41 | #include 42 | #include 43 | 44 | /* OpenMP header */ 45 | #include 46 | 47 | /* length of vector processing units and ideal memory alignement*/ 48 | #if defined(__AVX512BW__) 49 | #define VECLEN 8 50 | #define ALIGN 64 51 | #elif defined(__AVX2__) 52 | #define VECLEN 4 53 | #define ALIGN 64 54 | #elif defined(__AVX__) 55 | #define VECLEN 4 56 | #define ALIGN 32 57 | #elif defined(__SSE4_2__) 58 | #define VECLEN 2 59 | #define ALIGN 16 60 | #endif 61 | 62 | /* macro definitions*/ 63 | #define SIZE_D 4 64 | #define SIGMA0_BASE 10.0 65 | #define SIGMA1_BASE 2.5 66 | #define NUM_PARTICLES 500 67 | 68 | /* function prototype declarations*/ 69 | void 70 | compute_pvalues(VSLStreamStatePtr, double, double, unsigned int, 71 | double * restrict, double * restrict); 72 | void 73 | simulate_bioassay(VSLStreamStatePtr, int * restrict, double , double, 74 | double * restrict); 75 | unsigned int 76 | nCk(unsigned int, unsigned int); 77 | void 78 | loglike_bioassay(unsigned int, double * restrict, int * restrict, 79 | double * restrict, double * restrict); 80 | void 81 | bvnpdf(unsigned int, double * restrict, double * restrict, double * restrict, 82 | double * restrict); 83 | double 84 | SMC_RW(VSLStreamStatePtr, unsigned int, int* restrict, double* restrict, double, 85 | double); 86 | double 87 | quantile(unsigned int, double * restrict, double); 88 | void 89 | insertionSort(unsigned int, double * restrict); 90 | double 91 | compute_ESS_diff(double, double, double * restrict, double * restrict, 92 | unsigned int); 93 | double 94 | logsumexp(double * restrict, unsigned int ); 95 | 96 | 97 | /** 98 | * @brief program entry point 99 | * 100 | * @details computes weak information test for a set of prior hyperparameters. 101 | * p-values computations are distributed across cores. Each P-value calculation 102 | * further parallelised through SIMD operations withing the SMC step. 103 | * 104 | * @param argc number of command line arguments 105 | * @param argv vector of argument strings 106 | */ 107 | int 108 | main(int argc, char ** argv) 109 | { 110 | /*dose levels (standardised)*/ 111 | __declspec(align(ALIGN)) double d[SIZE_D]; 112 | unsigned int num_datasets, sims, seed; 113 | 114 | /*prior predictive p-values*/ 115 | double *pvals; 116 | 117 | /*our hyperparameters*/ 118 | double *sigma0, *sigma1; 119 | 120 | /*Intel MKL VSL random stream */ 121 | VSLStreamStatePtr stream; 122 | 123 | /*initialise RNG stream for sampling hyperparameter space*/ 124 | vslNewStream(&stream,VSL_BRNG_MT19937,1337); 125 | 126 | /* dose data*/ 127 | d[0] = -0.86; 128 | d[1] = -0.30; 129 | d[2] = -0.05; 130 | d[3] = -0.73; 131 | 132 | /* read commandline arguments*/ 133 | if (argc < 4) 134 | { 135 | fprintf(stderr,"Usage : [%s] numdatasets sims seed\n",argv[0]); 136 | exit(1); 137 | } 138 | num_datasets = (unsigned int)atoi(argv[1]); 139 | sims = (unsigned int)atoi(argv[2]); 140 | seed = (unsigned int)atoi(argv[3]); 141 | 142 | /* allcote aligned memory for hyperparameters and p-values*/ 143 | sigma0 = (double *)_mm_malloc(sims*sizeof(double),ALIGN); 144 | sigma1 = (double *)_mm_malloc(sims*sizeof(double),ALIGN); 145 | pvals = (double *)_mm_malloc(sims*sizeof(double),ALIGN); 146 | 147 | /* sample hyperparameter space*/ 148 | vdRngUniform(VSL_RNG_METHOD_UNIFORM_STD,stream,sims,sigma0,0.1,10.0); 149 | vdRngUniform(VSL_RNG_METHOD_UNIFORM_STD,stream,sims,sigma1,0.1,20.0); 150 | 151 | /* the base prior representing current best information is appended*/ 152 | sigma0[sims-1] = SIGMA0_BASE; 153 | sigma1[sims-1] = SIGMA1_BASE; 154 | 155 | /* create threads */ 156 | #pragma omp parallel shared(num_datasets,sims,sigma0,sigma1,pvals,d) 157 | { 158 | /*Intel MKL VSL random stream*/ 159 | VSLStreamStatePtr stream_sims; 160 | int thread_id, num_threads; 161 | 162 | /* get thread information*/ 163 | thread_id = omp_get_thread_num(); 164 | num_threads = omp_get_num_threads(); 165 | 166 | /*initialise independent MT RNG stream for this thread*/ 167 | vslNewStream(&stream_sims,VSL_BRNG_MT2203+thread_id,seed); 168 | 169 | /* distribute work among threads*/ 170 | #pragma omp for schedule(guided) 171 | for (int i=0;i D(t)) where 209 | * D(t|\lambda) = 1/p(t|\lambda) and D(t) = 1/p_base(t) 210 | * p(t|\lambda) and p_base(t) are prior predictive distributions under the 211 | * priors p(\theta | \lambda) and base prior p(\theta) respectively. 212 | * 213 | * @param stream state pointer to this threads RNG 214 | * @param sigma0, sigma1 hyperparameters to test for weak informativity 215 | * @param num_datasets number of datasets to compute p-values over 216 | * @param d known parameters of forwards simulationn 217 | * @param p pointer to array to store p-value for each hyperparameter 218 | * 219 | * @note numerical overflow/underflow is avoided through computing with logs, 220 | * that is we actually compute Pr(log D(t|\lambda) < log D(t)) 221 | */ 222 | void 223 | compute_pvalues(VSLStreamStatePtr stream,double sigma0, double sigma1, 224 | unsigned int num_datasets, double * restrict d, 225 | double * restrict p) 226 | { 227 | __declspec(align(ALIGN)) int y[SIZE_D]; 228 | double *theta; 229 | double *log_evidences_base; 230 | double *log_evidences_prior; 231 | 232 | /*allocate aligned memory for prior samples and evidences*/ 233 | theta = (double*)_mm_malloc(2*num_datasets*sizeof(double),ALIGN); 234 | log_evidences_base = (double*)_mm_malloc(num_datasets*sizeof(double),ALIGN); 235 | log_evidences_prior = (double*)_mm_malloc(num_datasets*sizeof(double),ALIGN); 236 | 237 | /*compute evidences 238 | * under proposad prior based on data simulated from the base prior 239 | */ 240 | vdRngGaussian(VSL_RNG_METHOD_GAUSSIAN_BOXMULLER2,stream,num_datasets, 241 | theta,0,SIGMA0_BASE); 242 | vdRngGaussian(VSL_RNG_METHOD_GAUSSIAN_BOXMULLER2,stream,num_datasets, 243 | theta+num_datasets,0,SIGMA1_BASE); 244 | 245 | /* generate data sets and compute evidences using SMC */ 246 | for (unsigned int i=0;i= N/2*/ 674 | /*compute normalised weight of large step in gamma*/ 675 | #pragma omp simd 676 | for (unsigned int i=0;i w_max) ? w[i] : w_max; 686 | } 687 | w_sum = 0; 688 | #pragma omp simd reduction(+:w_sum) 689 | for (unsigned int i=0;i= ((double)N)/2.0) 711 | { 712 | gammavar_tp1 = 1.0; 713 | } 714 | else 715 | { 716 | /*smaller temperature step required*/ 717 | double a = 0; 718 | double b = 0; 719 | double p = 0; 720 | double fa = 0; 721 | double fp = 0; 722 | double err = 0; 723 | 724 | /* find optimum step using bisection method*/ 725 | a = gammavar_t + 1e-6; 726 | b = 1.0; 727 | p = (a+b)/2.0; 728 | fp = compute_ESS_diff(p,gammavar_t,w_temp,loglike,N); 729 | err = fabs(fp); 730 | while (err > 1e-5) 731 | { 732 | fa = compute_ESS_diff(a,gammavar_t,w_temp,loglike,N); 733 | if (fa*fp < 0) 734 | { 735 | b = p; 736 | } 737 | else 738 | { 739 | a = p; 740 | } 741 | p = (a+b)/2.0; 742 | fp = compute_ESS_diff(p,gammavar_t,w_temp,loglike,N); 743 | err = fabs(fp); 744 | } 745 | gammavar_tp1 = p; 746 | } 747 | 748 | /*re-weighting particles*/ 749 | #pragma omp simd 750 | for (unsigned int i=0;i w_sum3 && k < N-1) 786 | { 787 | k++; 788 | w_sum3 += w[k]; 789 | } 790 | r[i] = k; 791 | } 792 | 793 | /* re-assign particles*/ 794 | for (unsigned int i=0;i u[i]); 881 | } 882 | expected_acc_probs = ((double)sum_acc_probs)/((double)N); 883 | if (expected_acc_probs <= 0.0) 884 | { 885 | expected_acc_probs =1e-6; 886 | } 887 | else if (expected_acc_probs >= 1.0) 888 | { 889 | expected_acc_probs = 1.0 - 1e-6; 890 | } 891 | 892 | /* determine the number of MCMC steps to ensure probability of 1-c that 893 | * a particle moves 894 | */ 895 | R_t = (unsigned int)ceil(log(0.01)/log(1.0 - expected_acc_probs )); 896 | 897 | /*perform remaining repeats in blocks of VECLEN*/ 898 | for (unsigned int i=0;i= 0 && x[j] > A) 1051 | { 1052 | x[j+1] = x[j]; 1053 | j--; 1054 | } 1055 | x[j+1] = A; 1056 | } 1057 | } 1058 | 1059 | --------------------------------------------------------------------------------