Compare commits

..

No commits in common. "98225e4bb90794e4e15612f539353315496fcd45" and "88d37f174ef387c514e741981c69742d2c5dc5d4" have entirely different histories.

3 changed files with 21 additions and 72 deletions

View File

@ -90,8 +90,6 @@ help
exit
```
You can see real life examples [here](https://x.com/NunoSempere/status/1831106442721452312), [here](https://x.com/NunoSempere/status/1829525844169248912), [here](https://x.com/NunoSempere/status/1818810770932568308), [here](https://x.com/NunoSempere/status/1816605190415401100), [here](https://x.com/NunoSempere/status/1816604386703081894), [here](https://x.com/NunoSempere/status/1815169781907042504)
## Tips & tricks
- It's conceptually clearer to have all the multiplications first and then all the divisions

BIN
fermi

Binary file not shown.

View File

@ -65,8 +65,7 @@ func (fs FilledSamples) Samples() []float64 {
}
/* Constants */
const HELP_MSG = "1. Grammar:\n" +
" Operation | Variable assignment | Special\n" +
const HELP_MSG = " Operation | Variable assignment | Special\n" +
" Operation: operator operand\n" +
" operator: (empty) | * | / | + | -\n" +
" operand: scalar | lognormal | beta | variable\n" +
@ -74,9 +73,8 @@ const HELP_MSG = "1. Grammar:\n" +
" beta: beta alpha beta\n" +
" Variable assignment: =: variable_name\n" +
" Variable assignment and clear stack: =. variable_name\n" +
" Special commands: \n" +
" Special: \n" +
" Comment: # this is a comment\n" +
" Summary stats: stats\n" +
" Clear stack: clear | c | .\n" +
" Print debug info: debug | d\n" +
" Print help message: help | h\n" +
@ -100,16 +98,7 @@ const HELP_MSG = "1. Grammar:\n" +
" 1 10\n" +
" + beta 1 100\n" +
" )\n" +
" exit\n" +
"\n" +
"2. Command flags:\n" +
" -echo\n" +
" Specifies whether inputs should be echoed back. Useful if reading from a file\n." +
" -f string\n" +
" Specifies a file with a model to run\n" +
" -n int\n" +
" Specifies the number of samples to draw when using samples (default 100000)\n" +
" -h Shows help message\n"
" exit\n"
const NORMAL90CONFIDENCE = 1.6448536269514727
const INIT_DIST Scalar = Scalar(1)
@ -133,15 +122,14 @@ func prettyPrintDist(dist Dist) {
pretty.PrettyPrintFloat(w)
fmt.Println()
case FilledSamples:
n := len(v.xs)
sorted_xs := make([]float64, n)
sorted_xs := make([]float64, N_SAMPLES)
copy(sorted_xs, v.xs)
sort.Slice(sorted_xs, func(i, j int) bool {
return sorted_xs[i] < sorted_xs[j]
})
low := sorted_xs[int(math.Round(float64(n)*0.05))]
high := sorted_xs[int(math.Round(float64(n)*0.95))]
low := sorted_xs[N_SAMPLES/20]
high := sorted_xs[N_SAMPLES*19/20]
fmt.Printf("=> ")
pretty.PrettyPrint2Floats(low, high)
@ -156,50 +144,10 @@ func prettyPrintDist(dist Dist) {
func printAndReturnErr(err_msg string) error {
fmt.Println(err_msg)
// fmt.Println(HELP_MSG)
fmt.Println("Type \"help\" (without quotes) to see a pseudogrammar and examples")
fmt.Println(HELP_MSG)
return errors.New(err_msg)
}
func prettyPrintStats(dist Dist) {
xs := dist.Samples()
n := len(xs)
mean := 0.0
for i := 0; i < n; i++ {
mean += xs[i]
}
mean /= float64(n)
fmt.Printf("Mean: %f\n", mean)
stdev := 0.0
for i := 0; i < n; i++ {
stdev += math.Pow(xs[i]-mean, 2)
}
stdev = math.Sqrt(stdev / float64(n))
fmt.Printf("Stdev: %f\n", stdev)
sorted_xs := make([]float64, n)
copy(sorted_xs, xs)
sort.Slice(sorted_xs, func(i, j int) bool {
return sorted_xs[i] < sorted_xs[j]
})
print_ci := func(ci float64, prefix string) {
x := sorted_xs[int(math.Round(float64(n)*ci))]
fmt.Printf("%s%f\n", prefix, x)
}
print_ci(0.01, "ci 1%: ")
print_ci(0.05, "ci 5%: ")
print_ci(0.10, "ci 10%: ")
print_ci(0.25, "ci 25%: ")
print_ci(0.50, "ci 50%: ")
print_ci(0.75, "ci 75%: ")
print_ci(0.90, "ci 90%: ")
print_ci(0.95, "ci 95%: ")
print_ci(0.99, "ci 99%: ")
}
/* Operations */
// Generic operations with samples
func operateDistsAsSamples(dist1 Dist, dist2 Dist, op string) (Dist, error) {
@ -223,9 +171,6 @@ func operateDistsAsSamples(dist1 Dist, dist2 Dist, op string) (Dist, error) {
zs[i] = xs[i] + ys[i]
case "-":
zs[i] = xs[i] - ys[i]
default:
fmt.Println("Error: Operation not recognized")
return nil, errors.New("Operation not recognized")
}
}
@ -264,6 +209,8 @@ func multiplyDists(old_dist Dist, new_dist Dist) (Dist, error) {
return multiplyLogDists(o, n), nil
case Scalar:
return multiplyLogDists(o, Lognormal{low: float64(n), high: float64(n)}), nil
default:
return operateDistsAsSamples(old_dist, new_dist, "*")
}
}
case Scalar:
@ -276,15 +223,20 @@ func multiplyDists(old_dist Dist, new_dist Dist) (Dist, error) {
return multiplyLogDists(Lognormal{low: float64(o), high: float64(o)}, n), nil
case Scalar:
return Scalar(float64(o) * float64(n)), nil
default:
return operateDistsAsSamples(old_dist, new_dist, "*")
}
}
case Beta:
switch n := new_dist.(type) {
case Beta:
return multiplyBetaDists(o, n), nil
}
}
default:
return operateDistsAsSamples(old_dist, new_dist, "*")
}
default:
return operateDistsAsSamples(old_dist, new_dist, "*")
}
}
func divideDists(old_dist Dist, new_dist Dist) (Dist, error) {
@ -305,6 +257,8 @@ func divideDists(old_dist Dist, new_dist Dist) (Dist, error) {
return nil, errors.New("Error: division by zero scalar")
}
return multiplyLogDists(o, Lognormal{low: 1.0 / float64(n), high: 1.0 / float64(n)}), nil
default:
return operateDistsAsSamples(old_dist, new_dist, "/")
}
}
case Scalar:
@ -318,10 +272,13 @@ func divideDists(old_dist Dist, new_dist Dist) (Dist, error) {
return nil, errors.New("Error: division by zero scalar")
}
return Scalar(float64(o) / float64(n)), nil
}
}
}
default:
return operateDistsAsSamples(old_dist, new_dist, "/")
}
}
default:
return operateDistsAsSamples(old_dist, new_dist, "/")
}
}
// Generic distribution operations
@ -432,8 +389,6 @@ replForLoop:
case words[0] == "clear" || words[0] == "c" || words[0] == ".":
stack.old_dist = INIT_DIST
fmt.Println()
case words[0] == "stats" || words[0] == "s":
prettyPrintStats(stack.old_dist)
/* Variable assignment */
case words[0] == "=:" && len(words) == 2:
stack.vars[words[1]] = stack.old_dist
@ -461,12 +416,8 @@ func main() {
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.")
help_flag := flag.Bool("h", false, "Shows help message")
flag.Parse()
N_SAMPLES = *num_samples_flag
if *help_flag {
fmt.Println(HELP_MSG)
}
var reader *bufio.Reader = nil
if *filename != "" {