moved error message sooner for invalid mean and stdev

This commit is contained in:
Quinn Dougherty 2022-07-04 09:57:00 -04:00
parent 5b41019c04
commit 7a419742ad

View File

@ -129,14 +129,18 @@ module Beta = {
// https://en.wikipedia.org/wiki/Beta_distribution#Mean_and_sample_size // https://en.wikipedia.org/wiki/Beta_distribution#Mean_and_sample_size
let alpha = mean *. sampleSize let alpha = mean *. sampleSize
let beta = (1.0 -. mean) *. sampleSize let beta = (1.0 -. mean) *. sampleSize
alpha->make(beta) make(alpha, beta)
} }
let fromMeanAndStdev = (mean, stdev) => { let fromMeanAndStdev = (mean, stdev) => {
// https://en.wikipedia.org/wiki/Beta_distribution#Mean_and_variance // https://en.wikipedia.org/wiki/Beta_distribution#Mean_and_variance
let var = stdev *. stdev if !(0.0 < stdev && stdev <= 0.5) || !(0.0 <= mean && mean <= 1.0) {
let sampleSize = mean *. (1.0 -. mean) /. var -. 1.0 "Beta mean must be in [0,1] and stdev must be in (1,0.5]"->Error
mean->fromMeanAndSampleSize(sampleSize) } else {
let var = stdev *. stdev
let sampleSize = mean *. (1.0 -. mean) /. var -. 1.0
fromMeanAndSampleSize(mean, sampleSize)
}
} }
} }