📄 histodem.html
字号:
<html xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <!--This HTML is auto-generated from an M-file.To make changes, update the M-file and republish this document. --> <title>Histogram approximations</title> <meta name="generator" content="MATLAB 7.1"> <meta name="date" content="2005-07-27"> <meta name="m-file" content="histodem"> <link rel="stylesheet" type="text/css" href="../../matlab/demos/private/style.css"> </head> <body> <div class="header"> <div class="left"><a href="matlab:edit histodem">Open histodem.m in the Editor</a></div> <div class="right"><a href="matlab:echodemo histodem">Run in the Command Window</a></div> </div> <div class="content"> <h1>Histogram approximations</h1> <introduction></introduction> <p>We would like to derive from this histogram a smoother approximation to the underlying distribution. We do this by constructing a spline function f whose average value over each bar interval equals the height of that bar. Here are the two commands that generated the histogram shown: </p><pre class="codeinput">y = randn(1,5001); hist(y);</pre><img vspace="5" hspace="5" src="histodem_01.png"> <p>If h is the height of one of these bars, and its left and right edge are at L and R, then we want our spline f to satisfy</p><pre>integral { f(x) : L < x < R }/(R - L) = h ,</pre><p>or, with F the indefinite integral of f, i.e., DF = f,</p><pre>F(R) - F(L) = h*(R - L).</pre><pre class="codeinput">[heights,centers] = hist(y);hold <span class="string">on</span>set(gca,<span class="string">'XTickLabel'</span>,[])n = length(centers); w = centers(2)-centers(1);t = linspace(centers(1)-w/2,centers(end)+w/2,n+1);p = fix(n/2);fill(t([p p p+1 p+1]),[0 heights([p p]),0],<span class="string">'w'</span>)plot(centers([p p]),[0 heights(p)],<span class="string">'r:'</span>)h = text(centers(p)-.2,heights(p)/2,<span class="string">' h'</span>);dep = -70;tL = text(t(p),dep,<span class="string">'L'</span>);tR = text(t(p+1),dep,<span class="string">'R'</span>);hold <span class="string">off</span></pre><img vspace="5" hspace="5" src="histodem_02.png"> <p>So, with t(i) the left edge of the i-th bar, dt(i) its width, and h(i) its height, we want</p><pre>F(t(i+1)) - F(t(i)) = h(i) * dt(i), i=1:n,</pre><p>or, setting arbitrarily F(t(1)) = 0,</p><pre>F(t(i)) = sum {h(j)*dt(j) : j=1:i-1}, i=1:n+1.</pre><p>Add to this the two end conditions DF(t(1)) = 0 = DF(t(n+1)), and we have all the data we need to get F as a complete cubic spline interpolant, and its derivative, f = DF, is what we want and plot, all in one statement. </p><pre class="codeinput">set(h,<span class="string">'String'</span>,<span class="string">'h(i)'</span>)set(tL,<span class="string">'String'</span>,<span class="string">'t(i)'</span>)set(tR,<span class="string">'String'</span>,<span class="string">'t(i+1)'</span>)dt = diff(t);hold <span class="string">on</span>fnplt(fnder(spline(t,[0,cumsum([0,heights.*dt]),0])), <span class="string">'r'</span>,2);hold <span class="string">off</span></pre><img vspace="5" hspace="5" src="histodem_03.png"> <p>Here is an explanation of the one-liner we used:</p><pre>>> fnplt(fnder(spline(t,[0,cumsum([0,h.*dt]),0])),'r',2)</pre><pre>Fvals = cumsum([0,h.*dt]); % provides the values of F at tF = spline( t , [0, Fvals, 0]); % constructs the cubic spline interpolant, % with zero endslopes, to these valuesDF = fnder(spline); % computes its first derivativefnplt(DF, 'r', 2) % plots DF, in red with linewidth 2</pre><p class="footer">Copyright 1987-2005 C. de Boor and The MathWorks, Inc.<br> Published with MATLAB® 7.1<br></p> </div> <!--##### SOURCE BEGIN #####%% Histogram approximations% Copyright 1987-2005 C. de Boor and The MathWorks, Inc.% $Revision: 1.11.4.2 $ $Date: 2005/06/21 19:44:32 $%%% We would like to derive from this histogram a smoother approximation to the% underlying distribution. We do this by constructing a spline function f% whose average value over each bar interval equals the height of that bar.% Here are the two commands that generated the histogram shown:y = randn(1,5001); hist(y);%%% If h is the height of one of these bars, and its left and right edge% are at L and R, then we want our spline f to satisfy%% integral { f(x) : L < x < R }/(R - L) = h ,%% or, with F the indefinite integral of f, i.e., DF = f,%% F(R) - F(L) = h*(R - L).[heights,centers] = hist(y);hold onset(gca,'XTickLabel',[])n = length(centers); w = centers(2)-centers(1);t = linspace(centers(1)-w/2,centers(end)+w/2,n+1);p = fix(n/2);fill(t([p p p+1 p+1]),[0 heights([p p]),0],'w')plot(centers([p p]),[0 heights(p)],'r:')h = text(centers(p)-.2,heights(p)/2,' h');dep = -70;tL = text(t(p),dep,'L');tR = text(t(p+1),dep,'R');hold off%%% So, with t(i) the left edge of the i-th bar, dt(i) its width, and% h(i) its height, we want%% F(t(i+1)) - F(t(i)) = h(i) * dt(i), i=1:n,%% or, setting arbitrarily F(t(1)) = 0,%% F(t(i)) = sum {h(j)*dt(j) : j=1:i-1}, i=1:n+1.%% Add to this the two end conditions DF(t(1)) = 0 = DF(t(n+1)), and we% have all the data we need to get F as a complete cubic spline% interpolant, and its derivative, f = DF, is what we want and plot, all% in one statement.set(h,'String','h(i)')set(tL,'String','t(i)')set(tR,'String','t(i+1)')dt = diff(t);hold onfnplt(fnder(spline(t,[0,cumsum([0,heights.*dt]),0])), 'r',2);hold off%%% Here is an explanation of the one-liner we used:%% >> fnplt(fnder(spline(t,[0,cumsum([0,h.*dt]),0])),'r',2)%% Fvals = cumsum([0,h.*dt]); % provides the values of F at t% F = spline( t , [0, Fvals, 0]); % constructs the cubic spline interpolant,% % with zero endslopes, to these values% DF = fnder(spline); % computes its first derivative% fnplt(DF, 'r', 2) % plots DF, in red with linewidth 2displayEndOfDemoMessage(mfilename)##### SOURCE END #####--> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -