Skip to content

Commit

Permalink
淘汰Stdev的高精度模式
Browse files Browse the repository at this point in the history
  • Loading branch information
TimLai666 committed Sep 14, 2024
1 parent d732e3b commit 5515d43
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 32 deletions.
34 changes: 8 additions & 26 deletions datalist.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type IDataList interface {
Upper() *DataList
Lower() *DataList
Capitalize() *DataList

// Statistics
Sum() float64
Max() float64
Expand All @@ -81,7 +82,7 @@ type IDataList interface {
Median() interface{}
Mode() interface{}
MAD() interface{}
Stdev(highPrecision ...bool) interface{}
Stdev() interface{}
StdevP(highPrecision ...bool) interface{}
Var(highPrecision ...bool) interface{}
VarP(highPrecision ...bool) interface{}
Expand Down Expand Up @@ -409,7 +410,7 @@ func (dl *DataList) ReplaceAll(oldValue, newValue interface{}) {
// ReplaceOutliers replaces outliers in the DataList with the specified replacement value (e.g., mean, median).
func (dl *DataList) ReplaceOutliers(stdDevs float64, replacement float64) *DataList {
mean := dl.Mean()
stddev := dl.Stdev(false).(float64)
stddev := dl.Stdev().(float64)
threshold := stdDevs * stddev

for i, v := range dl.Data() {
Expand Down Expand Up @@ -695,7 +696,7 @@ func (dl *DataList) ClearOutliers(stdDevs float64) *DataList {
}()

mean := dl.Mean()
stddev := dl.Stdev(false).(float64)
stddev := dl.Stdev().(float64)
threshold := stdDevs * stddev

// 打印調試信息,確保計算值與 R 相同
Expand Down Expand Up @@ -752,7 +753,7 @@ func (dl *DataList) Standardize() *DataList {
go dl.updateTimestamp()
}()
mean := dl.Mean()
stddev := dl.Stdev(false).(float64)
stddev := dl.Stdev().(float64)
dl.mu.Lock()
for i, v := range dl.Data() {
vfloat := conv.ParseF64(v)
Expand Down Expand Up @@ -882,7 +883,7 @@ func (dl *DataList) MovingStdev(windowSize int) *DataList {
movingStdDevData := make([]float64, dl.Len()-windowSize+1)
for i := 0; i < len(movingStdDevData); i++ {
window := NewDataList(dl.Data()[i : i+windowSize])
movingStdDevData[i] = window.Stdev(false).(float64)
movingStdDevData[i] = window.Stdev().(float64)
}
return NewDataList(movingStdDevData)
}
Expand Down Expand Up @@ -1356,38 +1357,19 @@ func (dl *DataList) MAD() interface{} {
// Returns the standard deviation.
// Returns nil if the DataList is empty.
// Stdev returns the standard deviation of the DataList.
func (dl *DataList) Stdev(highPrecision ...bool) interface{} {
func (dl *DataList) Stdev() interface{} {
if len(dl.data) == 0 {
LogWarning("DataList.Stdev(): DataList is empty, returning nil.")
return nil
}

// 判斷是否使用高精度模式
useHighPrecision := len(highPrecision) == 1 && highPrecision[0]
if len(highPrecision) > 1 {
LogWarning("DataList.Stdev(): Too many arguments, returning nil.")
return nil
}

var variance interface{}
if useHighPrecision {
variance = dl.Var(true)
} else {
variance = dl.Var(false)
}
variance := dl.Var(false)

if variance == nil {
LogWarning("DataList.Stdev(): Variance calculation failed, returning nil.")
return nil
}

if useHighPrecision {
// 高精度模式下使用 SqrtRat 進行開方運算
varianceRat := variance.(*big.Rat)
sqrtVariance := SqrtRat(varianceRat)
return sqrtVariance
}

// 普通模式下使用 float64 計算
return math.Sqrt(ToFloat64(variance))
}
Expand Down
4 changes: 2 additions & 2 deletions stats/correlation.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ func pearsonCorrelation(dlX, dlY insyra.IDataList) *big.Rat {
cov := Covariance(dlX, dlY)

// 計算標準差
stdX := dlX.Stdev(true).(*big.Rat)
stdY := dlY.Stdev(true).(*big.Rat)
stdX := new(big.Rat).SetFloat64(dlX.Stdev().(float64))
stdY := new(big.Rat).SetFloat64(dlY.Stdev().(float64))

// 防止除以0
if stdX.Sign() == 0 || stdY.Sign() == 0 {
Expand Down
8 changes: 4 additions & 4 deletions stats/ttest.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func SingleSampleTTest(data insyra.IDataList, mu float64) *TTestResult {
mean := data.Mean()

// 計算標準差和標準誤差
stddev := data.Stdev(false).(float64)
stddev := data.Stdev().(float64)
standardError := stddev / math.Sqrt(float64(n))

// 計算 t 值
Expand Down Expand Up @@ -60,8 +60,8 @@ func TwoSampleTTest(data1, data2 insyra.IDataList, equalVariance bool) *TTestRes
mean2 := data2.Mean()

// 計算兩個樣本的標準差
stddev1 := data1.Stdev(false).(float64)
stddev2 := data2.Stdev(false).(float64)
stddev1 := data1.Stdev().(float64)
stddev2 := data2.Stdev().(float64)

var standardError float64
var df int
Expand Down Expand Up @@ -110,7 +110,7 @@ func PairedTTest(data1, data2 insyra.IDataList) *TTestResult {

// 計算差值的均值和標準差
meanDiff := insyra.NewDataList(diffs).Mean()
stddevDiff := insyra.NewDataList(diffs).Stdev(false).(float64)
stddevDiff := insyra.NewDataList(diffs).Stdev().(float64)

// 計算 t 值
standardError := stddevDiff / math.Sqrt(float64(n))
Expand Down

0 comments on commit 5515d43

Please sign in to comment.