123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- package utils
- import (
- "fmt"
- "github.com/gonum/stat"
- "github.com/shopspring/decimal"
- "math"
- "strings"
- )
- type Series []Coordinate
- type Coordinate struct {
- X, Y float64
- }
- func GetLinearResult(s []Coordinate) (gradient, intercept float64) {
- if len(s) <= 1 {
- return
- }
- var sum [5]float64
- i := 0
- for ; i < len(s); i++ {
- sum[0] += s[i].X
- sum[1] += s[i].Y
- sum[2] += s[i].X * s[i].X
- sum[3] += s[i].X * s[i].Y
- sum[4] += s[i].Y * s[i].Y
- }
- f := float64(i)
- gradient = (f*sum[3] - sum[0]*sum[1]) / (f*sum[2] - sum[0]*sum[0])
- intercept = (sum[1] / f) - (gradient * sum[0] / f)
- return
- }
- func CalculateCorrelationByIntArr(xArr, yArr []float64) (ratio float64) {
- xLen := float64(len(xArr))
- yLen := float64(len(yArr))
- if xLen == 0 || xLen != yLen {
- return
- }
- var Xa, Ya float64
- for i := range xArr {
- Xa += xArr[i]
- }
- Mx := Xa / xLen
- for i := range yArr {
- Ya += yArr[i]
- }
- My := Ya / yLen
- var Xb, Yb, SDx, SDy float64
- for i := range xArr {
- Xb += (xArr[i] - Mx) * (xArr[i] - Mx)
- }
- SDx = math.Sqrt(1 / (xLen - 1) * Xb)
- for i := range yArr {
- Yb += (yArr[i] - My) * (yArr[i] - My)
- }
- SDy = math.Sqrt(1 / (yLen - 1) * Yb)
- var Nume, Deno float64
- for i := 0; i < int(xLen); i++ {
- Nume += (xArr[i] - Mx) * (yArr[i] - My)
- }
- Deno = (xLen - 1) * (SDx * SDy)
- ratio = Nume / Deno
- if math.IsNaN(ratio) {
- ratio = 0
- }
- return
- }
- func ComputeCorrelation(sList []Coordinate) (r float64) {
- var xBar, yBar float64
- lenSList := len(sList)
- if lenSList < 2 {
- return
- }
- decimalX := decimal.NewFromFloat(0)
- decimalY := decimal.NewFromFloat(0)
- for _, coordinate := range sList {
- decimalX = decimalX.Add(decimal.NewFromFloat(coordinate.X))
- decimalY = decimalY.Add(decimal.NewFromFloat(coordinate.Y))
- }
- xBar, _ = decimalX.Div(decimal.NewFromInt(int64(lenSList))).Round(4).Float64()
- yBar, _ = decimalY.Div(decimal.NewFromInt(int64(lenSList))).Round(4).Float64()
- varXDeci := decimal.NewFromFloat(0)
- varYDeci := decimal.NewFromFloat(0)
- ssrDeci := decimal.NewFromFloat(0)
- for _, coordinate := range sList {
- diffXXbarDeci := decimal.NewFromFloat(coordinate.X).Sub(decimal.NewFromFloat(xBar))
- diffYYbarDeci := decimal.NewFromFloat(coordinate.Y).Sub(decimal.NewFromFloat(yBar))
- ssrDeci = ssrDeci.Add(diffXXbarDeci.Mul(diffYYbarDeci))
- varXDeci = varXDeci.Add(diffXXbarDeci.Mul(diffXXbarDeci))
- varYDeci = varYDeci.Add(diffYYbarDeci.Mul(diffYYbarDeci))
- }
- if varXDeci.IsZero() && varYDeci.IsZero() {
- r = 1
- return
- }
- sqrtVal, _ := varXDeci.Mul(varYDeci).Round(4).Float64()
- sst := math.Sqrt(sqrtVal) // 平方根
- if sst == 0 {
- return
- }
- r, _ = ssrDeci.Div(decimal.NewFromFloat(sst)).Round(4).Float64()
- return
- }
- func CalculationDecisive(sList []Coordinate) (r2 float64) {
- r := ComputeCorrelation(sList)
- r2, _ = decimal.NewFromFloat(r).Mul(decimal.NewFromFloat(r)).Round(4).Float64()
- return
- }
- func CalculateStandardDeviation(data []float64) float64 {
- return stat.StdDev(data, nil)
- }
- func ReplaceFormula(valArr map[string]float64, formulaStr string) string {
- funMap := getFormulaMap()
- for k, v := range funMap {
- formulaStr = strings.Replace(formulaStr, k, v, -1)
- }
- replaceCount := 0
- for tag, val := range valArr {
- dvStr := fmt.Sprintf("%v", val)
- formulaStr = strings.Replace(formulaStr, tag, dvStr, -1)
- replaceCount++
- }
- for k, v := range funMap {
- formulaStr = strings.Replace(formulaStr, v, k, -1)
- }
- return formulaStr
- }
- type CellPosition struct {
- Tag string
- Row int
- Value float64
- }
- func ReplaceFormulaByCellList(cellList []CellPosition, formulaStr string) string {
- funMap := getFormulaMap()
- for k, v := range funMap {
- formulaStr = strings.Replace(formulaStr, k, v, -1)
- }
- replaceCount := 0
- for _, cell := range cellList {
- dvStr := fmt.Sprintf("%v", cell.Value)
- formulaStr = strings.Replace(formulaStr, fmt.Sprint(cell.Tag, cell.Row), dvStr, -1)
- replaceCount++
- }
- for k, v := range funMap {
- formulaStr = strings.Replace(formulaStr, v, k, -1)
- }
- return formulaStr
- }
- func getFormulaMap() map[string]string {
- funMap := make(map[string]string)
- funMap["MAX"] = "[@@]"
- funMap["MIN"] = "[@!]"
- funMap["ABS"] = "[@#]"
- funMap["CEIL"] = "[@$]"
- funMap["COS"] = "[@%]"
- funMap["FLOOR"] = "[@^]"
- funMap["MOD"] = "[@&]"
- funMap["POW"] = "[@*]"
- funMap["ROUND"] = "[@(]"
- return funMap
- }
|