nutterb / HydeNet

Hybrid Decision Networks in R
Other
23 stars 3 forks source link

Input CPT from console #59

Closed jarrod-dalton closed 9 years ago

jarrod-dalton commented 9 years ago

I've been working on a function inputCPT(). I will commit it to the development branch soon.

Here is a demo:

> inputCPT(wetGrass ~ rain + morning)
--------------------------------------------------------------------------------
Enter Factor Levels for node 'wetGrass':

If this is a binary variable, enter '<yn>' as a shortcut.
When finished, enter '<z>'.
To repeat entry of the last inputted factor level, enter <b>.
To start over entirely, enter '<s>'
--------------------------------------------------------------------------------
Level 1 of 'wetGrass'?
<yn>
--------------------------------------------------------------------------------
Enter Factor Levels for node 'rain':

If this is a binary variable, enter '<yn>' as a shortcut.
When finished, enter '<z>'.
To repeat entry of the last inputted factor level, enter <b>.
To start over entirely, enter '<s>'
--------------------------------------------------------------------------------
Level 1 of 'rain'?
<yn>
--------------------------------------------------------------------------------
Enter Factor Levels for node 'morning':

If this is a binary variable, enter '<yn>' as a shortcut.
When finished, enter '<z>'.
To repeat entry of the last inputted factor level, enter <b>.
To start over entirely, enter '<s>'
--------------------------------------------------------------------------------
Level 1 of 'morning'?
no
Level 2 of 'morning'?
yy
Level 3 of 'morning'?
<z>
--------------------------------------------------------------------------------
 Enter the following conditional probabilities, or positive
 numbers proportional to them (e.g., counts):
(Use '<q>' to halt execution)
--------------------------------------------------------------------------------
Pr(wetGrass=No | rain=No, morning=no):
.95
Pr(wetGrass=Yes | rain=No, morning=no):
.05
Pr(wetGrass=No | rain=Yes, morning=no):
.01
Pr(wetGrass=Yes | rain=Yes, morning=no):
.2
Pr(wetGrass=No | rain=No, morning=yy):
.5
Pr(wetGrass=Yes | rain=No, morning=yy):
1
Pr(wetGrass=No | rain=Yes, morning=yy):
.99
Pr(wetGrass=Yes | rain=Yes, morning=yy):
9999
, , wetGrass = No

     morning
rain          no           yy
  No  0.95000000 0.3333333333
  Yes 0.04761905 0.0000990001

, , wetGrass = Yes

     morning
rain        no        yy
  No  0.050000 0.6666667
  Yes 0.952381 0.9999010

attr(,"class")
[1] "cpt"

Here's another demo:

> inputCPT(wetGrass ~ rain + morning,
+          factorLevels <- list(wetGrass = c("nn","yy"),
+                               rain     = c("nope","yep"),
+                               morning  = c("NO","YES")),
+          reduce = TRUE)
--------------------------------------------------------------------------------
NOTE: parameter 'reduce' is set to TRUE in readCPTFromConsole().
      Conditional probabilities Pr(wetGrass=nn | rain, morning) will be calculated
      as the complement of the inputted probabilities Pr(wetGrass != nn | rain, morning).
--------------------------------------------------------------------------------
Enter the following conditional probabilities:
Use '<q>' to halt execution.
To go back one step and re-enter, enter '<b>'.
--------------------------------------------------------------------------------
Pr(wetGrass=yy | rain=nope, morning=NO  ):   -3
Invalid probability given. Enter a number in [0,1]:
Pr(wetGrass=yy | rain=nope, morning=NO  ):   notANumber!
Invalid numeric data entry. Try again:
Pr(wetGrass=yy | rain=nope, morning=NO  ):   .1
Pr(wetGrass=yy | rain=yep , morning=NO  ):   .2
Pr(wetGrass=yy | rain=nope, morning=YES ):   <b>
Pr(wetGrass=yy | rain=yep , morning=NO  ):   .3
Pr(wetGrass=yy | rain=nope, morning=YES ):   .4
Pr(wetGrass=yy | rain=yep , morning=YES ):   .5
, , wetGrass = nn

      morning
rain    NO YES
  nope 0.9 0.6
  yep  0.7 0.5

, , wetGrass = yy

      morning
rain    NO YES
  nope 0.1 0.4
  yep  0.3 0.5

attr(,"class")
[1] "cpt"
jarrod-dalton commented 9 years ago

cpt() and inputCPT() are finished. Try it out to see if there are any bugs that pop up.