classdef Fit
    %bgam.Fit - A class that stores values generated by a fit and can evaluate 
    %the resulting model
    %
    % bgam.Fit Properties:
    %    cs - 
    %    deviances - 
    %    mindeviance - 
    %    maxdeviance - 
    %    learners - 
    %    trainer - 
    %    fitParams - 
    %    eta - 
    %    deviance -

    properties
       %The model deviance at the last iteration
       deviance;
       
       %The sequence of measured deviances. For a likelihood function
       %L(y1,y2) y1 = observation and y2 = current model, then the deviance
       %is given by -2*(L(y1,y2)-L(y1,y1))
       deviances = [];
       
       %The maximum deviance of a model, measured when the model contains
       %only an offset (c + b)
       maxdeviance = []
       
       %The sequence of measured D^2 values. D^2 is the analogue of R^2 for
       %GLMs (and in fact is equal to it for the normal/identity
       %distro/link combo). It is given by
       %1-deviance(model)/maxdeviance(model)
       d2s = []
       
       %The sequence of learners learned
       learners;
       
       %The sequence of offset (c) values
       cs = [];
       
       %The trainer used to get this fit
       trainer;
       
       %Fit parameters
       fitParams;
       
       %The value of eta at the last iteration of boosting
       eta = [];
    end
    methods
        %Evaluate the sum of the learners
        function [v] = evaluate(this,X)
            v = 0;
            for ii = 1:length(this.learners)
                v = v + this.trainer.evaluate(this.learners(ii),X);
            end
        end
        
        %Evaluate the cumulative sum of the learners from iter rg(1) to
        %iter rg(2)
        function [v] = cumEvaluateRange(this,X,rg)
            v = cumsum(this.evaluateEachRange(X,rg),2);
        end
        
        %Evaluate the cumulative sum of the learners
        function [v] = cumEvaluate(this,X)
            v = cumsum(this.evaluateEach(X),2);
        end
        
        %Evaluate each learner
        function [v] = evaluateEach(this,X)
            v = zeros(size(X,1),length(this.learners));
            for ii = 1:length(this.learners)
                v(:,ii) = this.trainer.evaluate(this.learners(ii),X);
            end
        end
        
        %Evaluate each learner from iter rg(1) to rg(2)
        function [v] = evaluateEachRange(this,X,rg)
            v = zeros(size(X,1),range(rg)+1);
            for ii = rg(1):rg(2)
                v(:,ii-rg(1)+1) = this.trainer.evaluate(this.learners(ii),X);
            end
        end
    end
end