# fig6_1

## PURPOSE

Local Regression and Likelihood, Figure 6.1.

## SYNOPSIS

This is a script file.

## DESCRIPTION

     Local Regression and Likelihood, Figure 6.1.

     Derivative (local slope) estimation, for the Old Faithful Geyser Data.
     The 'deriv' argument specifies derivative estimation,
       'deriv',1        First-order derivative.
       'deriv',[1 1]    Second-order derivative.
       'deriv',2        For bivariate fits, partial deriv. wrt second variable.
       'deriv',[1 2]    Mixed second-order derivative.

     Density estimation is done on the log-scale. That is, the estimate
     is of g(x) = log(f(x)), where f(x) is the density.

     The relation between derivatives is therefore
         f'(x) = f(x)g'(x) = g'(x)exp(g(x)).
     To estimate f'(x), we must estimate g(x) and g'(x) (fit1 and fit2 below),
     evaluate on a grid of points (p1 and p2), and apply the back-transformation.

     Disclaimer: I don't consider derivative estimation from noisy data
     to be a well-defined problem. Use at your own risk.

     Author: Catherine Loader

     NEED: m argument passed to lfmarg().

## CROSS-REFERENCE INFORMATION

This function calls:

- lfmarg computes grid margins from a locfit object, used for plotting.
- 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 6.1.
    0002 %
    0003 % Derivative (local slope) estimation, for the Old Faithful Geyser Data.
    0004 % The 'deriv' argument specifies derivative estimation,
    0005 %   'deriv',1        First-order derivative.
    0006 %   'deriv',[1 1]    Second-order derivative.
    0007 %   'deriv',2        For bivariate fits, partial deriv. wrt second variable.
    0008 %   'deriv',[1 2]    Mixed second-order derivative.
    0009 %
    0010 % Density estimation is done on the log-scale. That is, the estimate
    0011 % is of g(x) = log(f(x)), where f(x) is the density.
    0012 %
    0013 % The relation between derivatives is therefore
    0014 %     f'(x) = f(x)g'(x) = g'(x)exp(g(x)).
    0015 % To estimate f'(x), we must estimate g(x) and g'(x) (fit1 and fit2 below),
    0016 % evaluate on a grid of points (p1 and p2), and apply the back-transformation.
    0017 %
    0018 % Disclaimer: I don't consider derivative estimation from noisy data
    0019 % to be a well-defined problem. Use at your own risk.
    0020 %
    0021 % Author: Catherine Loader
    0022 %
    0023 % NEED: m argument passed to lfmarg().
    0024
    0025 load geyser;
    0026 fit1 = locfit(geyser,'alpha',[0.1 0.6],'ll',1,'ur',6);
    0027 fit2 = locfit(geyser,'alpha',[0.1 0.6],'ll',1,'ur',6,'deriv',1);
    0028 z = lfmarg(fit1);
    0029 p1 = predict(fit1,z);
    0030 p2 = predict(fit2,z);
    0031 figure('Name','fig6_1: slope estimation: Old faithful data' );
    0032 plot(z{1},p2.*exp(p1));
    0033 xlabel('Eruption Duration (Minutes)');
    0034 ylabel('Density Derivative');