# Chapter 19: Stagewise minimization. Code

## 1D case

In the 1D case we may use an implementation of a stagewise minimization estimator that uses dictionaries of Gaussian functions. We simulate data from a 2-modal density.

```dendat<-sim.data(n=500,seed=1,type="1d2modal")
```

The estimator uses a dictionary phi((x-mu)/sigma)/sigma, where phi is the standard Gaussian density, and mu and sigma take values in a finite grid.

```mugrid<-seq(-1,5,0.3)    # grid of mu-values
siggrid<-seq(0.2,2,0.2)  # grid of sigma-values
M<-7                     # number of mixture components
pcf<-eval.stage.gauss(dendat,M,mugrid,siggrid)

dp<-draw.pcf(pcf)
plot(dp\$x,dp\$y,type="l")
```

We used the fact that a piecewise constant function may be plotted using function ``draw.pcf'', as shown in Code of Chapter 2, applying commands

```dp<-draw.pcf(pcf)
plot(dp\$x,dp\$y,type="l")
```

## Multivariate case

In the multivariate case we use stagewise minimization with greedy histograms as discussed in 19.1. We simulate 3-modal 2D data.

```dendat<-sim.data(n=1000,noisedim=0,seed=20,type="fssk")
```

Then the estimate is calculated.

```leafs<-6  # the number of leafs of the greedy histograms
M<-2      # the number of greedy histograms
pcf<-eval.stage(dendat,leafs,M)

dp<-draw.pcf(pcf)
persp(dp\$x,dp\$y,dp\$z,theta=120,phi=30)
```

Code of Chapter 2 gives the commands to make a perspective plot and a contour plot from a piecewise constant function. Code of Chapter 4 and Code of Chapter 5 give commands for using level set trees and shape trees to visualize kernel estimates.