Figures of levalg: Algorithms for the manipulation of level sets of nonparametric density estimates

# Figures of levalg: Algorithms for the manipulation of level sets of nonparametric density estimates

## Process of generating partition, Figure 2

```
postscript(file="~/levalg/process.pdf", horizontal=FALSE, onefile=FALSE,
width=15, height=3.5, paper="special")
par(mfrow=c(1,4))

plot("","",xlim=c(0,4),ylim=c(0,4),xlab="",ylab="",#axes=FALSE,
pch=21,cex=3)
xgrid0<-c(0,2,4)
ygrid0<-c(0,0,0)
xgrid1<-c(0,2,4)
ygrid1<-c(4,4,4)
segments(xgrid0,ygrid0,xgrid1,ygrid1)

xgrid0<-c(0,0)
ygrid0<-c(0,4)
xgrid1<-c(4,4)
ygrid1<-c(0,4)
segments(xgrid0,ygrid0,xgrid1,ygrid1)

text(1,2,"2")
text(3,2,"3")

title(sub="1st split in 1st direction")
#dev.off()

#postscript(file="~/levalg/processB.eps",horizontal=FALSE,onefile=FALSE,
#width=2,height=2.5,paper = "special")

plot("","",xlim=c(0,4),ylim=c(0,4),xlab="",ylab="",#axes=FALSE,
pch=21,cex=3)
xgrid0<-c(0,2,4)
ygrid0<-c(0,0,0)
xgrid1<-c(0,2,4)
ygrid1<-c(4,4,4)
segments(xgrid0,ygrid0,xgrid1,ygrid1)

xgrid0<-c(0,0)
ygrid0<-c(0,4)
xgrid1<-c(4,4)
ygrid1<-c(0,4)
segments(xgrid0,ygrid0,xgrid1,ygrid1)

xgrid0<-c(1,3)
ygrid0<-c(0,0)
xgrid1<-c(1,3)
ygrid1<-c(4,4)
segments(xgrid0,ygrid0,xgrid1,ygrid1)

text(0.5,2,"4")
text(1.5,2,"5")
text(2.5,2,"6")
text(3.5,2,"7")

title(sub="2nd splits in 1st direction")
#dev.off()

#postscript(file="~/levalg/processC.eps", horizontal=FALSE, onefile=FALSE,
#width=2, height=2.5, paper="special")

plot("","",xlim=c(0,4),ylim=c(0,4),xlab="",ylab="",#axes=FALSE,
pch=21,cex=3)
xgrid0<-c(0,2,4)
ygrid0<-c(0,0,0)
xgrid1<-c(0,2,4)
ygrid1<-c(4,4,4)
segments(xgrid0,ygrid0,xgrid1,ygrid1)

xgrid0<-c(0,0)
ygrid0<-c(0,4)
xgrid1<-c(4,4)
ygrid1<-c(0,4)
segments(xgrid0,ygrid0,xgrid1,ygrid1)

xgrid0<-c(1,3)
ygrid0<-c(0,0)
xgrid1<-c(1,3)
ygrid1<-c(4,4)
segments(xgrid0,ygrid0,xgrid1,ygrid1)

xgrid0<-c(0)
ygrid0<-c(2)
xgrid1<-c(4)
ygrid1<-c(2)
segments(xgrid0,ygrid0,xgrid1,ygrid1)

text(0.5,1,"8")
text(1.5,1,"10")
text(2.5,1,"12")
text(3.5,1,"14")
text(0.5,3,"9")
#text(1.5,3,"11")
text(2.5,3,"13")
text(3.5,3,"15")

title(sub="3rd split in 2nd direction")
#dev.off()

#postscript(file="~/levalg/processD.eps", horizontal=FALSE, onefile=FALSE,
#width=2, height=2.5, paper="special")

plot("","",xlim=c(0,4),ylim=c(0,4),xlab="",ylab="",#axes=FALSE,
pch=21,cex=3)
xgrid0<-c(0,2,4)
ygrid0<-c(0,0,0)
xgrid1<-c(0,2,4)
ygrid1<-c(4,4,4)
segments(xgrid0,ygrid0,xgrid1,ygrid1)

xgrid0<-c(0,0)
ygrid0<-c(0,4)
xgrid1<-c(4,4)
ygrid1<-c(0,4)
segments(xgrid0,ygrid0,xgrid1,ygrid1)

xgrid0<-c(1,3)
ygrid0<-c(0,0)
xgrid1<-c(1,3)
ygrid1<-c(4,4)
segments(xgrid0,ygrid0,xgrid1,ygrid1)

xgrid0<-c(0)
ygrid0<-c(2)
xgrid1<-c(4)
ygrid1<-c(2)
segments(xgrid0,ygrid0,xgrid1,ygrid1)

xgrid0<-c(0,0)
ygrid0<-c(1,3)
xgrid1<-c(4,4)
ygrid1<-c(1,3)
segments(xgrid0,ygrid0,xgrid1,ygrid1)

text(0.5,0.5,"16")
text(1.5,0.5,"20")
text(2.5,0.5,"24")
text(3.5,0.5,"28")
text(0.5,1.5,"17")
text(1.5,1.5,"21")
text(2.5,1.5,"25")
text(3.5,1.5,"29")
text(0.5,2.5,"18")
#text(1.5,2.5,"22")
#text(2.5,2.5,"26")
#text(3.5,2.5,"30")
text(0.5,3.5,"19")
#text(1.5,3.5,"23")
text(2.5,3.5,"27")
text(3.5,3.5,"31")

title(sub="4th splits in 2nd direction")
dev.off()

```
gv ~/levalg/process.pdf &

