simplify lognormal multiplication code

This commit is contained in:
NunoSempere 2024-06-09 14:00:30 +02:00
parent 3bb705d13d
commit 5bb015ab69

44
f2.go
View File

@ -26,6 +26,9 @@ type Dist struct {
// Parse line into Distribution
func parseLineErr(err_msg string) (string, Dist, error) {
general_err_msg := "Valid inputs: 2 || * 2 || / 2 || 2 20 || * 2 20 || / 2 20 || i || e"
fmt.Println(general_err_msg)
fmt.Println(err_msg)
return "", Dist{}, errors.New(err_msg)
}
func parseLine(line string) (string, Dist, error) {
@ -51,11 +54,11 @@ func parseLine(line string) (string, Dist, error) {
switch len(words) {
case 0:
return parseLineErr("Can't operate on nothing")
return parseLineErr("Operator must have operand; can't operate on nothing")
case 1:
single_float, err1 := strconv.ParseFloat(words[0], 64)
if err1 != nil {
fmt.Println("Trying to operate on a scalar, but scalar is not a float")
return parseLineErr("Trying to operate on a scalar, but scalar is not a float")
}
dist = Dist{Type: "Lognormal", Lognormal: Lognormal{low: single_float, high: single_float}, Samples: nil}
case 2:
@ -75,36 +78,27 @@ func parseLine(line string) (string, Dist, error) {
// 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)
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)
logmean_product, logstd_product := multiplyLogParams(logmean_old, logstd_old, logmean_new, logstd_new)
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)}
return logParamsToLogBounds(logmean_product, logstd_product)
}
func joinDists(old_dist Dist, new_dist Dist, op string) (Dist, error) {
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}, nil
return Dist{Type: "Lognormal", Lognormal: multiplyLogDists(old_dist.Lognormal, new_dist.Lognormal), Samples: nil}, nil
default:
fmt.Printf("For now, can't do anything besides multiplying lognormals\n")
}