全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 R语言论坛
31254 2
2013-07-10
新手,修改程序时输出总显示错误,程序如下:
#----------------------------------------------------------------------------
# Define the moment equations
#----------------------------------------------------------------------------
meqn <- function(b,Rct,Rbt,Rst,zt) {
  cols <- ncol(zt)
  rows <- nrow(zt)
  
  beta <- b[1]       #  Relative risk aversion parameter   
  gam  <- b[2]       #  Discount parameter                  
  ut1 <- beta*(1+Rct)^(-gam) * (1 + Rbt) - 1
  ut2 <- beta*(1+Rct)^(-gam) * (1 + Rst) - 1
  ut<- cbind(ut1,ut2)
  k <- 2      
  mt <- array(0, c(rows,cols))  
  for (j in seq(cols)) {
    mt[,k] <- zt[,j]*ut1
    k <- k+1   
  }  
  return(mt)  
}
   
#----------------------------------------------------------------------------
# Defines the mean of the moment conditions  
#----------------------------------------------------------------------------
meaneqn <- function(b,Rct,Rbt,Rst,zt) {
  ret <- colMeans(meqn(b,Rct,Rbt,Rst,zt))
  return(ret)
}
#----------------------------------------------------------------------------
# GMM objective function which also computes the optimal w   
#----------------------------------------------------------------------------
q <- function(b,Rct,Rbt,Rst,zt,lmax){
  
  d <- meqn(b,Rct,Rbt,Rst,zt)
  g <- cbind(colMeans(d))
  
  w  <- t(1) %*% 1
  tau <- 1
  while (tau <= lmax) {
    wtau <- t( d[(tau+1):nrow(d),] ) %*% d[1:(nrow(d)-tau),]
    w    <- w + (1.0-tau/(lmax+1))*(wtau + t(wtau))
    tau  <- tau + 1   
  }  
  t <- length(Rct)
  ret <- t(g) %*% inv(w) %*% g
  
  return(ret)  
}
#
#----------------------- The equity_mp Based on Gmm --------------------
#
##gmm_ccapm <- function()
##{
load('equity_mp.Rdata')   
St<-data[,2]
Dt<-data[,3]
Ct<-data[,4]
Rt<-data[,5]
Pt<-data[,6]
Rst<-(trimr(St,1,0)+trimr(Dt,0,1)-trimr(St,0,1))/trimr(St,0,1)
Rbt<-(1+trimr(Rt,0,1))*(trimr(Pt,0,1)/trimr(Pt,1,0))-1
Rct<-(trimr(Ct,1,0)-trimr(Ct,0,1))/trimr(Ct,0,1)
ydata<-cbind(Rst,Rbt,Rct)
Rst<-ydata[,1]
Rbt<-ydata[,2]
Rct<-ydata[,3]
  
  t <- length(Rct)-1
  
  # Instruments <- {const,Rbtlag,Rstlag}
  zt <- cbind(rep(1, t),trimr(Rbt,0,1),trimr(Rst,0,1)
  
  estResults <- optim(q, Rct=Rct, Rbt=Rbt, Rst=Rst,zt=zt, lmax=0, method="BFGS")
  bgmm <- estResults$par  
  qmin <- estResults$value

二维码

扫码加我 拉你入群

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

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

全部回复
2013-7-12 00:42:02
(10) Risk Aversion and the Equity Premium Puzzle
下载 gmm_risk_aversion.R               下载 equity_mp.Rdata
http://www.cambridge.org/features/econmodelling/chapter10.htm

刚尝试运行了楼主的编码,阁下应该没有以下文件吧?
复制代码
二维码

扫码加我 拉你入群

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

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

2015-2-13 22:13:39
问题出在lmax上,
q <- function(b,Rct,Rbt,Rst,zt,lmax)
这个function貌似缺少lmax的内容
二维码

扫码加我 拉你入群

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

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

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群