Institute of Information Theory and Automation

## Source code from presentation

``````#load Mudim
require(mudim)```
```
``````
#create two simple distributions
p1 <- Distribution("pi1")
data <- matrix(c(0,0,1,1,0,1,0,1,.25,.25,.25,.25),nrow=4)
data
setData(p1,data)
setVariables(p1,c("x1","x2"))
getData(p1)
p2<- Distribution("pi2")
data <- matrix(c(0,0,1,1,0,1,0,1,1,0,0,0),ncol=3)
data
setData(p2,data)
setVariables(p2,c("x2","x3"))
getData(p2)```
```
``````
#compose distributions
res <- p1 > p2
res <- p2 > p1
res
getData(res)
#create simple model
mod <- Model("testModel")
length(mod)
compose(mod,p2)
compose(mod,p1)
length(mod)
dim(mod)
getVariables(mod)
getVariablesUnion(mod)```
```
``````
#create perfect model
perfMod <- perfect(mod)
length(perfMod)```
```
``````
#compare data from perfect and non-perfect model
getData(getDistribution(perfMod,1))
getData(getDistribution(perfMod,2))
getData(getDistribution(mod,1))
getData(getDistribution(mod,2))
getData(toDistribution(perMod))
getData(toDistribution(perfMod))
getData(toDistribution(mod))```
```
``````
#create test model to marginalize via reduction
List <- list();
List <- list(
c(1,3,7,8),
c(1,2)
c(3,4)
List <- list(
c(1,3,7,8),
c(1,2),
c(3,4),
c(4,5),
c(5,6,7),
c(8,9),
c(9,10,11,12),
c(12,13,19),
c(10,14),
c(14,15,16),
c(16,17),
c(17,18),
c(11,18,19,20),
c(20,21)
);
############ generating model, which meets the conditions
demoMatrix <- NULL;
for(i in seq(unique(unlist(List))){
demoMatrix <- cbind(demoMatrix, sample(1:10, 100000, replace = TRUE));
}
demoMatrix <- cbind(demoMatrix,1);
dimnames(demoMatrix)[[2]]<-c(LETTERS[seq(unique(unlist(List)))],FREQ.NAME);
a <- Distribution("main");
setData(a,demoMatrix);
testModel <- Model("testovaci");
for(i in seq(List)){
b <- marginalize(a, dimnames(demoMatrix)[[2]][List[[i]]], kickThis = F);
compose(testModel, b);
}
marginal <-  LETTERS[c(11,12,18)]```
```
``````
#measuring marginalization with and without reduction
system.time(marginalize(testModel, marginal, kickThis=FALSE))
system.time(marginalize(testModel, marginal, kickThis=FALSE))
system.time(marginalize(testModel, marginal, kickThis=FALSE))
system.time(marginalizePerfect2(testModel, marginal, kickThis=FALSE))
system.time(marginalizePerfect2(testModel, marginal, kickThis=FALSE))
system.time(marginalizePerfect2(testModel, marginal, kickThis=FALSE))```
```
``````
#plot model
plot(testModel)

```
```
2008-10-01 15:09