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

    classdef (Sealed) xregstatsmodel < xregexportmodel
    %XREGSTATSMODEL public interface for Model-Based Calibration toolbox models
    %    XREGSTATSMODEL provides the ability to evaluate a model and
    %    calculate the prediction error variance for models fitted in the
    %    Model-Based Calibration toolbox.
    %
    %    xregstatsmodel methods:
    %      EvalModel - evaluate model
    %      pev       - evaluate prediction error variance
    %      ceval     - evaluate boundary model
    %      dferror   - degrees of freedom for error
    %      predint   - confidence intervals for model predictions
    %      nfactors  - number of input factors
    %
    %    Models can also be evaluated using parentheses. 
    %      y = model(X);
    
    %  Copyright 2000-2015 The MathWorks, Inc. and Ford Global Technologies, Inc.


    properties (Access=private)
        %MVMODEL internal MBC model (xregmodel)
        mvModel
    end
    methods(Hidden)
        function M= xregstatsmodel(mdl,name,i,constraints)
            %XREGSTATSMODEL constructor
            %  M = XREGSTATSMODEL returns an empty model object.
            %
            %  M = XREGSTATSMODEL(Model, Name, Info) returns an xregStatsModel object
            %  containing a model object and traceability information.  Model must be
            %  an xregmodel object.  Info is a structure with the following fields:
            %    User      - User who created model.
            %    Date      - Date created.
            %    Version   - Version of MBC used.
            %    Parent    - Name of parent project file.
            %    Variables - Model variables.
            %    new       - Structure with at least one field.  new{1} will contain
            %                the emdidata for the engine. new{i}, i>1 will be a
            %                structure with two fields, Title and Description, which
            %                the user creates during the export model process.
            %
            %  M = XREGSTATSMODEL(Model, Name, Info, Constraints) will produce an
            %  xregStatsModel with a non-empty constraints field.
            
            if nargin > 0
                % compress model so CAGE object is as small as possible
                % Exportmodel requires
                % (name,i,symbols,units,ranges,constraints)
                % so we need to manufacture symbols, units, ranges
                
                [~,Symbols,U] = nfactors(mdl);
                U = reshape(U,length(U),1);
                % nfactors gives us input units
                yinf = yinfo(mdl);
                U = [{yinf.Units};U]; % first element is output units
                [Low,Upp]=range(mdl);
                R = [Low(:)';Upp(:)'];
                
                if nargin < 4
                    i = [];
                    name = varname(mdl);
                    constraints = [];
                end
                ExportModelArgs = {name,i,Symbols,U,R,constraints};
            else
                mdl = [];
                ExportModelArgs = {};
            end
            
            M = M@xregexportmodel(ExportModelArgs{:});
            
            if ~isempty(mdl)
                M.mvModel = compress(mdl);
            end

        end
    end
    
    methods
        function df = dferror(m)
           %DFERROR degrees of freedom for model
           %    df = dferror(m);
           %    The degrees of freedom can be used to construct a
           %    confidence interval for predictions. To calculate a 95%
           %    confidence interval use:
           %      yhat = m(X);
           %      level = 0.95;
           %      ylower = yhat + tinv((1-level)/2,dferror(m))*sqrt(pev(m,X));
           %      yupper = yhat - tinv((1-level)/2,dferror(m))*sqrt(pev(m,X));
           %
           % See also pev, predint
           
            df = dferror(m.mvModel);
        end
        
        
        function Interval = predint(m,X,level)
            %PREDINT confidence interval for model prediction
            %    INTERVAL = predint(MODEL,X,LEVEL);
            %    X is a(N-by-NF) array, where NF is the number of inputs,
            %    and N the number of points to evaluate the model at.
            %    A LEVEL% confidence interval of the predictions is
            %    calculated about the predicted value. The default value
            %    for LEVEL is 99.
            %    INTERVAL is a Nx2 array where the first column is the
            %    lower bound and the second column is the upper bound.
            %
            % See also EvalModel, pev, dferror
            
            if nargin<3
                level = 99;
            end
            %convert level from a percentage to a fraction 
            level = level/100;
            if ~pevcheck(m)
                error(message('mbc:xregstatsmodel:InvalidState'))
            end
            
            % this calculation gives the lower tail of the t distribution
            bnd = -tinv((1-level)/2,dferror(m))*sqrt(pev(m,X));
            yhat = EvalModel(m,X);
            Interval = [yhat-bnd  yhat+bnd];
            
        end
    end
    
    methods(Hidden,Static)
        function obj = loadobj(obj)
            %LOADOBJ File-load filter for XREGSTATSMODEL
            %
            %  OBJ = LOADOBJ(OBJ)
            %
            %  Created to support new constraint object structure. Constraint now need
            %  to be "upgraded" at load time.
            %
            %  See also XREGSTATSMODEL, LOADOBJ.

            mdl = obj.mvModel;
            if isstruct(obj) && isfield(obj,'xregexportmodel')
                % loading old OOPS
                if isa(obj.mvModel,'xregmodswitch')
                    % xregmodswitch is now an xregexportmodel
                    NewObj = obj.mvModel;
                    obj = copyToExportModel(NewObj,obj.xregexportmodel);
                else
                    % other models need to be converted to an MCOS
                    % xregstatsmodel
                    NewObj = xregstatsmodel;
                    NewObj.mvModel = obj.mvModel;
                    obj = copyToExportModel(NewObj,obj.xregexportmodel);
                    % Constraint now need to be "upgraded" at load time.
                    con = obj.constraints;
                    if ~isempty( con ) && isa(mdl, 'xregmodel')
                        obj.constraints= upgrade( con, mdl );
                    end
                end
            end
            
        end
    end
    
    
    methods (Hidden)
        
        function s = str_func(m)
            %STR_FUNC summary string for model
            s = str_func(model(m));
        end        
        function OK = pevSupported(m)
        %pevSupported export PEV to Simulink
        OK = pevSupported(model(m));
        end
        % methods for internal use
         varargout = GenTable(m,x,varargin)
         OK = checkmodel(m)
         m = compress(m,EvalMode)
         display(m,varargin)
         varargout = eval(m,varargin)
         OK = loadmodel(m)
         mdl = model(m)
         OK = pevcheck(m)
         varargout = pevgrid(m, x, natural)
         m = revertRanges(m)
         m = saveobj(m)
         varargout = surface(m,varargin)
         t = type(m)
         hGrid = propPageModel(mdl, hGroup, hParent)
    end
end