codesimian
Class BayesNode

java.lang.Object
  extended by codesimian.CS
      extended by codesimian.DefaultCS
          extended by codesimian.BayesNode
All Implemented Interfaces:
CodeSimian, java.io.Serializable
Direct Known Subclasses:
ContinuousBayesNode

public class BayesNode
extends DefaultCS

ERROR IN JAVADOC 7/05: Executing this BayesNode does not add to the HISTORY (in param2). That is instead done externally by the bayesnet.

I'm a NODE (in a BAYESIAN NETWORK) with 2 or 3 params (each is a list), each with a different purpose (described below). This default implementation uses NAIVE BAYES, which means all nodes assume their inputs are independent, but subclasses or other CSs used with BayesNodes do not have to assume that.

Executing this BayesNode returns this BayesNode's CHANCE (0.0 to 1) based on the executes/chances of this node's params and the conditional-probabilities. If there is a param2, then param2 is a history list, and DForProxy() saves exec's returned value at the end of it.

HOW TO USE:
To make a normal bayesian network, connect many BayesNode's to each other, in a directed graph with no cycles, set all their myFuel's to 1, set the input values (input nodes should be leafs), and execute the top (output) node(s).

AVOIDING REDUNDANT OVERLAPPING RECURSIVE CALLS: It can cause exponential slowness, and infinite loops if you have cycles in your network. All BayesNode's must remember their own previous values of DForProxy() (their chances), so they can be used to calculate bayesian chances later. "Dynamic Programming". That remembering is a standard option that can be enabled in any CS object. Redundant recursive calls calculate the data only once if CS.myFuel() > 0 and that CS checks myFuel. When myFuel is finally decremented to 0, CS.D() returns the last double value again instead of calling CS.DForProxy() again, but only if you have that option turned on (see Exec.java).

WHICH PARAMS DO WHAT?:

Param0 contains a list of CSs that will be interpreted as bayesian nodes, the nodes whose chances I use to calculate my chance. These nodes can be almost any type of CS that has 3 params and lets them be manipulated like bayesian nodes. It does not matter if they give correct bayesian chances. For example, you might want to evolve something similar to a BayesNode but weirder and smarter, and use it with the normal BayesNodes.

Param1 contains a list of CONDITIONAL-PROBABILITIES, with size: 2 << param0.countP(). They should all sum to 1. A very small amount of error from 1 is allowed, but a lot more than roundoff error (6/05 it was .000001). They are interpreted as their absolute value. No negatives are summed.

The list uses the constant-pool (example Const.pool(.422124)) for efficiency.

It is 2 times as big as you'd expect because the chance of THIS NODE is included.

Example: If there are 3 child nodes, thats 2^(3+1)=16 condProb[] size. It contains the chance of each of the 16 possibilities of the 4 booleans. Each node's DForProxy() returns a chance describing that node's boolean value. An event either occurs or it does not, but we predict with chances.

The indexs for conditional-probabilities represent the combinations of the child nodes's being true or false. The 0s and 1s in the index are falses and trues. There is 1 more bit than childs. The last bit is for this (parent) node's chance.

8/05 I DONT THINK THIS PARAGRAPH IS ACCURATE ANYMORE: Param2 contains a (partial) list of the previous DForProxy() values of this BayesNode, the history, which is later used to calculate new conditional-probabilities. Param2 is optional. If countP()==2 then history of my chance is not saved. To start using history again, put any CS in my param2 (third param). If that CS can not store numbers, it will be replaced by a CS that can. If that CS contained some numbers, those numbers are copied to the new CS. To learn, a bayesian network's nodes must ALL have the same size histories. All numbers from a certain index in all histories (in the same bayesian network) are from the same calculation.

ERRORS:
The bayesian-network may contain any types of CSs, not just BayesNodes, therefore if a chance is not in range 0.0 to 1, wrap it around that range. If any other error occurs, assume that one chance is .5, or better: use the last known non-weird estimate of that chance, and continue the calculations. The system should be fuzzy and error-tolerant.

CONDITIONAL-CHANCE BITS AND PARAM-INDEXS:
The highest half of indexs in condChanceValues[] are all for THIS NODE being true because this node is represented by the highest bit in the indexs. The highest bit has position P(0).countP(). All nodes are represented by the bit number equal to their param-index in my param0 (their parent). The first bit is for param0.param0 and has position 0. Second bit for param0.param1...

