Class: CompletionKit::MetricCalibrationStats

Inherits:
Object
  • Object
show all
Defined in:
app/services/completion_kit/metric_calibration_stats.rb

Defined Under Namespace

Classes: Result

Constant Summary collapse

PROVISIONAL_MIN =
10
FIRM_MIN =
30
CURRENT =
:current

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metric:, judge_version: nil, all_versions: false) ⇒ MetricCalibrationStats

Returns a new instance of MetricCalibrationStats.



45
46
47
48
49
# File 'app/services/completion_kit/metric_calibration_stats.rb', line 45

def initialize(metric:, judge_version: nil, all_versions: false)
  @metric = metric
  @judge_version = judge_version
  @all_versions = all_versions
end

Class Method Details

.for(metric, judge_version: CURRENT) ⇒ Object



36
37
38
39
40
41
42
43
# File 'app/services/completion_kit/metric_calibration_stats.rb', line 36

def self.for(metric, judge_version: CURRENT)
  resolved = case judge_version
             when CURRENT then JudgeVersion.current.find_by(metric_id: metric.id)
             when nil then nil
             else judge_version
             end
  new(metric: metric, judge_version: resolved, all_versions: judge_version.nil?).call
end

Instance Method Details

#callObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/services/completion_kit/metric_calibration_stats.rb', line 51

def call
  scope = Calibration.where(metric_id: @metric.id)
  if @judge_version
    scope = scope.where(judge_version_id: @judge_version.id)
  elsif !@all_versions
    scope = scope.none
  end

  verdicts = scope.pluck(:verdict, :corrected_score, :response_id)
  n = verdicts.length
  agrees = verdicts.count { |v, _, _| v == "agree" }
  disagrees = verdicts.count { |v, _, _| v == "disagree" }
  borderlines = verdicts.count { |v, _, _| v == "borderline" }

  ci = CalibrationMath.wilson_interval(successes: agrees, n: n)

  pairs = score_pairs(verdicts)
  mae_value = CalibrationMath.mae(pairs)
  pearson_value = CalibrationMath.pearson(pairs)
  kappa_value = CalibrationMath.quadratic_weighted_kappa(pairs, categories: 1..5)

  Result.new(
    sample_size: n,
    agree_count: agrees,
    disagree_count: disagrees,
    borderline_count: borderlines,
    agreement_point: ci[:point],
    agreement_low: ci[:low],
    agreement_high: ci[:high],
    borderline_rate: n.zero? ? nil : borderlines.to_f / n,
    mae: mae_value,
    pearson: pearson_value,
    kappa: kappa_value,
    gate: gate_for(n)
  )
end