KerMor  0.9
Model order reduction for nonlinear dynamical systems and nonlinear approximation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ScalarNuSVR.m
Go to the documentation of this file.
1 namespace general{
2 namespace regression{
3 
4 
5 /* (Autoinserted by mtoc++)
6  * This source code has been filtered by the mtoc++ executable,
7  * which generates code that can be processed by the doxygen documentation tool.
8  *
9  * On the other hand, it can neither be interpreted by MATLAB, nor can it be compiled with a C++ compiler.
10  * Except for the comments, the function bodies of your M-file functions are untouched.
11  * Consequently, the FILTER_SOURCE_FILES doxygen switch (default in our Doxyfile.template) will produce
12  * attached source files that are highly readable by humans.
13  *
14  * Additionally, links in the doxygen generated documentation to the source code of functions and class members refer to
15  * the correct locations in the source code browser.
16  * However, the line numbers most likely do not correspond to the line numbers in the original MATLAB source files.
17  */
18 
46  public: /* ( setObservable ) */
47 
48  nu = .4;
70  public: /* ( Transient ) */
71 
73 
74 
75  public: /* ( Transient ) */
76 
77 
79  this = this@general.regression.BaseQPSVR;
80  this.registerProps(" nu ");
81  }
82 
83 
84  function [ai , sf ] = regress(fxi,ainit) {
85 
86  /* % Compile quadratic program */
87 
88  /* Total number of samples */
89  m = size(this.K,1);
90 
91  /* Storage: alpha(1..m) = alpha_i, alpha(m+1..2m) = alpha_i*
92  * T performs alpha_i* - alpha_i each */
93  T = [diag(ones(1,m)) -diag(ones(1,m))];
94 
95  Q = T^t*this.K*T;
96  Q = (Q+Q^t)/2;
97  c = -(fxi*T)^t;
98  A = ones(1,2*m);
99 
100  /* Starting point */
101  if nargin < 3 || isempty(ainit)
102  ainit = ones(m,1)*this.C/m;
103  /* ainit = []; */
104  end
105 
106  /* Bounds */
107  lbA = 0;
108  ubA = this.C * this.nu;
109  lb = zeros(2*m,1);
110  ub = ones(2*m,1)*(this.C/m);
111 
112  /* % Call solver */
113  [p,d,info] = this.solve(Q,c,lb,ub,A,lbA,ubA,T^t*ainit);
114 
115  /* % Convert results */
116  ai = T*p;
117  this.LastEpsilon= d(end);
118 
120  sf = StopFlag.SUCCESS;
121  }
135 #if 0 //mtoc++: 'set.nu'
136 function nu(value) {
137  if ~isposrealscalar(value)
138  error(" nu must be a positive scalar ");
139  elseif value >= 1
140  error(" nu must be lower than one ");
141  end
142  this.nu= value;
143  }
144 
145 #endif
146 
147 
148  public: /* ( Sealed ) */ /* ( Transient ) */
149 
150  function copy = clone() {
151  copy = general.regression.ScalarNuSVR;
152  /* Call superclass clone */
153  copy = clone@general.regression.BaseScalarSVR(this, copy);
154  /* Copy local props */
155  copy.nu= this.nu;
156  }
169  public: /* ( Static ) */ /* ( Transient ) */
170 
171 
172  static function res = test_ScalarNuSVR() {
173 
174  x = -5:.1:5;
175  fx = sinc(x);
176  /* x = 1:10;
177  *fx = ones(size(x))*5; */
178 
179  svr = general.regression.ScalarNuSVR;
180  svr.nu= .1;
181  svr.Lambda= 1/100;
182  /* kernel = kernels.PolyKernel(2);
183  *kernel = kernels.LinearKernel; */
184  kernel = kernels.GaussKernel(1);
185  svr.K= kernel.evaluate(x,x);
186 
187  figure(2);
188  [ai, svidx] = svr.computeKernelCoefficients(fx, []);
189  epsi = svr.LastEpsilon;
190  plot(x,fx," r ",x,[fx-epsi; fx+epsi]," r-- ");
191 
192  sv = x(:,svidx);
193  svfun = @(x)ai^t*kernel.evaluate(sv,x);
194 
195  fsvr = svfun(x);
196 
197  /* Plot approximated function */
198  hold on;
199  plot(x,fsvr," b ",x,[fsvr-epsi; fsvr+epsi]," b-- ");
200  skipped = setdiff(1:length(x),svidx);
201  plot(sv,fx(svidx)," .r ",x(skipped),fx(skipped)," xr ");
202 
203  fdiff = abs(fsvr(svidx)-fx(svidx));
204  errors = find(fdiff < .999*epsi);
205  res = isempty(errors);
206  if ~res
207  plot(x(svidx(errors)),fx(svidx(errors))," blackx "," LineWidth ",4);
208  end
209 
210  tit = sprintf(" nu = %f, epsilon = %f, #SV=%d ",svr.nu,epsi,length(svidx));
211  title(tit);
212  disp(tit);
213  hold off;
214  }
222  static function res = test_ScalarNuSVR_to_EpsSVR() {
223 
224  x = -5:.1:5;
225 
226  r = rand(1,length(x));
227  fx = sinc(x);
228  fx(r<.2) = fx(r<.2)-.5;
229  fx(r>.8) = fx(r>.8)+.5;
230 
231  /* fx = sinc(x) + (rand(1,length(x))-.5)*.3; */
232 
233  svr = general.regression.ScalarNuSVR;
234  svr.nu= 0.3;
235  svr.Lambda= 1/20;
236  kernel = kernels.GaussKernel(1);
237  svr.K= kernel.evaluate(x,x);
238  [ai,svidx] = svr.computeKernelCoefficients(fx,[]);
239  epsi = svr.LastEpsilon;
240  sv = x(:,svidx);
241  svfun = @(x)ai^t*kernel.evaluate(sv,x);
242 
243  /* Create eps-SVR and feed with computed epsilon */
244  esvr = general.regression.ScalarEpsSVR;
245  esvr.Eps= epsi;
246  esvr.K= svr.K;
247  esvr.Lambda= svr.Lambda;
248  [eai,esvidx] = esvr.computeKernelCoefficients(fx,[]);
249  esv = x(:,esvidx);
250  esvfun = @(x)eai^t*kernel.evaluate(esv,x);
251 
252  figure(1);
253  xp = x(1,:);
254  subplot(1,2,1);
255  plot(xp,fx," r ");/* ,xp,[fx-eps; fx+eps],'r--'); */
256 
257  fsvr = svfun(x);
258  hold on;
259 
260  /* Plot approximated function */
261  plot(xp,fsvr," b ",xp,[fsvr-epsi; fsvr+epsi]," b-- ");
262  skipped = setdiff(1:length(x),svidx);
263  plot(sv(1,:),fx(svidx)," .r ",xp(skipped),fx(skipped)," xr ");
264  title(sprintf(" nu = %f, epsilon = %f, #SV = %d, C = %d ",svr.nu,epsi,length(svidx),svr.C));
265  hold off;
266  subplot(1,2,2);
267  plot(xp,fx," r ");/* ,xp,[fx-eps; fx+eps],'r--'); */
268 
269  efsvr = esvfun(x);
270  hold on;
271 
272  /* Plot approximated function */
273  plot(xp,efsvr," b ",xp,[efsvr-epsi; efsvr+epsi]," b-- ");
274  skipped = setdiff(1:length(x),esvidx);
275  plot(esv(1,:),fx(esvidx)," .r ",xp(skipped),fx(skipped)," xr ");
276  title(sprintf(" epsilon = %f, #SV = %d, C = %d ",epsi,length(esvidx),esvr.C));
277  hold off;
278 
279  res = norm(fsvr-efsvr) < sqrt(eps);
280  }
292 };
293 }
294 }
295 
296 
297 
SCALARSVR Scalar support vector regression.
Definition: ScalarNuSVR.m:19
nu
The weighting factor for epsilon against slack variables. Value may range between 0 < nu < 1...
Definition: ScalarNuSVR.m:48
function [ ai , sf ] = regress(fxi, ainit)
Performs scalar nu-support vector regression.
Definition: ScalarNuSVR.m:84
function [ p , d , info ] = solve(matrix Q,colvec c,colvec lb,colvec ub,matrix A,matrix Alb,matrix Aub, x0)
Solves the given quadratic problem according to the subclasses' algorithm.
Definition: BaseQPSVR.m:100
static function res = test_ScalarNuSVR_to_EpsSVR()
Tests with random outliers.
Definition: ScalarNuSVR.m:222
function registerProps(varargin)
Call this method at any class that defines DPCM observed properties.
Definition: DPCMObject.m:125
static const SUCCESS
Algorithm terminated otherwisely successful.
Definition: StopFlag.m:83
BaseQPSVR: SVR variant that is solved using quadratic programs.
Definition: BaseQPSVR.m:19
data.FileMatrix K
The kernel matrix to use.
Definition: BaseScalarSVR.m:75
function copy = clone()
Create new instance.
Definition: ScalarNuSVR.m:150
disp
Handle object disp method which is called by the display method. See the MATLAB disp function...
double C
The weighting of the slack variables.
static function res = test_ScalarNuSVR()
Performs a test of this class.
Definition: ScalarNuSVR.m:172
function res = isposrealscalar(value)
isposintscalar: Backwards-compatibility function for matlab versions greater than 2012a ...
StopFlag: Flags that algorithms can use to specify the reason for their termination.
Definition: StopFlag.m:17