Example #1
0
func Correlation(data1 interface{}, stride1 int, data2 interface{}, stride2 int, n int) (r float64) {
	var ratio, delta_x, delta_y, sum_xsq, sum_ysq, sum_cross, mean_x, mean_y float64

	arry1 := reflect.ValueOf(data1)
	arry2 := reflect.ValueOf(data2)

	/*
	 Compute:
	 sum_xsq = Sum [ (x_i - mu_x)^2 ],
	 sum_ysq = Sum [ (y_i - mu_y)^2 ] and
	 sum_cross = Sum [ (x_i - mu_x) * (y_i - mu_y) ]
	 using the above relation from Welford's paper
	*/

	mean_x = number.Float(arry1.Index(0 * stride1))
	mean_y = number.Float(arry2.Index(0 * stride2))

	for i := 0; i < n; i++ {
		v1 := number.Float(arry1.Index(i * stride1))
		v2 := number.Float(arry2.Index(i * stride2))
		ratio = float64(i) / float64(i+1)
		delta_x = v1 - mean_x
		delta_y = v2 - mean_y
		sum_xsq += delta_x * delta_x * ratio
		sum_ysq += delta_y * delta_y * ratio
		sum_cross += delta_x * delta_y * ratio
		mean_x += delta_x / float64(i+1)
		mean_y += delta_y / float64(i+1)
	}

	r = sum_cross / (math.Sqrt(sum_xsq) * math.Sqrt(sum_ysq))

	return
}
Example #2
0
func computeCovariance(data1 interface{}, stride1 int, data2 interface{}, stride2 int, n int, mean1, mean2 float64) (covariance float64) {
	arry1 := reflect.ValueOf(data1)
	arry2 := reflect.ValueOf(data2)
	// find the sum of the square
	for i := 0; i < n; i++ {
		v1 := number.Float(arry1.Index(i * stride1))
		v2 := number.Float(arry2.Index(i * stride2))
		delta1 := v1 - mean1
		delta2 := v2 - mean2
		covariance += (delta1*delta2 - covariance) / float64(i+1)
	}
	return
}
Example #3
0
func Mean(data interface{}, stride, n int) (mean float64) {
	arry := reflect.ValueOf(data)
	for i := 0; i < n; i++ {
		v := arry.Index(i * stride)
		fv := number.Float(v)
		mean += (fv - mean) / float64(i+1)
	}
	return
}
Example #4
0
// Take a dataset and finds the variance
func computeVariance(data interface{}, stride, n int, mean float64) (variance float64) {
	arry := reflect.ValueOf(data)
	// find the sum of the squares
	for i := 0; i < n; i++ {
		v := arry.Index(i * stride)
		f := number.Float(v)
		delta := f - mean
		variance += (delta*delta - variance) / float64(i+1)
	}
	return
}
Example #5
0
// Takes a dataset and finds the sum of squares about the mean
func computeTss(data interface{}, stride, n int, mean float64) (tss float64) {
	arry := reflect.ValueOf(data)
	// find the sum of the squares
	for i := 0; i < n; i++ {
		v := arry.Index(i * stride)
		f := number.Float(v)
		delta := f - mean
		tss += delta * delta
	}
	return
}
Example #6
0
// Takes a dataset and finds the absolute deviation with a fixed mean
func AbsdevM(data interface{}, stride, n int, mean float64) (absdev float64) {
	arry := reflect.ValueOf(data)
	sum := 0.0
	for i := 0; i < n; i++ {
		v := arry.Index(i * stride)
		f := number.Float(v)
		delta := math.Abs(f - mean)
		sum += delta
	}
	absdev = sum / float64(n)
	return
}