# typed: strict
# frozen_string_literal: true

module Stats
  class LinearNormalizer < DataNormalizer
    # Denormalize linear regression results back to original scale
    sig do
      override
        .params(regression_x: T::Array[Float], weight_vec: T::Array[Float])
        .returns(T::Array[Float])
    end
    def denormalize_regression(regression_x, weight_vec)
      norm_slope = T.cast(weight_vec[1], Float)
      norm_intercept = T.cast(weight_vec[0], Float)
      regression_x.map do |x|
        x_norm = @x.normalize(x)
        y_norm = norm_slope * x_norm + norm_intercept
        @y.denormalize(y_norm)
      end
    end

    # Denormalize linear regression coefficients back to original scale
    sig { params(coefficients: [Float, Float]).returns([Float, Float]) }
    def denormalize_coefficients(coefficients)
      norm_slope = coefficients[0]
      norm_intercept = coefficients[1]
      slope_orig = norm_slope * @y.scale / @x.scale
      intercept_orig =
        (norm_intercept * @y.scale + @y.min) - slope_orig * @x.min

      [intercept_orig, slope_orig]
    end

    protected

    sig { override.returns(T.class_of(Stats::Axis)) }
    def axis_class
      Stats::LinearAxis
    end
  end
end
