📄 roc.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"><html><head> <title>Description of roc</title> <meta name="keywords" content="roc"> <meta name="description" content="roc"> <meta http-equiv="Content-Type" content="text/html; charset=big5"> <meta name="generator" content="m2html © 2003 Guillaume Flandin"> <meta name="robots" content="index, follow"> <link type="text/css" rel="stylesheet" href="../m2html.css"></head><body><a name="_top"></a><div><a href="../index.html">Home</a> > <a href="index.html">dcpr</a> > roc.m</div><!--<table width="100%"><tr><td align="left"><a href="../index.html"><img alt="<" border="0" src="../left.png"> Master index</a></td><td align="right"><a href="index.html">Index for dcpr <img alt=">" border="0" src="../right.png"></a></td></tr></table>--><h1>roc</h1><h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2><div class="box"><strong>roc</strong></div><h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2><div class="box"><strong>function [threshold, fp, fn, mu1, var1, mu2, var2, a, b]=roc(data1, data2, plotOpt); </strong></div><h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2><div class="fragment"><pre class="comment">roc
Usage: [threshold, fp, fn, mu1, var1, mu2, var2, a, b]=roc(data1, data2, plotOpt);
data1: vector for data of negative set
data2: vector for data of positive set
threshold: threhold (using Baysian rule where priors are multiplied)
fp: false positive error rate (using Baysian rule where priors are multiplied)
fn: false negative error rate (using Baysian rule where priors are multiplied)
mu1: mu of class 1
var1: variance of class 1
mu2: mu of class 2
var2: variance of class 2
a, b: the fitting parameters of y=1/(1+exp(-a*(x-b))) for class-2 conditional probability (priors are multiplied)
For example:
data1=randn(1,100)/2;
data2=randn(1,800)+3;
roc(data1, data2, 1);</pre></div><!-- crossreference --><h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>This function calls:<ul style="list-style-image:url(../matlabicon.gif)"><li><a href="gaussian.html" class="code" title="function out = gaussian(data, gParam);">gaussian</a> gaussian: Multi-dimensional Gaussian propability density function</li><li><a href="gaussianMle.html" class="code" title="function gaussianParam = gaussianMle(feature, plotOpt)">gaussianMle</a> mleGaussian: Maximum likelihood estimator for Gaussian distribution</li><li><a href="rocPlot.html" class="code" title="function [fn, fp]=rocPlot(data1, data2, scaleStr)">rocPlot</a> rocPlot: Plot for receiver operating curve</li></ul>This function is called by:<ul style="list-style-image:url(../matlabicon.gif)"></ul><!-- crossreference --><h2><a name="_subfunctions"></a>SUBFUNCTIONS <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2><ul style="list-style-image:url(../matlabicon.gif)"><li><a href="#_sub1" class="code">function selfdemo</a></li></ul><h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2><div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function [threshold, fp, fn, mu1, var1, mu2, var2, a, b]=roc(data1, data2, plotOpt);</a>0002 <span class="comment">%roc</span>0003 <span class="comment">% Usage: [threshold, fp, fn, mu1, var1, mu2, var2, a, b]=roc(data1, data2, plotOpt);</span>0004 <span class="comment">% data1: vector for data of negative set</span>0005 <span class="comment">% data2: vector for data of positive set</span>0006 <span class="comment">% threshold: threhold (using Baysian rule where priors are multiplied)</span>0007 <span class="comment">% fp: false positive error rate (using Baysian rule where priors are multiplied)</span>0008 <span class="comment">% fn: false negative error rate (using Baysian rule where priors are multiplied)</span>0009 <span class="comment">% mu1: mu of class 1</span>0010 <span class="comment">% var1: variance of class 1</span>0011 <span class="comment">% mu2: mu of class 2</span>0012 <span class="comment">% var2: variance of class 2</span>0013 <span class="comment">% a, b: the fitting parameters of y=1/(1+exp(-a*(x-b))) for class-2 conditional probability (priors are multiplied)</span>0014 <span class="comment">%</span>0015 <span class="comment">% For example:</span>0016 <span class="comment">% data1=randn(1,100)/2;</span>0017 <span class="comment">% data2=randn(1,800)+3;</span>0018 <span class="comment">% roc(data1, data2, 1);</span>0019 0020 <span class="comment">% Roger Jang, 20050717</span>0021 0022 <span class="keyword">if</span> nargin<1; <a href="#_sub1" class="code" title="subfunction selfdemo">selfdemo</a>; <span class="keyword">return</span>; <span class="keyword">end</span>0023 <span class="keyword">if</span> nargin<3, plotOpt=0; <span class="keyword">end</span>0024 0025 data1=data1(:)';0026 data2=data2(:)';0027 gaussianParam1=<a href="gaussianMle.html" class="code" title="function gaussianParam = gaussianMle(feature, plotOpt)">gaussianMle</a>(data1);0028 gaussianParam2=<a href="gaussianMle.html" class="code" title="function gaussianParam = gaussianMle(feature, plotOpt)">gaussianMle</a>(data2);0029 n1=length(data1);0030 n2=length(data2);0031 0032 x=linspace(min([data1, data2]), max([data1, data2]));0033 g1=<a href="gaussian.html" class="code" title="function out = gaussian(data, gParam);">gaussian</a>(x, gaussianParam1);0034 g2=<a href="gaussian.html" class="code" title="function out = gaussian(data, gParam);">gaussian</a>(x, gaussianParam2);0035 0036 absDiff1=abs(n1*g1-n2*g2);0037 0038 <span class="comment">%temp=absDiff1;</span>0039 <span class="comment">%[junk, id1]=max(n1*g1);</span>0040 <span class="comment">%[junk, id2]=max(n2*g2);</span>0041 <span class="comment">%temp=inf*absDiff1;</span>0042 <span class="comment">%temp(id1:id2)=absDiff1(id1:id2);</span>0043 <span class="comment">%[junk, minIndex]=min(temp); % The threshold must fall between x(id1) and x(id2)</span>0044 <span class="comment">%threshold=x(minIndex);</span>0045 <span class="comment">%if mu1<threshold && threshold < mu2</span>0046 <span class="comment">% fp=sum(data1>=threshold)/(n1+n2);</span>0047 <span class="comment">% fn=sum(data2<threshold)/(n1+n2);</span>0048 <span class="comment">%elseif mu2<threshold && threshold < mu1</span>0049 <span class="comment">% fp=sum(data1<=threshold)/(n1+n2);</span>0050 <span class="comment">% fn=sum(data2>threshold)/(n1+n2);</span>0051 <span class="comment">%else</span>0052 <span class="comment">% fp=inf;</span>0053 <span class="comment">% fn=inf;</span>0054 <span class="comment">% fprintf('Something wrong in roc.m!\n');</span>0055 <span class="comment">%end</span>0056 0057 thresholds=x;0058 <span class="comment">%allData=[data1(:); data2(:)]';</span>0059 <span class="comment">%sorted=sort(allData);</span>0060 <span class="comment">%thresholds=([sorted(1)-1, sorted]+[sorted, sorted(end)+1])/2;</span>0061 0062 fps=0*thresholds;0063 fns=0*thresholds;0064 <span class="keyword">for</span> i=1:length(thresholds);0065 th=thresholds(i);0066 fps(i)=sum(data1>=th)/(n1+n2);0067 fns(i)=sum(data2< th)/(n1+n2);0068 <span class="keyword">end</span>0069 [minValue, minIndex]=min(fps+fns);0070 index=find((fps+fns)==minValue); <span class="comment">% Check if there is more than one minValue</span>0071 0072 <span class="keyword">if</span> length(index)>1 <span class="comment">% If there is more than one minValue (usually a plateau of perfect separation), taking weighted average</span>0073 start=thresholds(index(1));0074 stop=thresholds(index(end));0075 w1=sum(abs(start-data1));0076 w2=sum(abs(data2-stop));0077 minIndex=round((w1*index(end)+w2*index(1))/(w1+w2));0078 <span class="keyword">end</span>0079 0080 fp=fps(minIndex);0081 fn=fns(minIndex);0082 threshold=thresholds(minIndex);0083 0084 absDiff2=abs(n1*g1./(n1*g1+n2*g2)-n2*g2./(n1*g1+n2*g2));0085 0086 <span class="comment">% ?p???????∮?u??
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -