楼主: tulipsliu
2776 10

[程序分享] 半参数以及混合藤copula 的MATLAB程序分享 [推广有奖]

促进中国计量经济学发展学科带头人

学科带头人

43%

还不是VIP/贵宾

-

威望
0
论坛币
386079 个
通用积分
470.0054
学术水平
127 点
热心指数
140 点
信用等级
103 点
经验
46957 点
帖子
1769
精华
0
在线时间
2477 小时
注册时间
2007-11-5
最后登录
2024-4-24

初级热心勋章

相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
已经下载论坛很多书籍,大概6000论坛币已经剩下只有300左右吧,此贴不收金币。我太累,已经看不了那么多书。

tulipsliu  中国数量经济(群)  中国数量金融(群)  群主

原则上中国数量经济已经封闭群,只实行邀请制,没通过我的邀请无法进群。

中国数量金融群为开放群,这里分享群号码,想一起学习金融的可以加入。

群嘛,不知道怎么介绍。中国数量金融群号码:949614744

其中应该是半参数那个,作者在程序里设置了  Frank copula ,程序运行中途无法继续。我已经修改为 t copula
可以放心运行。贴上半参数里其中一个主程序的代码,可以不复制到你的文件里,因为程序全部在 ZIP 文件里。
这个程序应该是我在 github 网站下载,如果想看更新最新的代码,可以自己去搜索。

  1. %This MATLAB script runs the Semi-Parametric Copula Method presented in Manheim &
  2. %Detwiler (2019) for various analytical test cases. It was originally coded and developed by Derek Manheim,
  3. %at the University of California, Irvine, Department of Civil & Environmental
  4. %Engineering using MATLAB r2015b. It is a fully Global, Moment Independent, Monte Carlo (MC) based sampling method
  5. %that relies on the use of parametric copula models to describe the joint pdf between
  6. %model inputs (i.e., parameters) and outputs. The user must specify what
  7. %test case to run (testfunc, options are 1,2,3), the copula model used
  8. %(options are 'Gaussian','t','Clayton','Frank','Gumbel'), the number of MC sampling points used to construct
  9. %and sample the Copula model (Nevalpts) as well as the repetition number (Rep), which specifies the
  10. %location of the Sobol sequence used for QMC sampling. In addition to the files provided in this download folder,
  11. %in order to run this code users must have the statistics and machine learning toolbox package installed
  12. %and should be running MATLAB r2015 or later. The results are automatically
  13. %saved after running this script. *Note - running over 65536 Nevalpts will
  14. %require more advanced parallel computation (8-16 cores or more) and is not recommended to be run locally.
  15. %Final Version, updated 5-8-2019
  16. clear ; close all; clc

  17. %Run specifications
  18. Rep = 1; %Location in the sobol sequence (1,2,3,4) to set up different repetitions
  19. testfunc = 1; %Which analytical test cases?
  20. CopulaModel = 't'; %Which parametric Copula Model to use?
  21. Nevalpts = 256; %How many MC evaluation points used to construct and sample the Copula model? should be a factor of 2
  22. Delay=500+((Rep-1)*Nevalpts); %This sets the location in the sobol sequence for QMC sampling

  23. %Set up variables required for MLE optimization
  24. if strcmp(CopulaModel,'Clayton')
  25.     model = 1;
  26. elseif strcmp(CopulaModel,'Frank')
  27.     model = 2;
  28. elseif strcmp(CopulaModel,'Gumbel')
  29.     model = 3;
  30. elseif strcmp(CopulaModel,'Gaussian')
  31.     model = 4;
  32. elseif strcmp(CopulaModel,'t')
  33.     model = 5;
  34. end

  35. %%Set up variables to evaluate each test case
  36. if testfunc == 1 %linear test case
  37.    Nvar = 6;
  38.    f = @(Xs) (1.5.*Xs(:,1)+1.6.*(Xs(:,2))+1.7.*(Xs(:,3))+1.8.*(Xs(:,4))+1.9.*(Xs(:,5))+2.*(Xs(:,6)));
  39.    p = sobolset(Nvar,'skip',Delay);
  40.    X = p(1:Nevalpts,:);
  41.    for j = 1:Nvar
  42.        Xs(:,j) = sqrt(2)*erfinv(2.*X(:,j)-1); %Model inputs
  43.    end
  44.   [Yn] = f(Xs);
  45.    Y = Yn; %Model output
  46. elseif testfunc == 2 %non-linear test case
  47.     Nvar = 6;
  48.     bi = [4.5,2.5,1.5,0.5,0.3,0.1];
  49.     f = @(x,bi) exp(sum(bi.*x,2))-prod((exp(bi)-1)./bi);
  50.     p = sobolset(Nvar,'skip',Delay);
  51.     X = p(1:Nevalpts,:);
  52.     for j = 1:Nevalpts
  53.         Yn(j,1) = exp(sum(bi.*X(j,:),2))-prod((exp(bi)-1)./bi);
  54.     end
  55.     Y = Yn; %model outputs
  56.     for j = 1:Nvar
  57.        Xs(:,j) = sqrt(2)*erfinv(2.*X(:,j)-1); %model inputs
  58.     end
  59. elseif testfunc == 3 %Ishigami test case (non-linear/non-monotonic)
  60.     Nvar = 3;
  61.     lb1 = repmat([-pi -pi -pi],[Nevalpts 1]);
  62.     ub1 = repmat([pi pi pi],[Nevalpts 1]);
  63.     %Parameters
  64.     a = 7; b = 0.1;
  65.     f = @(x) (sin(x(:,1))+ a.*(sin(x(:,2)).^2)+(b.*(x(:,3).^4).*sin(x(:,1))));
  66.     p = sobolset(Nvar,'skip',Delay);
  67.     X = p(1:Nevalpts,:);
  68.     Xss = lb1+((ub1-lb1).*X);
  69.     %Evaluate model
  70.     [Yn] = f(Xss);
  71.     Y = Yn; %model output
  72.     %Std normal space for Xs
  73.     for j = 1:Nvar
  74.         Xs(:,j) = sqrt(2)*erfinv(2.*X(:,j)-1); %model inputs
  75.     end
  76. end

  77. %%Estimate alpha values for the Rolling Pin method
  78. %using the maximum likelihood (MLE) optimization approach
  79. %Here, an external call to LSHADEEpSinNLS algorithm is performed
  80. %Note, on 2 cores this will likely take a bit of time, especially if
  81. %Nevalpts is high
  82. ry = Y;
  83. rx = Xs;
  84. Ngens = 20000; %This is the number of generations used for MLE optimization
  85. Npop = 50; %This is the number of population members used for MLE optimization
  86. nTol = 1E-6; %This is the tolerance used to moniter convergence
  87. paramL = zeros(1,Nvar+1); paramU = ones(1,Nvar+1); %boundaries of the alpha parameter values, Rolling Pin method
  88. paramU(1,1) = 0;
  89. x2{1,1} = ry; x2{1,2} = rx; x2{1,3} = model;
  90. [solset] = LSHADEpSinNLS(@MLEObjFuncFinalAll, Nvar+1, paramL, paramU, Npop, nTol, Ngens,x2); %Run optimization call
  91. [m,p] = find(nonzeros(solset.Xbest(:,2)));
  92. alpha = solset.Xbest(m(end),:); %Extract best alpha values for Rolling Pin method

  93. %%Semi-Parametric method begins here
  94. %Estimate Yi, transformed values, Equation 4
  95. for j = 1:Nvar
  96.     Yi(:,j) = (1-alpha(1,j+1)).*(ry(:,1))+alpha(1,j+1).*(rx(:,j));
  97. end

  98. %Find the PDFs and CDFs of the transformed distributions, Yi and Xs (Eqs.
  99. %5 and 6)
  100. parfor j = 1:Nvar
  101.    tic
  102.    pd1{j,1} = fitdist(Yi(:,j),'Kernel');
  103.    FYr(:,j) = cdf(pd1{j,1},Yi(:,j));
  104.    FX(:,j) = normcdf(rx(:,j),0,1);
  105.    toc
  106. end

  107. %Fit Elliptical or Archimidean parametric copula model here using MATLAB's built in
  108. %functions
  109. for j = 1:Nvar
  110.    u{j,1} = [FYr(:,j) FX(:,j)];
  111.    %Construct Parametric Copula from CDF matrix, t-distribution is a
  112.    %special case
  113.    if model == 5
  114.        [r{j,1},nus{j,1}] = copulafit(CopulaModel,u{j,1});
  115.    else
  116.        [r{j,1}] = copulafit(CopulaModel,u{j,1});
  117.    end
  118. end

  119. %Sample from constructed parametric copula model, using MATLAB's built in
  120. %functions
  121. parfor j = 1:Nvar
  122.     tic
  123.     if model == 5
  124.         cs = copularnd(CopulaModel,r{j,1},nus{j,1},Nevalpts);
  125.     else
  126.         cs = copularnd(CopulaModel,r{j,1},Nevalpts);
  127.     end
  128.     FYrn(:,j) = cs(:,1);
  129.     FXn(:,j) = cs(:,2);
  130.     Yin(:,j) = icdf(pd1{j,1},FYrn(:,j));
  131.     Xn(:,j) = norminv(FXn(:,j),0,1);
  132.     toc
  133. end

  134. %Find the marginal PDFs of the sampled dists, Yin and Xn (Eqs. 5 and 6)
  135. parfor j = 1:Nvar
  136.    tic
  137.    FYpdf1(:,j) = pdf(pd1{j,1},Yin(:,j));
  138.    Fpdf(:,j) = normpdf(Xn(:,j),0,1);
  139.    toc
  140. end

  141. %Transform the sample back to original space, inverse of Equation 4
  142. parfor j = 1:Nvar
  143.    Ys(:,j) = (Yin(:,j)-alpha(1,j+1).*Xn(:,j))./(1-alpha(1,j+1));
  144.    pd3{j,1} = fitdist(Ys(:,j),'Kernel');
  145.    FyPDF(:,j) = pdf(pd3{j,1},Ys(:,j));
  146. end

  147. %Evaluate the parametric copula density
  148. for j = 1:Nvar
  149.     %Evaluate joint pdf directly at pts
  150.     a = FYpdf1(:,j).*(1-alpha(1,j+1));
  151.     b = normpdf(Xn(:,j),0,1).*(1-alpha(1,1));
  152.     if model == 5
  153.         fXa1(:,j) = copulapdf(CopulaModel,[FYrn(:,j),FXn(:,j)],r{j,1},nus{j,1});
  154.     else
  155.         fXa1(:,j) = copulapdf(CopulaModel,[FYrn(:,j),FXn(:,j)],r{j,1});
  156.     end
  157.     fX(:,j) = fXa1(:,j).*a.*b; %Equation 7
  158. end

  159. %Estimate the MI sensitivity indices, Equation 8
  160. for j = 1:Nvar
  161.     deltast(:,j) = abs(((FyPDF(:,j).*Fpdf(:,j))./(fX(:,j)))-1);
  162.     delta(1,j) = 0.5*mean(deltast(:,j));
  163. end

  164. %Save results here
  165. filename = strcat(['SemiParamCopula_',CopulaModel,'_Test',num2str(testfunc),'_N',num2str(Nevalpts),'_Rep',num2str(Rep),'.mat']);
  166. save(filename,'delta','deltast','solset','Xs','Y','ry','rx');
