From 5515d43c13572c2dc1ca34d09809fbe92b617357 Mon Sep 17 00:00:00 2001 From: TimLai666 <43640816+TimLai666@users.noreply.github.com> Date: Sat, 14 Sep 2024 16:08:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=98=E6=B1=B0Stdev=E7=9A=84=E9=AB=98?= =?UTF-8?q?=E7=B2=BE=E5=BA=A6=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- datalist.go | 34 ++++++++-------------------------- stats/correlation.go | 4 ++-- stats/ttest.go | 8 ++++---- 3 files changed, 14 insertions(+), 32 deletions(-) diff --git a/datalist.go b/datalist.go index e437070..4844a4f 100644 --- a/datalist.go +++ b/datalist.go @@ -71,6 +71,7 @@ type IDataList interface { Upper() *DataList Lower() *DataList Capitalize() *DataList + // Statistics Sum() float64 Max() float64 @@ -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{} @@ -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() { @@ -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 相同 @@ -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) @@ -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) } @@ -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)) } diff --git a/stats/correlation.go b/stats/correlation.go index 8b57c31..f2a7ac6 100644 --- a/stats/correlation.go +++ b/stats/correlation.go @@ -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 { diff --git a/stats/ttest.go b/stats/ttest.go index 6584d9f..0dcfb60 100644 --- a/stats/ttest.go +++ b/stats/ttest.go @@ -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 值 @@ -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 @@ -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))