add num samples as command line flag

This commit is contained in:
NunoSempere 2024-07-12 12:11:25 -04:00
parent 233b4cfb30
commit 298e579480
4 changed files with 50 additions and 69 deletions

View File

@ -21,17 +21,17 @@ $ 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:
@ -39,48 +39,15 @@ 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
=> 5.0M 12.0M beta 1 200 # fraction of people that have a piano
* 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 30 180 # minutes it takes to tune a piano, including travel time
122.9K 11.7M / 48 52 # weeks a year that piano tuners work for
/ 48 52 # weeks a year pianotuners work for / 5 6 # days a week in which piano tuners work
2.5K 234.6K / 6 8 # hours a day in which piano tuners work
/ 6 8 # hours a day
353.9 34.1K
/ 60 # minutes to an hour / 60 # minutes to an hour
5.9 568.3 =: piano_tuners
=: 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:
``` ```
@ -95,24 +62,31 @@ 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:
Clear stack: clear | c | .
Print this help message: help | h
Print debug info: debug | d
Exit: exit | e
Comment: # this is a comment 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: Examples:
+ 2 + 2
/ 2.5 # this is a comment
* 1 10 (interpreted as lognormal) / 2.5 # this is an operation followed by a comment
* 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
# this is a comment * 1 12
* 1 12 # this is an operation followed by a comment * (
1 10
+ beta 1 100
)
=. y # save to variable and clear stack
exit exit
``` ```
@ -160,11 +134,12 @@ 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
To (possibly) do: To (possibly) do:
- [ ] Specify number of samples as a command line option - [ ] Specify number of samples as a command line option
- [ ] Document parenthesis syntax - [ ] Figure out how to make models executable, by adding a #!/bin/bash-style command at the top?
- [ ] 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

8
f.go
View File

@ -3,6 +3,7 @@ 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"
@ -101,7 +102,8 @@ 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 = 1_000_000
/* Printers */ /* Printers */
func prettyPrintDist(dist Dist) { func prettyPrintDist(dist Dist) {
@ -408,6 +410,10 @@ replForLoop:
} }
func main() { func main() {
num_samples_flag := flag.Int("n", N_SAMPLES, "Specifies the number of samples to draw when using samples")
flag.Parse()
N_SAMPLES = *num_samples_flag
reader := bufio.NewReader(os.Stdin) 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) runRepl(stack, reader)

View File

@ -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

View 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