See Also:
Serialized Form

Nested Class Summary
static class BayesNode.VerifyCountP
           
 
Field Summary
static CS TYPE
          fuzzy-type of a bayes node.
 
Fields inherited from class codesimian.CS
DESCRIPTION, END, EXECPROXY, FUEL, HEAP, JAVACODE, MYFUEL, NAME, NEWINSTANCE, NULL, PARENT, PARSEPRIORITY, PREV, TESTER
 
Constructor Summary
BayesNode()
           
 
Method Summary
 boolean addToHistory(double addThisChance)
          adds a CS (from the constant-pool) with the value addThisChance to the end of the history list contained in param2.
static void adjustBitChance(double[] conditionalChances, int bitNum, double newChance)
          Changes all values in conditionalChances[] so bitChance(conditionalChances,bitNum) would return approximately newChance.
static double bitChance(double[] conditionalChances, int bitNum)
          Returns the CHANCE that the bayes-node represented by bitNum is true.
static int childCountForCondProbSize(int condProbSize)
          condProbSize is exponentially bigger than childCount.
 double cost()
          Returns size of conditional-probability table in param1, which is EXPONENTIALLY bigger than quantity of child nodes and anything else in this BayesNode.
 java.lang.String description()
          a short description of this CS, shorter than the javadoc, but long enough to tell what the params are for.
 double DForProxy()
          returns this node's CHANCE (0.0 to 1) based on the DForProxy()s/chances of param0's params and the conditional-probabilities in param1.
 java.lang.String keyword()
          For the CodeSimian language as a String.
CodeSimian language keyword, like "+" "*" "max" ">" etc.

Override this function if you want to specify a keyword other than how I derive them from the class name, like + for Add.

Some CSs might never be intended to be used in the language by their keyword.
The best example (4/05) is Num, because it is used in the language like "3.4" instead of "num()".
static void makeArraySumTo1(double[] d)
          assumes the array contains no negatives, and
 int maxP()
          Maximum quantity of Params
 int minP()
          For DForProxy().
Minimum number of parameters in param[] needed to call DForProxy().
Defines which indexs of param[] DForProxy() can use.
Functions with a different number of parameters must override this.
OVERRIDE THIS FUNCTION IF EXEC USES A DIFFERENT NUMBER OF PARAMETERS.
Default is 1.
 CS PType(int index)
          This must always be true of a bayesnode: P(0).countP() + 1 == 2 exponent P(1).countP()
therefore PType(int) returns

Need to test the log/exponential calculations against bayes countP requirements
 boolean setD(double execValue)
          Removes all child bayes-nodes and all conditional-probabilities.
static double sum(double[] d)
           
 
Methods inherited from class codesimian.DefaultCS
B, C, countP, decrementMyFuel, deleteP, F, fuel, getExec, getObject, heap, I, indexP, indexPName, insertB, insertC, insertD, insertF, insertI, insertJ, insertL, insertL, insertL1, insertP, insertS, insertZ, J, javaCode, LForProxy, LForProxy, myFuel, name, newInstance, objectToCS, objectToCSArray, objectToCSArray, P, prevD, prevL, S, setB, setC, setCountP, setD, setExec, setF, setFuel, setI, setJ, setL, setL, setL, setL1, setMyFuel, setName, setObject, setP, setPrevExec, setS, setZ, start, toString, V, Z
 
Methods inherited from class codesimian.CS
addP, addP, addP, addP, addP, BForProxy, CForProxy, clone, D, deleteP, FForProxy, IForProxy, isIllusion, javaCode, JForProxy, L, L, L, L, L, maxD, minD, overwrites, parent, parsePriority, PB, PC, PD, PF, PI, PJ, PL, prevB, prevC, prevF, prevI, prevJ, prevS, prevZ, proxyOf, PS, PZ, reflect, reflect, setB, setC, setCost, setDescription, setF, setHeap, setI, setJ, setL, setL, setParent, setParsePriority, setProxyOf, setPType, setS, setTester, setZ, SForProxy, tester, VForProxy, voidReflect, ZForProxy
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

TYPE

public static CS TYPE
fuzzy-type of a bayes node. Does not have to subclass BayesNode, but does have to be similar.

Constructor Detail

BayesNode

public BayesNode()
Method Detail

DForProxy

