150 lines
4.1 KiB
Markdown
150 lines
4.1 KiB
Markdown
# A minimalist calculator for fermi estimation
|
|
|
|
This project is a minimalist, stack-based DSL for Fermi estimation. It can multiply and divide scalars, lognormals and beta distributions.
|
|
|
|
## Motivation
|
|
|
|
Sometimes, [Squiggle](https://github.com/quantified-uncertainty/squiggle), [simple squiggle](https://git.nunosempere.com/quantified.uncertainty/simple-squiggle) or [squiggle.c](https://git.nunosempere.com/personal/squiggle.c) are still too complicated and un-unix-like.
|
|
|
|
## Usage
|
|
|
|
Here is an example
|
|
|
|
```
|
|
$ go run fermi.go
|
|
5000000 12000000
|
|
=> 5.0M 12.0M
|
|
* beta 1 200
|
|
1.9K 123.1K
|
|
* 30 180
|
|
122.9K 11.7M
|
|
/ 48 52
|
|
2.5K 234.6K
|
|
/ 5 6
|
|
448.8 43.0K
|
|
/ 6 8
|
|
64.5 6.2K
|
|
/ 60
|
|
1.1 103.7
|
|
```
|
|
|
|
Perhaps this example is more understandable with comments and better units:
|
|
|
|
```
|
|
$ sed -u "s|#.*||" | sed -u 's|M|000000|g' | go run fermi.go
|
|
5M 12M # number of people living in Chicago
|
|
=> 5.0M 12.0M
|
|
* beta 1 200 # fraction of people that have a piano
|
|
1.9K 123.1K
|
|
30 180 # minutes it takes to tune a piano, including travel time
|
|
122.9K 11.7M
|
|
/ 48 52 # weeks a year pianotuners work for
|
|
2.5K 234.6K
|
|
/ 6 8 # hours a day
|
|
353.9 34.1K
|
|
/ 60 # minutes to an hour
|
|
5.9 568.3
|
|
=: piano_tuners_in_Chicago
|
|
piano_tuners_in_Chicago => 5.9 568.3
|
|
```
|
|
|
|
Here is instead an example using beta distributions and variables:
|
|
|
|
```
|
|
1 2
|
|
=> 1.0 2.0
|
|
* 1_000_000_000
|
|
=> 1000.0M 2.0B
|
|
=: x # assign to variable
|
|
x => 1000.0M 2.0B
|
|
. # clear the stack, i.e., make it be 1
|
|
|
|
beta 1 2
|
|
=> beta 1.0 2.0
|
|
beta 12 300
|
|
=> beta 13.0 302.0
|
|
=. y # assign to variable and clear the stack (return it to 1)
|
|
y => beta 13.0 302.0
|
|
|
|
x
|
|
=> 1000.0M 2.0B
|
|
* y
|
|
=> samples 31.3M 98.2M
|
|
```
|
|
|
|
The difference between `=: x` and `=. y` is that `=.` clears the stack after the assignment.
|
|
|
|
## Installation
|
|
|
|
```
|
|
make build
|
|
sudo make install
|
|
f # rather than the previous go run fermi.go
|
|
```
|
|
|
|
Why use make instead of the built-in go commands? Because the point of make is to be able to share command-line recipes.
|
|
|
|
## Usage together with standard Linux utilities
|
|
|
|
```bash
|
|
f
|
|
sed -u "s|#.*||" | sed -u 's|M|000000|g' | f
|
|
|
|
cat more/piano-tuners.f | f
|
|
cat more/piano-tuners-commented.f | sed -u "s|#.*||" | sed -u 's|M|000000|g' | f
|
|
|
|
tee -a input.log | go run fermi.go | tee -a output.log
|
|
tee -a io.log | go run fermi.go | tee -a io.log
|
|
|
|
function f(){
|
|
sed -u "s|#.*||" |
|
|
sed -u "s|//.*||" |
|
|
sed -u 's|K|000|g' |
|
|
sed -u 's|M|000000|g' |
|
|
sed -u 's|B|000000000|g' |
|
|
/usr/bin/f
|
|
}
|
|
```
|
|
|
|
Note that these sed commands are just hacks, and won't parse e.g., `3.5K` correctly—it will just substitute for 3.5000
|
|
|
|
## Tips & tricks
|
|
|
|
- It's conceptually clearer to have all the multiplications first and then all the divisions
|
|
- Sums and divisions now also supported
|
|
- For things between 0 and 1, consider using a beta distribution
|
|
|
|
## Roadmap
|
|
|
|
- [x] Write README
|
|
- [x] Add division?
|
|
- [x] Read from file?
|
|
- [x] Save to file?
|
|
- [x] Allow comments?
|
|
- [x] Use a sed filter?
|
|
- [x] Add show more info version
|
|
- [x] Scalar multiplication and division
|
|
- [ ] Program into a small device, like a calculator?
|
|
- [-] Think of some way of calling bc
|
|
- [x] Think how to integrate with squiggle.c to draw samples
|
|
- [x] Copy the time to botec go code
|
|
- [x] Define samplers
|
|
- [x] Call those samplers when operating on distributions that can't be operted on algebraically
|
|
- [ ] Think about how to draw a histogram from samples
|
|
- [x] Display output more nicely, with K/M/B/T
|
|
- [x] Consider the following: make this into a stack-based DSL, with:
|
|
- [x] Variables that can be saved to and then displayed
|
|
- [x] Other types of distributions, particularly beta distributions? => But then this requires moving to bags of samples. It could still be ~instantaneous though.
|
|
- [x] Figure out go syntax for
|
|
- Maps
|
|
- Joint types
|
|
- Enums
|
|
- [x] Fix correlation problem, by spinning up a new randomness thing every time some serial computation is done.
|
|
- [ ] Maintain *both* a more complex thing that's more featureful *and* the more simple multiplication of lognormals thing.
|
|
- [ ] Clean up error code. Right now only needed for division
|
|
- [ ] Dump samples to file
|
|
- [ ] Represent samples/statistics in some other way
|
|
- [ ] Perhaps use qsort rather than full sorting
|
|
|
|
Some possible syntax for a more expressive stack-based DSL (now implemented)
|