continue with feng shui

This commit is contained in:
NunoSempere 2024-06-18 22:02:42 -04:00
parent a212a6c3af
commit 039ff56bbe

207
fermi.go
View File

@ -12,29 +12,62 @@ import (
"strings"
)
const NORMAL90CONFIDENCE = 1.6448536269514727
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 = 100_000
/* Types and interfaces */
// Distribution interface
// https://go.dev/tour/methods/9
type Stack struct {
old_dist Dist
vars map[string]Dist
}
type Dist interface {
Samples() []float64
}
type Scalar float64
type Lognormal struct {
low float64
high float64
}
type Beta struct {
a float64
b float64
}
type FilledSamples struct {
xs []float64
}
/* 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
/* Dist interface functions */
// https://go.dev/tour/methods/9
func (p Scalar) Samples() []float64 {
xs := make([]float64, N_SAMPLES)
for i := 0; i < N_SAMPLES; i++ {
@ -42,29 +75,34 @@ func (p Scalar) Samples() []float64 {
}
return xs
}
func (ln Lognormal) Samples() []float64 {
sampler := func(r sample.Src) float64 { return sample.Sample_to(ln.low, ln.high, r) }
// return sample.Sample_parallel(sampler, N_SAMPLES)
// Can't do parallel because then I'd have to await throughout the code
return sample.Sample_serially(sampler, N_SAMPLES)
}
func (beta Beta) Samples() []float64 {
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_serially(sampler, N_SAMPLES)
}
func (fs FilledSamples) Samples() []float64 {
return fs.xs
}
// Parse line into Distribution
func parseLineErr(err_msg string) (string, Dist, error) {
fmt.Println(GENERAL_ERR_MSG)
fmt.Println(err_msg)
var errorDist Dist
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), " ")
op := ""
@ -138,7 +176,7 @@ func multiplyBetaDists(beta1 Beta, beta2 Beta) Beta {
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()
ys := dist2.Samples()
@ -179,7 +217,7 @@ func multiplyDists(old_dist Dist, new_dist Dist) (Dist, error) {
case Scalar:
return multiplyLogDists(o, Lognormal{low: float64(n), high: float64(n)}), nil
default:
return operateAsSamples(old_dist, new_dist, "*")
return operateDistsAsSamples(old_dist, new_dist, "*")
}
}
case Scalar:
@ -193,7 +231,7 @@ func multiplyDists(old_dist Dist, new_dist Dist) (Dist, error) {
case Scalar:
return Scalar(float64(o) * float64(n)), nil
default:
return operateAsSamples(old_dist, new_dist, "*")
return operateDistsAsSamples(old_dist, new_dist, "*")
}
}
case Beta:
@ -201,10 +239,10 @@ func multiplyDists(old_dist Dist, new_dist Dist) (Dist, error) {
case Beta:
return multiplyBetaDists(o, n), nil
default:
return operateAsSamples(old_dist, new_dist, "*")
return operateDistsAsSamples(old_dist, new_dist, "*")
}
default:
return operateAsSamples(old_dist, new_dist, "*")
return operateDistsAsSamples(old_dist, new_dist, "*")
}
}
@ -219,7 +257,7 @@ func divideDists(old_dist Dist, new_dist Dist) (Dist, error) {
case Scalar:
return multiplyLogDists(o, Lognormal{low: 1.0 / float64(n), high: 1.0 / float64(n)}), nil
default:
return operateAsSamples(old_dist, new_dist, "/")
return operateDistsAsSamples(old_dist, new_dist, "/")
}
}
case Scalar:
@ -230,31 +268,16 @@ func divideDists(old_dist Dist, new_dist Dist) (Dist, error) {
case Scalar:
return Scalar(float64(o) / float64(n)), nil
default:
return operateAsSamples(old_dist, new_dist, "/")
return operateDistsAsSamples(old_dist, new_dist, "/")
}
}
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) {
switch op {
case "*":
return multiplyDists(old_dist, new_dist)
case "/":
return divideDists(old_dist, new_dist)
case "+":
return operateAsSamples(old_dist, new_dist, "+")
case "-":
return operateAsSamples(old_dist, new_dist, "-")
default:
return old_dist, errors.New("Can't combine distributions in this way")
}
}
/* Pretty print distributions */
/* Pretty print distributions
*/
func prettyPrintInt(n int) {
switch {
case math.Abs(float64(n)) >= 1_000_000_000_000:
@ -338,11 +361,32 @@ func prettyPrintDist(dist Dist) {
/* Combine old dist and new line */
// We want this as a function to be able to have parenthesis/recusion, possibly functions
const init_dist Scalar = Scalar(1)
func combineStackAndDist(stack Stack, new_dist Dist, op string) Stack {
var combined_dist Dist
var err error
switch op {
case "*":
if combined_dist, err = multiplyDists(stack.old_dist, new_dist); err == nil {
stack.old_dist = combined_dist
}
case "/":
if combined_dist, err = divideDists(stack.old_dist, new_dist); err == nil {
stack.old_dist = combined_dist
}
case "+":
if combined_dist, err = operateDistsAsSamples(stack.old_dist, new_dist, "+"); err == nil {
stack.old_dist = combined_dist
}
case "-":
if combined_dist, err = operateDistsAsSamples(stack.old_dist, new_dist, "-"); err == nil {
stack.old_dist = combined_dist
}
default:
fmt.Println("Can't combine distributions in this way")
}
return stack
type Stack struct {
old_dist Dist
vars map[string]Dist
}
func combineStackAndNewLine(stack Stack, new_line string) Stack {
@ -351,67 +395,58 @@ func combineStackAndNewLine(stack Stack, new_line string) Stack {
return stack
}
{
words := strings.Split(strings.TrimSpace(new_line), " ")
switch {
case words[0] == "exit" || words[0] == "e":
os.Exit(0)
case words[0] == "help" || words[0] == "h":
fmt.Println(GENERAL_ERR_MSG)
return stack
case words[0] == "debug" || words[0] == "d":
fmt.Printf("Old dist: %v\n", stack.old_dist)
fmt.Printf("Vars: %v\n", stack.vars)
return stack
case words[0] == "=:" && len(words) == 2:
stack.vars[words[1]] = stack.old_dist
fmt.Printf("%s ", words[1])
prettyPrintDist(stack.old_dist)
return stack
case words[0] == "." || words[0] == "clean" || words[0] == "c":
stack.old_dist = init_dist
fmt.Println()
return stack
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()
return stack
// 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(new_line, stack.vars)
if err != nil {
// Various stack manipulations that don't involve distributions
words := strings.Split(strings.TrimSpace(new_line), " ")
switch {
case words[0] == "exit" || words[0] == "e":
os.Exit(0)
case words[0] == "help" || words[0] == "h":
fmt.Println(GENERAL_ERR_MSG)
return stack
}
joint_dist, err := joinDists(stack.old_dist, new_dist, op)
if err != nil {
fmt.Printf("%v\n", err)
fmt.Printf("Dist on stack: ")
case words[0] == "debug" || words[0] == "d":
fmt.Printf("Old dist: %v\n", stack.old_dist)
fmt.Printf("Vars: %v\n", stack.vars)
return stack
case words[0] == "=:" && len(words) == 2:
stack.vars[words[1]] = stack.old_dist
fmt.Printf("%s ", words[1])
prettyPrintDist(stack.old_dist)
return stack
case words[0] == "." || words[0] == "clear" || words[0] == "c":
stack.old_dist = INIT_DIST
fmt.Println()
return stack
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()
return stack
// Other possible cases: save to file, sample n samples, save stack to a variable, clear stack, define a function
}
stack.old_dist = joint_dist
op, new_dist, err := parseLineIntoOpAndDist(new_line, stack.vars)
if err != nil {
return stack
}
stack = combineStackAndDist(stack, new_dist, op)
prettyPrintDist(stack.old_dist)
return stack
}
func runRepl(stack Stack, reader *bufio.Reader) Stack {
for {
new_line, _ := reader.ReadString('\n')
stack = combineStackAndNewLine(stack, new_line)
}
}
/* Main event loop */
func main() {
reader := bufio.NewReader(os.Stdin)
stack := Stack{old_dist: init_dist, vars: make(map[string]Dist)}
for {
new_line, _ := reader.ReadString('\n')
stack = combineStackAndNewLine(stack, new_line)
}
stack := Stack{old_dist: INIT_DIST, vars: make(map[string]Dist)}
runRepl(stack, reader)
}