fig8_2.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  2. "http://www.w3.org/TR/REC-html40/loose.dtd">
  3. <html>
  4. <head>
  5. <title>Description of fig8_2</title>
  6. <meta name="keywords" content="fig8_2">
  7. <meta name="description" content="Local Regression and Likelihood, Figure 8.2.">
  8. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  9. <meta name="generator" content="m2html &copy; 2005 Guillaume Flandin">
  10. <meta name="robots" content="index, follow">
  11. <link type="text/css" rel="stylesheet" href="../../../m2html.css">
  12. <script type="text/javascript">
  13. if (top.frames.length == 0) { top.location = "../../../index.html"; };
  14. </script>
  15. </head>
  16. <body>
  17. <a name="_top"></a>
  18. <!-- ../../menu.html chronux_2_10 --><!-- # locfit --><!-- menu.html Book -->
  19. <h1>fig8_2
  20. </h1>
  21. <h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../../../up.png"></a></h2>
  22. <div class="box"><strong>Local Regression and Likelihood, Figure 8.2.</strong></div>
  23. <h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../../../up.png"></a></h2>
  24. <div class="box"><strong>This is a script file. </strong></div>
  25. <h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../../../up.png"></a></h2>
  26. <div class="fragment"><pre class="comment"> Local Regression and Likelihood, Figure 8.2.
  27. Discrimination/Classification, simple example using
  28. density estimation.
  29. First, compute density estimates fit0, fit1 ('family','rate'
  30. - output is in events per unit area) for each class in the
  31. training sample. The ratio fit1/(fit1+fit0) estimates the
  32. posterior probability that an observation comes from population 1.
  33. plotting the classification boundary is slightly tricky - it depends
  34. on both fits, so lfplot() can't be used. Instead, both fits must be
  35. evaluated on the same grid of values, which is then used to make a
  36. contour plot.
  37. Author: Catherine Loader</pre></div>
  38. <!-- crossreference -->
  39. <h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../../../up.png"></a></h2>
  40. This function calls:
  41. <ul style="list-style-image:url(../../../matlabicon.gif)">
  42. <li><a href="../../../chronux_2_10/locfit/m/locfit.html" class="code" title="function fit=locfit(varargin)">locfit</a> Smoothing noisy data using Local Regression and Likelihood.</li><li><a href="../../../chronux_2_10/locfit/m/predict.html" class="code" title="function [y, se] = predict(varargin)">predict</a> Interpolate a fit produced by locfit().</li></ul>
  43. This function is called by:
  44. <ul style="list-style-image:url(../../../matlabicon.gif)">
  45. <li><a href="runbook.html" class="code" title="">runbook</a> </li></ul>
  46. <!-- crossreference -->
  47. <h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../../../up.png"></a></h2>
  48. <div class="fragment"><pre>0001 <span class="comment">% Local Regression and Likelihood, Figure 8.2.</span>
  49. 0002 <span class="comment">%</span>
  50. 0003 <span class="comment">% Discrimination/Classification, simple example using</span>
  51. 0004 <span class="comment">% density estimation.</span>
  52. 0005 <span class="comment">%</span>
  53. 0006 <span class="comment">% First, compute density estimates fit0, fit1 ('family','rate'</span>
  54. 0007 <span class="comment">% - output is in events per unit area) for each class in the</span>
  55. 0008 <span class="comment">% training sample. The ratio fit1/(fit1+fit0) estimates the</span>
  56. 0009 <span class="comment">% posterior probability that an observation comes from population 1.</span>
  57. 0010 <span class="comment">%</span>
  58. 0011 <span class="comment">% plotting the classification boundary is slightly tricky - it depends</span>
  59. 0012 <span class="comment">% on both fits, so lfplot() can't be used. Instead, both fits must be</span>
  60. 0013 <span class="comment">% evaluated on the same grid of values, which is then used to make a</span>
  61. 0014 <span class="comment">% contour plot.</span>
  62. 0015 <span class="comment">%</span>
  63. 0016 <span class="comment">% Author: Catherine Loader</span>
  64. 0017
  65. 0018 load cltrain;
  66. 0019 u0 = find(y==0);
  67. 0020 u1 = find(y==1);
  68. 0021 fit0 = <a href="../../../chronux_2_10/locfit/m/locfit.html" class="code" title="function fit=locfit(varargin)">locfit</a>([x1(u0) x2(u0)],y(u0),<span class="string">'family'</span>,<span class="string">'rate'</span>,<span class="string">'scale'</span>,0);
  69. 0022 fit1 = <a href="../../../chronux_2_10/locfit/m/locfit.html" class="code" title="function fit=locfit(varargin)">locfit</a>([x1(u1) x2(u1)],y(u1),<span class="string">'family'</span>,<span class="string">'rate'</span>,<span class="string">'scale'</span>,0);
  70. 0023
  71. 0024 v0 = -3+6*(0:50)'/50;
  72. 0025 v1 = -2.2+4.2*(0:49)'/49;
  73. 0026 <span class="comment">% predict returns log(rate)</span>
  74. 0027 z = <a href="../../../chronux_2_10/locfit/m/predict.html" class="code" title="function [y, se] = predict(varargin)">predict</a>(fit0,{v0 v1})-<a href="../../../chronux_2_10/locfit/m/predict.html" class="code" title="function [y, se] = predict(varargin)">predict</a>(fit1,{v0 v1});
  75. 0028 z = reshape(z,51,50);
  76. 0029 figure(<span class="string">'Name'</span>,<span class="string">'fig8_2: classification'</span>);
  77. 0030 contour(v0,v1,z',[0 0]);
  78. 0031 hold on;
  79. 0032 plot(x1(u0),x2(u0),<span class="string">'.'</span>);
  80. 0033 plot(x1(u1),x2(u1),<span class="string">'.'</span>,<span class="string">'color'</span>,<span class="string">'red'</span>);
  81. 0034 hold off;
  82. 0035
  83. 0036 p0 = <a href="../../../chronux_2_10/locfit/m/predict.html" class="code" title="function [y, se] = predict(varargin)">predict</a>(fit0,[x1 x2]);
  84. 0037 p1 = <a href="../../../chronux_2_10/locfit/m/predict.html" class="code" title="function [y, se] = predict(varargin)">predict</a>(fit1,[x1 x2]);
  85. 0038 py = (p1 &gt; p0);
  86. 0039 disp(<span class="string">'Classification table for training data'</span>);
  87. 0040 tabulate(10*y+py);
  88. 0041
  89. 0042 load cltest;
  90. 0043 p0 = <a href="../../../chronux_2_10/locfit/m/predict.html" class="code" title="function [y, se] = predict(varargin)">predict</a>(fit0,[x1 x2]);
  91. 0044 p1 = <a href="../../../chronux_2_10/locfit/m/predict.html" class="code" title="function [y, se] = predict(varargin)">predict</a>(fit1,[x1 x2]);
  92. 0045 py = (p1 &gt; p0);
  93. 0046 disp(<span class="string">'Classification table for test data'</span>);
  94. 0047 tabulate(10*y+py);
  95. 0048</pre></div>
  96. <hr><address>Generated on Fri 12-Aug-2011 11:36:15 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/" target="_parent">m2html</a></strong> &copy; 2005</address>
  97. </body>
  98. </html>