www.gusucode.com > stats 源码程序 matlab案例代码 > stats/EstimateConditionalCumulativeDistributionQuantileRegExample.m

    %% Estimate Conditional Cumulative Distribution Using Quantile Regression
%%
% Load the |carsmall| data set.  Consider a model that predicts the fuel
% economy of a car given its engine displacement.
load carsmall
%%
% Train an ensemble of bagged regression trees using the entire data set.
% Specify 100 weak learners.
rng(1); % For reproducibility
Mdl = TreeBagger(100,Displacement,MPG,'Method','regression');
%%
% Estimate the response weights for a random sample of four training
% observations.  Plot the training sample and identify the chosen
% observations.
[predX,idx] = datasample(Mdl.X,4);
[~,YW] = quantilePredict(Mdl,predX);
n = numel(Mdl.Y);

figure;
plot(Mdl.X,Mdl.Y,'o');
hold on
plot(predX,Mdl.Y(idx),'*','MarkerSize',10);
text(predX-10,Mdl.Y(idx)+1.5,{'obs. 1' 'obs. 2' 'obs. 3' 'obs. 4'});
legend('Training Data','Chosen Observations');
xlabel('Engine displacement')
ylabel('Fuel economy')
hold off
%%
% |YW| is an |n|-by-4 sparse matrix containing the response weights.
% Columns correspond to test observations and rows correspond to responses
% in the training sample. Response weights are independent of the
% specified quantile probability.
%%
% Estimate the conditional cumulative distribution function (C.C.D.F.) of
% the responses by:
%
% # Sorting the responses is ascending order, and then sorting the response
% weights using the indices induced by sorting the responses.
% # Computing the cumulative sums over each column of the sorted response
% weights.
%
[sortY,sortIdx] = sort(Mdl.Y);
cpdf = full(YW(sortIdx,:));
ccdf = cumsum(cpdf);
%%
% |ccdf(:,j)| is the empirical C.C.D.F. of the
% response given test observation |j|.
%%
% Plot the four empirical C.C.D.F. in the same figure.
figure;
plot(sortY,ccdf);
legend('C.C.D.F. given test obs. 1','C.C.D.F. given test obs. 2',...
    'C.C.D.F. given test obs. 3','C.C.D.F. given test obs. 4',...
    'Location','SouthEast')
title('Conditional Cumulative Distribution Functions')
xlabel('Fuel economy')
ylabel('Empirical CDF')