fig6_1.m 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. % Local Regression and Likelihood, Figure 6.1.
  2. %
  3. % Derivative (local slope) estimation, for the Old Faithful Geyser Data.
  4. % The 'deriv' argument specifies derivative estimation,
  5. % 'deriv',1 First-order derivative.
  6. % 'deriv',[1 1] Second-order derivative.
  7. % 'deriv',2 For bivariate fits, partial deriv. wrt second variable.
  8. % 'deriv',[1 2] Mixed second-order derivative.
  9. %
  10. % Density estimation is done on the log-scale. That is, the estimate
  11. % is of g(x) = log(f(x)), where f(x) is the density.
  12. %
  13. % The relation between derivatives is therefore
  14. % f'(x) = f(x)g'(x) = g'(x)exp(g(x)).
  15. % To estimate f'(x), we must estimate g(x) and g'(x) (fit1 and fit2 below),
  16. % evaluate on a grid of points (p1 and p2), and apply the back-transformation.
  17. %
  18. % Disclaimer: I don't consider derivative estimation from noisy data
  19. % to be a well-defined problem. Use at your own risk.
  20. %
  21. % Author: Catherine Loader
  22. %
  23. % NEED: m argument passed to lfmarg().
  24. load geyser;
  25. fit1 = locfit(geyser,'alpha',[0.1 0.6],'ll',1,'ur',6);
  26. fit2 = locfit(geyser,'alpha',[0.1 0.6],'ll',1,'ur',6,'deriv',1);
  27. z = lfmarg(fit1);
  28. p1 = predict(fit1,z);
  29. p2 = predict(fit2,z);
  30. figure('Name','fig6_1: slope estimation: Old faithful data' );
  31. plot(z{1},p2.*exp(p1));
  32. xlabel('Eruption Duration (Minutes)');
  33. ylabel('Density Derivative');