# fig8_2

## PURPOSE

Local Regression and Likelihood, Figure 8.2.

## SYNOPSIS

This is a script file.

## DESCRIPTION

     Local Regression and Likelihood, Figure 8.2.

     Discrimination/Classification, simple example using
     density estimation.

     First, compute density estimates fit0, fit1 ('family','rate'
     - output is in events per unit area) for each class in the
     training sample. The ratio fit1/(fit1+fit0) estimates the
     posterior probability that an observation comes from population 1.

     plotting the classification boundary is slightly tricky - it depends
     on both fits, so lfplot() can't be used. Instead, both fits must be
     evaluated on the same grid of values, which is then used to make a
     contour plot.

     Author: Catherine Loader

## CROSS-REFERENCE INFORMATION

This function calls:

- locfit Smoothing noisy data using Local Regression and Likelihood.
- predict Interpolate a fit produced by locfit().

This function is called by:

- runbook

## SOURCE CODE

    0001 % Local Regression and Likelihood, Figure 8.2.
    0002 %
    0003 % Discrimination/Classification, simple example using
    0004 % density estimation.
    0005 %
    0006 % First, compute density estimates fit0, fit1 ('family','rate'
    0007 % - output is in events per unit area) for each class in the
    0008 % training sample. The ratio fit1/(fit1+fit0) estimates the
    0009 % posterior probability that an observation comes from population 1.
    0010 %
    0011 % plotting the classification boundary is slightly tricky - it depends
    0012 % on both fits, so lfplot() can't be used. Instead, both fits must be
    0013 % evaluated on the same grid of values, which is then used to make a
    0014 % contour plot.
    0015 %
    0016 % Author: Catherine Loader
    0017
    0018 load cltrain;
    0019 u0 = find(y==0);
    0020 u1 = find(y==1);
    0021 fit0 = locfit([x1(u0) x2(u0)],y(u0),'family','rate','scale',0);
    0022 fit1 = locfit([x1(u1) x2(u1)],y(u1),'family','rate','scale',0);
    0023
    0024 v0 = -3+6*(0:50)'/50;
    0025 v1 = -2.2+4.2*(0:49)'/49;
    0026 % predict returns log(rate)
    0027 z = predict(fit0,{v0 v1})-predict(fit1,{v0 v1});
    0028 z = reshape(z,51,50);
    0029 figure('Name','fig8_2: classification');
    0030 contour(v0,v1,z',[0 0]);
    0031 hold on;
    0032 plot(x1(u0),x2(u0),'.');
    0033 plot(x1(u1),x2(u1),'.','color','red');
    0034 hold off;
    0035
    0036 p0 = predict(fit0,[x1 x2]);
    0037 p1 = predict(fit1,[x1 x2]);
    0038 py = (p1 > p0);
    0039 disp('Classification table for training data');
    0040 tabulate(10*y+py);
    0041
    0042 load cltest;
    0043 p0 = predict(fit0,[x1 x2]);
    0044 p1 = predict(fit1,[x1 x2]);
    0045 py = (p1 > p0);
    0046 disp('Classification table for test data');
    0047 tabulate(10*y+py);
    0048