## Evaluation tree, Figure 1

```nodenum<-31
parent<-c(0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,14,14,
15,15)
level<- c(4,3,3,2,2,2,2,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
volume<-matrix(0,nodenum,1)
tree<-list(parent=parent,level=level,volume=volume)

info<-seq(1,31)

binlabel<-matrix(1,31,1)
binlabel[11]<-0
binlabel[22]<-0
binlabel[23]<-0
binlabel[26]<-0
binlabel[30]<-0

source("~/Art/levalg.R")

pdf(file="~/levalg/partgentr.pdf")

levalg(tree,dimen=1,modelabel=F,info=info,xmarginright=0.05,infopos=-0.5,
binlabel=binlabel)

dev.off()

gv ~/levalg/partgentr.pdf &

```

## Process of joining, Figure 3

```
plotnet<-function(){

plot("","",xlim=c(0,4),ylim=c(0,4),xlab="",ylab="",#axes=FALSE,
pch=21,cex=3)
xgrid0<-c(0,2,4)
ygrid0<-c(0,0,0)
xgrid1<-c(0,2,4)
ygrid1<-c(4,4,4)
segments(xgrid0,ygrid0,xgrid1,ygrid1)

xgrid0<-c(0,0)
ygrid0<-c(0,4)
xgrid1<-c(4,4)
ygrid1<-c(0,4)
segments(xgrid0,ygrid0,xgrid1,ygrid1)

xgrid0<-c(1,3)
ygrid0<-c(0,0)
xgrid1<-c(1,3)
ygrid1<-c(4,4)
segments(xgrid0,ygrid0,xgrid1,ygrid1)

xgrid0<-c(0)
ygrid0<-c(2)
xgrid1<-c(4)
ygrid1<-c(2)
segments(xgrid0,ygrid0,xgrid1,ygrid1)

xgrid0<-c(0,0)
ygrid0<-c(1,3)
xgrid1<-c(4,4)
ygrid1<-c(1,3)
segments(xgrid0,ygrid0,xgrid1,ygrid1)

text(0.5,0.5,"16")
text(1.5,0.5,"20")
text(2.5,0.5,"24")
text(3.5,0.5,"28")
text(0.5,1.5,"17")
text(1.5,1.5,"21")
text(2.5,1.5,"25")
text(3.5,1.5,"29")
text(0.5,2.5,"18")
#text(1.5,2.5,"22")
#text(2.5,2.5,"26")
#text(3.5,2.5,"30")
text(0.5,3.5,"19")
#text(1.5,3.5,"23")
text(2.5,3.5,"27")
text(3.5,3.5,"31")
}

pdf(file="~/levalg/joining.pdf", horizontal=FALSE, onefile=FALSE,
width=15, height=3.5, paper="special")
par(mfrow=c(1,4))

plotnet()

for (i in 0:3){
x0<-i+0.1
y0<-0.1
x1<-i+0.1
y1<-1.9
segments(x0,y0,x1,y1)
}
for (i in 0:3){
x0<-i+0.9
y0<-0.1
x1<-i+0.9
y1<-1.9
segments(x0,y0,x1,y1)
}
for (i in 0:3){
x0<-i+0.1
y0<-0.1
x1<-i+0.9
y1<-0.1
segments(x0,y0,x1,y1)
}
for (i in 0:3){
x0<-i+0.1
y0<-1.9
x1<-i+0.9
y1<-1.9
segments(x0,y0,x1,y1)
}

x<-c(0.1,0.1,0.9,0.9,0.1)
y<-c(2.1,3.9,3.9,2.1,2.1)
lines(x,y)

xs<-c(2.1,2.1,2.9,2.9,2.1)
ys<-c(3.1,3.9,3.9,3.1,3.1)
lines(xs,ys)

xu<-xs+1
y<-c(3.1,3.9,3.9,3.1,3.1)
lines(xu,y)

#title(sub="")
#######################################

plotnet()

x<-c(0.1,0.1,0.9,0.9,0.1)
y<-c(0.1,3.9,3.9,0.1,0.1)
lines(x,y)

x<-c(1.1,1.1,1.9,1.9,1.1)
y<-c(0.1,1.9,1.9,0.1,0.1)
lines(x,y)

lines(x+1,y)

lines(x+2,y)

xs<-c(2.1,2.1,2.9,2.9,2.1)
ys<-c(3.1,3.9,3.9,3.1,3.1)
lines(xs,ys)

lines(xs+1,ys)

#title(sub="")
#######################################

plotnet()

x<-c(0.1,0.1,0.9,0.9,1.9,1.9,0.1)
y<-c(0.1,3.9,3.9,1.9,1.9,0.1,0.1)
lines(x,y)

x<-c(2.1,2.1,3.9,3.9,2.1)
y<-c(0.1,1.9,1.9,0.1,0.1)
lines(x,y)

x<-c(2.1,2.1,3.9,3.9,2.1)
y<-c(3.1,3.9,3.9,3.1,3.1)
lines(x,y)

#title(sub="")
#######################################

plotnet()

x<-c(0.1,0.1,0.9,0.9,3.9,3.9,0.1)
y<-c(0.1,3.9,3.9,1.9,1.9,0.1,0.1)
lines(x,y)

x<-c(2.1,2.1,3.9,3.9,2.1)
y<-c(3.1,3.9,3.9,3.1,3.1)
lines(x,y)

dev.off()

gv ~/levalg/joining.pdf &

```

