Class: Vangrail::DirichletThreatPrior

Inherits:
Object
  • Object
show all
Defined in:
lib/vangrail/risk_scenario.rb

Overview

Immutable Dirichlet posterior over named attack families.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values = nil, **families) ⇒ DirichletThreatPrior

Returns a new instance of DirichletThreatPrior.

Raises:

  • (ArgumentError)


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

#concentrationsObject (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

Raises:

  • (ArgumentError)


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

#meanObject



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_hObject



95
96
97
# File 'lib/vangrail/risk_scenario.rb', line 95

def to_h
  { 'concentrations' => concentrations, 'mean' => mean }.freeze
end