public double DForProxy()
returns this node's CHANCE (0.0 to 1) based on the DForProxy()s/chances of param0's params and the conditional-probabilities in param1. DOES NOT DO THIS ANYMORE (7/05)... If there is a param2, then param2 is a history list, and DForProxy() saves DForProxy()'s returned value at the end of it.

Specified by:
DForProxy in class DefaultCS

bitChance

public static double bitChance(double[] conditionalChances,
                               int bitNum)
Returns the CHANCE that the bayes-node represented by bitNum is true.

Assumes conditionalChances[] sums to 1 and contains only values between 0 and 1.

Example: If bitNum==0, returns conditionalChances[1]+[3]+[5]... because all those indexs have a 1 in bit0. If bitNum is the last bit, returns the sum of the last half of the array.


childCountForCondProbSize

public static int childCountForCondProbSize(int condProbSize)
condProbSize is exponentially bigger than childCount. If condProbSize is not a power of 2, returns -1.


adjustBitChance

public static void adjustBitChance(double[] conditionalChances,
                                   int bitNum,
                                   double newChance)
Changes all values in conditionalChances[] so bitChance(conditionalChances,bitNum) would return approximately newChance.

There are 2 groups of values in conditionalChances[]. In each group, all values are multiplied by the same number for that group. One group gets bigger, and one smaller. The array still sums to 1 (if it did before).


makeArraySumTo1

public static void makeArraySumTo1(double[] d)
assumes the array contains no negatives, and


sum

public static double sum(double[] d)

addToHistory

public boolean addToHistory(double addThisChance)
adds a CS (from the constant-pool) with the value addThisChance to the end of the history list contained in param2.

If it can not be added to the history, then the history is replaced with a more cooperative list, and history's values are copied to it.

Returns false if there is no param2. Returns true if the current history list now has a new addThisChance at the end.


minP

public int minP()
Description copied from class: DefaultCS
For DForProxy().
Minimum number of parameters in param[] needed to call DForProxy().
Defines which indexs of param[] DForProxy() can use.
Functions with a different number of parameters must override this.
OVERRIDE THIS FUNCTION IF EXEC USES A DIFFERENT NUMBER OF PARAMETERS.
Default is 1.

Overrides:
minP in class DefaultCS

maxP

public int maxP()
Description copied from class: CS
Maximum quantity of Params

Overrides:
maxP in class CS

keyword

public java.lang.String keyword()
Description copied from class: DefaultCS
For the CodeSimian language as a String.
CodeSimian language keyword, like "+" "*" "max" ">" etc.

Override this function if you want to specify a keyword other than how I derive them from the class name, like + for Add.

Some CSs might never be intended to be used in the language by their keyword.
The best example (4/05) is Num, because it is used in the language like "3.4" instead of "num()".
Default: Returns class name, minus package name (and its dots), and change the first letter to lowercase.

For example, CS.MaxParams does not override keyword(), which returns "maxP".

Overrides:
keyword in class DefaultCS
See Also:
CS.parent(), CS.newInstance(), CS.name()

description

public java.lang.String description()
Description copied from class: CS
a short description of this CS, shorter than the javadoc, but long enough to tell what the params are for. Example use: in automatically generated webpages for CodeSimian. Example: "returns sum of all params" for Add.

Overrides:
description in class DefaultCS

cost

public double cost()
Returns size of conditional-probability table in param1, which is EXPONENTIALLY bigger than quantity of child nodes and anything else in this BayesNode. Exec() iterates through param1's params.

Overrides:
cost in class CS

setD

public boolean setD(double execValue)
Removes all child bayes-nodes and all conditional-probabilities. Adds 2 conditional-probabilities. The normal bayes algorithm makes DForProxy() return the second value.

I PLAN TO DO THIS LATER, BUT ITS NOT YET IMPLEMENTED: Executes this BayesNode to determine its current value. Then adjusts the conditional-probabilities in param1 to make this node's DForProxy() return execShouldReturnThisValue (without damaging the other ratios). WARNING: executing this BayesNode could cause infinite loops because the caller might not know this function executes it and therefore did not setMyFuel(to some small number) yet.

Overrides:
setD in class DefaultCS

PType

public CS PType(int index)
This must always be true of a bayesnode: P(0).countP() + 1 == 2 exponent P(1).countP()
therefore PType(int) returns

Need to test the log/exponential calculations against bayes countP requirements

Overrides:
PType in class DefaultCS