fermi/f.go

338 lines
8.6 KiB
Go
Raw Normal View History

2024-05-10 18:05:03 +00:00
package main
import (
"bufio"
2024-06-09 13:15:53 +00:00
"errors"
2024-05-10 18:05:03 +00:00
"fmt"
2024-06-09 20:46:08 +00:00
"git.nunosempere.com/NunoSempere/fermi/sample"
2024-05-10 18:25:23 +00:00
"math"
2024-05-10 18:05:03 +00:00
"os"
"strconv"
"strings"
)
2024-05-10 18:25:23 +00:00
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"
// Distribution interface
// https://go.dev/tour/methods/9
2024-06-09 21:35:36 +00:00
type Dist interface {
Samples() []float64
}
// Point implementing distribution
type Scalar struct {
p float64
}
func (p Scalar) Samples() []float64 {
xs := make([]float64, 1_000_000)
for i := 0; i < 1_000_000; i++ {
xs[i] = p.p
}
return xs
}
// Lognormal implementing Distribution
type Lognormal struct {
low float64
high float64
}
func (ln Lognormal) Samples() []float64 {
sampler := func(r sample.Src) float64 { return sample.Sample_to(ln.low, ln.high, r) }
2024-06-09 20:46:08 +00:00
return sample.Sample_parallel(sampler, 1_000_000)
}
2024-06-09 21:35:36 +00:00
func (ln Lognormal) Type() string {
return "Lognormal"
}
2024-05-10 18:25:23 +00:00
// Beta implementing Distribution
type Beta struct {
a float64
b float64
}
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, 1_000_000)
2024-06-09 21:35:36 +00:00
}
func (beta Beta) Type() string {
return "Beta"
}
// FilledSamples implementing Distribution
type FilledSamples struct {
xs []float64
}
func (fs FilledSamples) Samples() []float64 {
return fs.xs
}
2024-06-09 21:35:36 +00:00
func (beta FilledSamples) Type() string {
return "FilledSamples"
}
2024-06-09 13:15:53 +00:00
// Parse line into Distribution
func parseLineErr(err_msg string) (string, Dist, error) {
fmt.Println(GENERAL_ERR_MSG)
2024-06-09 13:15:53 +00:00
fmt.Println(err_msg)
2024-06-09 21:35:36 +00:00
var errorDist Dist
return "", errorDist, errors.New(err_msg)
2024-05-10 18:25:23 +00:00
}
2024-06-09 13:15:53 +00:00
func parseLine(line string, vars map[string]Dist) (string, Dist, error) {
words := strings.Split(strings.TrimSpace(line), " ")
op := ""
var dist Dist
switch words[0] {
case "*":
op = "*"
words = words[1:]
case "/":
op = "/"
words = words[1:]
case "+":
return parseLineErr("+ operation not implemented yet")
case "-":
return parseLineErr("- operation not implemented yet")
default:
op = "*" // later, change the below to
}
switch len(words) {
case 0:
return parseLineErr("Operator must have operand; can't operate on nothing")
case 1:
var_word, var_word_exists := vars[words[0]]
2024-06-09 13:25:50 +00:00
single_float, err1 := strconv.ParseFloat(words[0], 64) // abstract this away to search for K/M/B/T/etc.
2024-06-09 13:15:53 +00:00
switch {
case var_word_exists:
dist = var_word
case err1 == nil:
2024-06-09 21:35:36 +00:00
dist = Lognormal{low: single_float, high: single_float}
2024-06-09 13:15:53 +00:00
case err1 != nil && !var_word_exists:
return parseLineErr("Trying to operate on a scalar, but scalar is neither a float nor an assigned variable")
}
case 2:
new_low, err1 := strconv.ParseFloat(words[0], 64)
new_high, err2 := strconv.ParseFloat(words[1], 64)
if err1 != nil || err2 != nil {
return parseLineErr("Trying to operate by a distribution, but distribution is not specified as two floats")
}
2024-06-09 21:35:36 +00:00
dist = Lognormal{low: new_low, high: new_high}
2024-06-09 13:15:53 +00:00
default:
return parseLineErr("Other input methods not implemented yet")
}
return op, dist, nil
2024-05-10 18:25:23 +00:00
}
2024-06-09 13:15:53 +00:00
// Join distributions
// Multiply lognormals
func multiplyLogDists(l1 Lognormal, l2 Lognormal) Lognormal {
logmean1 := (math.Log(l1.high) + math.Log(l1.low)) / 2.0
logstd1 := (math.Log(l1.high) - math.Log(l1.low)) / (2.0 * NORMAL90CONFIDENCE)
2024-05-10 18:25:23 +00:00
2024-06-09 13:15:53 +00:00
logmean2 := (math.Log(l2.high) + math.Log(l2.low)) / 2.0
logstd2 := (math.Log(l2.high) - math.Log(l2.low)) / (2.0 * NORMAL90CONFIDENCE)
logmean_product := logmean1 + logmean2
logstd_product := math.Sqrt(logstd1*logstd1 + logstd2*logstd2)
h := logstd_product * NORMAL90CONFIDENCE
loglow := logmean_product - h
loghigh := logmean_product + h
return Lognormal{low: math.Exp(loglow), high: math.Exp(loghigh)}
2024-05-10 18:25:23 +00:00
2024-05-10 18:07:58 +00:00
}
func multiplyBetaDists(beta1 Beta, beta2 Beta) Beta {
return Beta{a: beta1.a + beta2.a, b: beta1.b + beta2.b}
}
func multiplyAsSamples(dist1 Dist, dist2 Dist) Dist {
xs := dist1.Samples()
ys := dist2.Samples()
zs := make([]float64, 1_000_000)
for i, x := range xs {
zs[i] = x * ys[i]
2024-06-09 13:15:53 +00:00
}
2024-06-09 21:35:36 +00:00
return FilledSamples{xs: xs}
}
func multiplyDists(old_dist Dist, new_dist Dist) (Dist, error) {
switch o := old_dist.(type) {
case Lognormal:
{
switch n := new_dist.(type) {
case Lognormal:
return multiplyLogDists(o, n), nil
case Scalar:
return multiplyLogDists(o, Lognormal{low: n.p, high: n.p}), nil
default:
return multiplyAsSamples(o, n), nil
}
}
case Scalar:
{
switch n := new_dist.(type) {
case Lognormal:
return multiplyLogDists(Lognormal{low: o.p, high: o.p}, n), nil
case Scalar:
return Scalar{p: o.p * n.p}, nil
default:
return multiplyAsSamples(o, n), nil
}
}
default:
return nil, errors.New("Can't multiply dists")
}
}
func joinDists(old_dist Dist, new_dist Dist, op string) (Dist, error) {
switch op {
case "*":
return multiplyDists(old_dist, new_dist)
default:
return old_dist, errors.New("Can't combine distributions in this way")
}
2024-06-09 21:35:36 +00:00
/*
switch {
case old_dist.Type == "Lognormal" && new_dist.Type == "Lognormal" && op == "*":
return Dist{Type: "Lognormal", Lognormal: multiplyLogDists(old_dist.Lognormal, new_dist.Lognormal), Samples: nil}, nil
case old_dist.Type == "Lognormal" && new_dist.Type == "Lognormal" && op == "/":
tmp_dist := Lognormal{low: 1.0 / new_dist.Lognormal.high, high: 1.0 / new_dist.Lognormal.low}
return Dist{Type: "Lognormal", Lognormal: multiplyLogDists(old_dist.Lognormal, tmp_dist), Samples: nil}, nil
default:
fmt.Printf("For now, can't do anything besides multiplying lognormals\n")
}
*/
// return old_dist, errors.New("Can't combine distributions in this way")
2024-06-09 13:15:53 +00:00
}
/* Pretty print distributions */
func prettyPrintLognormal(low float64, high float64) {
2024-06-03 06:45:33 +00:00
// fmt.Printf("=> %.1f %.1f\n", low, high)
fmt.Printf("=> ")
switch {
case math.Abs(low) >= 1_000_000_000_000:
fmt.Printf("%.1fT", low/1_000_000_000_000)
case math.Abs(low) >= 1_000_000_000:
fmt.Printf("%.1fB", low/1_000_000_000)
case math.Abs(low) >= 1_000_000:
fmt.Printf("%.1fM", low/1_000_000)
case math.Abs(low) >= 1_000:
fmt.Printf("%.1fK", low/1_000)
case math.Abs(low) >= 1_000:
fmt.Printf("%.1fK", low/1_000)
default:
fmt.Printf("%.1f", low)
}
fmt.Printf(" ")
switch {
case math.Abs(high) >= 1_000_000_000_000:
fmt.Printf("%.1fT", high/1_000_000_000_000)
case math.Abs(high) >= 1_000_000_000:
fmt.Printf("%.1fB", high/1_000_000_000)
case math.Abs(high) >= 1_000_000:
fmt.Printf("%.1fM", high/1_000_000)
case math.Abs(high) >= 1_000:
fmt.Printf("%.1fK", high/1_000)
case math.Abs(high) >= 1_000:
fmt.Printf("%.1fK", high/1_000)
default:
fmt.Printf("%.1f", high)
}
fmt.Printf("\n")
// fmt.Printf("=> %.1f %.1f\n", low, high)
}
2024-06-09 13:15:53 +00:00
func prettyPrintDist(dist Dist) {
2024-06-09 22:05:23 +00:00
switch v := dist.(type) {
case Lognormal:
prettyPrintLognormal(v.low, v.high)
default:
fmt.Printf("%v", v)
2024-05-10 18:07:36 +00:00
}
2024-06-09 13:15:53 +00:00
}
2024-05-10 18:05:03 +00:00
2024-06-09 13:15:53 +00:00
/* Main event loop */
func main() {
2024-06-09 20:46:08 +00:00
sample_0 := func(r sample.Src) float64 { return 0 }
x := sample.Sample_parallel(sample_0, 10)
fmt.Printf("%v\n", x)
2024-06-09 13:15:53 +00:00
reader := bufio.NewReader(os.Stdin)
2024-06-09 22:05:23 +00:00
var init_dist Dist
init_dist = Lognormal{low: 1, high: 1}
2024-06-09 13:15:53 +00:00
old_dist := init_dist
vars := make(map[string]Dist)
// Could eventually be a more complex struct with:
// { Dist, VariableMaps, ConfigParams } or smth
2024-05-10 21:38:20 +00:00
EventForLoop:
2024-05-10 18:05:03 +00:00
for {
2024-06-09 13:15:53 +00:00
input, _ := reader.ReadString('\n')
2024-05-10 18:07:58 +00:00
if strings.TrimSpace(input) == "" {
2024-05-10 21:38:20 +00:00
continue EventForLoop
2024-05-10 18:05:03 +00:00
}
2024-05-11 10:08:03 +00:00
2024-06-09 13:15:53 +00:00
{
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)
2024-05-10 21:47:34 +00:00
continue EventForLoop
2024-06-09 13:15:53 +00:00
case words[0] == "debug" || words[0] == "d":
fmt.Printf("Old dist: %v\n", old_dist)
fmt.Printf("Vars: %v\n", vars)
2024-05-11 09:27:05 +00:00
continue EventForLoop
2024-06-09 13:15:53 +00:00
case words[0] == "=:" && len(words) == 2:
vars[words[1]] = old_dist
fmt.Printf("%s ", words[1])
prettyPrintDist(old_dist)
2024-05-10 21:38:20 +00:00
continue EventForLoop
2024-06-09 13:15:53 +00:00
case words[0] == "." || words[0] == "clean" || words[0] == "c":
old_dist = init_dist
fmt.Println()
2024-05-11 09:27:05 +00:00
continue EventForLoop
2024-06-09 13:15:53 +00:00
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()
2024-05-10 21:38:20 +00:00
continue EventForLoop
2024-06-09 13:15:53 +00:00
// 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
2024-05-10 19:50:55 +00:00
}
}
2024-05-11 09:27:05 +00:00
2024-06-09 13:15:53 +00:00
op, new_dist, err := parseLine(input, vars)
if err != nil {
continue EventForLoop
}
joint_dist, err := joinDists(old_dist, new_dist, op)
if err != nil {
continue EventForLoop
}
old_dist = joint_dist
prettyPrintDist(old_dist)
2024-05-10 18:05:03 +00:00
}
}