www.gusucode.com > mbcmodels 工具箱 matlab 源码程序 > mbcmodels/@xregmodel/xregmodel.m

    function m=xregmodel(varargin)
%XREGMODEL Base class for all MBC models
%
% m= xregmodel;
% m= xregmodel('nfactors',nf);
%
% The XREGMODEL class is the base, abstract, model class for use with MBC. 
% The model class is responsible for transformations in x and y for both 
% fitting and evaluation tasks. Bad data is also removed in model/checkdata 
% before estimating the model parameters.
% 

%  Copyright 2000-2012 The MathWorks, Inc. and Ford Global Technologies, Inc.


VerNum = 7;

if nargin==1  
   m=varargin{1};
   if isstruct(m)
		m= i_UpdateOld(m);
        m.version = VerNum;
	elseif isa(m,'xregmodel')
		return
	else
		error(message('mbc:xregmodel:InvalidInput1'))
   end
else
   if nargin==2 && strcmp(varargin{1},'nfactors')
      Nf= varargin{2};
   else
      Nf=1;
   end
  
   if nargin==0 && (~license('checkout', 'mbc_toolbox') || ~ispc)
       error(message('mbc:xregmodel:InvalidLicense'));
   end
   
   m.version= VerNum;
   % Transformation of y variable (either empty or an inline object)
   
   % X transformation [min,max] -> [-1,1]. 
   % x' = 2*(g(x) - (g(min)+g(max))/2 )/(g(max)-g(min))
   % A nonlinear transformation can be specified using the inline object, g.
   % It is also possible to set a different target range
   
   
   % independent factor info
   m.Comments  = '';
   
   % xregmodel/fitmodel will call this method
   m.FitAlgorithm = 'leastsq';
   % transform both sides
   m.TransBS = 0;
	
	
	% new version 4 properties
	m.Stats.Rinv = [];
	m.Stats.mse = 0;
	m.Stats.df  = Inf;
    m.Stats.Summary=[];
    
	% version 6 properties
	m.Outliers= [];

    % version 7 properties
    m.Inputs = mbcinputfactor(Nf);
    m.Output= mbcmodeloutput('Name','y',...
        'Units','',...
        'Symbol','y');

end
   
m= class(m,'xregmodel');



function m= i_UpdateOld(m)

if m.version<3
	
	% new fields for version 3
	m.version = 3;        
	
	% update coding structure to include range field
	c= m.code;
	if ~isempty(c)
		if ~isfield(c,'mid')
			% version 1 didn't have this field
			for i=1:length(c)
				c(i).mid= (c(i).max+c(i).min)/2;
			end
		end
		% new coding field   Target interval= [-1,1]
		[c.range]= deal(2);
	else
		% make empty structure
		c   = struct('min',{},'max',{},'g',{},'mid',{},'range',{});
	end
	m.code= c;
	
	% version 2 stored variable info in store field as a hack
	% this cleans up everything and makes a new structure
	OldStore= m.store;
	m= mv_rmfield(m,'store');
	if isempty(OldStore)
		% make some default values
		nf= max(length(c),1);
		xsym= cell(nf,1);
		for i=1:nf;
			xsym{i}= sprintf('X%1d',i);
		end
		OldStore= struct('Name','Y',...
			'YUnits','',...
			'Symbols',{xsym},...
			'XUnits', '',...
			'Comments','');
	end
	% new xinfo structure
	m.Xinfo= struct('Names',{OldStore.Symbols},...
		'Units',{OldStore.XUnits},...
		'Symbols',{OldStore.Symbols});   % won't be cells
	% new yinfo structure
	m.Yinfo= struct('Name',OldStore.Name,...
		'Units',OldStore.YUnits,...
		'Symbol',OldStore.Name);   % won't be cells
	m.Comments  = OldStore.Comments;
	
	% fitmodel will call this method
	m.FitAlgorithm = 'leastsq';
	% transform both sides
	m.TransBS = 0;
end
if m.version<4
	% new version 4 properties for storing stats info 
	m.version=4;
	m.Stats.Rinv = [];
	m.Stats.mse = 0;
	m.Stats.df  = Inf;
    m.Stats.Summary=[];
end
if m.version<6
	m.Outliers= [];
end
if m.version<7
    Inputs = iMakeInputs(m);
    m = rmfield(m,'Xinfo');
    m = rmfield(m,'code');
    m.Inputs = Inputs;
    
    yi = m.Yinfo;
    ytrans = char(m.ytrans);
    if strcmp(ytrans,'y') || strcmp(ytrans,'x')
        ytrans = '';
    end
    m.Output = mbcmodeloutput('Name',yi.Name,...
        'Symbol',yi.Symbol,...
        'Units',char(yi.Units),...
        'Transform',ytrans);
    m = rmfield(m,'Yinfo');
    m = rmfield(m,'ytrans');
    m = rmfield(m,'yinv');
end


function Inputs = iMakeInputs(m)

xi = m.Xinfo;
if isempty(xi.Units)
    xi.Units = cell(size(xi.Symbols));
end

nf = length(xi.Symbols);
Scale = cell(nf,1);
Offset = Scale;
if isempty(m.code)
    Rng = repmat({[-1 1]},nf,1);
    g = cell(nf,1);
    Target = repmat({[-Inf Inf]},nf,1);
    Scale(:)={1};
    Offset(:)={0};
else
    Rng = [m.code.min;m.code.max]';
    g = {m.code.g};
    Target = Rng;
    ws = warning('off','symbolic:sym:finverse:warnmsg2');
    for i=1:nf
        c = m.code(i);
        r = c.range;
        if isfinite(r)
            st = -(c.mid- c.min)/(c.max-c.min)*r;
            Target(i,:)= [st st+r];
            if abs(Target(i,:)-Rng(i,:))<1e-8
                Target(i,:) = [-Inf Inf];
            end
            Scale{i} = c.range/(c.max-c.min);
        else
            Scale{i} = 1;
            Target(i,:) = [-Inf Inf];
        end
        if isfinite(c.mid)
            Offset{i} = c.mid;
        else
            Offset{i} = 0;
        end

        if ~isempty(g{i}) && ~strcmp(char(g{i}),'x')
            % invert transform
            gs=sym(char(c(i).g));
            ginv = finverse( gs );
            ginv = mbcinline(ginv);
            Rng(i,:) = ginv(Rng(i,:));
            
            g{i} = mbctransform(g{i});
        else
            g{i} = '';
        end

    end
    warning(ws);
    Rng = num2cell(Rng,2);
    Target = num2cell(Target,2);
end

istr = struct('pName',xi.Names(:),...
    'pSymbol', xi.Symbols(:),...
    'pUnits',cellfun(@char,xi.Units(:),'UniformOutput',false),...
    'pRange',Rng, ....
    'Transform',g(:),...
    'pTarget',Target,...
    'Scale',Scale,...
    'Offset',Offset);

Inputs = mbcinputfactor(istr);
% Inputs = setupTargets(Inputs,Target);