## 3 examples

ssh -X jw0@kde01.urz.uni-heidelberg.de scp /home/jsk/denpro/R/plotasso.R jw0@kde01.urz.uni-heidelberg.de:/u/math/jw0 scp /home/jsk/delt/R/simpp.R jw0@kde01.urz.uni-heidelberg.de:/u/math/jw0 library(denpro) library(delt) source("plotasso.R") source("simpp.R")

### Kernel estimate

```n<-2000
noisedim<-2  #1
d<-2+noisedim
D<-4
siemen<-1
dendat<-simpp(n,noisedim,D,siemen)    #simfssk(n,noisedim,siemen)

h<-1.4
N<-c(16,16,16,16)  #c(32,32,32)
Q<-60  #80

pk<-profkern(dendat,h,N,Q)   # 40 seconds

#plotasso(pk,xlim=c(5000,7000))
#plotbary(pk,coordi=1)

```

```n<-1000
noisedim<-8
d<-2+noisedim
D<-6
siemen<-3
dendat<-simpp(n,noisedim,D,siemen)

minlkm<-5
suppo<-supp(dendat)
bt<-densplit(dendat,minlkm,suppo)
treeseq<-prune(bt)
#treeseq\$leafs

leafnum<-33
tr<-picktree(treeseq,leafnum)

pr<-proftree(tr)

#plotasso(pr)

```

