simple-squiggle

A restricted subset of Squiggle
Log | Files | Refs | README

README.md (4996B)


      1 # Simple Squiggle
      2 
      3 ## About
      4 
      5 ![](imgs/simple-rick.jpg)
      6 
      7 "Simple Squiggle" is a simple parser that manipulates multiplications and divisions between numbers and lognormal distributions. It uses an extremely restricted subset of [Squiggle](https://github.com/quantified-uncertainty/squiggle)'s syntax, and unlike it, the underlying code is not easily extensible.
      8 
      9 It may be useful for testing correctness of limited features of the full Squiggle, or for sanity-checking the validity of some Squiggle models.
     10 
     11 ![](imgs/simple-squiggle2.png)
     12 
     13 ## Built with
     14 
     15 - [Node.js](https://nodejs.org/en/)
     16 - [Math.js](https://mathjs.org/)
     17 - [Best Readme template](https://github.com/othneildrew/Best-README-Template/blob/master/README.md)
     18 
     19 ## Getting started
     20 
     21 ### Prerequisites
     22 
     23 - npm
     24 - nodejs
     25 
     26 ### Installation
     27 
     28 #### For command line usage
     29 
     30 ```
     31 git clone https://github.com/quantified-uncertainty/simple-squiggle.git
     32 cd simple-squiggle
     33 ## npm install
     34 ```
     35 
     36 The last line is not necessary, since I'm saving node_packages in the repository.
     37 
     38 #### For use inside another node program
     39 
     40 ```
     41 npm install @forecasting/simple-squiggle
     42 ```
     43 
     44 ## Usage
     45 
     46 ### General usage
     47 
     48 Consider a squiggle model which only uses lognormals:
     49 
     50 ```
     51 initialPrisonPopulation = 1.8M to 2.5M # Data for 2022 prison population has not yet been published, though this estimate is perhaps too wide.
     52 reductionInPrisonPopulation = 0.25 to 0.75
     53 badnessOfPrisonInQALYs = 0.2 to 5 # 80% as good as being alive to 5 times worse than living is good
     54 accelerationInYears = 5 to 50
     55 probabilityOfSuccess = 0.01 to 0.1 # 1% to 10%.
     56 estimateQALYs = leftTruncate(
     57     initialPrisonPopulation *
     58     reductionInPrisonPopulation *
     59     badnessOfPrisonInQALYs *
     60     accelerationInYears *
     61     probabilityOfSuccess
     62     , 0)
     63 cost = 2B to 20B
     64 costEffectivenessPerQALY = leftTruncate(cost / estimateQALYs, 0)
     65 costEffectivenessPerQALY
     66 ```
     67 
     68 It can be simplified to the following simple squiggle model:
     69 
     70 ```
     71 ( 2000000000 to 20000000000 ) / ( (1800000 to 2500000) * (0.25 to 0.75) * (0.2 to 5) * (5 to 50) * (0.01 to 0.1) )
     72 ```
     73 
     74 I provide both an exportable library and a command line interface (cli).
     75 
     76 ### Command line interface
     77 
     78 After cloning this repository through github (see installation section), the cli can be run with `npm run cli`, which produces a prompt:
     79 
     80 ```
     81 > npm run cli
     82 
     83 Model:
     84 ```
     85 
     86 After filling in the prompt
     87 
     88 ```
     89 > npm run cli
     90 
     91 Model: ( 2000000000 to 20000000000 ) / ( (1800000 to 2500000) * (0.25 to 0.75) * (0.2 to 5) * (5 to 50) * (0.01 to 0.1) )
     92 ```
     93 
     94 the output looks as follows:
     95 
     96 ```
     97 > npm run cli
     98 
     99 Model: ( 2000000000 to 20000000000 ) / ( (1800000 to 2500000) * (0.25 to 0.75) * (0.2 to 5) * (5 to 50) * (0.01 to 0.1) )
    100 
    101         = (lognormal(22.57, 0.70)) / ((lognormal(14.57, 0.10)) * (lognormal(-0.84, 0.33)) * (lognormal(0.00, 0.98)) * (lognormal(2.76, 0.70)) * (lognormal(-3.45, 0.70)))
    102         -> lognormal(22.57, 0.70) / (lognormal(14.57, 0.10) * lognormal(-0.84, 0.33) * lognormal(0.00, 0.98) * lognormal(2.76, 0.70) * lognormal(-3.45, 0.70))
    103         -> lognormal(22.57, 0.70) / (lognormal(13.73, 0.35) * lognormal(0.00, 0.98) * lognormal(2.76, 0.70) * lognormal(-3.45, 0.70))
    104         -> lognormal(22.57, 0.70) / (lognormal(13.73, 1.04) * lognormal(2.76, 0.70) * lognormal(-3.45, 0.70))
    105         -> lognormal(22.57, 0.70) / (lognormal(16.49, 1.25) * lognormal(-3.45, 0.70))
    106         -> lognormal(22.57, 0.70) / (lognormal(13.04, 1.43))
    107         -> lognormal(22.57, 0.70) / lognormal(13.04, 1.43)
    108         -> lognormal(9.53, 1.60)
    109 
    110 => lognormal(9.530291704996749, 1.596443005980748)
    111    ( => ~996.6270585961881 to ~190271.4039258926 )
    112 
    113 ----------------------------------------------------
    114 
    115 ```
    116 
    117 For ease of representation, the intermediary outputs are printed only to two decimal points. But this is just a display decision; the innards of the program work with the full set of decimals.
    118 
    119 You can also run tests with `npm run test`
    120 
    121 ### Exportable library
    122 
    123 I also provide an exportable library. After installing it with npm (see installation section), you can call it with:
    124 
    125 ```
    126 import { transformer } from "@forecasting/simple-squiggle";
    127 
    128 // Helpers
    129 let printer = (_) => null;
    130 let getSimpleSquiggleOutput = (string) => transformer(string, printer);
    131 
    132 // Model
    133 let model = "( 2000000000 to 20000000000 ) / ( (1800000 to 2500000) * (0.25 to 0.75) * (0.2 to 5) * (5 to 50) * (0.01 to 0.1) )"
    134 let result = getSimpleSquiggleOutput(model);
    135 console.log(result); /* {
    136   squiggleString: 'lognormal(-0.3465735902799725, 1.1485521838283161)',
    137   lognormalParameters: [ -0.3465735902799725, 1.1485521838283161 ],
    138   shortGuesstimateString: '0.11 to 4.7',
    139   array90CI: [ 0.10690936969938292, 4.676858552304103 ]
    140 }
    141 */
    142 
    143 
    144 ```
    145 
    146 ## Roadmap
    147 
    148 I consider this repository to be feature complete. As such, I may tinker with the code which wraps around the core logic, but I don't really intend to add further functionality.
    149 
    150 - [ ] Make wrapper code less hacky
    151 - [x] Display final lognormal as a 90% confidence interval as well
    152 
    153 ## License
    154 
    155 Distributed under the MIT License