楼主: 飘零的枫叶
51969 206

[问答] 用lingo软件如何对DEA-DA模型进行编程 [推广有奖]

141
epoh 发表于 2011-8-3 19:35:55
# Load Data into vector y #
#y <- read.table("C:\\lhmu.dat")
#y <- as.vector(y)   #Wrong!!!
#class(y)   #data.frame
y <- read.table("lhmu.dat")
z=y[,1]
class(z)    #[1] "numeric"
length(z)   #585
z[1:10]
#[1] 1948.01 1996.07 1233.00 1281.00 1334.00
library(PerformanceAnalytics)
#calculate compound returns from prices
x=CalculateReturns(z)
x[1:10]
#0.024371909 -0.481730023  0.038190799  0.040540925
x=as.numeric(100*x)
class(x)    #[1] "numeric"
x[1:10]
#[1]   2.4371909 -48.1730023   3.8190799   4.0540925
length(x)   #584
## RUN THE SAMPLER (2 chains)
library(bayesGARCH)
MCMC <- bayesGARCH(x, control = list(n.chain = 2, l.chain = 2000))
## MCMC ANALYSIS (using coda)
plot(MCMC)
autocorr.diag(MCMC)
gelman.diag(MCMC)
1-rejectionRate(MCMC)
## FORM THE POSTERIOR SAMPLE
smpl <- formSmpl(MCMC, l.bi = 500)
## POSTERIOR STATISTICS
summary(smpl)
smpl <- as.matrix(smpl)
pairs(smpl)
## GARCH(1,1) WITH NORMAL INNOVATIONS
MCMC <- bayesGARCH(x, lambda = 100, delta = 500,
                     control = list(n.chain = 2, l.chain = 2000))
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
zhangtao + 5 + 5 + 5 非常非常非常感谢epoh导师的指导!

总评分: 学术水平 + 5  热心指数 + 5  信用等级 + 5   查看全部评分

142
zhangtao 发表于 2011-8-4 16:16:44
DSGE_v3.rar (648.22 KB)

epoh老师,您好!
          为什么我的dynarev3中的examples运行会提示以下错误:
??? Error using ==> mrdivide
Matrix dimensions must agree.
Error in ==> fs2000a_steadystate at 9
  gst = 1/dA;
??? Error: File: F:\0计量经济学前沿\Dynare 宏观工具箱\dynare_v3\examples\example5.m Line: 1 Column: 1
Missing variable or function.
??? Error: File: F:\0计量经济学前沿\Dynare 宏观工具箱\dynare_v3\examples\example1.m Line: 1 Column: 1
Missing variable or function.
??? Error: File: F:\0计量经济学前沿\Dynare 宏观工具箱\dynare_v3\examples\example2.m Line: 1 Column: 1
Missing variable or function.
??? Error: File: F:\0计量经济学前沿\Dynare 宏观工具箱\dynare_v3\examples\ramst.m Line: 15 Column: 1
Illegal use of reserved keyword "end".
??? Error: File: F:\0计量经济学前沿\Dynare 宏观工具箱\dynare_v3\examples\ramst.m Line: 15 Column: 1
Illegal use of reserved keyword "end".
>>
这些问题如何解决?程序在附件中,另外,DsgeVarLikelihood.m这个m文件如何调用?
非常感谢!

数学好就是要天天学

143
epoh 发表于 2011-8-4 19:59:08

你的安装方法错误

请参考底下安装Dynare Windows installer

及设好路径

执行时

也请参考Running and editing a Dynare example

  http://www.dynare.org/documentation-and-support/quick-start

%%%%%%%%%%%%%%%%%

Starting Dynare (version 4.2.1).
Starting preprocessing of the model file ...
Found 6 equation(s).
Evaluating expressions...done
......
......
THEORETICAL MOMENTS

VARIABLE    MEAN       STD. DEV.  VARIANCE   
y              1.0847     0.0897     0.0080
c              0.8065     0.0529     0.0028
k             11.1870     1.2603     1.5883
a              0.0000     0.0340     0.0012
h              0.2917     0.0119     0.0001
b              0.0000     0.0340     0.0012


VARIANCE DECOMPOSITION (in percent)

     e       u      
y     70.30   29.70
c     65.16   34.84
k     55.00   45.00
a     88.20   11.80
h     55.00   45.00
b     17.43   82.57


MATRIX OF CORRELATIONS

Variables    y        c        k        a        h        b        
y            1.0000  0.8742  0.8548  0.9563  0.6237  0.7773
c            0.8742  1.0000  0.9704  0.8396  0.1656  0.6138
k            0.8548  0.9704  1.0000  0.7504  0.1739  0.7504
a            0.9563  0.8396  0.7504  1.0000  0.5906  0.5627
h            0.6237  0.1656  0.1739  0.5906  1.0000  0.5906
b            0.7773  0.6138  0.7504  0.5627  0.5906  1.0000


