package datascience

import "math"

// DetectOutliers devuelve un []bool donde true indica que el valor es un outlier
// según z-score. Un valor es outlier si |z-score| > threshold.
func DetectOutliers(data []float64, threshold float64) []bool {
	n := len(data)
	if n == 0 {
		return []bool{}
	}

	var sum float64
	for _, v := range data {
		sum += v
	}
	mean := sum / float64(n)

	var sqSum float64
	for _, v := range data {
		d := v - mean
		sqSum += d * d
	}
	stddev := math.Sqrt(sqSum / float64(n))

	result := make([]bool, n)
	if stddev == 0 {
		return result
	}
	for i, v := range data {
		z := (v - mean) / stddev
		if z < 0 {
			z = -z
		}
		result[i] = z > threshold
	}
	return result
}
