Posted By


stevejb on 03/24/11

Tagged


Statistics


Viewed 159 times
Favorited by 0 user(s)

simulateMoments.R


/ Published in: R
Save to your folder(s)



Copy this code and paste it in your HTML
  1. #
  2. #
  3. # Stephen J. Barr
  4. #
  5. # get moments from the simulated data
  6. #
  7. #
  8.  
  9. #
  10. # derivative
  11. #
  12.  
  13. require(moments)
  14.  
  15. getMomentsFromSim <- function(mySim, moment.par) {
  16.  
  17. # there are 50 time periods. Drop first 10
  18. T <- 50
  19. Tsub <- T - 10;
  20. N <- moment.par$N
  21. STATEMAT <- moment.par$STATEMAT
  22. ValFn <- moment.par$VALFN
  23. sumStat <- matrix(nrow=(Tsub*N), ncol=2);
  24. KLDmat <- matrix(nrow=(Tsub*N), ncol=3)
  25. colnames(sumStat) <- c("investment", "tobinsq")
  26. colnames(KLDmat) <- c("K", "L", "D")
  27. KM <- 1
  28. LM <- 2
  29. DM <- 3
  30. delta <- .15
  31.  
  32. for(i in 1:N) {
  33. # bellmanSubset <- bellmanEQ[(length(S)*(i-1) + 1):(i*length(S)),]
  34. startIdx <- (Tsub*(i-1) + 1)
  35. endIdx <- Tsub * i
  36. POLICY <- mySim[[i]]$KLDVAR
  37. ZTMP <- mySim[[i]]$Z
  38.  
  39. kprimeTMP <- STATEMAT[POLICY[(T-Tsub+2):T], KM]
  40. kTMP <- STATEMAT[POLICY[(T-Tsub+1):(T-1)], KM]
  41. investTMP <- (kprimeTMP - (1-delta)*kTMP)/kTMP;
  42. sumStat[(startIdx+1):endIdx, 1] <- investTMP;
  43.  
  44. kt1 <- STATEMAT[POLICY[(T-Tsub):(T-1)], KM]
  45. lt1 <- STATEMAT[POLICY[(T-Tsub):(T-1)], LM]
  46. dt1 <- STATEMAT[POLICY[(T-Tsub):(T-1)], DM]
  47.  
  48. KLDmat[startIdx:endIdx,KM] <- kt1
  49. KLDmat[startIdx:endIdx,LM] <- lt1
  50. KLDmat[startIdx:endIdx,DM] <- dt1
  51.  
  52. # calculate Tobin's Q
  53. count <- 0
  54. for(j in (T-Tsub+1):T) {
  55. # val function at j
  56. vfj <- ValFn[ZTMP[j], POLICY[j]]
  57. kj <- STATEMAT[POLICY[j], KM]
  58. tqTmp <- vfj/kj
  59. sumStat[(startIdx + count), 2] <- tqTmp;
  60. count <- count+1
  61. } # end for j
  62. } # end for N
  63. data.filtered <- as.data.frame(na.omit(sumStat))
  64. simRes <- colMeans(data.filtered)
  65. ## colMeans(na.omit(sumStat))
  66. ## investment tobinsq
  67. ## 0.2099869 4.7489438
  68. partEreg <- lm(investment ~ tobinsq, data=data.filtered)
  69. sdvec <- apply(KLDmat, 2, function(x) {moment(x,order=2)})
  70. sdc <- apply(KLDmat, 2, function(x) {moment(x,order=2, central=TRUE)})
  71. thirdmoment <- apply(KLDmat, 2, function(x) {moment(x,order=3)})
  72. thirdmoment.d <- apply(KLDmat, 2, function(x) {moment(x,order=3, central=TRUE)})
  73. momentVec <- c(simRes, coefficients(partEreg)[2], sdvec, sdc, thirdmoment, thirdmoment.d)
  74. return(momentVec)
  75. }
  76. ###########################################################3
  77. #
  78. # MAIN:
  79. #
  80. #
  81. # I have to do bit of regeneration here, but should be alright
  82. #
  83. #
  84. # the results are in NEILDIAMOND
  85. # the model list is in MODELLIST.Robj
  86. #
  87. # all I need to do is the following
  88. load("../data/MODELLIST.Robj")
  89. load("../data/RESULTLIST.Robj")
  90. # there should be 834 elements in each of these lists
  91. #
  92. #
  93. require(doMC)
  94. registerDoMC()
  95. momentmat <- matrix(nrow=15, ncol=length(modellist))
  96. mm <- foreach(d = 1:length(modellist), .combine=cbind) %dopar% {
  97. cat(paste("Starting model:", d, "\n"))
  98. MODEL.TMP <- modellist[[d]]
  99. RESULTS.TMP <- NEILDIAMOND[[d]]
  100. # construct the paramters necessary to get the moments
  101. moment.par <- list(STATEMAT = MODEL.TMP[[1]]$STATEMATRIX,
  102. VALFN = MODEL.TMP[[2]]$VALUEFN,
  103. N=10000)
  104. mymoments <- getMomentsFromSim(RESULTS.TMP, moment.par)
  105. momentmat[,d] <- momentVec
  106. cat(paste("FINISHED model:", d, "!!! I love Jo !!\n"))
  107. }
  108. filename <- paste(format(Sys.time(), "%s"), "-momentsmat.Robj", sep="")
  109. save(momentmat, file=filename)

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.