Class: Sbn::NumericVariable

Inherits:
Variable show all
Defined in:
lib/sbn/numeric_variable.rb

Constant Summary collapse

DEFAULT_FIRST_STDEV_STATE_COUNT =
14
DEFAULT_SECOND_STDEV_STATE_COUNT =
6

Constants inherited from Variable

Variable::NEGLIGIBLE_PROBABILITY

Instance Attribute Summary collapse

Attributes inherited from Variable

#children, #name, #parents, #probability_table, #states

Instance Method Summary collapse

Methods inherited from Variable

#==, #===, #add_child, #add_child_no_recurse, #add_parent, #add_parent_no_recurse, #add_sample_point, #can_be_evaluated?, #eql?, #evaluate_marginal, #evidence_name, #generate_probability_table, #get_random_state, #get_random_state_with_markov_blanket, #is_complete_evidence?, #set_in_evidence?, #set_probabilities, #set_probability, #set_states, #to_s, #to_xmlbif_definition

Constructor Details

#initialize(net, name, probabilities = [], state_thresholds = [], options = {}) ⇒ NumericVariable

Returns a new instance of NumericVariable.



8
9
10
11
12
13
14
15
16
# File 'lib/sbn/numeric_variable.rb', line 8

def initialize(net, name, probabilities = [], state_thresholds = [], options = {})
  @state_count_one = options.fetch(:first_stdev_state_count, DEFAULT_FIRST_STDEV_STATE_COUNT).to_f.round
  @state_count_two = options.fetch(:second_stdev_state_count, DEFAULT_SECOND_STDEV_STATE_COUNT).to_f.round
  @state_count_one += 1 if @state_count_one.odd?
  @state_count_two += 1 if @state_count_two.odd?
  @state_thresholds = state_thresholds
  states = generate_states_from_thresholds
  super(net, name, probabilities, states)
end

Instance Attribute Details

#state_thresholdsObject (readonly)

Returns the value of attribute state_thresholds.



6
7
8
# File 'lib/sbn/numeric_variable.rb', line 6

def state_thresholds
  @state_thresholds
end

Instance Method Details

#get_observed_state(evidence) ⇒ Object

:nodoc:



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sbn/numeric_variable.rb', line 57

def get_observed_state(evidence) # :nodoc:
  num = evidence[@name]
  thresholds = @state_thresholds.dup
  index = 0
  t = thresholds.shift
  while num >= t and !thresholds.empty? do
    t = thresholds.shift
    index += 1
  end
  index += 1 if num >= t and thresholds.empty?
  @states[index]
end

#set_probabilities_from_sample_points!Object

alter the state table based on the variance of the sample points



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/sbn/numeric_variable.rb', line 19

def set_probabilities_from_sample_points! # :nodoc:
  values = []
  @sample_points.each {|evidence| values << evidence[@name] }
  stdev = values.standard_deviation
  average = values.average
  increment_amount_for_first_stdev = stdev * 2.0 / @state_count_one.to_f
  increment_amount_for_second_stdev = stdev * 2.0 / @state_count_two.to_f
  current_position = average - (stdev * 2.0)
  @state_thresholds = []
  
  # start on the left, two standard deviations away from the average
  (@state_count_two / 2).times do
    @state_thresholds << current_position
    current_position += increment_amount_for_second_stdev
  end

  # continue to add thresholds within the first standard deviation
  @state_count_one.times do
    @state_thresholds << current_position
    current_position += increment_amount_for_first_stdev
  end

  # add thresholds to the second standard deviation on the right
  (@state_count_two / 2).times do
    @state_thresholds << current_position
    current_position += increment_amount_for_second_stdev
  end
  @states = generate_states_from_thresholds
  
  # Now that states have been determined, call parent
  # class to finish processing sample points.
  super
end

#to_xmlbif_variable(xml) ⇒ Object

:nodoc:



53
54
55
# File 'lib/sbn/numeric_variable.rb', line 53

def to_xmlbif_variable(xml) # :nodoc:
  super(xml) {|x| x.property("StateThresholds = #{@state_thresholds.join(',')}") }
end

#transform_evidence_value(val) ⇒ Object

:nodoc:



70
71
72
# File 'lib/sbn/numeric_variable.rb', line 70

def transform_evidence_value(val) # :nodoc:
  val.to_f
end