package predict

import (

"fmt"
"math"
"strconv"
"strings"
"util"

)

const (

uvinf    = 0x7FF0000000000000 //正无穷大
uvneginf = 0xFFF0000000000000 //负无穷大

)

//计算威尔逊区间
func ConfidenceInterval2(n int64, k int, alpha float64, arr *[]float64) (float64, float64) {

z, _ := GetQuantile(1-0.5*alpha, arr)
p := float64(k) / float64(n)
s := (1 - p) * p

left := p + z*z/(2*float64(n))
right := z * math.Sqrt(s/float64(n)+z*z/(4*float64(n)*float64(n)))
factor := 1 / (1 + z*z/float64(n))
return factor * (left - right), factor * (left + right)

}

//加载分位数表
func LoadQuantile() (*[]float64, error) {

var features []float64
content, err := util.ReadFile("./src/predict/Quantile.txt")
if err != nil {
    fmt.Print(err)
}
for _, line := range strings.Split(string(content), "\n") {
    if !strings.Contains(line, "P") {
        lineNew := strings.Split(line, "\t")
        i := 0
        lineNew = append(lineNew[:i], lineNew[i+1:]...)
        for _, value := range lineNew {
            fValue, err := strconv.ParseFloat(strings.ReplaceAll(value, "\r", ""), 64)
            if err == nil {
                features = append(features, fValue)
            } else {
                fmt.Printf("eValue:%s,%s", value, err)
            }
        }
    }
}
return &features, err

}

//查分位数表
func GetQuantile(p float64, arr *[]float64) (float64, error) {

if p < 0 || p > 1 {
    return 0, fmt.Errorf("p value error")
}
switch {
case p == 0:
    return uvneginf, nil
case p == 1:
    return uvinf, nil
case p >= 0.5:
    return (*arr)[int64(p*1000-500)], nil
case p < 0.5:
    return -(*arr)[-int64(p*1000 - 500)], nil
}
return 0, fmt.Errorf("p value error")

}

转自:https://blog.csdn.net/u014628146/article/details/91993713

最后修改:2020 年 11 月 25 日 12 : 21 PM