Class: Legion::Extensions::Agentic::Inference::HypothesisTesting::Helpers::HypothesisEngine

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/inference/hypothesis_testing/helpers/hypothesis_engine.rb

Constant Summary

Constants included from Constants

Constants::CONFIDENCE_LABELS, Constants::CONFIRMATION_THRESHOLD, Constants::DISCONFIRMATION_THRESHOLD, Constants::EVIDENCE_WEIGHT, Constants::MAX_HYPOTHESES, Constants::PRIOR_DEFAULT, Constants::STATUS_LABELS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHypothesisEngine

Returns a new instance of HypothesisEngine.



14
15
16
# File 'lib/legion/extensions/agentic/inference/hypothesis_testing/helpers/hypothesis_engine.rb', line 14

def initialize
  @hypotheses = {}
end

Instance Attribute Details

#hypothesesObject (readonly)

Returns the value of attribute hypotheses.



12
13
14
# File 'lib/legion/extensions/agentic/inference/hypothesis_testing/helpers/hypothesis_engine.rb', line 12

def hypotheses
  @hypotheses
end

Instance Method Details

#competing_hypotheses(domain:) ⇒ Object



49
50
51
# File 'lib/legion/extensions/agentic/inference/hypothesis_testing/helpers/hypothesis_engine.rb', line 49

def competing_hypotheses(domain:)
  @hypotheses.values.select { |h| h.domain == domain }
end

#confirmation_rateObject



59
60
61
62
63
64
65
# File 'lib/legion/extensions/agentic/inference/hypothesis_testing/helpers/hypothesis_engine.rb', line 59

def confirmation_rate
  total = @hypotheses.values.count { |h| %i[confirmed disconfirmed].include?(h.status) }
  return 0.0 if total.zero?

  confirmed = @hypotheses.values.count { |h| h.status == :confirmed }
  (confirmed.to_f / total).round(10)
end

#evaluate(hypothesis_id) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/legion/extensions/agentic/inference/hypothesis_testing/helpers/hypothesis_engine.rb', line 35

def evaluate(hypothesis_id)
  h = @hypotheses[hypothesis_id]
  return nil unless h
  return h if %i[confirmed disconfirmed].include?(h.status)

  if h.posterior >= Constants::CONFIRMATION_THRESHOLD
    h.confirm!
  elsif h.posterior <= Constants::DISCONFIRMATION_THRESHOLD
    h.disconfirm!
  else
    h
  end
end

#hypothesis_reportObject



67
68
69
70
71
72
73
74
75
# File 'lib/legion/extensions/agentic/inference/hypothesis_testing/helpers/hypothesis_engine.rb', line 67

def hypothesis_report
  by_status = @hypotheses.values.group_by(&:status).transform_values(&:count)
  {
    total:             @hypotheses.size,
    by_status:         by_status,
    confirmation_rate: confirmation_rate,
    most_confident:    most_confident(limit: 3).map(&:to_h)
  }
end

#most_confident(limit: 5) ⇒ Object



53
54
55
56
57
# File 'lib/legion/extensions/agentic/inference/hypothesis_testing/helpers/hypothesis_engine.rb', line 53

def most_confident(limit: 5)
  @hypotheses.values
             .sort_by { |h| -h.posterior }
             .first(limit)
end

#propose(description:, domain: 'general', prior: Constants::PRIOR_DEFAULT) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/legion/extensions/agentic/inference/hypothesis_testing/helpers/hypothesis_engine.rb', line 18

def propose(description:, domain: 'general', prior: Constants::PRIOR_DEFAULT)
  evict_oldest! if @hypotheses.size >= Constants::MAX_HYPOTHESES

  h = Hypothesis.new(description: description, domain: domain, prior: prior)
  @hypotheses[h.id] = h
  h
end

#test_hypothesis(hypothesis_id:, evidence_strength:, supporting: true) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/legion/extensions/agentic/inference/hypothesis_testing/helpers/hypothesis_engine.rb', line 26

def test_hypothesis(hypothesis_id:, evidence_strength:, supporting: true)
  h = @hypotheses[hypothesis_id]
  return nil unless h

  h.update_posterior!(evidence_strength: evidence_strength, supporting: supporting)
  evaluate(hypothesis_id)
  h
end

#to_hObject



77
78
79
80
81
82
83
# File 'lib/legion/extensions/agentic/inference/hypothesis_testing/helpers/hypothesis_engine.rb', line 77

def to_h
  {
    hypotheses:        @hypotheses.values.map(&:to_h),
    confirmation_rate: confirmation_rate,
    total:             @hypotheses.size
  }
end