Compare commits

...

7 Commits

3 changed files with 72 additions and 21 deletions

View File

@ -90,6 +90,8 @@ help
exit 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 ## Tips & tricks
- 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

BIN
fermi Executable file

Binary file not shown.

View File

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