Class: Bayesnet::Node
- Inherits:
-
Object
- Object
- Bayesnet::Node
- Defined in:
- lib/bayesnet/node.rb
Instance Attribute Summary collapse
-
#factor ⇒ Object
readonly
Returns the value of attribute factor.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent_nodes ⇒ Object
readonly
Returns the value of attribute parent_nodes.
Instance Method Summary collapse
- #as(distribution, given:) ⇒ Object
- #distributions(&block) ⇒ Object
-
#initialize(name, parent_nodes) ⇒ Node
constructor
A new instance of Node.
-
#parameters ⇒ Object
— Node DSL —.
- #resolve_factor(parent_nodes) ⇒ Object
-
#values(hash_or_array = nil, &block) ⇒ Object
+++ Node DSL +++.
Constructor Details
Instance Attribute Details
#factor ⇒ Object (readonly)
Returns the value of attribute factor.
3 4 5 |
# File 'lib/bayesnet/node.rb', line 3 def factor @factor end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/bayesnet/node.rb', line 3 def name @name end |
#parent_nodes ⇒ Object (readonly)
Returns the value of attribute parent_nodes.
3 4 5 |
# File 'lib/bayesnet/node.rb', line 3 def parent_nodes @parent_nodes end |
Instance Method Details
#as(distribution, given:) ⇒ Object
42 43 44 45 46 |
# File 'lib/bayesnet/node.rb', line 42 def as(distribution, given:) @values.zip(distribution).each do |value, probability| @factor.val [value] + given + [probability] end end |
#distributions(&block) ⇒ Object
33 34 35 |
# File 'lib/bayesnet/node.rb', line 33 def distributions(&block) instance_eval(&block) end |
#parameters ⇒ Object
— Node DSL —
38 39 40 |
# File 'lib/bayesnet/node.rb', line 38 def parameters (values.size - 1) * parent_nodes.values.reduce(1) { |mul, n| mul * n.values.size } end |
#resolve_factor(parent_nodes) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/bayesnet/node.rb', line 48 def resolve_factor(parent_nodes) @parent_nodes = parent_nodes if @factor.is_a?(Proc) proc = @factor node = self @factor = Factor.build do scope node.name => node.values node.parent_nodes.each do |parent_node_name, parent_node| scope parent_node_name => parent_node.values end end instance_eval(&proc) end end |
#values(hash_or_array = nil, &block) ⇒ Object
+++ Node DSL +++
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/bayesnet/node.rb', line 13 def values(hash_or_array = nil, &block) case hash_or_array when NilClass @values when Hash @values = hash_or_array.keys node = self @factor = Factor.build do scope node.name => node.values hash_or_array.each do |value, probability| val [value, probability] end end when Array raise Error, "DSL error, #values requires a &block when first argument is an Array" unless block @values = hash_or_array @factor = block end end |