Class: Legion::Extensions::Agentic::Inference::Bayesian::Helpers::Belief

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

Constant Summary

Constants included from Constants

Constants::CONFIDENCE_LABELS, Constants::DECAY_RATE, Constants::DEFAULT_PRIOR, Constants::LIKELIHOOD_CEILING, Constants::LIKELIHOOD_FLOOR, Constants::MAX_EVIDENCE, Constants::MAX_HISTORY, Constants::MAX_HYPOTHESES, Constants::PRIOR_CEILING, Constants::PRIOR_FLOOR, Constants::STALE_THRESHOLD

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content:, domain:, prior: Constants::DEFAULT_PRIOR) ⇒ Belief

Returns a new instance of Belief.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/legion/extensions/agentic/inference/bayesian/helpers/belief.rb', line 17

def initialize(content:, domain:, prior: Constants::DEFAULT_PRIOR)
  @id               = SecureRandom.uuid
  @content          = content
  @domain           = domain
  @prior            = prior.clamp(Constants::PRIOR_FLOOR, Constants::PRIOR_CEILING)
  @posterior        = @prior
  @evidence_history = []
  @update_count     = 0
  @created_at       = Time.now.utc
  @last_updated_at  = @created_at
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



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

def content
  @content
end

#created_atObject (readonly)

Returns the value of attribute created_at.



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

def created_at
  @created_at
end

#domainObject (readonly)

Returns the value of attribute domain.



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

def domain
  @domain
end

#evidence_historyObject (readonly)

Returns the value of attribute evidence_history.



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

def evidence_history
  @evidence_history
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#last_updated_atObject (readonly)

Returns the value of attribute last_updated_at.



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

def last_updated_at
  @last_updated_at
end

#posteriorObject (readonly)

Returns the value of attribute posterior.



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

def posterior
  @posterior
end

#priorObject (readonly)

Returns the value of attribute prior.



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

def prior
  @prior
end

#update_countObject (readonly)

Returns the value of attribute update_count.



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

def update_count
  @update_count
end

Instance Method Details

#confidence_labelObject



51
52
53
54
55
56
# File 'lib/legion/extensions/agentic/inference/bayesian/helpers/belief.rb', line 51

def confidence_label
  Constants::CONFIDENCE_LABELS.each do |range, label|
    return label if range.cover?(@posterior)
  end
  :unknown
end

#log_oddsObject



47
48
49
# File 'lib/legion/extensions/agentic/inference/bayesian/helpers/belief.rb', line 47

def log_odds
  Math.log(@posterior / (1.0 - @posterior))
end

#reset_to_prior!Object



63
64
65
66
67
68
69
# File 'lib/legion/extensions/agentic/inference/bayesian/helpers/belief.rb', line 63

def reset_to_prior!
  @posterior       = @prior
  @update_count    = 0
  @evidence_history = []
  @last_updated_at  = Time.now.utc
  @posterior
end

#stale?(threshold: Constants::STALE_THRESHOLD) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/legion/extensions/agentic/inference/bayesian/helpers/belief.rb', line 71

def stale?(threshold: Constants::STALE_THRESHOLD)
  (Time.now.utc - @last_updated_at) > threshold
end

#surprise(observation_likelihood:) ⇒ Object



58
59
60
61
# File 'lib/legion/extensions/agentic/inference/bayesian/helpers/belief.rb', line 58

def surprise(observation_likelihood:)
  clamped = observation_likelihood.clamp(Constants::LIKELIHOOD_FLOOR, 1.0)
  -Math.log2(clamped)
end

#to_hObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/legion/extensions/agentic/inference/bayesian/helpers/belief.rb', line 75

def to_h
  {
    id:               @id,
    content:          @content,
    domain:           @domain,
    prior:            @prior,
    posterior:        @posterior,
    confidence_label: confidence_label,
    log_odds:         log_odds,
    update_count:     @update_count,
    evidence_history: @evidence_history,
    created_at:       @created_at,
    last_updated_at:  @last_updated_at
  }
end

#update(likelihood:, evidence_id:) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/legion/extensions/agentic/inference/bayesian/helpers/belief.rb', line 29

def update(likelihood:, evidence_id:)
  clamped_likelihood = likelihood.clamp(Constants::LIKELIHOOD_FLOOR, Constants::LIKELIHOOD_CEILING)
  marginal = (clamped_likelihood * @posterior) + ((1.0 - clamped_likelihood) * (1.0 - @posterior))
  new_posterior = (clamped_likelihood * @posterior) / marginal
  @posterior = new_posterior.clamp(Constants::PRIOR_FLOOR, Constants::PRIOR_CEILING)
  @update_count    += 1
  @last_updated_at  = Time.now.utc

  @evidence_history << {
    evidence_id:     evidence_id,
    likelihood:      clamped_likelihood,
    posterior_after: @posterior
  }
  @evidence_history.shift while @evidence_history.size > Constants::MAX_HISTORY

  @posterior
end