COEFFICIENTS OF AUTOCORRELATION

Order    1       2       3       4       5      
y        0.9762  0.9530  0.9303  0.9081  0.8864
c        0.9949  0.9889  0.9819  0.9741  0.9656
k        0.9992  0.9971  0.9937  0.9891  0.9834
a        0.9641  0.9299  0.8973  0.8662  0.8365
h        0.9195  0.8442  0.7739  0.7082  0.6468
b        0.9641  0.9299  0.8973  0.8662  0.8365
Total computing time : 0h00m15s

已有 2 人评分学术水平 热心指数 信用等级 收起 理由
tawjuan + 1 + 1 + 1 有热心,有耐心,很执著,是个大好人!非常.
zhangtao + 5 + 5 + 5 非常感谢epoh导师的指导!

总评分: 学术水平 + 6  热心指数 + 6  信用等级 + 6   查看全部评分

144
zhangtao 发表于 2011-8-5 15:42:41
epoh 发表于 2011-8-4 19:59
你的安装方法错误请参考底下安装Dynare Windows installer 及设好路径执行时也请参考Running and editing a ...
jplv7spatialpanel.rar (56.75 KB)
epoh老师,您好!
      我运行jplv7中的spatial 下的panel中的demodynfeerror.m文件时提示以下错误:
??? Error: File: F:\0计量经济学前沿\000booksnmaterials\jplv7\spatial\panel\demodynfeerror.m Line: 324 Column: 1
Function definitions are not permitted at the prompt or in scripts.
您看问题在什么地方,如何解决?这个问题我经常碰到。
非常感谢!

数学好就是要天天学

145
zhangtao 发表于 2011-8-5 16:20:18
epoh 发表于 2011-7-28 19:01
%%%TVCmodel-final子文件
TVCmodel-final子文件内的文件
主要是供TVCestimate_Figures3_4.m
zhcopular.rar (75.65 KB)

epoh老师,您好!
         我运行附件中的程序,提示以下错误:
>> data=xlsread('stockindex.xls','B2:E57');
>> empiricalCDF(data,shindex)
??? Undefined function or variable 'shindex'.
>>
>> empiricalCDF(data,xx)
??? Undefined function or variable 'xx'.
>>
您看如何解决?
非常感谢!


数学好就是要天天学

146
epoh 发表于 2011-8-5 18:04:29

Function definitions are not permitted at the prompt or in scripts.

意思是script 里不能定义函数.

也就是原来demodynfeerror.m

要拆成9个文件

这跟R有很大的不同,使用时要小心.

demodynfeerror.m(pure script)
f_demodynerrorBS_del.m
f_demodynerrorBS_gam.m
......

......

demodynfeerror.rar

   demodynfeerror.rar (10.01 KB)


%%%%%%%%empiricalCDF
data=xlsread('stockindex.xls','B2:E57');
empiricalCDF(data)
ans =
    0.2807    0.3509    0.1754    0.5789
    0.8947    0.4737    0.3860    0.2982
    0.7719    0.4561    0.8246    0.8070

    ....................................

    ....................................

已有 1 人评分学术水平 热心指数 信用等级 收起 理由
zhangtao + 5 + 5 + 5 非常感谢epoh导师!

总评分: 学术水平 + 5  热心指数 + 5  信用等级 + 5   查看全部评分

147
zhangtao 发表于 2011-8-6 07:58:32
epoh 发表于 2011-8-5 18:04
Function definitions are not permitted at the prompt or in scripts.意思是script 里不能定义函数.也就是 ...
influence.rar (9.37 KB)

计算:iotalbecoe.xls中所有行的和除以最后一列的和。

例如:K2/sum(K2:K11)

K3/sum(K2:K11)

………

K11/ sum(K2:K11)

Epoh老师,您好!

以上这个计算在matlab中实现计算?要求输出为excel格式。

在附件中。我是在excel中实现计算的,当时我想,要是在matlab中能实现计算,
当然更好,但是我发现这个算法在matlab中要实现要做一个for循环,我做了好久没
做出来。我也想:要是出来的结果是excel格式那多好。但是我不知道在matlab中能不能
实现excel输出。
另外,我想问一下:在R中要输出为excel格式要用什么包或命令?
非常感谢epoh老师!

数学好就是要天天学

148
epoh 发表于 2011-8-6 14:08:02

data=xlsread('iotablecoe.xls','B2:K11');

size(data) % 10 10

           %你的数据有 10rows,10 columns

%你要取最后一个column,语法如下

data(:,10)   %in R data[,10]

%你要取第一个row,语法如下

