www.gusucode.com > stats 源码程序 matlab案例代码 > stats/FittingCopulasToDataExample.m
%% Fitting Copulas to Data % This example shows how to use |copulafit| to calibrate copulas with data. % To generate data |Xsim| with a distribution "just like" (in terms of % marginal distributions and correlations) the distribution of data in the % matrix |X| , you need to fit marginal distributions to the columns of |X| % , use appropriate cdf functions to transform |X| to |U| , so that |U| has % values between 0 and 1, use |copulafit| to fit a copula to |U| , generate % new data |Usim| from the copula, and use appropriate inverse cdf % functions to transform |Usim| to |Xsim| . %% % Load and plot the simulated stock return data. % Copyright 2015 The MathWorks, Inc. load stockreturns x = stocks(:,1); y = stocks(:,2); scatterhist(x,y,'Direction','out') %% % Transform the data to the copula scale (unit square) using a kernel % estimator of the cumulative distribution function. u = ksdensity(x,x,'function','cdf'); v = ksdensity(y,y,'function','cdf'); scatterhist(u,v,'Direction','out') xlabel('u') ylabel('v') %% % Fit a _t_ copula. [Rho,nu] = copulafit('t',[u v],'Method','ApproximateML') %% % Generate a random sample from the _t_ copula. r = copularnd('t',Rho,nu,1000); u1 = r(:,1); v1 = r(:,2); scatterhist(u1,v1,'Direction','out') xlabel('u') ylabel('v') set(get(gca,'children'),'marker','.') %% % Transform the random sample back to the original scale of the data. x1 = ksdensity(x,u1,'function','icdf'); y1 = ksdensity(y,v1,'function','icdf'); scatterhist(x1,y1,'Direction','out') set(get(gca,'children'),'marker','.') %% % As the example illustrates, copulas integrate naturally with other % distribution fitting functions.