简体   繁体   English

建模冲击以最大化R

[英]Modeling Shocks to a Maximization in R

I am currently trying to write a code that will solve the consumption path over a 100x100 state space, subject to possible shocks in production. 我目前正在尝试编写代码来解决100x100状态空间上的消耗路径,这可能会对生产造成冲击。 I currently have 我目前有

 ###################################Part 3.1###################################################

nonpersist<-matrix(c(.5,.5,.5,.5),nrow=2,ncol=2) 
persist<-matrix(c(.9,.1,.3,.7),nrow=2,ncol=2) 



space<-seq(length=100, from=0, to=7.2) ##Create a sequence of 100 values ending at the                      point where value goes negative
alpha<-0.3 #take alpha as given
beta<-0.98 #take beta as given
vprime <- c(1:100) ##create a vector length 100 to be replaced later
t_vj <- c(1:100)  ##create a vector length 100 to be replaced later
A <- c(rep(4,100))
random<-c(runif(100))
iterater<-function(space){  ##create a function to perform the iteration
  for(i in 1:100){  
    for(j in 1:100){  
      if((A[i]*(space[i]^alpha)-space[j])<0){
          t_vj[j]=-99
      }
      else if(random[i]<.5){
              A[i]<-4
              t_vj[j+1] <- (log(A[i]*(space[i]^alpha)-space[j])+ beta*t_vj[j])
      }
      else{
              A[i]<-20
              t_vj[j+1] <- (log(A[i]*(space[i]^alpha)-space[j])+ beta*t_vj[j])
      }
    }
  }
  vprime[i]<-max(t_vj)  
  plot(space,vprime, xlab="State Space", ylab="Value")
}
iterater(space)  #call the function

Which unfortunately is currently yielding the graph http://i.stack.imgur.com/UXoUt.png 不幸的是,当前正在产生该图形http://i.stack.imgur.com/UXoUt.png

Which is not what we should expect in a non-linear function. 在非线性函数中这不是我们应该期望的。

Any ideas? 有任何想法吗?

Any help would be much appreciated. 任何帮助将非常感激。

You need 你需要

vprime[i] <- max(t_vj)

To appear within the first for loop. 出现在第一个for循环中。 Specifically, the last 3 lines of your iterater function should look like: 具体来说,您的iterater函数的最后3行应如下所示:

  vprime[i]<-max(t_vj)
}
plot(space,vprime, xlab="State Space", ylab="Value")

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM