Class: RailsErrorDashboard::Services::PearsonCorrelation
- Inherits:
-
Object
- Object
- RailsErrorDashboard::Services::PearsonCorrelation
- Defined in:
- lib/rails_error_dashboard/services/pearson_correlation.rb
Overview
Pure algorithm: Calculate Pearson correlation coefficient between two series
No database access — accepts arrays, returns a correlation value. Used by ErrorCorrelation query to find time-correlated error types.
Class Method Summary collapse
-
.call(series_a, series_b) ⇒ Float
Correlation coefficient between -1.0 and 1.0.
Class Method Details
.call(series_a, series_b) ⇒ Float
Returns Correlation coefficient between -1.0 and 1.0.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rails_error_dashboard/services/pearson_correlation.rb', line 17 def self.call(series_a, series_b) return 0.0 if series_a.empty? || series_b.empty? return 0.0 if series_a.sum.zero? || series_b.sum.zero? n = series_a.length return 0.0 if n.zero? mean_a = series_a.sum.to_f / n mean_b = series_b.sum.to_f / n covariance = 0.0 std_a = 0.0 std_b = 0.0 n.times do |i| diff_a = series_a[i] - mean_a diff_b = series_b[i] - mean_b covariance += diff_a * diff_b std_a += diff_a**2 std_b += diff_b**2 end denominator = Math.sqrt(std_a * std_b) return 0.0 if denominator.zero? (covariance / denominator).round(3) end |