# Chapter 20: Stagewise minimization

## Figure 1

```# 2-modal density
n<-500
seed<-5
dendat<-sim.data(n=n,seed=seed,type="1d2modal")

mugrid<-seq(-1,5,0.3)
siggrid<-seq(0.2,2,0.2)
M<-7
sg7<-eval.stage.gauss(dendat,M,mugrid,siggrid)
M<-300
sg300<-eval.stage.gauss(dendat,M,mugrid,siggrid)

pnum<-80
N<-pnum
xala<--2
xyla<-7
support<-c(xala,xyla)

sg<-sg7
pcf<-pcf.func("mixt",N,sig=sg\$sigit,M=sg\$muut,p=sg\$curmix,support=support)
dp7<-draw.pcf(pcf,pnum=pnum)

sg<-sg300
pcf<-pcf.func("mixt",N,sig=sg\$sigit,M=sg\$muut,p=sg\$curmix,support=support)
dp300<-draw.pcf(pcf,pnum=pnum)

# the true density
N<-length(dp7\$x)
sd<-sim.data(N=N,type="1d2modal")
dt<-draw.pcf(sd,pnum=N)

# log-normal density
n<-100
seed<-20
set.seed(seed)
dendat<-rlnorm(n)

mugrid<-seq(-1,5,0.3)
siggrid<-seq(0.2,2,0.2)
M<-7
sg7<-eval.stage.gauss(dendat,M,mugrid,siggrid)
M<-300
sg300<-eval.stage.gauss(dendat,M,mugrid,siggrid)

pnum<-80
xala<--1
xyla<-10
support<-c(xala,xyla)
sg<-sg7
pcf<-pcf.func("mixt",N,sig=sg\$sigit,M=sg\$muut,p=sg\$curmix,support=support)
dpp7<-draw.pcf(pcf,pnum=pnum)
sg<-sg300
pcf<-pcf.func("mixt",N,sig=sg\$sigit,M=sg\$muut,p=sg\$curmix,support=support)
dpp300<-draw.pcf(pcf,pnum=pnum)

# the true density
dtx<-dpp7\$x  #seq(-1,10,0.1)
dty<-dlnorm(dtx)

# frame 1
matplot(
matrix(c(dt\$x,dt\$x,dt\$x),length(dt\$x),3),
matrix(c(dt\$y,dp7\$y,dp300\$y),length(dt\$y),3),
type="l",xlab="",ylab="",col=c("black","red","blue"))

# frame 2
matplot(
matrix(c(dtx,dtx,dtx),length(dtx),3),
matrix(c(dty,dpp7\$y,dpp300\$y),length(dty),3),
type="l",xlab="",ylab="",col=c("black","red","blue"))
```