gitee.com/quant1x/num@v0.3.2/ztable.go (about)

     1  package num
     2  
     3  import "sort"
     4  
     5  const (
     6  	ztableMaximumPercentage = float64(0.9999) // 最大百分比
     7  	ztableMinimumPercentage = float64(0.0001) // 最小百分比
     8  	ztableMaxScale          = 10000           // 最大尺寸
     9  )
    10  
    11  func FindPercent(zScore float64) (percent float64) {
    12  	index := sort.SearchFloat64s(__percentToZscore, zScore)
    13  	percent = float64(index) / float64(ztableMaxScale)
    14  	return percent
    15  }
    16  
    17  func FindZScore(percent float64) (zScore float64) {
    18  	// 第一步约束 percentage在0~9999范围内
    19  	index := int(percent*(ztableMaxScale)) % ztableMaxScale
    20  	return __percentToZscore[index]
    21  }
    22  
    23  // ConfidenceIntervalToZscore 通过置信区间百分比查找Z分值
    24  func ConfidenceIntervalToZscore(confidenceInterval float64) (zScore float64) {
    25  	// 约束 percentage在0~9999范围内
    26  	index := int(confidenceInterval*(ztableMaxScale)) % ztableMaxScale
    27  	return __z_table[index]
    28  }
    29  
    30  // ZscoreToConfidenceInterval 通过分值查找置信区间
    31  func ZscoreToConfidenceInterval(zScore float64) (confidenceInterval float64) {
    32  	index := __SearchFloat64s(__z_table, zScore)
    33  	confidenceInterval = float64(index) / float64(ztableMaxScale)
    34  	return confidenceInterval
    35  }
    36  
    37  func __SearchFloat64s(a []float64, x float64) int {
    38  	n, found := sort.Find(len(a), func(i int) int {
    39  		m := x - a[i]
    40  		return int(m * ztableMaxScale)
    41  	})
    42  	if !found {
    43  		n = n - 1
    44  	}
    45  	return n
    46  }