// Estimates the Mean based on the data set // If the data set is relatively small (< 1000 examples), then remove 1 from the total func (ad *AnomalyDetection) estimateMean() *big.Float { // initialize the total to zero totalMean := big.NewFloat(0) mean := big.NewFloat(0) // Loop thorugh the data set for _, element := range ad.dataSet { // sum up its elements totalMean.Add(totalMean, &element) //e, _ := element.Float64() } // make a copy of the total sum and assign it to the anomaly detection object ad.totalSum.Copy(totalMean) // calculate the mean mean.Quo(totalMean, &ad.totalSamples) // assign the mean to the anomaly detection object ad.mean = *mean return mean }
// PUT /servers/{server_id}/hardware func (api *API) UpdateServerHardware(server_id string, hardware *Hardware) (*Server, error) { var vc, cpp *int var ram *float32 if hardware.Vcores > 0 { vc = new(int) *vc = hardware.Vcores } if hardware.CoresPerProcessor > 0 { cpp = new(int) *cpp = hardware.CoresPerProcessor } if big.NewFloat(float64(hardware.Ram)).Cmp(big.NewFloat(0)) != 0 { ram = new(float32) *ram = hardware.Ram } req := struct { VCores *int `json:"vcore,omitempty"` Cpp *int `json:"cores_per_processor,omitempty"` Ram *float32 `json:"ram,omitempty"` Flavor string `json:"fixed_instance_size_id,omitempty"` }{VCores: vc, Cpp: cpp, Ram: ram, Flavor: hardware.FixedInsSizeId} result := new(Server) url := createUrl(api, serverPathSegment, server_id, "hardware") err := api.Client.Put(url, &req, &result, http.StatusAccepted) if err != nil { return nil, err } result.api = api result.decodeRaws() return result, nil }
func AddBigFloat() (calculated *big.Float) { bigFloatStartFloat := big.NewFloat(StartFloat) bigFloatFactor := big.NewFloat(Factor) calculated = bigFloatStartFloat.Add(bigFloatStartFloat, bigFloatFactor) return }
func MultiplyBigFloat() (calculated *big.Float) { bigFloatStartFloat := big.NewFloat(StartFloat) bigFloatFactor := big.NewFloat(Factor) calculated = bigFloatStartFloat.Mul(bigFloatStartFloat, bigFloatFactor) return }
func BenchmarkAddBigFloatWithoutNew(b *testing.B) { bigFloatStartFloat := big.NewFloat(StartFloat) bigFloatFactor := big.NewFloat(Factor) for n := 0; n < b.N; n++ { AddBigFloatWithoutNew(bigFloatStartFloat, bigFloatFactor) } }
func TestSmallDataSet(t *testing.T) { dataSet := fakeSmallData() ad := NewAnomalyDetection(dataSet...) totalSamples, _ := ad.totalSamples.Uint64() if totalSamples != 20 { t.Fatal("Wrong number of element in the set. There are for sure 20, instead we got:", totalSamples) } totalSum, _ := ad.totalSum.Uint64() if totalSum != 83 { t.Fatal("Total sum does not add up, should be 83, we got:", totalSum) } mean, _ := ad.mean.Float64() if mean != 4.15 { t.Fatal("Mean is wrong, should be 4.15, we got:", mean) } deviation, _ := ad.deviation.Float64() if deviation != 1.3199999999999996 { t.Error("Deviation is wrong, should be 1.32 (or 1.3199999999999996), we got:", deviation) } variance, _ := ad.variance.Float64() if variance != 2.3325000000000005 { t.Error("Variance is wrong, should be 2.3325 (or 2.3325000000000005), we got:", variance) } anomaly, result := ad.EventIsAnomalous(*big.NewFloat(4.3), big.NewFloat(0.02)) if anomaly { t.Errorf("5.3 should not be an anomaly !!! %f\n", result) } // stricter threshold anomaly, _ = ad.EventIsAnomalous(*big.NewFloat(7.3), big.NewFloat(0.1)) if !anomaly { t.Error("7.3 should be an anomaly !!!") } ad.ExpandDataSet(fakeSmallData()...) ad.ExpandDataSet(fakeSmallData()...) anomaly, _ = ad.EventIsAnomalous(*big.NewFloat(5.3), big.NewFloat(0.01)) if anomaly { t.Error("5.3 should not be an anomaly !!!") } // stricter threshold anomaly, _ = ad.EventIsAnomalous(*big.NewFloat(7.3), big.NewFloat(0.1)) if !anomaly { t.Error("7.3 should be an anomaly !!!") } }
func BigFloatGen(r Int64F) Generator { return func() interface{} { a := float64(r()) b := float64(r()) + 1.0 x := big.NewFloat(a) y := big.NewFloat(b) y = y.Quo(x, y) return y } }
func splitRangeString(start, end string, splits int) []string { results := []string{start} if start == end { return results } if end < start { tmp := start start = end end = tmp } // find longest common prefix between strings minLen := len(start) if len(end) < minLen { minLen = len(end) } prefix := "" for i := 0; i < minLen; i++ { if start[i] == end[i] { prefix = start[0 : i+1] } else { break } } // remove prefix from strings to split start = start[len(prefix):] end = end[len(prefix):] ordStart := stringToOrd(start) ordEnd := stringToOrd(end) tmp := new(big.Int) tmp.Sub(ordEnd, ordStart) stride := new(big.Float) stride.SetInt(tmp) stride.Quo(stride, big.NewFloat(float64(splits))) for i := 1; i <= splits; i++ { tmp := new(big.Float) tmp.Mul(stride, big.NewFloat(float64(i))) tmp.Add(tmp, new(big.Float).SetInt(ordStart)) result, _ := tmp.Int(new(big.Int)) value := prefix + ordToString(result, 0) if value != results[len(results)-1] { results = append(results, value) } } return results }
func TestRoot(t *testing.T) { x := big.NewFloat(0.12381245613960218386) n := 16 res := Root(x, n) exp := big.NewFloat(0.8776023372475015) diff := new(big.Float).Sub(res, exp) diff = diff.Abs(diff) if diff.Cmp(big.NewFloat(0.00000001)) >= 0 { log.Fatal("Exp failed:", exp, res) } }
func TestPow(t *testing.T) { x := big.NewFloat(0.12381245613960218386) n := 3 res := Pow(x, n) exp := big.NewFloat(0.00189798605) diff := new(big.Float).Sub(res, exp) diff = diff.Abs(diff) if diff.Cmp(big.NewFloat(0.00000001)) >= 0 { log.Fatal("Pow failed:", exp, res) } }
// Sqrt returns a big.Float representation of the square root of // z. Precision is the same as the one of the argument. The function // panics if z is negative, returns ±0 when z = ±0, and +Inf when z = // +Inf. func Sqrt(z *big.Float) *big.Float { // panic on negative z if z.Sign() == -1 { panic("Sqrt: argument is negative") } // √±0 = ±0 if z.Sign() == 0 { return big.NewFloat(float64(z.Sign())) } // √+Inf = +Inf if z.IsInf() { return big.NewFloat(math.Inf(+1)) } // Compute √(a·2**b) as // √(a)·2**b/2 if b is even // √(2a)·2**b/2 if b > 0 is odd // √(0.5a)·2**b/2 if b < 0 is odd // // The difference in the odd exponent case is due to the fact that // exp/2 is rounded in different directions when exp is negative. mant := new(big.Float) exp := z.MantExp(mant) switch exp % 2 { case 1: mant.Mul(big.NewFloat(2), mant) case -1: mant.Mul(big.NewFloat(0.5), mant) } // Solving x² - z = 0 directly requires a Quo call, but it's // faster for small precisions. // // Solving 1/x² - z = 0 avoids the Quo call and is much faster for // high precisions. // // Use sqrtDirect for prec <= 128 and sqrtInverse for prec > 128. var x *big.Float if z.Prec() <= 128 { x = sqrtDirect(mant) } else { x = sqrtInverse(mant) } // re-attach the exponent and return return x.SetMantExp(x, exp/2) }
func BenchmarkLog(b *testing.B) { z := big.NewFloat(2).SetPrec(1e5) _ = bigfloat.Log(z) // fill pi cache before benchmarking for _, prec := range []uint{1e2, 1e3, 1e4, 1e5} { z = big.NewFloat(2).SetPrec(prec) b.Run(fmt.Sprintf("%v", prec), func(b *testing.B) { b.ReportAllocs() for n := 0; n < b.N; n++ { bigfloat.Log(z) } }) } }
func mandelbrot(z complex128) color.Color { const iterations = 200 const contrast = 15 var v complex128 limit := new(big.Float).SetFloat64(2.0) for n := uint8(0); n < iterations; n++ { v = v*v + z if hypot(big.NewFloat(real(v)), big.NewFloat(imag(v))).Cmp(limit) > 2 { return color.Gray{255 - contrast*n} } } return color.Black }
func mandelbrotFloat(r, i *big.Float) color.Color { const iterations = 200 const contrast = 15 a := big.NewFloat(0) //実部の初期値 b := big.NewFloat(0) //虚部の初期値 for n := uint8(0); n < iterations; n++ { a = addF(subF(mulF(a, a), mulF(b, b)), r) b = addF(mulF(big.NewFloat(2), mulF(a, b)), i) if addF(mulF(a, a), mulF(b, b)).Cmp(big.NewFloat(4)) == 1 { return color.Gray{255 - contrast*n} } } return color.Black }
func (m *meanagg) String() string { if m.d == 0 { return "NaN" } v := new(big.Float).Quo(m.v, big.NewFloat(m.d)) return v.Text('f', -1) }
func networkGetIP(subnet *net.IPNet, host int64) net.IP { // Convert IP to a big int bigIP := big.NewInt(0) bigIP.SetBytes(subnet.IP.To16()) // Deal with negative offsets bigHost := big.NewInt(host) bigCount := big.NewInt(host) if host < 0 { mask, size := subnet.Mask.Size() bigHosts := big.NewFloat(0) bigHosts.SetFloat64((math.Pow(2, float64(size-mask)))) bigHostsInt, _ := bigHosts.Int(nil) bigCount.Set(bigHostsInt) bigCount.Add(bigCount, bigHost) } // Get the new IP int bigIP.Add(bigIP, bigCount) // Generate an IPv6 if subnet.IP.To4() == nil { newIp := make(net.IP, 16) newIp = bigIP.Bytes() return newIp } // Generate an IPv4 newIp := make(net.IP, 4) binary.BigEndian.PutUint32(newIp, uint32(bigIP.Int64())) return newIp }
func TestFixedEstimateMean(t *testing.T) { dataSet := fakeFixedData() ad := NewAnomalyDetection(dataSet...) mean, _ := ad.mean.Float64() if mean != 5052.0320320320325 { t.Fatal("With the current code the mean should be 5052.0320320320325, instead we got", mean) } variance, _ := ad.variance.Float64() //fmt.Println("Variance:", variance) if variance != 1536.5114864614086 { t.Fatal("With the current code the variance should be 1536.5114864614086, instead we got", variance) } // threshold: 0.1% anomaly, result := ad.EventIsAnomalous(*big.NewFloat(5050), big.NewFloat(0.001)) if anomaly { t.Errorf("5050 should NOT be an anomaly !!! %f\n", result) } }
func (proxy BigSequenceNumericsProxy) Extrinsically(f func()) { cmin := bigbase.BigComplex{proxy.RealMin, proxy.ImagMin} cmax := bigbase.BigComplex{proxy.RealMax, proxy.ImagMax} orig := []*big.Float{ &cmin.R, &cmin.I, &cmax.R, &cmax.I, } copy := make([]*big.Float, len(orig)) for i, bound := range orig { cp := big.NewFloat(0.0) cp.Copy(bound) copy[i] = cp } proxy.RealMin = *copy[0] proxy.ImagMin = *copy[1] proxy.RealMax = *copy[2] proxy.ImagMax = *copy[3] proxy.ClaimExtrinsics() f() proxy.RealMin = cmin.R proxy.ImagMin = cmin.I proxy.RealMax = cmax.R proxy.ImagMax = cmax.I // Should we cache this somewhere? New object? proxy.RestorePicBounds() }
// compute √z using newton to solve // t² - z = 0 for t func sqrtDirect(z *big.Float) *big.Float { // f(t)/f'(t) = 0.5(t² - z)/t half := big.NewFloat(0.5) f := func(t *big.Float) *big.Float { x := new(big.Float).Mul(t, t) // x = t² x.Sub(x, z) // x = t² - z x.Mul(half, x) // x = 0.5(t² - z) return x.Quo(x, t) // return x = 0.5(t² - z)/t } // initial guess zf, _ := z.Float64() guess := big.NewFloat(math.Sqrt(zf)) return newton(f, guess, z.Prec()) }
/*红包算法 输入 红包总金额, red_mon 红包格式, red_cou 红包最小值, red_min 红包最大值, red_max 自动返回红包 */ func red(red_mon float64, red_cou float64, red_min float64, red_max float64) { fmt.Println("参数", red_mon, red_cou, red_min, red_max) red_evr := math.Floor(red_mon / red_cou) fmt.Println("平均值", red_evr) red_c := int(red_cou) red_res := make([]float64, red_c, 2*red_c) var re float64 // var re_coun string //在最小值和最大值之间取随机值,和平均值比较 for i := 0; i < red_c; i++ { ran := xrand(red_min, red_max) //因为出现比平均值大的概率比较大, if ran > red_evr { //为了保证小红包比较多,所以比平均值大时,生成小红包 re = red_min + xrand(red_min, red_evr) } else { //比平均值小的生成大红包 re = xrand(red_evr, red_max) } //s := fmt.Sprintf("%.2f", re) //f, _ := strconv.ParseFloat(s, 64) red_res[i] = re red_mon -= re } //还有剩余,加到小红包 for red_mon >= 1 { for i, _ := range red_res { if red_mon >= 1 { red_res[i]++ red_mon-- } // fmt.Println(red_mon) } } //还有剩余,加到小红包 for red_mon <= -1 { for i, _ := range red_res { if red_mon <= -1 { red_res[i]-- red_mon++ } // fmt.Println(red_mon) } } for i, v := range red_res { f := big.NewFloat(v) // red_res[i] = f // re_coun += f fmt.Println(f) } // red_res[0] += red_mon // fmt.Println(red_res) // s := fmt.Sprintf("%.2f", re_coun) // fmt.Println(re_coun) // s := fmt.Sprintf("%.2f", red_mon) // fmt.Println(s) }
// helper function to fake fixed data func fakeRandomData() []big.Float { var dataSet []big.Float baseInt := big.NewFloat(5000) // fake sample data for i := 0; i < 100000; i++ { baseInt = big.NewFloat(5000) if i > 200 && i < 800 { dataSet = append(dataSet, *baseInt.Add(baseInt, getDeviation(deviationMax))) } else { dataSet = append(dataSet, *baseInt.Add(baseInt, getDeviation(higherDeviation))) } } return dataSet }
// hypot for big.Float func hypot(p, q *big.Float) *big.Float { // special cases switch { case p.IsInf() || q.IsInf(): return big.NewFloat(math.Inf(1)) } p = p.Abs(p) q = q.Abs(q) if p.Cmp(p) < 0 { p, q = q, p } if p.Cmp(big.NewFloat(0)) == 0 { return big.NewFloat(0) } q = q.Quo(q, p) return sqrt(q.Mul(q, q).Add(q, big.NewFloat(1))).Mul(q, p) }
// Implements the nth root algorithm from // https://en.wikipedia.org/wiki/Nth_root_algorithm // return: nth root of x within some epsilon func Root(x *big.Float, n int64) *big.Float { guess := new(big.Float).Quo(x, big.NewFloat(float64(n))) diff := big.NewFloat(1) ep := big.NewFloat(0.00000001) abs := new(big.Float).Abs(diff) for abs.Cmp(ep) >= 0 { //fmt.Println(guess, abs) prev := Pow(guess, n-1) diff = new(big.Float).Quo(x, prev) diff = diff.Sub(diff, guess) diff = diff.Quo(diff, big.NewFloat(float64(n))) guess = guess.Add(guess, diff) abs = new(big.Float).Abs(diff) } return guess }
func TestPowSpecialValues(t *testing.T) { for _, f := range []struct { z, w float64 }{ {2, +0.0}, {2, -0.0}, {4.2, 1.0}, {math.Inf(+1), 2.0}, } { z := big.NewFloat(f.z).SetPrec(53) w := big.NewFloat(f.w).SetPrec(53) x64, acc := bigfloat.Pow(z, w).Float64() want := math.Pow(f.z, f.w) if x64 != want || acc != big.Exact { t.Errorf("Pow(%g, %g) =\n got %g (%s);\nwant %g (Exact)", f.z, f.w, x64, acc, want) } } }
func main() { const ( xmin, ymin, xmax, ymax = -2, -2, +2, +2 width, height = 512, 512 ) img := image.NewRGBA(image.Rect(0, 0, width, height)) for py := 0; py < height; py++ { y := float64(py)/height*(ymax-ymin) + ymin for px := 0; px < width; px++ { x := float64(px)/width*(xmax-xmin) + xmin z := complex{big.NewFloat(x), big.NewFloat(y)} // Image point (px, py) represents complex value z. img.Set(px, py, mandelbrot(z)) } } png.Encode(os.Stdout, img) // NOTE: ignoring errors }
// Exp returns a big.Float representation of exp(z). Precision is // the same as the one of the argument. The function returns +Inf // when z = +Inf, and 0 when z = -Inf. func Exp(z *big.Float) *big.Float { // exp(0) == 1 if z.Sign() == 0 { return big.NewFloat(1).SetPrec(z.Prec()) } // Exp(+Inf) = +Inf if z.IsInf() && z.Sign() > 0 { return big.NewFloat(math.Inf(+1)).SetPrec(z.Prec()) } // Exp(-Inf) = 0 if z.IsInf() && z.Sign() < 0 { return big.NewFloat(0).SetPrec(z.Prec()) } guess := new(big.Float) // try to get initial estimate using IEEE-754 math zf, _ := z.Float64() if zfs := math.Exp(zf); zfs == math.Inf(+1) || zfs == 0 { // too big or too small for IEEE-754 math, // perform argument reduction using // e^{2z} = (e^z)² halfZ := new(big.Float).Mul(z, big.NewFloat(0.5)) halfExp := Exp(halfZ.SetPrec(z.Prec() + 64)) return new(big.Float).Mul(halfExp, halfExp).SetPrec(z.Prec()) } else { // we got a nice IEEE-754 estimate guess.SetFloat64(zfs) } // f(t)/f'(t) = t*(log(t) - z) f := func(t *big.Float) *big.Float { x := new(big.Float) x.Sub(Log(t), z) return x.Mul(x, t) } x := newton(f, guess, z.Prec()) return x }
// return: x^y func Pow(x *big.Float, n int64) *big.Float { res := new(big.Float).Copy(x) if n < 0 { res = res.Quo(big.NewFloat(1), res) n = -n } else if n == 0 { return big.NewFloat(1) } y := big.NewFloat(1) for i := n; i > 1; { if i%2 == 0 { i /= 2 } else { y = y.Mul(res, y) i = (i - 1) / 2 } res = res.Mul(res, res) } return res.Mul(res, y) }
// Estimates the Variance based on the data set // If the data set is relatively small (< 1000 examples), then remove 1 from the total func (ad *AnomalyDetection) estimateVariance() *big.Float { // this means that the mean was never calculated before, therefore do it now // the means is needed for the cimputation of the deviation if ad.mean.Cmp(zero) == 0 { ad.estimateMean() } // initialize the total to zero totalVariance := big.NewFloat(0) totalDeviation := big.NewFloat(0) var deviation big.Float var deviationCopy big.Float var singleVariance big.Float // Loop while a is smaller than 1e100. for _, element := range ad.dataSet { // first calculate the deviation for each element, by subtracting the mean, take the absolute value deviation.Sub(&element, &ad.mean).Abs(&deviation) // add it to the total totalDeviation.Add(totalDeviation, &deviation) // calculate the variance by squaring it singleVariance = *deviationCopy.Mul(&deviation, &deviation) // ^2 // the calculate the variance totalVariance.Add(totalVariance, &singleVariance) } // calculate the variance // assign the variance to the anomaly detection object ad.variance = *totalVariance.Quo(totalVariance, &ad.totalSamples) // calculate the deviation ad.deviation = *totalDeviation.Quo(totalDeviation, &ad.totalSamples) return &ad.variance }
// Pow returns a big.Float representation of z**w. Precision is the same as the one // of the first argument. The function panics when z is negative. func Pow(z *big.Float, w *big.Float) *big.Float { if z.Sign() < 0 { panic("Pow: negative base") } // Pow(z, 0) = 1.0 if w.Sign() == 0 { return big.NewFloat(1).SetPrec(z.Prec()) } // Pow(z, 1) = z // Pow(+Inf, n) = +Inf if w.Cmp(big.NewFloat(1)) == 0 || z.IsInf() { return new(big.Float).Copy(z) } // Pow(z, -w) = 1 / Pow(z, w) if w.Sign() < 0 { x := new(big.Float) zExt := new(big.Float).Copy(z).SetPrec(z.Prec() + 64) wNeg := new(big.Float).Neg(w) return x.Quo(big.NewFloat(1), Pow(zExt, wNeg)).SetPrec(z.Prec()) } // w integer fast path if w.IsInt() { wi, _ := w.Int64() return powInt(z, int(wi)) } // compute w**z as exp(z log(w)) x := new(big.Float).SetPrec(z.Prec() + 64) logZ := Log(new(big.Float).Copy(z).SetPrec(z.Prec() + 64)) x.Mul(w, logZ) x = Exp(x) return x.SetPrec(z.Prec()) }
func TestFormatNumberBigFloat(t *testing.T) { AssertEqual(t, FormatNumberBigFloat(big.NewFloat(123456789.213123), 3, ",", "."), "123,456,789.213") AssertEqual(t, FormatNumberBigFloat(big.NewFloat(-12345.123123), 5, ",", "."), "-12,345.12312") AssertEqual(t, FormatNumberBigFloat(big.NewFloat(-1234.123123), 5, ",", "."), "-1,234.12312") AssertEqual(t, FormatNumberBigFloat(big.NewFloat(-123.123123), 5, ",", "."), "-123.12312") AssertEqual(t, FormatNumberBigFloat(big.NewFloat(-12.123123), 5, ",", "."), "-12.12312") AssertEqual(t, FormatNumberBigFloat(big.NewFloat(-1.123123), 5, ",", "."), "-1.12312") }