### Bagged histogram

```n<-1500
noisedim<-3
d<-2+noisedim
D<-5
siemen<-2
dendat<-simpp(n,noisedim,D,siemen)

leaf<-15
seed<-5
sample="baggworpl"
prune="on"
B<-5
trb<-densfore(dendat,B,leaf,seed=seed,sample=sample,prune=prune)

prb<-proftree(trb,Q=60)   # 10 seconds

#plotasso(prb,xlim=c(215000,285000))
#plotbary(prb,coordi=2)

```

### Figure

```postscript(file="3examp.eps", horizontal=FALSE, onefile=FALSE,
width=15, height=5, paper="special")
par(mfrow=c(1,3))

plotasso(pk,ptext=0.00005,xlim=c(4500,7500))
title(sub="a) kernel estimate, 3D")

plotasso(pr,xlim=c(4.77e+11,5.0e+11),ptext=0.1e-10)
title(sub="b) CART histogram, 10D")

plotasso(prb,xlim=c(215000,285000),ptext=0.1e-05)
title(sub="c) bagged histogram, 5D")

dev.off()

pdf(file="3examp.pdf", horizontal=FALSE, onefile=FALSE,
width=15, height=5, paper="special")
par(mfrow=c(1,3))

plotasso(pk,ptext=0.00005,xlim=c(4500,7500))
title(sub="a) kernel estimate, 3D")

plotasso(pr,xlim=c(4.77e+11,5.0e+11),ptext=0.1e-10)
title(sub="b) CART histogram, 10D")

plotasso(prb,xlim=c(215000,285000),ptext=0.1e-05)
title(sub="c) bagged histogram, 5D")

dev.off()

```
scp jw0@kde01.urz.uni-heidelberg.de:/u/math/jw0/3examp.pdf /home/jsk/levalg gv /home/jsk/levalg/3examp.pdf &

## Overlaying partitions

```
library(denpro)
library(delt)

n<-225  #500
noisedim<-0
siemen<-3
dendat<-simfssk(n,noisedim,siemen)
suppo<-supp(dendat)

B<-2
leaf<-9  #16
seed<-1
sample="baggworpl"
prune="off"
tr3<-densfore(dendat,B,leaf,seed=seed,sample=sample,prune=prune)
pa3<-partigen(tr3)

dendat1<-bootworpl(dendat,seed,scatter=0)
tr1<-myosplit(dendat1,suppo,leaf)
pa1<-partigen(tr1)

dendat2<-bootworpl(dendat,seed=2,scatter=0)
tr2<-myosplit(dendat2,suppo,leaf)
pa2<-partigen(tr2)

pdf(file="/home/jsk/levalg/overlay.pdf", horizontal=FALSE,
onefile=FALSE, width=11, height=3.5, paper="special")
#par(plt=c(0,1,0,1))
par(mfrow=c(1,3))

plotparti(pa1\$recs,dendat,pch=20)
title(sub="a) partition to 9 rectangles")

plotparti(pa2\$recs,dendat,pch=20)
title(sub="b) partition to 9 rectangles")

plotparti(pa3\$recs,dendat,pch=20)
title(sub="c) partitions a) and b) overlayed")

dev.off()

gv /home/jsk/levalg/overlay.pdf &

```