Compare commits
No commits in common. "06602cd291cb46953bc079c31e8e1e37ad3c7d0e" and "a859f2572f611b590ee2c17007eb4a176e36837f" have entirely different histories.
06602cd291
...
a859f2572f
135
README.md
135
README.md
|
@ -1,6 +1,6 @@
|
||||||
# A calculator for distributions, for Fermi estimation
|
# A minimalist calculator 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, and supports variables.
|
This project is a minimalist, calculator-style DSL for fermi estimation. It can multiply, divide, add and substract scalars, lognormals and beta distributions.
|
||||||
|
|
||||||
## Motivation
|
## Motivation
|
||||||
|
|
||||||
|
@ -21,33 +21,66 @@ $ fermi
|
||||||
5000000 12000000
|
5000000 12000000
|
||||||
=> 5.0M 12.0M
|
=> 5.0M 12.0M
|
||||||
* beta 1 200
|
* beta 1 200
|
||||||
=> 1.9K 123.1K
|
1.9K 123.1K
|
||||||
* 30 180
|
* 30 180
|
||||||
=> 122.9K 11.7M
|
122.9K 11.7M
|
||||||
/ 48 52
|
/ 48 52
|
||||||
=> 2.5K 234.6K
|
2.5K 234.6K
|
||||||
/ 5 6
|
/ 5 6
|
||||||
=> 448.8 43.0K
|
448.8 43.0K
|
||||||
/ 6 8
|
/ 6 8
|
||||||
=> 64.5 6.2K
|
64.5 6.2K
|
||||||
/ 60
|
/ 60
|
||||||
=> 1.1 103.7
|
1.1 103.7
|
||||||
```
|
```
|
||||||
|
|
||||||
Perhaps this example is more understandable with comments and better units:
|
Perhaps this example is more understandable with comments and better units:
|
||||||
|
|
||||||
```
|
```
|
||||||
$ fermi
|
$ fermi
|
||||||
5M 12M # number of people living in Chicago
|
5M 12M # number of people living in Chicago
|
||||||
beta 1 200 # fraction of people that have a piano
|
=> 5.0M 12.0M
|
||||||
30 180 # minutes it takes to tune a piano, including travel time
|
* beta 1 200 # fraction of people that have a piano
|
||||||
/ 48 52 # weeks a year that piano tuners work for
|
1.9K 123.1K
|
||||||
/ 5 6 # days a week in which piano tuners work
|
30 180 # minutes it takes to tune a piano, including travel time
|
||||||
/ 6 8 # hours a day in which piano tuners work
|
122.9K 11.7M
|
||||||
/ 60 # minutes to an hour
|
/ 48 52 # weeks a year pianotuners work for
|
||||||
=: piano_tuners
|
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:
|
||||||
|
|
||||||
|
```
|
||||||
|
$ 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:
|
If you type "help", you can see a small grammar:
|
||||||
|
|
||||||
```
|
```
|
||||||
|
@ -62,31 +95,24 @@ help
|
||||||
Variable assignment: =: variable_name
|
Variable assignment: =: variable_name
|
||||||
Variable assignment and clear stack: =. variable_name
|
Variable assignment and clear stack: =. variable_name
|
||||||
Special:
|
Special:
|
||||||
Comment: # this is a comment
|
|
||||||
Clear stack: clear | c | .
|
Clear stack: clear | c | .
|
||||||
|
Print this help message: help | h
|
||||||
Print debug info: debug | d
|
Print debug info: debug | d
|
||||||
Print help message: help | h
|
|
||||||
Start additional stack: operator (
|
|
||||||
Return from additional stack )
|
|
||||||
Exit: exit | e
|
Exit: exit | e
|
||||||
|
Comment: # this is a comment
|
||||||
Examples:
|
Examples:
|
||||||
+ 2
|
+ 2
|
||||||
# this is a comment
|
/ 2.5
|
||||||
/ 2.5 # this is an operation followed by a comment
|
* 1 10 (interpreted as lognormal)
|
||||||
* 1 10 # "low high" is interpreted as lognormal
|
|
||||||
+ 1 10
|
+ 1 10
|
||||||
* beta 1 10
|
* beta 1 10
|
||||||
1 10 # multiplication taken as default operation)
|
1 10 (multiplication taken as default operation)
|
||||||
=: x
|
=: x
|
||||||
. # return the stack to 1.
|
.
|
||||||
1 100
|
1 100
|
||||||
+ x
|
+ x
|
||||||
* 1 12
|
# this is a comment
|
||||||
* (
|
* 1 12 # this is an operation followed by a comment
|
||||||
1 10
|
|
||||||
+ beta 1 100
|
|
||||||
)
|
|
||||||
=. y # save to variable and clear stack
|
|
||||||
exit
|
exit
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -94,45 +120,11 @@ help
|
||||||
|
|
||||||
- It's conceptually clearer to have all the multiplications first and then all the divisions
|
- 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
|
- 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
|
## Different levels of complexity
|
||||||
|
|
||||||
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:
|
The top level f.go file (400 lines) has a bunch of complexity: variables, parenthesis, samples, beta distributions. In the simple/ folder:
|
||||||
|
|
||||||
- f_simple.go (370 lines) strips variables and parenthesis, but keeps beta distributions, samples, and addition and substraction
|
- 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.
|
- f_minimal.go (140 lines) strips everything that isn't lognormal and scalar multiplication and addition, plus a few debug options.
|
||||||
|
@ -167,14 +159,11 @@ Done:
|
||||||
- [x] Clean up error code. Right now only needed for division
|
- [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] 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] 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:
|
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
|
- [ ] Add functions. Now easier to do with an explicit representation of the stakc
|
||||||
- [ ] Think about how to draw a histogram from samples
|
- [ ] Think about how to draw a histogram from samples
|
||||||
- [ ] Dump samples to file
|
- [ ] Dump samples to file
|
||||||
|
|
|
@ -3,7 +3,6 @@ package main
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"errors"
|
"errors"
|
||||||
"flag"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.nunosempere.com/NunoSempere/fermi/pretty"
|
"git.nunosempere.com/NunoSempere/fermi/pretty"
|
||||||
"git.nunosempere.com/NunoSempere/fermi/sample"
|
"git.nunosempere.com/NunoSempere/fermi/sample"
|
||||||
|
@ -102,8 +101,7 @@ const HELP_MSG = " Operation | Variable assignment | Special\n" +
|
||||||
|
|
||||||
const NORMAL90CONFIDENCE = 1.6448536269514727
|
const NORMAL90CONFIDENCE = 1.6448536269514727
|
||||||
const INIT_DIST Scalar = Scalar(1)
|
const INIT_DIST Scalar = Scalar(1)
|
||||||
|
const N_SAMPLES = 1_000_000
|
||||||
var N_SAMPLES = 100_000
|
|
||||||
|
|
||||||
/* Printers */
|
/* Printers */
|
||||||
func prettyPrintDist(dist Dist) {
|
func prettyPrintDist(dist Dist) {
|
||||||
|
@ -356,13 +354,10 @@ func parseWordsIntoOpAndDist(words []string, vars map[string]Dist) (string, Dist
|
||||||
// We want this as a function (rather than just be in main)
|
// We want this as a function (rather than just be in main)
|
||||||
// to be able to have parenthesis/recusion, possibly functions
|
// to be able to have parenthesis/recusion, possibly functions
|
||||||
|
|
||||||
func runRepl(stack Stack, reader *bufio.Reader, echo_flag *bool) Stack {
|
func runRepl(stack Stack, reader *bufio.Reader) Stack {
|
||||||
replForLoop:
|
replForLoop:
|
||||||
for {
|
for {
|
||||||
new_line, _ := reader.ReadString('\n')
|
new_line, _ := reader.ReadString('\n')
|
||||||
if *echo_flag {
|
|
||||||
fmt.Printf(new_line)
|
|
||||||
}
|
|
||||||
new_line_before_comments, _, _ := strings.Cut(new_line, "#")
|
new_line_before_comments, _, _ := strings.Cut(new_line, "#")
|
||||||
new_line_trimmed := strings.TrimSpace(new_line_before_comments)
|
new_line_trimmed := strings.TrimSpace(new_line_before_comments)
|
||||||
words := strings.Split(new_line_trimmed, " ")
|
words := strings.Split(new_line_trimmed, " ")
|
||||||
|
@ -372,7 +367,7 @@ replForLoop:
|
||||||
continue replForLoop
|
continue replForLoop
|
||||||
/* Parenthesis */
|
/* Parenthesis */
|
||||||
case len(words) == 2 && (words[0] == "*" || words[0] == "+" || words[0] == "-" || words[0] == "/") && words[1] == "(":
|
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, echo_flag)
|
new_stack := runRepl(Stack{old_dist: INIT_DIST, vars: stack.vars}, reader)
|
||||||
combined_dist, err := operateDists(stack.old_dist, new_stack.old_dist, words[0])
|
combined_dist, err := operateDists(stack.old_dist, new_stack.old_dist, words[0])
|
||||||
if err == nil {
|
if err == nil {
|
||||||
stack.old_dist = combined_dist
|
stack.old_dist = combined_dist
|
||||||
|
@ -413,25 +408,7 @@ replForLoop:
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
num_samples_flag := flag.Int("n", N_SAMPLES, "Specifies the number of samples to draw when using samples")
|
reader := bufio.NewReader(os.Stdin)
|
||||||
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)}
|
stack := Stack{old_dist: INIT_DIST, vars: make(map[string]Dist)}
|
||||||
runRepl(stack, reader, echo_flag)
|
runRepl(stack, reader)
|
||||||
|
|
||||||
}
|
}
|
20
makefile
20
makefile
|
@ -1,11 +1,17 @@
|
||||||
build:
|
|
||||||
go build fermi.go
|
|
||||||
|
|
||||||
run:
|
run:
|
||||||
go run fermi.go
|
go run f.go
|
||||||
|
|
||||||
install: fermi
|
build:
|
||||||
|
go build f.go
|
||||||
|
|
||||||
|
install: f
|
||||||
rm /usr/bin/fermi
|
rm /usr/bin/fermi
|
||||||
rm /usr/bin/f
|
rm /usr/bin/f
|
||||||
sudo mv fermi /usr/bin/fermi
|
sudo mv f /usr/bin/f
|
||||||
sudo ln -s /usr/bin/fermi /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
|
||||||
|
|
|
@ -1,10 +0,0 @@
|
||||||
#!/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
|
|
9
more/piano-tuners-commented.f
Normal file
9
more/piano-tuners-commented.f
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
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
|
||||||
|
|
|
@ -1,9 +0,0 @@
|
||||||
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) {
|
func PrettyPrintInt(n int) {
|
||||||
switch {
|
switch {
|
||||||
case math.Abs(float64(n)) >= 1_000_000_000_000:
|
case math.Abs(float64(n)) >= 1_000_000_000_000:
|
||||||
fmt.Printf("%.2fT", float64(n)/1_000_000_000_000.0)
|
fmt.Printf("%dT", n/1_000_000_000_000)
|
||||||
case math.Abs(float64(n)) >= 1_000_000_000:
|
case math.Abs(float64(n)) >= 1_000_000_000:
|
||||||
fmt.Printf("%.2fB", float64(n)/1_000_000_000.0)
|
fmt.Printf("%dB", n/1_000_000_000)
|
||||||
case math.Abs(float64(n)) >= 1_000_000:
|
case math.Abs(float64(n)) >= 1_000_000:
|
||||||
fmt.Printf("%.2fM", float64(n)/1_000_000.0)
|
fmt.Printf("%dM", n/1_000_000)
|
||||||
case math.Abs(float64(n)) >= 1_000:
|
case math.Abs(float64(n)) >= 1_000:
|
||||||
fmt.Printf("%.2fK", float64(n)/1_000.0)
|
fmt.Printf("%dK", n/1_000)
|
||||||
default:
|
default:
|
||||||
fmt.Printf("%df", n)
|
fmt.Printf("%df", n)
|
||||||
}
|
}
|
||||||
|
@ -25,24 +25,24 @@ func PrettyPrintInt(n int) {
|
||||||
func PrettyPrintFloat(f float64) {
|
func PrettyPrintFloat(f float64) {
|
||||||
switch {
|
switch {
|
||||||
case math.Abs(f) >= 1_000_000_000_000:
|
case math.Abs(f) >= 1_000_000_000_000:
|
||||||
fmt.Printf("%.2fT", f/1_000_000_000_000)
|
fmt.Printf("%.1fT", f/1_000_000_000_000)
|
||||||
case math.Abs(f) >= 1_000_000_000:
|
case math.Abs(f) >= 1_000_000_000:
|
||||||
fmt.Printf("%.2fB", f/1_000_000_000)
|
fmt.Printf("%.1fB", f/1_000_000_000)
|
||||||
case math.Abs(f) >= 1_000_000:
|
case math.Abs(f) >= 1_000_000:
|
||||||
fmt.Printf("%.2fM", f/1_000_000)
|
fmt.Printf("%.1fM", f/1_000_000)
|
||||||
case math.Abs(f) >= 1_000:
|
case math.Abs(f) >= 1_000:
|
||||||
fmt.Printf("%.2fK", f/1_000)
|
fmt.Printf("%.1fK", f/1_000)
|
||||||
|
|
||||||
case math.Abs(f) <= 0.0001:
|
case math.Abs(f) <= 0.0001:
|
||||||
fmt.Printf("%.6f", f)
|
|
||||||
case math.Abs(f) <= 0.001:
|
|
||||||
fmt.Printf("%.5f", f)
|
fmt.Printf("%.5f", f)
|
||||||
case math.Abs(f) <= 0.01:
|
case math.Abs(f) <= 0.001:
|
||||||
fmt.Printf("%.4f", f)
|
fmt.Printf("%.4f", f)
|
||||||
case math.Abs(f) <= 0.1:
|
case math.Abs(f) <= 0.01:
|
||||||
fmt.Printf("%.3f", f)
|
fmt.Printf("%.3f", f)
|
||||||
default:
|
case math.Abs(f) <= 0.1:
|
||||||
fmt.Printf("%.2f", f)
|
fmt.Printf("%.2f", f)
|
||||||
|
default:
|
||||||
|
fmt.Printf("%.1f", f)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user