复制代码



Semi-Parametric-Copula-Approach-master.zip (3.39 MB)

MixedVineToolbox-master.zip (41.03 KB)

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝


劳动经济学
沙发
781036048 学生认证  发表于 2019-9-24 18:51:21 |只看作者 |坛友微信交流群
谢谢分享!

使用道具

藤椅
joycelee727 发表于 2020-7-30 19:17:47 |只看作者 |坛友微信交流群
非常感謝您的分享!

使用道具

板凳
tulipsliu 在职认证  发表于 2020-7-31 18:25:01 |只看作者 |坛友微信交流群
joycelee727 发表于 2020-7-30 19:17
非常感謝您的分享!
能有用就好,这是源代码的。 要是你们能还创新就好了,修改作者的代码。哈哈

使用道具

报纸
wenchuceng6 发表于 2020-12-8 09:50:37 |只看作者 |坛友微信交流群
最近急用,非常感谢您的分享

使用道具

地板
tulipsliu 在职认证  发表于 2021-5-28 15:06:01 |只看作者 |坛友微信交流群
已经修改售价为 0

使用道具

7
xkt510587 发表于 2021-6-12 19:47:34 |只看作者 |坛友微信交流群
您好,能否加上代码解析,看不懂啊

使用道具

8
tulipsliu 在职认证  发表于 2021-6-13 06:53:17 |只看作者 |坛友微信交流群
xkt510587 发表于 2021-6-12 19:47
您好,能否加上代码解析,看不懂啊
不太明白你说的, 需要我加什么代码解析?

使用道具

9
leeyaya 在职认证  发表于 2021-6-18 16:33:32 来自手机 |只看作者 |坛友微信交流群
tulipsliu 发表于 2019-9-20 18:15
已经下载论坛很多书籍,大概6000论坛币已经剩下只有300左右吧,此贴不收金币。我太累,已经看不了那么多书。 ...
感谢分享!!!

使用道具

10
tulipsliu 在职认证  发表于 2021-6-18 20:13:27 |只看作者 |坛友微信交流群
leeyaya 发表于 2021-6-18 16:33
感谢分享!!!
客气了。

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-24 22:33