Compare commits

...

8 Commits

4 changed files with 228 additions and 152 deletions

View File

@ -11,7 +11,7 @@ Sometimes, [Squiggle](https://github.com/quantified-uncertainty/squiggle), [simp
Here is an example Here is an example
``` ```
$ go run f.go $ go run fermi.go
5000000 12000000 5000000 12000000
=> 5.0M 12.0M => 5.0M 12.0M
* beta 1 200 * beta 1 200
@ -31,7 +31,7 @@ $ go run f.go
Perhaps this example is more understandable with comments and better units: Perhaps this example is more understandable with comments and better units:
``` ```
$ sed -u "s|#.*||" | sed -u 's|M|000000|g' | go run f.go $ sed -u "s|#.*||" | sed -u 's|M|000000|g' | go run fermi.go
5M 12M # number of people living in Chicago 5M 12M # number of people living in Chicago
=> 5.0M 12.0M => 5.0M 12.0M
* beta 1 200 # fraction of people that have a piano * beta 1 200 # fraction of people that have a piano
@ -79,7 +79,7 @@ The difference between `=: x` and `=. y` is that `=.` clears the stack after the
``` ```
make build make build
sudo make install sudo make install
f # rather than the previous go run f.go f # rather than the previous go run fermi.go
``` ```
Why use make instead of the built-in go commands? Because the point of make is to be able to share command-line recipes. Why use make instead of the built-in go commands? Because the point of make is to be able to share command-line recipes.
@ -93,8 +93,8 @@ sed -u "s|#.*||" | sed -u 's|M|000000|g' | f
cat more/piano-tuners.f | f cat more/piano-tuners.f | f
cat more/piano-tuners-commented.f | sed -u "s|#.*||" | sed -u 's|M|000000|g' | f cat more/piano-tuners-commented.f | sed -u "s|#.*||" | sed -u 's|M|000000|g' | f
tee -a input.log | go run f.go | tee -a output.log tee -a input.log | go run fermi.go | tee -a output.log
tee -a io.log | go run f.go | tee -a io.log tee -a io.log | go run fermi.go | tee -a io.log
function f(){ function f(){
sed -u "s|#.*||" | sed -u "s|#.*||" |

View File

@ -5,6 +5,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"git.nunosempere.com/NunoSempere/fermi/sample" "git.nunosempere.com/NunoSempere/fermi/sample"
"git.nunosempere.com/NunoSempere/fermi/pretty"
"math" "math"
"os" "os"
"sort" "sort"
@ -12,29 +13,35 @@ import (
"strings" "strings"
) )
const NORMAL90CONFIDENCE = 1.6448536269514727 /* Types and interfaces */
const GENERAL_ERR_MSG = "Valid inputs: 2 || * 2 || / 2 || 2 20 || * 2 20 || / 2 20 || clean || =: var || op var || clean || help || debug || exit"
const N_SAMPLES = 1_000_000
// Distribution interface type Stack struct {
// https://go.dev/tour/methods/9 old_dist Dist
vars map[string]Dist
}
type Dist interface { type Dist interface {
Samples() []float64 Samples() []float64
} }
type Scalar float64 type Scalar float64
type Lognormal struct { type Lognormal struct {
low float64 low float64
high float64 high float64
} }
type Beta struct { type Beta struct {
a float64 a float64
b float64 b float64
} }
type FilledSamples struct { type FilledSamples struct {
xs []float64 xs []float64
} }
/* Dist interface functions */
// https://go.dev/tour/methods/9
func (p Scalar) Samples() []float64 { func (p Scalar) Samples() []float64 {
xs := make([]float64, N_SAMPLES) xs := make([]float64, N_SAMPLES)
for i := 0; i < N_SAMPLES; i++ { for i := 0; i < N_SAMPLES; i++ {
@ -42,21 +49,88 @@ func (p Scalar) Samples() []float64 {
} }
return xs return xs
} }
func (ln Lognormal) Samples() []float64 { func (ln Lognormal) Samples() []float64 {
sampler := func(r sample.Src) float64 { return sample.Sample_to(ln.low, ln.high, r) } sampler := func(r sample.Src) float64 { return sample.Sample_to(ln.low, ln.high, r) }
// return sample.Sample_parallel(sampler, N_SAMPLES) // return sample.Sample_parallel(sampler, N_SAMPLES)
// Can't do parallel because then I'd have to await throughout the code // Can't do parallel because then I'd have to await throughout the code
return sample.Sample_serially(sampler, N_SAMPLES) return sample.Sample_serially(sampler, N_SAMPLES)
} }
func (beta Beta) Samples() []float64 { func (beta Beta) Samples() []float64 {
sampler := func(r sample.Src) float64 { return sample.Sample_beta(beta.a, beta.b, r) } sampler := func(r sample.Src) float64 { return sample.Sample_beta(beta.a, beta.b, r) }
// return sample.Sample_parallel(sampler, N_SAMPLES) // return sample.Sample_parallel(sampler, N_SAMPLES)
return sample.Sample_serially(sampler, N_SAMPLES) return sample.Sample_serially(sampler, N_SAMPLES)
} }
func (fs FilledSamples) Samples() []float64 { func (fs FilledSamples) Samples() []float64 {
return fs.xs return fs.xs
} }
/* Constants */
const GENERAL_ERR_MSG = " Operation | Variable assignment | Special\n" +
" Operation: operator operand\n" +
" operator: (empty) | * | / | + | -\n" +
" operand: scalar | lognormal | beta | variable\n" +
" lognormal: low high\n" +
" beta: beta alpha beta\n" +
" Variable assignment: =: variable_name\n" +
" Clear stack: . | c | clear\n" +
" Variable assignment and clear stack: =. variable_name\n" +
" Other special operations: help | debug | exit\n" +
" Examples: \n" +
" + 2\n" +
" / 2.5\n" +
" * 1 10 (interpreted as lognormal)\n" +
" + 1 10\n" +
" * beta 1 10\n" +
" 1 10 (multiplication taken as default operation)\n" +
" =: x\n" +
" .\n" +
" 1 100\n" +
" + x\n" +
" exit\n"
const NORMAL90CONFIDENCE = 1.6448536269514727
const INIT_DIST Scalar = Scalar(1)
const N_SAMPLES = 100_000
/* Pretty print for distributions */
// Needs types
func prettyPrintDist(dist Dist) {
switch v := dist.(type) {
case Lognormal:
fmt.Printf("=> ")
pretty.PrettyPrint2Floats(v.low, v.high)
case FilledSamples:
tmp_xs := make([]float64, N_SAMPLES)
copy(tmp_xs, v.xs)
sort.Slice(tmp_xs, func(i, j int) bool {
return tmp_xs[i] < tmp_xs[j]
})
low_int := N_SAMPLES / 20
low := tmp_xs[low_int]
high_int := N_SAMPLES * 19 / 20
high := tmp_xs[high_int]
fmt.Printf("=> ")
pretty.PrettyPrintFloat(low)
fmt.Printf(" ")
pretty.PrettyPrintFloat(high)
fmt.Printf(" (")
pretty.PrettyPrintInt(N_SAMPLES)
fmt.Printf(" samples)\n")
case Beta:
fmt.Printf("=> beta ")
pretty.PrettyPrint2Floats(v.a, v.b)
case Scalar:
fmt.Printf("=> scalar ")
w := float64(v)
pretty.PrettyPrintFloat(w)
fmt.Println()
default:
fmt.Printf("%v", v)
}
}
// Parse line into Distribution // Parse line into Distribution
func parseLineErr(err_msg string) (string, Dist, error) { func parseLineErr(err_msg string) (string, Dist, error) {
fmt.Println(GENERAL_ERR_MSG) fmt.Println(GENERAL_ERR_MSG)
@ -64,7 +138,8 @@ func parseLineErr(err_msg string) (string, Dist, error) {
var errorDist Dist var errorDist Dist
return "", errorDist, errors.New(err_msg) return "", errorDist, errors.New(err_msg)
} }
func parseLine(line string, vars map[string]Dist) (string, Dist, error) {
func parseLineIntoOpAndDist(line string, vars map[string]Dist) (string, Dist, error) {
words := strings.Split(strings.TrimSpace(line), " ") words := strings.Split(strings.TrimSpace(line), " ")
op := "" op := ""
@ -138,7 +213,7 @@ func multiplyBetaDists(beta1 Beta, beta2 Beta) Beta {
return Beta{a: beta1.a + beta2.a, b: beta1.b + beta2.b} return Beta{a: beta1.a + beta2.a, b: beta1.b + beta2.b}
} }
func operateAsSamples(dist1 Dist, dist2 Dist, op string) (Dist, error) { func operateDistsAsSamples(dist1 Dist, dist2 Dist, op string) (Dist, error) {
xs := dist1.Samples() xs := dist1.Samples()
ys := dist2.Samples() ys := dist2.Samples()
@ -179,7 +254,7 @@ func multiplyDists(old_dist Dist, new_dist Dist) (Dist, error) {
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: default:
return operateAsSamples(old_dist, new_dist, "*") return operateDistsAsSamples(old_dist, new_dist, "*")
} }
} }
case Scalar: case Scalar:
@ -193,7 +268,7 @@ func multiplyDists(old_dist Dist, new_dist Dist) (Dist, error) {
case Scalar: case Scalar:
return Scalar(float64(o) * float64(n)), nil return Scalar(float64(o) * float64(n)), nil
default: default:
return operateAsSamples(old_dist, new_dist, "*") return operateDistsAsSamples(old_dist, new_dist, "*")
} }
} }
case Beta: case Beta:
@ -201,10 +276,10 @@ func multiplyDists(old_dist Dist, new_dist Dist) (Dist, error) {
case Beta: case Beta:
return multiplyBetaDists(o, n), nil return multiplyBetaDists(o, n), nil
default: default:
return operateAsSamples(old_dist, new_dist, "*") return operateDistsAsSamples(old_dist, new_dist, "*")
} }
default: default:
return operateAsSamples(old_dist, new_dist, "*") return operateDistsAsSamples(old_dist, new_dist, "*")
} }
} }
@ -215,11 +290,13 @@ func divideDists(old_dist Dist, new_dist Dist) (Dist, error) {
{ {
switch n := new_dist.(type) { switch n := new_dist.(type) {
case Lognormal: case Lognormal:
// to do: check division by zero
return multiplyLogDists(o, Lognormal{low: 1.0 / n.high, high: 1.0 / n.low}), nil return multiplyLogDists(o, Lognormal{low: 1.0 / n.high, high: 1.0 / n.low}), nil
case Scalar: case Scalar:
// to do: check division by zero
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: default:
return operateAsSamples(old_dist, new_dist, "/") return operateDistsAsSamples(old_dist, new_dist, "/")
} }
} }
case Scalar: case Scalar:
@ -228,91 +305,100 @@ func divideDists(old_dist Dist, new_dist Dist) (Dist, error) {
case Lognormal: case Lognormal:
return multiplyLogDists(Lognormal{low: float64(o), high: float64(o)}, Lognormal{low: 1.0 / n.high, high: 1.0 / n.low}), nil return multiplyLogDists(Lognormal{low: float64(o), high: float64(o)}, Lognormal{low: 1.0 / n.high, high: 1.0 / n.low}), nil
case Scalar: case Scalar:
// to do: check division by zero
return Scalar(float64(o) / float64(n)), nil return Scalar(float64(o) / float64(n)), nil
default: default:
return operateAsSamples(old_dist, new_dist, "/") return operateDistsAsSamples(old_dist, new_dist, "/")
} }
} }
default: default:
return operateAsSamples(old_dist, new_dist, "/") return operateDistsAsSamples(old_dist, new_dist, "/")
} }
} }
func joinDists(old_dist Dist, new_dist Dist, op string) (Dist, error) { /* Combine old dist and new line */
// We want this as a function to be able to have parenthesis/recusion, possibly functions
func operateStackWithDist(stack Stack, new_dist Dist, op string) Stack {
var combined_dist Dist
var err error
switch op { switch op {
case "*": case "*":
return multiplyDists(old_dist, new_dist) if combined_dist, err = multiplyDists(stack.old_dist, new_dist); err == nil {
stack.old_dist = combined_dist
}
case "/": case "/":
return divideDists(old_dist, new_dist) if combined_dist, err = divideDists(stack.old_dist, new_dist); err == nil {
stack.old_dist = combined_dist
}
case "+": case "+":
return operateAsSamples(old_dist, new_dist, "+") if combined_dist, err = operateDistsAsSamples(stack.old_dist, new_dist, "+"); err == nil {
stack.old_dist = combined_dist
}
case "-": case "-":
return operateAsSamples(old_dist, new_dist, "-") if combined_dist, err = operateDistsAsSamples(stack.old_dist, new_dist, "-"); err == nil {
default: stack.old_dist = combined_dist
return old_dist, errors.New("Can't combine distributions in this way")
} }
default:
fmt.Println("Can't combine distributions in this way")
}
return stack
} }
/* Pretty print distributions */ func runRepl(stack Stack, reader *bufio.Reader) Stack {
func prettyPrintFloat(f float64) {
replForLoop:
for {
new_line, _ := reader.ReadString('\n')
words := strings.Split(strings.TrimSpace(new_line), " ")
switch { switch {
case math.Abs(f) >= 1_000_000_000_000: /* Empty line case */
fmt.Printf("%.1fT", f/1_000_000_000_000) case strings.TrimSpace(new_line) == "":
case math.Abs(f) >= 1_000_000_000: continue replForLoop
fmt.Printf("%.1fB", f/1_000_000_000) /* Parenthesis */
case math.Abs(f) >= 1_000_000: case len(words) == 2 && (words[0] == "*" || words[0] == "+" || words[0] == "-" || words[0] == "/") && words[1] == "(":
fmt.Printf("%.1fM", f/1_000_000) new_stack := runRepl(Stack{old_dist: INIT_DIST, vars: stack.vars}, reader) // to do: think if I want to shadow variables or not (right now, variables persist, since I'm copying the map(.
case math.Abs(f) >= 1_000: stack = operateStackWithDist(stack, new_stack.old_dist, words[0])
fmt.Printf("%.1fK", f/1_000) prettyPrintDist(stack.old_dist)
case len(words) == 1 && words[0] == ")":
case math.Abs(f) <= 0.0001: return stack
fmt.Printf("%.5f", f) /* Special operations */
case math.Abs(f) <= 0.001: case words[0] == "exit" || words[0] == "e":
fmt.Printf("%.4f", f) os.Exit(0)
case math.Abs(f) <= 0.01: case words[0] == "help" || words[0] == "h":
fmt.Printf("%.3f", f) fmt.Println(GENERAL_ERR_MSG)
case math.Abs(f) <= 0.1: continue replForLoop
fmt.Printf("%.2f", f) case words[0] == "debug" || words[0] == "d":
default: fmt.Printf("Old dist: %v\n", stack.old_dist)
fmt.Printf("%.1f", f) fmt.Printf("Vars: %v\n", stack.vars)
} continue replForLoop
case words[0] == "clear" || words[0] == "c" || words[0] == ".":
} stack.old_dist = INIT_DIST
func prettyPrint2Floats(low float64, high float64) {
prettyPrintFloat(low)
fmt.Printf(" ")
prettyPrintFloat(high)
fmt.Printf("\n")
}
func prettyPrintDist(dist Dist) {
switch v := dist.(type) {
case Lognormal:
fmt.Printf("=> ")
prettyPrint2Floats(v.low, v.high)
case FilledSamples:
tmp_xs := make([]float64, N_SAMPLES)
copy(tmp_xs, v.xs)
sort.Slice(tmp_xs, func(i, j int) bool {
return tmp_xs[i] < tmp_xs[j]
})
low_int := N_SAMPLES / 20
low := tmp_xs[low_int]
high_int := N_SAMPLES * 19 / 20
high := tmp_xs[high_int]
fmt.Printf("=> samples ")
prettyPrint2Floats(low, high)
case Beta:
fmt.Printf("=> beta ")
prettyPrint2Floats(v.a, v.b)
case Scalar:
fmt.Printf("=> scalar ")
w := float64(v)
prettyPrintFloat(w)
fmt.Println() fmt.Println()
continue replForLoop
/* Variable assignment */
case words[0] == "=:" && len(words) == 2:
stack.vars[words[1]] = stack.old_dist
fmt.Printf("%s ", words[1])
prettyPrintDist(stack.old_dist)
continue replForLoop
case words[0] == "=." && len(words) == 2:
stack.vars[words[1]] = stack.old_dist
fmt.Printf("%s ", words[1])
prettyPrintDist(stack.old_dist)
stack.old_dist = INIT_DIST
fmt.Println()
continue replForLoop
default: default:
fmt.Printf("%v", v) op, new_dist, err := parseLineIntoOpAndDist(new_line, stack.vars)
if err != nil {
continue replForLoop
}
stack = operateStackWithDist(stack, new_dist, op)
prettyPrintDist(stack.old_dist)
}
} }
} }
@ -320,69 +406,6 @@ func prettyPrintDist(dist Dist) {
func main() { func main() {
reader := bufio.NewReader(os.Stdin) reader := bufio.NewReader(os.Stdin)
var init_dist Dist stack := Stack{old_dist: INIT_DIST, vars: make(map[string]Dist)}
init_dist = Scalar(1) // Lognormal{low: 1, high: 1} runRepl(stack, reader)
old_dist := init_dist
vars := make(map[string]Dist)
// Could eventually be a more complex struct with:
// { Dist, VariableMaps, ConfigParams } or smth
EventForLoop:
for {
input, _ := reader.ReadString('\n')
if strings.TrimSpace(input) == "" {
continue EventForLoop
}
{
words := strings.Split(strings.TrimSpace(input), " ")
switch {
case words[0] == "exit" || words[0] == "e":
break EventForLoop
case words[0] == "help" || words[0] == "h":
fmt.Println(GENERAL_ERR_MSG)
continue EventForLoop
case words[0] == "debug" || words[0] == "d":
fmt.Printf("Old dist: %v\n", old_dist)
fmt.Printf("Vars: %v\n", vars)
continue EventForLoop
case words[0] == "=:" && len(words) == 2:
vars[words[1]] = old_dist
fmt.Printf("%s ", words[1])
prettyPrintDist(old_dist)
continue EventForLoop
case words[0] == "." || words[0] == "clean" || words[0] == "c":
old_dist = init_dist
fmt.Println()
continue EventForLoop
case words[0] == "=." && len(words) == 2:
vars[words[1]] = old_dist
fmt.Printf("%s ", words[1])
prettyPrintDist(old_dist)
old_dist = init_dist
fmt.Println()
continue EventForLoop
// Other possible cases:
// Save to file
// Sample n samples
// Save stack to a variable?
// clean stack
// Define a function? No, too much of a nerdsnipea
}
}
op, new_dist, err := parseLine(input, vars)
if err != nil {
continue EventForLoop
}
joint_dist, err := joinDists(old_dist, new_dist, op)
if err != nil {
fmt.Printf("%v\n", err)
fmt.Printf("Dist on stack: ")
prettyPrintDist(old_dist)
continue EventForLoop
}
old_dist = joint_dist
prettyPrintDist(old_dist)
}
} }

View File

@ -1,8 +1,9 @@
run: run:
go run f.go go run fermi.go
build: build:
go build f.go go build fermi.go
install: f install: fermi
sudo mv f /usr/bin/f sudo mv fermi /usr/bin/fermi
sudo ln -s /usr/bin/fermi /usr/bin/f

52
pretty/pretty.go Normal file
View File

@ -0,0 +1,52 @@
package pretty
import (
"fmt"
"math"
)
func PrettyPrintInt(n int) {
switch {
case math.Abs(float64(n)) >= 1_000_000_000_000:
fmt.Printf("%dT", n/1_000_000_000_000)
case math.Abs(float64(n)) >= 1_000_000_000:
fmt.Printf("%dB", n/1_000_000_000)
case math.Abs(float64(n)) >= 1_000_000:
fmt.Printf("%dM", n/1_000_000)
case math.Abs(float64(n)) >= 1_000:
fmt.Printf("%dK", n/1_000)
default:
fmt.Printf("%df", n)
}
}
func PrettyPrintFloat(f float64) {
switch {
case math.Abs(f) >= 1_000_000_000_000:
fmt.Printf("%.1fT", f/1_000_000_000_000)
case math.Abs(f) >= 1_000_000_000:
fmt.Printf("%.1fB", f/1_000_000_000)
case math.Abs(f) >= 1_000_000:
fmt.Printf("%.1fM", f/1_000_000)
case math.Abs(f) >= 1_000:
fmt.Printf("%.1fK", f/1_000)
case math.Abs(f) <= 0.0001:
fmt.Printf("%.5f", f)
case math.Abs(f) <= 0.001:
fmt.Printf("%.4f", f)
case math.Abs(f) <= 0.01:
fmt.Printf("%.3f", f)
case math.Abs(f) <= 0.1:
fmt.Printf("%.2f", f)
default:
fmt.Printf("%.1f", f)
}
}
func PrettyPrint2Floats(low float64, high float64) {
PrettyPrintFloat(low)
fmt.Printf(" ")
PrettyPrintFloat(high)
fmt.Printf("\n")
}