package finance

import "math"

// AnnualizedVolatility calcula la volatilidad anualizada a partir de una serie de retornos.
// periodsPerYear indica cuantos periodos hay en un anio (e.g. 252 para retornos diarios).
// Retorna stddev(returns) * sqrt(periodsPerYear).
func AnnualizedVolatility(returns []float64, periodsPerYear float64) float64 {
	n := len(returns)
	if n < 2 {
		return 0
	}
	var sum float64
	for _, r := range returns {
		sum += r
	}
	mean := sum / float64(n)
	var variance float64
	for _, r := range returns {
		diff := r - mean
		variance += diff * diff
	}
	variance /= float64(n - 1)
	return math.Sqrt(variance) * math.Sqrt(periodsPerYear)
}
