Class: Legion::Extensions::Agentic::Attention::SignalDetection::Helpers::Detector

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/attention/signal_detection/helpers/detector.rb

Constant Summary

Constants included from Constants

Constants::BIAS_LABELS, Constants::CRITERION_CEILING, Constants::CRITERION_FLOOR, Constants::DECAY_RATE, Constants::DEFAULT_CRITERION, Constants::DEFAULT_SENSITIVITY, Constants::LEARNING_RATE, Constants::MAX_DETECTORS, Constants::MAX_HISTORY, Constants::MAX_TRIALS, Constants::SENSITIVITY_CEILING, Constants::SENSITIVITY_FLOOR, Constants::SENSITIVITY_LABELS, Constants::TRIAL_OUTCOMES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Constants

bias_label, clamp_criterion, clamp_sensitivity, sensitivity_label

Constructor Details

#initialize(domain:) ⇒ Detector

Returns a new instance of Detector.



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/legion/extensions/agentic/attention/signal_detection/helpers/detector.rb', line 17

def initialize(domain:)
  @id               = SecureRandom.uuid
  @domain           = domain
  @sensitivity      = Constants::DEFAULT_SENSITIVITY
  @criterion        = Constants::DEFAULT_CRITERION
  @hits             = 0
  @misses           = 0
  @false_alarms     = 0
  @correct_rejections = 0
  @trial_count      = 0
  @created_at       = Time.now.utc
  @last_trial_at    = nil
end

Instance Attribute Details

#correct_rejectionsObject (readonly)

Returns the value of attribute correct_rejections.



14
15
16
# File 'lib/legion/extensions/agentic/attention/signal_detection/helpers/detector.rb', line 14

def correct_rejections
  @correct_rejections
end

#created_atObject (readonly)

Returns the value of attribute created_at.



14
15
16
# File 'lib/legion/extensions/agentic/attention/signal_detection/helpers/detector.rb', line 14

def created_at
  @created_at
end

#criterionObject (readonly)

Returns the value of attribute criterion.



14
15
16
# File 'lib/legion/extensions/agentic/attention/signal_detection/helpers/detector.rb', line 14

def criterion
  @criterion
end

#domainObject (readonly)

Returns the value of attribute domain.



14
15
16
# File 'lib/legion/extensions/agentic/attention/signal_detection/helpers/detector.rb', line 14

def domain
  @domain
end

#false_alarmsObject (readonly)

Returns the value of attribute false_alarms.



14
15
16
# File 'lib/legion/extensions/agentic/attention/signal_detection/helpers/detector.rb', line 14

def false_alarms
  @false_alarms
end

#hitsObject (readonly)

Returns the value of attribute hits.



14
15
16
# File 'lib/legion/extensions/agentic/attention/signal_detection/helpers/detector.rb', line 14

def hits
  @hits
end

#idObject (readonly)

Returns the value of attribute id.



14
15
16
# File 'lib/legion/extensions/agentic/attention/signal_detection/helpers/detector.rb', line 14

def id
  @id
end

#last_trial_atObject (readonly)

Returns the value of attribute last_trial_at.



14
15
16
# File 'lib/legion/extensions/agentic/attention/signal_detection/helpers/detector.rb', line 14

def last_trial_at
  @last_trial_at
end

#missesObject (readonly)

Returns the value of attribute misses.



14
15
16
# File 'lib/legion/extensions/agentic/attention/signal_detection/helpers/detector.rb', line 14

def misses
  @misses
end

#sensitivityObject (readonly)

Returns the value of attribute sensitivity.



14
15
16
# File 'lib/legion/extensions/agentic/attention/signal_detection/helpers/detector.rb', line 14

def sensitivity
  @sensitivity
end

#trial_countObject (readonly)

Returns the value of attribute trial_count.



14
15
16
# File 'lib/legion/extensions/agentic/attention/signal_detection/helpers/detector.rb', line 14

def trial_count
  @trial_count
end

Instance Method Details

#accuracyObject



67
68
69
70
71
# File 'lib/legion/extensions/agentic/attention/signal_detection/helpers/detector.rb', line 67

def accuracy
  return 0.0 if @trial_count.zero?

  (@hits + @correct_rejections).to_f / @trial_count
end

#adjust_criterion(amount:) ⇒ Object



81
82
83
# File 'lib/legion/extensions/agentic/attention/signal_detection/helpers/detector.rb', line 81

def adjust_criterion(amount:)
  @criterion = Constants.clamp_criterion(@criterion + amount)
end

#bias_labelObject



77
78
79
# File 'lib/legion/extensions/agentic/attention/signal_detection/helpers/detector.rb', line 77

def bias_label
  Constants.bias_label(@criterion)
end

#compute_criterionObject



62
63
64
65
# File 'lib/legion/extensions/agentic/attention/signal_detection/helpers/detector.rb', line 62

def compute_criterion
  c = -0.5 * (z_score(hit_rate) + z_score(false_alarm_rate))
  Constants.clamp_criterion(c)
end

#compute_dprimeObject



57
58
59
60
# File 'lib/legion/extensions/agentic/attention/signal_detection/helpers/detector.rb', line 57

def compute_dprime
  d = z_score(hit_rate) - z_score(false_alarm_rate)
  Constants.clamp_sensitivity(d)
end

#false_alarm_rateObject



52
53
54
55
# File 'lib/legion/extensions/agentic/attention/signal_detection/helpers/detector.rb', line 52

def false_alarm_rate
  noise_total = @false_alarms + @correct_rejections + 1
  (@false_alarms + 0.5) / noise_total.to_f
end

#hit_rateObject



47
48
49
50
# File 'lib/legion/extensions/agentic/attention/signal_detection/helpers/detector.rb', line 47

def hit_rate
  signal_total = @hits + @misses + 1
  (@hits + 0.5) / signal_total.to_f
end

#record_trial(outcome:) ⇒ Object

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/legion/extensions/agentic/attention/signal_detection/helpers/detector.rb', line 31

def record_trial(outcome:)
  raise ArgumentError, "invalid outcome: #{outcome}" unless Constants::TRIAL_OUTCOMES.include?(outcome)

  case outcome
  when :hit               then @hits += 1
  when :miss              then @misses += 1
  when :false_alarm       then @false_alarms += 1
  when :correct_rejection then @correct_rejections += 1
  end

  @trial_count   += 1
  @last_trial_at  = Time.now.utc

  update_sensitivity
end

#sensitivity_labelObject



73
74
75
# File 'lib/legion/extensions/agentic/attention/signal_detection/helpers/detector.rb', line 73

def sensitivity_label
  Constants.sensitivity_label(@sensitivity)
end

#to_hObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/legion/extensions/agentic/attention/signal_detection/helpers/detector.rb', line 85

def to_h
  {
    id:                 @id,
    domain:             @domain,
    sensitivity:        @sensitivity,
    criterion:          @criterion,
    hits:               @hits,
    misses:             @misses,
    false_alarms:       @false_alarms,
    correct_rejections: @correct_rejections,
    trial_count:        @trial_count,
    hit_rate:           hit_rate,
    false_alarm_rate:   false_alarm_rate,
    accuracy:           accuracy,
    sensitivity_label:  sensitivity_label,
    bias_label:         bias_label,
    created_at:         @created_at,
    last_trial_at:      @last_trial_at
  }
end