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
ScalarEpsSVR.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 
50  public: /* ( setObservable ) */
51 
52  Eps = 0.05;
71  public: /* ( setObservable ) */
72 
74  this = this@general.regression.BaseQPSVR;
75  this.registerProps(" eps ");
76  }
84  function [ai , sf ] = regress(fxi,ainit) {
85  m = size(this.K,1);
86  T = [diag(ones(1,m)) -diag(ones(1,m))];
87 
88  /* Problem setup */
89  Q = T^t*this.K*T;
90  Q = (Q+Q^t)/2;
91  c = (this.Eps*ones(2*m,1) - (fxi*T)^t);
92 
93  /* Starting point */
94  if nargin < 3
95  /* ainit = ones(2*m,1)*this.C/(2*m); */
96  ainit = [];
97  end
98 
99  /* Bounds */
100  lb = zeros(2*m,1);
101  ub = ones(2*m,1)*(this.C/m);
102 
103  /* Call solver */
104  p = this.solve(Q,c,lb,ub,[],[],[],ainit);
105 
106  /* Convert results */
107  ai = T*p;
108 
110  sf = StopFlag.SUCCESS;
111  }
125 #if 0 //mtoc++: 'set.Eps'
126 function Eps(value) {
127  if ~isposrealscalar(value)
128  error(" Value must be a positive real scalar ");
129  end
130  this.Eps= value;
131  }
132 
133 #endif
134 
135 
136  public: /* ( Sealed ) */ /* ( setObservable ) */
137 
138  function copy = clone() {
139  copy = general.regression.ScalarEpsSVR;
140  /* Call superclass clone */
141  copy = clone@general.regression.BaseQPSVR(this, copy);
142  /* Copy local props */
143  copy.Eps= this.Eps;
144  }
157  public: /* ( Static ) */ /* ( setObservable ) */
158 
159  static function res = test_ScalarEpsSVR() {
160 
161  x = -5:.1:5;
162  fx = sinc(x)-1;
163  /* fx(30) = .5;
164  *x = 1:10;
165  *fx = ones(size(x))*5; */
166 
167  svr = general.regression.ScalarEpsSVR;
168  /* svr.eps = 0.073648; */
169  svr.Eps= .1;
170  svr.Lambda= 1/20;
171  /* svr.MaxIterations = 1000; */
172 
173  /* kernel = kernels.PolyKernel(2);
174  *kernel = kernels.LinearKernel; */
175  kernel = kernels.GaussKernel(.5);
176  svr.K= kernel.evaluate(x,x);
177 
178  figure;
179  plot(x,fx," r ",x,[fx-svr.Eps; fx+svr.Eps]," r-- ");
180 
181  [ai, svidx] = svr.computeKernelCoefficients(fx, []);
182  sv = x(:,svidx);
183  svfun = @(x)ai" *(kernel.evaluate(x,sv) ");
184 
185  fsvr = svfun(x);
186 
187  fdiff = abs(fsvr(svidx)-fx(svidx));
188  errors = find(fdiff < .999*svr.Eps);
189  res = isempty(errors);
190 
191  /* Plot approximated function */
192  hold on;
193  plot(x,fsvr," b ",x,[fsvr-svr.Eps; fsvr+svr.Eps]," b-- ");
194  skipped = setdiff(1:length(x),svidx);
195  plot(sv,fx(svidx)," .r ",x(skipped),fx(skipped)," xr ");
196 
197  if ~res
198  plot(x(svidx(errors)),fx(svidx(errors))," blackx "," LineWidth ",4);
199  end
200 
201  tit = sprintf(" #SV=%d, eps=%f ",length(svidx),svr.Eps);
202  title(tit);
203  disp(tit);
204  hold off;
205  }
213  static function test2_CustomSVR() {
214 
215  x = [0 0.3000 0.6000 0.9000;
216  0.8321 1.0756 1.3557 1.6539];
217  fx = [0.7393 0.8799 0.9769 0.9966];
218 
219  svr = general.regression.ScalarEpsSVR;
220  svr.Eps= 0.5;
221  svr.C= 10;
222  kernel = kernels.GaussKernel(1);
223  svr.K= kernel.evaluate(x,x);
224 
225  figure(1);
226  xp = x(1,:);
227  plot(xp,fx," r ",xp,[fx-svr.Eps; fx+svr.Eps]," r-- ");
228 
229  [ai,svidx] = svr.regress(fx);
230  sv = x(:,svidx);
231  svfun = @(x)ai^t*kernel.evaluate(sv,x);
232 
233  fsvr = svfun(x);
234  hold on;
235 
236  /* Plot approximated function */
237  plot(xp,fsvr," b ",xp,[fsvr-svr.Eps; fsvr+svr.Eps]," b-- ");
238  skipped = setdiff(1:length(x),svidx);
239  plot(sv(1,:),fx(svidx)," .r ",xp(skipped),fx(skipped)," xr ");
240 
241  hold off;
242  }
253 };
254 }
255 }
256 
257 
258 
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_ScalarEpsSVR()
Performs a test of this class.
Definition: ScalarEpsSVR.m:159
function registerProps(varargin)
Call this method at any class that defines DPCM observed properties.
Definition: DPCMObject.m:125
ScalarEpsSVR()
Default constructor. Calls superconstructor to initialize properties.
Definition: ScalarEpsSVR.m:73
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: ScalarEpsSVR.m:138
disp
Handle object disp method which is called by the display method. See the MATLAB disp function...
static function test2_CustomSVR()
Performs a test of this class.
Definition: ScalarEpsSVR.m:213
ScalarEpsSVR: Scalar support vector regression.
Definition: ScalarEpsSVR.m:19
double C
The weighting of the slack variables.
function [ ai , sf ] = regress(fxi, ainit)
Performs epsilon-SVR regression.
Definition: ScalarEpsSVR.m:84
Eps
The margin for the approximation-to-source distance. Effectiveness also depends on C...
Definition: ScalarEpsSVR.m:52
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