Compare commits
7 Commits
88d37f174e
...
98225e4bb9
Author | SHA1 | Date | |
---|---|---|---|
98225e4bb9 | |||
3924027c31 | |||
4573faee10 | |||
e992a26bd7 | |||
98814edb62 | |||
b01337378a | |||
6686e4f919 |
|
@ -90,6 +90,8 @@ 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
|
||||
|
|
91
fermi.go
91
fermi.go
|
@ -65,7 +65,8 @@ func (fs FilledSamples) Samples() []float64 {
|
|||
}
|
||||
|
||||
/* Constants */
|
||||
const HELP_MSG = " Operation | Variable assignment | Special\n" +
|
||||
const HELP_MSG = "1. Grammar:\n" +
|
||||
" Operation | Variable assignment | Special\n" +
|
||||
" Operation: operator operand\n" +
|
||||
" operator: (empty) | * | / | + | -\n" +
|
||||
" operand: scalar | lognormal | beta | variable\n" +
|
||||
|
@ -73,8 +74,9 @@ const HELP_MSG = " Operation | Variable assignment | Special\n" +
|
|||
" beta: beta alpha beta\n" +
|
||||
" Variable assignment: =: variable_name\n" +
|
||||
" Variable assignment and clear stack: =. variable_name\n" +
|
||||
" Special: \n" +
|
||||
" Special commands: \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" +
|
||||
|
@ -98,7 +100,16 @@ const HELP_MSG = " Operation | Variable assignment | Special\n" +
|
|||
" 1 10\n" +
|
||||
" + beta 1 100\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 INIT_DIST Scalar = Scalar(1)
|
||||
|
@ -122,14 +133,15 @@ func prettyPrintDist(dist Dist) {
|
|||
pretty.PrettyPrintFloat(w)
|
||||
fmt.Println()
|
||||
case FilledSamples:
|
||||
sorted_xs := make([]float64, N_SAMPLES)
|
||||
n := len(v.xs)
|
||||
sorted_xs := make([]float64, n)
|
||||
copy(sorted_xs, v.xs)
|
||||
sort.Slice(sorted_xs, func(i, j int) bool {
|
||||
return sorted_xs[i] < sorted_xs[j]
|
||||
})
|
||||
|
||||
low := sorted_xs[N_SAMPLES/20]
|
||||
high := sorted_xs[N_SAMPLES*19/20]
|
||||
low := sorted_xs[int(math.Round(float64(n)*0.05))]
|
||||
high := sorted_xs[int(math.Round(float64(n)*0.95))]
|
||||
fmt.Printf("=> ")
|
||||
pretty.PrettyPrint2Floats(low, high)
|
||||
|
||||
|
@ -144,10 +156,50 @@ func prettyPrintDist(dist Dist) {
|
|||
|
||||
func printAndReturnErr(err_msg string) error {
|
||||
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)
|
||||
}
|
||||
|
||||
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) {
|
||||
|
@ -171,6 +223,9 @@ 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")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -209,8 +264,6 @@ 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:
|
||||
|
@ -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
|
||||
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, "*")
|
||||
}
|
||||
return operateDistsAsSamples(old_dist, new_dist, "*")
|
||||
}
|
||||
|
||||
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 multiplyLogDists(o, Lognormal{low: 1.0 / float64(n), high: 1.0 / float64(n)}), nil
|
||||
default:
|
||||
return operateDistsAsSamples(old_dist, new_dist, "/")
|
||||
}
|
||||
}
|
||||
case Scalar:
|
||||
|
@ -272,13 +318,10 @@ 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, "/")
|
||||
}
|
||||
return operateDistsAsSamples(old_dist, new_dist, "/")
|
||||
}
|
||||
|
||||
// Generic distribution operations
|
||||
|
@ -389,6 +432,8 @@ 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
|
||||
|
@ -416,8 +461,12 @@ 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 != "" {
|
||||
|
|
Loading…
Reference in New Issue
Block a user