#example of "polynomials for classification trees" in R code
#for finding or estimating Boolean network dynamics in the presence of noise with CART
#simple Boolean network on 2 nodes is (x1 and x2, x1 or x2) = (x1 & x2, x1 | x2)
# run it 10000 time steps
n=10000
x=matrix(nrow=n, ncol=2)
f1=function(x){as.numeric(x[1]&x[2])}
f2=function(x){as.numeric(x[1]|x[2])}
x[1,]=c(0,0) #initialize at (0,0), not important
# add a little noise to Boolean dynamics to be realistic and to get consistent estimation from CART:
for (i in 2:n){
x1=(f1(x[i-1,])+rbinom(1,1,.1)) %%2
x2=(f2(x[i-1,])+rbinom(1,1,.1)) %%2
x[i,]=c(x1,x2)
} #end loop
#load tree library for CART, must be installed
library(tree)
#autogression is done by shifting time indices
D=data.frame(y1=x[2:n,1],x1=x[1:(n-1),1],x2=x[1:(n-1),2],y2=x[2:n,2])
T1=tree(y1~x1+x2,D)
T2=tree(y2~x1+x2,D)
#the trees T1 and T2 can be read either logically or as polynomials, and are close to the underlying dynamics
#decimal values near 1 are taken to be "true" or 1.