Sha256: e73f672cf496db1c12406c86fe47af1701f345689aaf65e4ce6353a33bc3c0bb

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

module Bayesnet
  class Node
    attr_reader :name, :parent_nodes, :factor

    def initialize(name, parent_nodes)
      @name = name
      @parent_nodes = parent_nodes
      @values = []
    end

    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
        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(&block)
      end
    end

    def distributions(&block)
      instance_eval(&block)
    end

    def as(distribution, given:)
      @values.zip(distribution).each do |value, probability|
        @factor.val [value] + given + [probability]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bayesnet-0.0.3 lib/bayesnet/node.rb