continue with new parser, steps now seem way more clear
This commit is contained in:
parent
f5e7148148
commit
de72432c53
76
f2.go
76
f2.go
|
@ -2,13 +2,17 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
// "math"
|
// "math"
|
||||||
"os"
|
"os"
|
||||||
// "strconv"
|
// "strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const NORMAL90CONFIDENCE = 1.6448536269514727
|
||||||
|
|
||||||
// Actually, I should look up how do do a) enums in go, b) union types
|
// Actually, I should look up how do do a) enums in go, b) union types
|
||||||
type Lognormal struct {
|
type Lognormal struct {
|
||||||
low float64
|
low float64
|
||||||
|
@ -21,21 +25,83 @@ type Dist struct {
|
||||||
Samples []float64
|
Samples []float64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Parse line into Distribution,
|
||||||
func parseLine(line string) (string, Dist, error) {
|
func parseLine(line string) (string, Dist, error) {
|
||||||
|
|
||||||
words := strings.Split(strings.TrimSpace(line), " ")
|
words := strings.Split(strings.TrimSpace(line), " ")
|
||||||
|
op = ""
|
||||||
|
var dist Dist
|
||||||
|
|
||||||
switch {
|
switch words[0] {
|
||||||
case len(words) == 2 && words[0] == "*":
|
case "*":
|
||||||
fmt.Printf("Hello world")
|
op = "*"
|
||||||
|
case "/":
|
||||||
|
op = "/"
|
||||||
|
case "+":
|
||||||
|
return "", Dist{}, error.New("+ operation not implemented yet")
|
||||||
|
case "-":
|
||||||
|
return "", Dist{}, error.New("- operation not implemented yet")
|
||||||
|
default:
|
||||||
|
return "", Dist{}, error.New("operation not recognized")
|
||||||
}
|
}
|
||||||
return "Lognormal", Dist{}, nil
|
|
||||||
|
switch len(words) {
|
||||||
|
case 1:
|
||||||
|
return "", Dist{}, error.New("Can't operate on nothing")
|
||||||
|
case 2:
|
||||||
|
single_float, err1 := strconv.ParseFloat(words[1], 64)
|
||||||
|
if err1 != nil {
|
||||||
|
fmt.Println("Trying to operate on a scalar, but scalar is not a float")
|
||||||
|
fmt.Println(error_msg_cont)
|
||||||
|
continue EventForLoop
|
||||||
|
}
|
||||||
|
new_low = single_float
|
||||||
|
new_high = single_float
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Join distributions
|
||||||
|
// Multiply lognormals
|
||||||
|
|
||||||
|
func logBoundsToLogParams(low float64, high float64) (float64, float64) {
|
||||||
|
loglow := math.Log(low)
|
||||||
|
loghigh := math.Log(high)
|
||||||
|
logmean := (loghigh + loglow) / 2.0
|
||||||
|
logstd := (loghigh - loglow) / (2.0 * NORMAL90CONFIDENCE)
|
||||||
|
return logmean, logstd
|
||||||
|
}
|
||||||
|
func logParamsToLogBounds(logmean float64, logstd float64) (float64, float64) {
|
||||||
|
h := logstd * NORMAL90CONFIDENCE
|
||||||
|
loglow := logmean - h
|
||||||
|
loghigh := logmean + h
|
||||||
|
return math.Exp(loglow), math.Exp(loghigh)
|
||||||
|
}
|
||||||
|
func multiplyLogParams(logmean1 float64, logstd1 float64, logmean2 float64, logstd2 float64) (float64, float64) {
|
||||||
|
return logmean1 + logmean2, math.Sqrt(logstd1*logstd1 + logstd2*logstd2)
|
||||||
|
}
|
||||||
|
func multiplyLogBounds(old_low, old_high, new_low, new_high float64) (float64, float64) {
|
||||||
|
logmean_old, logstd_old := logBoundsToLogParams(old_low, old_high)
|
||||||
|
logmean_new, logstd_new := logBoundsToLogParams(new_low, new_high)
|
||||||
|
|
||||||
|
logmean_product, logstd_product := multiplyLogParams(logmean_old, logstd_old, logmean_new, logstd_new)
|
||||||
|
|
||||||
|
return logParamsToLogBounds(logmean_product, logstd_product)
|
||||||
}
|
}
|
||||||
|
|
||||||
func joinDists(old_dist Dist, new_dist Dist, op string) (Dist, error) {
|
func joinDists(old_dist Dist, new_dist Dist, op string) (Dist, error) {
|
||||||
return old_dist, nil
|
switch {
|
||||||
|
case old_dist.Type == "Lognormal" && new_dist.Type == "Lognormal" && op == "*":
|
||||||
|
low, high := multiplyLogBounds(old_dist.Lognormal.low, old_dist.Lognormal.high, new_dist.Lognormal.low, new_dist.Lognormal.high)
|
||||||
|
return Dist{Type: "Lognormal", Lognormal: Lognormal{low: low, high: high}, Samples: nil}
|
||||||
|
default:
|
||||||
|
fmt.Printf("For now, can't do anything besides multiplying lognormals")
|
||||||
|
}
|
||||||
|
return old_dist, errors.New("Error blah blah")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Main event loop */
|
||||||
func main() {
|
func main() {
|
||||||
reader := bufio.NewReader(os.Stdin)
|
reader := bufio.NewReader(os.Stdin)
|
||||||
old_dist := Dist{Type: "Lognormal", Lognormal: Lognormal{low: 1, high: 1}, Samples: nil} // Could also just be a scalar
|
old_dist := Dist{Type: "Lognormal", Lognormal: Lognormal{low: 1, high: 1}, Samples: nil} // Could also just be a scalar
|
||||||
|
|
Loading…
Reference in New Issue
Block a user