Class: Vangrail::DirichletThreatPrior
- Inherits:
-
Object
- Object
- Vangrail::DirichletThreatPrior
- Defined in:
- lib/vangrail/risk_scenario.rb
Overview
Immutable Dirichlet posterior over named attack families.
Instance Attribute Summary collapse
-
#concentrations ⇒ Object
readonly
Returns the value of attribute concentrations.
Instance Method Summary collapse
- #adjudicate(family) ⇒ Object
-
#initialize(values = nil, **families) ⇒ DirichletThreatPrior
constructor
A new instance of DirichletThreatPrior.
- #intervals(confidence: 0.95) ⇒ Object
- #mean ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(values = nil, **families) ⇒ DirichletThreatPrior
Returns a new instance of DirichletThreatPrior.
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/vangrail/risk_scenario.rb', line 60 def initialize(values = nil, **families) raise ArgumentError, 'pass threat concentrations once' if values && !families.empty? raw = values || families raise ArgumentError, 'threat concentrations must be a nonempty hash' unless raw.is_a?(Hash) && !raw.empty? @concentrations = raw.to_h do |family, concentration| [family.to_s.freeze, positive(concentration, family)] end.freeze freeze end |
Instance Attribute Details
#concentrations ⇒ Object (readonly)
Returns the value of attribute concentrations.
58 59 60 |
# File 'lib/vangrail/risk_scenario.rb', line 58 def concentrations @concentrations end |
Instance Method Details
#adjudicate(family) ⇒ Object
88 89 90 91 92 93 |
# File 'lib/vangrail/risk_scenario.rb', line 88 def adjudicate(family) name = family.to_s raise ArgumentError, "unknown threat family #{family.inspect}" unless concentrations.key?(name) self.class.new(concentrations.merge(name => concentrations.fetch(name) + 1)) end |
#intervals(confidence: 0.95) ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/vangrail/risk_scenario.rb', line 77 def intervals(confidence: 0.95) validate_confidence!(confidence) total = concentrations.values.sum tail = (1 - confidence) / 2.0 concentrations.to_h do |family, value| bounds = [Beta.quantile(tail, value, total - value), Beta.quantile(1 - tail, value, total - value)].freeze [family, bounds] end.freeze end |
#mean ⇒ Object
72 73 74 75 |
# File 'lib/vangrail/risk_scenario.rb', line 72 def mean total = concentrations.values.sum concentrations.to_h { |family, value| [family, value.fdiv(total)] }.freeze end |
#to_h ⇒ Object
95 96 97 |
# File 'lib/vangrail/risk_scenario.rb', line 95 def to_h { 'concentrations' => concentrations, 'mean' => mean }.freeze end |