Class: Prolly::RandVar

Inherits:
Object
  • Object
show all
Includes:
Entropy, Infogain, Pdf, Prob
Defined in:
lib/prolly/rand_var.rb,
lib/prolly/rand_var/pdf.rb,
lib/prolly/rand_var/prob.rb,
lib/prolly/rand_var/entropy.rb,
lib/prolly/rand_var/infogain.rb

Defined Under Namespace

Modules: Entropy, Infogain, Pdf, Prob

Instance Method Summary collapse

Methods included from Infogain

#infogain

Methods included from Entropy

#entropy

Methods included from Pdf

#pdf

Methods included from Prob

#prob

Constructor Details

#initialize(pspace, *rand_vars) ⇒ RandVar

Returns a new instance of RandVar.



15
16
17
18
19
20
21
22
# File 'lib/prolly/rand_var.rb', line 15

def initialize(pspace, *rand_vars)
  @pspace = pspace

  @uspec_rv, @spec_rv = parse(rand_vars)

  @uspec_gv = []
  @spec_gv = {}
end

Instance Method Details

#countObject



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/prolly/rand_var.rb', line 55

def count
  if !@spec_rv.empty?
    if @uspec_gv.empty? and @spec_gv.empty?
      @pspace.count(@spec_rv)
    else
      @pspace.count(@spec_rv.merge(@spec_gv))
    end
  else
    @pspace.count(@uspec_rv)
  end
end

#given(*rand_vars) ⇒ Object



49
50
51
52
53
# File 'lib/prolly/rand_var.rb', line 49

def given(*rand_vars)
  @uspec_gv, @spec_gv = parse(rand_vars)

  return self
end

#parse(rand_vars) ⇒ Object

parses rand_var arguments

random variable are passed in as arguments to a method. It can take the format of:

:size

{ size: :large, color: :green }

:size, { color: :green, texture: :rough }


34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/prolly/rand_var.rb', line 34

def parse(rand_vars)
  if rand_vars.kind_of?(Hash)
    specified_rvs = rand_vars
    unspecified_rvs = []
  elsif rand_vars.kind_of?(Array)
    specified_rvs, unspecified_rvs = rand_vars.partition { |e| e.kind_of?(Hash) }
    specified_rvs = specified_rvs.inject({}) { |t, e| t.merge(e) }
  else # if it's a symbol
    specified_rvs = []
    unspecified_rvs = [rand_vars]
  end

  return unspecified_rvs, specified_rvs
end