data(1,:)    %in R data[1,]

%取总和

sum(data(1,:))

孰悉了这些指令

你可以自己来的.

别跟R的语法搞混就是了.

已有 1 人评分学术水平 热心指数 信用等级 收起 理由
zhangtao + 5 + 5 + 5 感谢epoh大师!

总评分: 学术水平 + 5  热心指数 + 5  信用等级 + 5   查看全部评分

149
zhangtao 发表于 2011-8-6 18:41:56
epoh 发表于 2011-8-6 14:08
data=xlsread('iotablecoe.xls','B2:K11');size(data) % 10 10           %你的数据有 10rows,10 columns%你 ...
T Bekk.rar (15.43 KB)
F:\0计量经济学前沿\000booksnmaterials\jplv7\Ucsd_garch\MV Garch\T Bekk中的diagonal_bekk_likelihood.m文件,
我实在弄不明白:这里的parameters,errors,p,q,k,k2,t这些参数如何设定?如何利用diagonal_bekk_likelihood.m文件
估计附件中的数据stockindex.xls?


function [LLF,likelihoods,Ht]=diagonal_bekk_likelihood(parameters,errors,p,q,k,k2,t);
% PURPOSE:
%      To Estimate a diagonal BEKK multivariate GARCH likelihood.
%
%
% USAGE:
%      [LLF,likelihoods,Ht]=diagonal_bekk_mvgarch_likelihood(parameters,errors,p,q,k,k2,t);
%
%
% INPUTS:
%      parameters - a k*(k+1)/2 + k*p +k*q vector of model parameters of the form
%                   [ivech(C);diag(A(1));...;diag(A(p));diag(B,); ...diag(B(q))]
%      errors     - A zeromean t by k martix of residuals
%      p          - The lag length of the innovation process
%      q          - The lag length of the AR process
%      k          - The number of data series
%      k2         - k*k
%      t          - the length of the data series
%
% OUTPUTS:
%      LLF           - The loglikelihood of the function at the optimum
%      Ht            - A k x k x t 3 dimension matrix of conditional covariances
%      likelihoods   - A t by 1 vector of individual likelihoods
%
% COMMENTS:
%
%
% Author: Kevin Sheppard
% kevin.sheppard@economics.ox.ac.uk
% Revision: 2    Date: 12/31/2001


%The first k(k+1)/2 parameters are C, the next p are A, and the next q are B
C=parameters(1:(k2));
A=parameters(k2+1:k2+k*p);
B=parameters(k2+k*p+1:k2+k*p+k*q);
nu=parameters(k2+k*p+k*q+1)^2+2.1;
tempA=zeros(k,k,p);
tempB=zeros(k,k,p);
for i=1:p
    tempA(:,:,i)=diag(A((k*(i-1)+1):(k*i)));
end
for i=1:q
    tempB(:,:,i)=diag(B((k*(i-1)+1):(k*i)));
end
A=tempA;
B=tempB;
C=ivech(C);
const=C*C';
uncond=cov(errors);
% for starting up, both ee' and H have expectation uncond.  We cna leverage thsi to help the loops.
m=max(p,q);
eeprime=zeros(k,k,t+m);
Ht=zeros(k,k,t+m);
for i=1:m
    eeprime(:,:,i)=uncond;
    Ht(:,:,i)=uncond;
end

