package finance

import "math"

// SharpeRatio calcula el ratio de Sharpe.
// riskFreeRate es la tasa libre de riesgo por periodo.
// periodsPerYear indica cuantos periodos hay en un anio (e.g. 252 para diario).
// Formula: (mean(returns) - riskFreeRate) / stddev(returns) * sqrt(periodsPerYear)
func SharpeRatio(returns []float64, riskFreeRate 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
	}
	std := math.Sqrt(variance / float64(n-1))
	if std == 0 {
		return 0
	}
	return (mean - riskFreeRate) / std * math.Sqrt(periodsPerYear)
}
