Methods
Attributes
[R] extra_info
[R] name
[R] outcomes
[R] relations
[R] value
Public Class methods
new( name , outcomes = [true, false], extra_info = nil)

create de BayesNetNode name is the identifier of the node in the Bayesian Network outcomes is an array with the posible values that this node can have, by default [true, false].

optional
extra_info extra information that can be putted in the node
     # File lib/bn4r/bn.rb, line 172
172:   def initialize ( name , outcomes = [true, false], extra_info = nil)
173:     @name = name
174:     @outcomes = outcomes
175:     @extra_info = extra_info
176:     @givens = []
177:     @relations = []
178:   end
Public Instance methods
all_parents_with_values?()

Returns true if all parents of the node in the bn have values

     # File lib/bn4r/bn.rb, line 232
232:   def all_parents_with_values?
233:     parents.select {|v| !v.value.nil? }.size == parents.size
234:   end
clear_value()
     # File lib/bn4r/bn.rb, line 197
197:   def clear_value
198:     @value = nil
199:   end
copy()

Returns a copy of the node itself

     # File lib/bn4r/bn.rb, line 186
186:   def copy
187:     tmp = BayesNetNode.new(@name, @outcomes, @extra_info)
188:     tmp.set_value(@value)
189:     tmp.set_probability_table(@givens, @table)
190:     tmp
191:   end
deep(node=self)

Returns de deep of the node ( larger path to root nodes )

     # File lib/bn4r/bn.rb, line 217
217:   def deep(node=self)
218:     res = 1
219:     res += node.parents.collect { |parent|
220:       parent.deep  
221:     }.max unless node.root?
222:     return res
223:   end
get_probability(node_assignment = value, givens_assignments = parents_assignments)

Returns the probability of an assignment to a node conditioned to given_assignments

   P(node = node_assignment | givens = givens_assignments)

All givens_assigments must have value.

     # File lib/bn4r/bn.rb, line 280
280:   def get_probability(node_assignment = value, givens_assignments = parents_assignments)
281: #    raise "Node must have a value and a givens_assignments, otherwise put" \

282:  #         + "them in function call" if node_assignment.nil? or givens_assignments.nil?

283:     # if there's a cached table take the index

284:     return @table[get_table_index(node_assignment, givens_assignments)] if @table_is_a_proc.nil? or !@table_is_a_proc
285:     # if the value is calculated on the fly using a function instead of

286:     # a table

287:     return @table[node_assignment, givens_assignments]
288:   end
get_table_index(node_assignment, givens_assignments)

Returns the corresponding index for the probability table given node_assignment and givens_assignments

     # File lib/bn4r/bn.rb, line 292
292:   def get_table_index(node_assignment, givens_assignments)
293:     x = []
294:     indices = []
295:     index = 0
296:     
297:     if givens_assignments.length != @givens.length
298:       raise "Error. Number of assignments does not match node."
299:     end
300:   
301:     if @givens.length > 0
302:       # create a indices array with the position of each value of

303:       # given assignments.

304:       givens_assignments.length.times { |i|
305:         assignment = givens_assignments[i]
306:         indices[i] = @givens[i].outcomes.index(assignment)
307:       }
308:   
309:       # create a array with the number of possible values each

310:       # given node and this node itself can have ( node.outcomes.length )

311:       # plus all next nodes.

312:       x[givens_assignments.length-1] = @outcomes.length
313:       (givens_assignments.length-2).downto(0) { |j|
314:         x[j] = x[j+1] * @givens[j+1].outcomes.length
315:       }      
316:   
317:       # to get the index, sum for each assignment the

318:       # product of each given assignment outcomes size

319:       # by its value index.

320:       givens_assignments.length.times { |i|
321:         index += x[i] * indices[i]
322:       }
323:     end
324:     
325:     index += @outcomes.index(node_assignment)
326:     
327:     return index
328:   end
get_table_size()

returns the number of cells that conditional probability table ( CPT ) haves.

     # File lib/bn4r/bn.rb, line 264
264:   def get_table_size
265:     num = @outcomes.size
266:     @givens.each { |given| num = num * given.outcomes.size }
267:     return num
268:   end
num_parents()

Return the number of parents

     # File lib/bn4r/bn.rb, line 212
212:   def num_parents
213:     parents.size
214:   end
parents()

Return node parents

     # File lib/bn4r/bn.rb, line 202
202:   def parents
203:     return @givens
204:   end
parents_assignments()
     # File lib/bn4r/bn.rb, line 258
258:   def parents_assignments
259:     parents.collect { |p| p.value }
260:   end
relations()

Return node relations

     # File lib/bn4r/bn.rb, line 207
207:   def relations
208:     return @relations
209:   end
rename(new_name)

Renames a node

     # File lib/bn4r/bn.rb, line 181
181:   def rename(new_name)
182:     @name = new_name  
183:   end
root?()

Returns true if the node is a root node ( doesn’t have parents ).

     # File lib/bn4r/bn.rb, line 226
226:   def root?
227:     return true if num_parents == 0
228:     false
229:   end
set_probability(number, node_assignment, givens_assignments)

Sets a probability to the node with a node_assignment conditioned to given_assignments

  P (node = node_assignment | givens = givens_assignments) = number
     # File lib/bn4r/bn.rb, line 272
272:   def set_probability(number, node_assignment, givens_assignments)
273:     @table[get_table_index(node_assignment, givens_assignments)] = number
274:   end
set_probability_table(givens, table)

If givens don’t exist, it adds them

if givens is nil, then internal givens is assumed table must have probability values in order like BAD!!! [g0=pos0 & g1=pos0 & … & node_value=pos0, …, g0=pos0 & g1=pos0 & … & node_value=posN,

     # File lib/bn4r/bn.rb, line 246
246:   def set_probability_table (givens, table)
247:     # perhaps we should do some error checking on the table entries here?

248:     @table_is_a_proc = (table.class != Array)
249:     @givens = givens if !givens.nil?
250:     
251:     raise "Error table incorrect number of positions (" \
252:       + table.size.to_s + " of " + self.get_table_size.to_s \
253:       + ")" if table.size != self.get_table_size
254: 
255:     @table = table
256:   end
set_value(value)
     # File lib/bn4r/bn.rb, line 193
193:   def set_value(value)
194:     @value = value
195:   end
to_s()
     # File lib/bn4r/bn.rb, line 236
236:   def to_s
237:     return name + (value.nil? ? "" : (" = " + value.to_s))
238:   end