LLF=0;
errors=[repmat(sqrt(diag(uncond))',m,1);errors];
likelihoods=zeros(t+m,1);
constant=(k/2)*(log(nu)-log(nu-2))+gammaln(0.5*(k+nu))-(k/2)*log(nu*pi)-gammaln(0.5*nu);
for i=m+1:t+m;
    Ht(:,:,i)=const;
    for j=1:p
         Ht(:,:,i)=Ht(:,:,i)+A(:,:,j)*(errors(i-j,:))'*(errors(i-j,:))*A(:,:,j)';
    end
    for j=1:q
         Ht(:,:,i)=Ht(:,:,i)+B(:,:,j)*Ht(:,:,i-j)*B(:,:,j)';
    end
    likelihoods(i)=constant-0.5*log(det(Ht(:,:,i))) - 0.5*(k+nu)*log(1+((1/(nu-2))*errors(i,:)*Ht(:,:,i)^(-1)*errors(i,:)'));
%    likelihoods(i)=k*log(2*pi)+(log(det(Ht(:,:,i)))+errors(i,:)*Ht(:,:,i)^(-1)*errors(i,:)');
    LLF=LLF+likelihoods(i);
end
LLF=-LLF;
likelihoods=-likelihoods(m+1:t+m);
Ht=Ht(:,:,m+1:t+m);
if isnan(LLF)
    LLF=1e6;
end
数学好就是要天天学

150
zhangtao 发表于 2011-8-6 19:25:54
epoh 发表于 2011-7-24 12:12
function u1cor = acf(amat,bmat,shockvar,yvar,dypos);
及impulse.m
由于作者并没对各种参数加予注解
epoh老师,您好!
     您设定的这些参数amat,bmat,shockvar,yvar,dypos为什么我在相关程序中没有找到?
我觉得理解这些参数的设定对调用这些参数至关重要。
相关程序在TVmodel文件夹中。

function u1cor = acf(amat,bmat,shockvar,yvar,dypos);
及impulse.m
由于作者并没对各种参数加予注解
所以不容易理解.
%%%%%%%%%%
function yvec1=impf(a,b,shock,nstep,neq);
%Proc to compute impulse response from system.
个参数设置如下:


amat=[   0         0         0    0.0494    0.0222         0         0         0         0         0 0 0 0 0 0 0 0 0;
         0         0         0    0.8588    0.3865         0         0         0         0         0 0 0 0 0 0 0 0 0;
         0         0         0   -0.0555   -0.0250         0         0         0         0         0 0 0 0 0 0 0 0 0;
         0         0         0    0.6730    0.3029         0         0         0         0         0 0 0 0 0 0 0 0 0;
         0         0         0   -0.3270   -0.1471         0         0         0         0         0 0 0 0 0 0 0 0 0;
         0         0         0    0.1483    0.0667         0         0         0         0         0 0 0 0 0 0 0 0 0;
         0         0         0    0.0494    0.0222         0         0         0         0         0 0 0 0 0 0 0 0 0;
         0         0         0         0         0         0         0         0         0         0 0 0 0 0 0 0 0 0;
         0         0         0         0         0         0         0         0         0         0 0 0 0 0 0 0 0 0;
    1.0000         0         0         0         0         0         0         0         0         0 0 0 0 0 0 0 0 0;
         0    1.0000         0         0         0         0         0         0         0         0 0 0 0 0 0 0 0 0;
         0         0    1.0000         0         0         0         0         0         0         0 0 0 0 0 0 0 0 0;
         0         0         0    1.0000         0         0         0         0         0         0 0 0 0 0 0 0 0 0;
         0         0         0         0    1.0000         0         0         0         0         0 0 0 0 0 0 0 0 0;
         0         0         0         0         0    1.0000         0         0         0         0 0 0 0 0 0 0 0 0;
         0         0         0         0         0         0    1.0000         0         0         0 0 0 0 0 0 0 0 0;
         0         0         0         0         0         0         0    1.0000         0         0 0 0 0 0 0 0 0 0;
         0         0         0         0         0         0         0         0    1.0000         0 0 0 0 0 0 0 0 0];


b =[-0.0423   -0.0423    0.0423    0.0682    0.0494    0.0727         0   -0.0727   -0.0494
    0.3068   -0.6932   -0.3068    1.1956    0.8588    1.8911         0   -1.8911   -0.8588
    0.0885    0.0885   -1.0885   -0.0655   -0.0555    0.5458         0   -0.5458    0.0555
    0.0974    0.0974   -0.0974    0.6107    0.6730    0.6002         0   -0.6002   -0.6730
    0.0974    0.0974   -0.0974    0.6107   -0.3270    0.6002         0   -0.6002    0.3270
   -0.1268   -0.1268    0.1268    0.2046    0.1483   -0.7818         0    0.7818   -0.1483
   -0.0423   -0.0423    0.0423    0.0682    0.0494    0.0727   -1.0000   -0.0727   -0.0494
         0         0         0         0         0         0         0    1.0000         0
         0         0         0         0         0         0         0         0    1.0000
         0         0         0         0         0         0         0         0         0
         0         0         0         0         0         0         0         0         0
         0         0         0         0         0         0         0         0         0
         0         0         0         0         0         0         0         0         0
         0         0         0         0         0         0         0         0         0
         0         0         0         0         0         0         0         0         0
         0         0         0         0         0         0         0         0         0
         0         0         0         0         0         0         0         0         0
         0         0         0         0         0         0         0         0         0];

shock =[0;0;0;0;0;0;0;0;1];
     
neq = 9;

imptech = impf(amat,b,shock,20,neq);
imptech
pipos   =   1;
dpos   =   2;
epos   =   3;
xpos   =   4;
gypos  =   5;
rpos   =   6;
dppos  =   7;
erpos =   8;
eapos =   9;
IRF=[imptech(:,pipos) imptech(:,xpos) imptech(:,gypos) imptech(:,rpos)];
IRF

本文来自: 人大经济论坛 Matlab及其他计量软件专版 版,详细出处参考: https://bbs.pinggu.org/forum.php? ... age=9&from^^uid=11232
数学好就是要天天学

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-2-3 11:52