package datascience

import "math"

// Standardize aplica Z-score normalización a un slice de float64.
// Cada valor se transforma a (x - mean) / stddev.
// Si stddev es 0, retorna un slice de ceros.
func Standardize(data []float64) []float64 {
	n := len(data)
	if n == 0 {
		return []float64{}
	}

	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([]float64, n)
	if stddev == 0 {
		return result
	}
	for i, v := range data {
		result[i] = (v - mean) / stddev
	}
	return result
}
