Compare commits
10 Commits
a859f2572f
...
06602cd291
Author | SHA1 | Date | |
---|---|---|---|
06602cd291 | |||
a1595935d6 | |||
0fb2e7acb3 | |||
b665262e33 | |||
254b9068af | |||
07f0fd1d3f | |||
ade8947734 | |||
1c6c2068ff | |||
298e579480 | |||
233b4cfb30 |
139
README.md
139
README.md
|
@ -1,6 +1,6 @@
|
|||
# A minimalist calculator for fermi estimation
|
||||
# A calculator for distributions, for Fermi estimation
|
||||
|
||||
This project is a minimalist, calculator-style DSL for fermi estimation. It can multiply, divide, add and substract scalars, lognormals and beta distributions.
|
||||
This project is a minimalist, calculator-style DSL for fermi estimation. It can multiply, divide, add and substract scalars, lognormals and beta distributions, and supports variables.
|
||||
|
||||
## Motivation
|
||||
|
||||
|
@ -21,66 +21,33 @@ $ fermi
|
|||
5000000 12000000
|
||||
=> 5.0M 12.0M
|
||||
* beta 1 200
|
||||
1.9K 123.1K
|
||||
=> 1.9K 123.1K
|
||||
* 30 180
|
||||
122.9K 11.7M
|
||||
=> 122.9K 11.7M
|
||||
/ 48 52
|
||||
2.5K 234.6K
|
||||
=> 2.5K 234.6K
|
||||
/ 5 6
|
||||
448.8 43.0K
|
||||
=> 448.8 43.0K
|
||||
/ 6 8
|
||||
64.5 6.2K
|
||||
=> 64.5 6.2K
|
||||
/ 60
|
||||
1.1 103.7
|
||||
=> 1.1 103.7
|
||||
```
|
||||
|
||||
Perhaps this example is more understandable with comments and better units:
|
||||
|
||||
```
|
||||
$ fermi
|
||||
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
|
||||
5M 12M # number of people living in Chicago
|
||||
beta 1 200 # fraction of people that have a piano
|
||||
30 180 # minutes it takes to tune a piano, including travel time
|
||||
/ 48 52 # weeks a year that piano tuners work for
|
||||
/ 5 6 # days a week in which piano tuners work
|
||||
/ 6 8 # hours a day in which piano tuners work
|
||||
/ 60 # minutes to an hour
|
||||
=: piano_tuners
|
||||
```
|
||||
|
||||
Here is instead an example using beta distributions and variables:
|
||||
|
||||
```
|
||||
$ fermi
|
||||
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.
|
||||
|
||||
If you type "help", you can see a small grammar:
|
||||
|
||||
```
|
||||
|
@ -95,24 +62,31 @@ help
|
|||
Variable assignment: =: variable_name
|
||||
Variable assignment and clear stack: =. variable_name
|
||||
Special:
|
||||
Clear stack: clear | c | .
|
||||
Print this help message: help | h
|
||||
Print debug info: debug | d
|
||||
Exit: exit | e
|
||||
Comment: # this is a comment
|
||||
Clear stack: clear | c | .
|
||||
Print debug info: debug | d
|
||||
Print help message: help | h
|
||||
Start additional stack: operator (
|
||||
Return from additional stack )
|
||||
Exit: exit | e
|
||||
Examples:
|
||||
+ 2
|
||||
/ 2.5
|
||||
* 1 10 (interpreted as lognormal)
|
||||
# this is a comment
|
||||
/ 2.5 # this is an operation followed by a comment
|
||||
* 1 10 # "low high" is interpreted as lognormal
|
||||
+ 1 10
|
||||
* beta 1 10
|
||||
1 10 (multiplication taken as default operation)
|
||||
=: x
|
||||
.
|
||||
1 10 # multiplication taken as default operation)
|
||||
=: x
|
||||
. # return the stack to 1.
|
||||
1 100
|
||||
+ x
|
||||
# this is a comment
|
||||
* 1 12 # this is an operation followed by a comment
|
||||
* 1 12
|
||||
* (
|
||||
1 10
|
||||
+ beta 1 100
|
||||
)
|
||||
=. y # save to variable and clear stack
|
||||
exit
|
||||
```
|
||||
|
||||
|
@ -120,11 +94,45 @@ help
|
|||
|
||||
- It's conceptually clearer to have all the multiplications first and then all the divisions
|
||||
- For things between 0 and 1, consider using a beta distribution
|
||||
- Because the model reads from standard input, you can pipe a model to it. For instance, try cat more/piano-tuners.f | fermi
|
||||
|
||||
### Command line options
|
||||
|
||||
You can specify the number of samples to draw when algebraic manipulations are not sufficient:
|
||||
|
||||
```
|
||||
$ fermi -n 1000000
|
||||
$ fermi -n 1_000_000
|
||||
```
|
||||
|
||||
You also run a file with the -f option
|
||||
|
||||
```
|
||||
$ fermi -f more/piano-tuners.fermi
|
||||
```
|
||||
|
||||
### Integrations with linux utilities
|
||||
|
||||
Because the model reads from standard input, you can a model to it:
|
||||
|
||||
```
|
||||
$ cat more/piano-tuners.fermi | fermi
|
||||
```
|
||||
|
||||
You can make a model an executable file by running `$ chmod -x model.fermi` and then adding the following at the top!
|
||||
|
||||
```
|
||||
#!/bin/usr/fermi -f
|
||||
```
|
||||
|
||||
You can save a session to a logfile with tee:
|
||||
|
||||
```
|
||||
fermi | tee -a fermi.log
|
||||
```
|
||||
|
||||
## Different levels of complexity
|
||||
|
||||
The top level f.go file (400 lines) has a bunch of complexity: variables, parenthesis, samples, beta distributions. In the simple/ folder:
|
||||
The top level f.go file (420 lines) has a bunch of complexity: variables, parenthesis, samples, beta distributions, number of samples, etc. In the simple/ folder:
|
||||
|
||||
- f_simple.go (370 lines) strips variables and parenthesis, but keeps beta distributions, samples, and addition and substraction
|
||||
- f_minimal.go (140 lines) strips everything that isn't lognormal and scalar multiplication and addition, plus a few debug options.
|
||||
|
@ -159,11 +167,14 @@ Done:
|
|||
- [x] Clean up error code. Right now only needed for division
|
||||
- [x] Maintain *both* a more complex thing that's more featureful *and* the more simple multiplication of lognormals thing.
|
||||
- [x] Allow input with K/M/T
|
||||
- [x] Document parenthesis syntax
|
||||
- [x] Specify number of samples as a command line option
|
||||
- [x] Figure out how to make models executable, by adding a #!/bin/bash-style command at the top?
|
||||
- [x] Make -n flag work
|
||||
- [x] Add flag to repeat input lines (useful when reading from files)
|
||||
|
||||
To (possibly) do:
|
||||
|
||||
- [ ] Specify number of samples as a command line option
|
||||
- [ ] Document parenthesis syntax
|
||||
- [ ] Add functions. Now easier to do with an explicit representation of the stakc
|
||||
- [ ] Think about how to draw a histogram from samples
|
||||
- [ ] Dump samples to file
|
||||
|
|
|
@ -3,6 +3,7 @@ package main
|
|||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"git.nunosempere.com/NunoSempere/fermi/pretty"
|
||||
"git.nunosempere.com/NunoSempere/fermi/sample"
|
||||
|
@ -101,7 +102,8 @@ const HELP_MSG = " Operation | Variable assignment | Special\n" +
|
|||
|
||||
const NORMAL90CONFIDENCE = 1.6448536269514727
|
||||
const INIT_DIST Scalar = Scalar(1)
|
||||
const N_SAMPLES = 1_000_000
|
||||
|
||||
var N_SAMPLES = 100_000
|
||||
|
||||
/* Printers */
|
||||
func prettyPrintDist(dist Dist) {
|
||||
|
@ -354,10 +356,13 @@ func parseWordsIntoOpAndDist(words []string, vars map[string]Dist) (string, Dist
|
|||
// We want this as a function (rather than just be in main)
|
||||
// to be able to have parenthesis/recusion, possibly functions
|
||||
|
||||
func runRepl(stack Stack, reader *bufio.Reader) Stack {
|
||||
func runRepl(stack Stack, reader *bufio.Reader, echo_flag *bool) Stack {
|
||||
replForLoop:
|
||||
for {
|
||||
new_line, _ := reader.ReadString('\n')
|
||||
if *echo_flag {
|
||||
fmt.Printf(new_line)
|
||||
}
|
||||
new_line_before_comments, _, _ := strings.Cut(new_line, "#")
|
||||
new_line_trimmed := strings.TrimSpace(new_line_before_comments)
|
||||
words := strings.Split(new_line_trimmed, " ")
|
||||
|
@ -367,7 +372,7 @@ replForLoop:
|
|||
continue replForLoop
|
||||
/* Parenthesis */
|
||||
case len(words) == 2 && (words[0] == "*" || words[0] == "+" || words[0] == "-" || words[0] == "/") && words[1] == "(":
|
||||
new_stack := runRepl(Stack{old_dist: INIT_DIST, vars: stack.vars}, reader)
|
||||
new_stack := runRepl(Stack{old_dist: INIT_DIST, vars: stack.vars}, reader, echo_flag)
|
||||
combined_dist, err := operateDists(stack.old_dist, new_stack.old_dist, words[0])
|
||||
if err == nil {
|
||||
stack.old_dist = combined_dist
|
||||
|
@ -408,7 +413,25 @@ replForLoop:
|
|||
}
|
||||
|
||||
func main() {
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
num_samples_flag := flag.Int("n", N_SAMPLES, "Specifies the number of samples to draw when using samples")
|
||||
filename := flag.String("f", "", "Specifies a file with a model to run")
|
||||
echo_flag := flag.Bool("echo", false, "Specifies whether inputs should be echoed back. Useful if reading from a file.")
|
||||
flag.Parse()
|
||||
N_SAMPLES = *num_samples_flag
|
||||
|
||||
var reader *bufio.Reader = nil
|
||||
if *filename != "" {
|
||||
file, err := os.Open(*filename)
|
||||
if err == nil {
|
||||
reader = bufio.NewReader(file)
|
||||
} else {
|
||||
fmt.Printf("Error opening filename; reading from stdin instead\n")
|
||||
}
|
||||
}
|
||||
if reader == nil {
|
||||
reader = bufio.NewReader(os.Stdin)
|
||||
}
|
||||
stack := Stack{old_dist: INIT_DIST, vars: make(map[string]Dist)}
|
||||
runRepl(stack, reader)
|
||||
runRepl(stack, reader, echo_flag)
|
||||
|
||||
}
|
20
makefile
20
makefile
|
@ -1,17 +1,11 @@
|
|||
run:
|
||||
go run f.go
|
||||
|
||||
build:
|
||||
go build f.go
|
||||
go build fermi.go
|
||||
|
||||
install: f
|
||||
run:
|
||||
go run fermi.go
|
||||
|
||||
install: fermi
|
||||
rm /usr/bin/fermi
|
||||
rm /usr/bin/f
|
||||
sudo mv f /usr/bin/f
|
||||
sudo ln -s /usr/bin/f /usr/bin/fermi
|
||||
|
||||
install_xl: f
|
||||
rm /usr/bin/fermixl
|
||||
rm /usr/bin/fxl
|
||||
sudo mv f /usr/bin/fxl
|
||||
sudo ln -s /usr/bin/fxl /usr/bin/fermixl
|
||||
sudo mv fermi /usr/bin/fermi
|
||||
sudo ln -s /usr/bin/fermi /usr/bin/f
|
||||
|
|
10
more/executable-model.fermi
Executable file
10
more/executable-model.fermi
Executable file
|
@ -0,0 +1,10 @@
|
|||
#!/bin/fermi -f
|
||||
5M 12M # number of people living in Chicago
|
||||
beta 1 200 # fraction of people that have a piano
|
||||
30 180 # minutes it takes to tune a piano, including travel time
|
||||
/ 48 52 # weeks a year that piano tuners work for
|
||||
/ 5 6 # days a week in which piano tuners work
|
||||
/ 6 8 # hours a day in which piano tuners work
|
||||
/ 60 # minutes to an hour
|
||||
=: piano_tuners
|
||||
exit
|
|
@ -1,9 +0,0 @@
|
|||
5M 12M # number of people living in Chicago
|
||||
0.002 0.01 # fraction of people that have a piano
|
||||
30 180 # minutes it takes to tune a piano, including travel time
|
||||
/ 48 52 # weeks a year that piano tuners work for
|
||||
/ 5 6 # days a week in which piano tuners work
|
||||
/ 6 8 # hours a day in which piano tuners work
|
||||
/ 60 # minutes to an hour
|
||||
# ^ piano tuners in Chicago
|
||||
|
9
more/piano-tuners-commented.fermi
Normal file
9
more/piano-tuners-commented.fermi
Normal file
|
@ -0,0 +1,9 @@
|
|||
5M 12M # number of people living in Chicago
|
||||
beta 1 200 # fraction of people that have a piano
|
||||
30 180 # minutes it takes to tune a piano, including travel time
|
||||
/ 48 52 # weeks a year that piano tuners work for
|
||||
/ 5 6 # days a week in which piano tuners work
|
||||
/ 6 8 # hours a day in which piano tuners work
|
||||
/ 60 # minutes to an hour
|
||||
=: piano_tuners
|
||||
exit
|
|
@ -10,13 +10,13 @@ import (
|
|||
func PrettyPrintInt(n int) {
|
||||
switch {
|
||||
case math.Abs(float64(n)) >= 1_000_000_000_000:
|
||||
fmt.Printf("%dT", n/1_000_000_000_000)
|
||||
fmt.Printf("%.2fT", float64(n)/1_000_000_000_000.0)
|
||||
case math.Abs(float64(n)) >= 1_000_000_000:
|
||||
fmt.Printf("%dB", n/1_000_000_000)
|
||||
fmt.Printf("%.2fB", float64(n)/1_000_000_000.0)
|
||||
case math.Abs(float64(n)) >= 1_000_000:
|
||||
fmt.Printf("%dM", n/1_000_000)
|
||||
fmt.Printf("%.2fM", float64(n)/1_000_000.0)
|
||||
case math.Abs(float64(n)) >= 1_000:
|
||||
fmt.Printf("%dK", n/1_000)
|
||||
fmt.Printf("%.2fK", float64(n)/1_000.0)
|
||||
default:
|
||||
fmt.Printf("%df", n)
|
||||
}
|
||||
|
@ -25,24 +25,24 @@ func PrettyPrintInt(n int) {
|
|||
func PrettyPrintFloat(f float64) {
|
||||
switch {
|
||||
case math.Abs(f) >= 1_000_000_000_000:
|
||||
fmt.Printf("%.1fT", f/1_000_000_000_000)
|
||||
fmt.Printf("%.2fT", f/1_000_000_000_000)
|
||||
case math.Abs(f) >= 1_000_000_000:
|
||||
fmt.Printf("%.1fB", f/1_000_000_000)
|
||||
fmt.Printf("%.2fB", f/1_000_000_000)
|
||||
case math.Abs(f) >= 1_000_000:
|
||||
fmt.Printf("%.1fM", f/1_000_000)
|
||||
fmt.Printf("%.2fM", f/1_000_000)
|
||||
case math.Abs(f) >= 1_000:
|
||||
fmt.Printf("%.1fK", f/1_000)
|
||||
fmt.Printf("%.2fK", f/1_000)
|
||||
|
||||
case math.Abs(f) <= 0.0001:
|
||||
fmt.Printf("%.5f", f)
|
||||
fmt.Printf("%.6f", f)
|
||||
case math.Abs(f) <= 0.001:
|
||||
fmt.Printf("%.4f", f)
|
||||
fmt.Printf("%.5f", f)
|
||||
case math.Abs(f) <= 0.01:
|
||||
fmt.Printf("%.3f", f)
|
||||
fmt.Printf("%.4f", f)
|
||||
case math.Abs(f) <= 0.1:
|
||||
fmt.Printf("%.2f", f)
|
||||
fmt.Printf("%.3f", f)
|
||||
default:
|
||||
fmt.Printf("%.1f", f)
|
||||
fmt.Printf("%.2f", f)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user