# \donttest{
#### an example with a Cobb-Douglas intertemporal utility function
np <- 5 # the number of internal periods, firms.
zeta <- 1.25 # the ratio of repayments to loans
S <- matrix(NA, 2 * np, np + 1)
S[(np + 1):(2 * np), np + 1] <- 100
B <- matrix(0, 2 * np, np + 1)
B[1:np, 1:np] <- diag(np)[, c(2:np, 1)]
B[1, np] <- 1 / zeta
dstl.firm <- list()
for (k in 1:np) {
dstl.firm[[k]] <- node_new(
"prod",
type = "CD", alpha = 2,
beta = c(0.5, 0.5),
paste0("lab", k), paste0("prod", k)
)
}
dst.consumer <- node_new(
"util",
type = "CD", alpha = 1,
beta = prop.table(rep(1, np)),
paste0("prod", 1:np)
)
ge <- sdm2(
A = c(dstl.firm, dst.consumer),
B = B,
S0Exg = S,
names.commodity = c(paste0("prod", 1:np), paste0("lab", 1:np)),
names.agent = c(paste0("firm", 1:np), "consumer"),
numeraire = "lab1",
ts = TRUE
)
ge$p
ge$z
ge$D
ge$S
ge$DV
ge$SV
## an example with a Leontief intertemporal utility function
dst.consumer <- node_new(
"util",
type = "Leontief",
a = rep(1, np),
paste0("prod", 1:np)
)
ge2 <- sdm2(
A = c(dstl.firm, dst.consumer),
B = B,
S0Exg = S,
names.commodity = c(paste0("prod", 1:np), paste0("lab", 1:np)),
names.agent = c(paste0("firm", 1:np), "consumer"),
numeraire = "lab1",
ts = TRUE
)
ge2$p
ge2$z
ge2$D
ge2$S
ge2$DV
ge2$SV
## Use a mean-value policy function to accelerate convergence.
ge3 <- sdm2(
A = c(dstl.firm, dst.consumer),
B = B,
S0Exg = S,
names.commodity = c(paste0("prod", 1:np), paste0("lab", 1:np)),
names.agent = c(paste0("firm", 1:np), "consumer"),
numeraire = "lab1",
ts = TRUE,
policy = makePolicyMeanValue(30)
)
#### an example with a linear intertemporal utility function (e.g. beta1*x1+beta2*x2)
## The demand structure of the consumer will be adjusted sluggishly to accelerate convergence.
np <- 5 # the number of internal periods, firms.
rho <- 0.9 # subjective discount factor
beta.consumer <- rep(rho ^ (0:(np - 1)))
zeta <- (1 / rho) ^ np
S <- matrix(NA, 2 * np, np + 1)
S[(np + 1):(2 * np), np + 1] <- 100
B <- matrix(0, 2 * np, np + 1)
B[1:np, 1:np] <- diag(np)[, c(2:np, 1)]
B[1, np] <- 1 / zeta
dstl.firm <- list()
for (k in 1:np) {
dstl.firm[[k]] <- node_new(
"prod",
type = "CD",
alpha = 2,
beta = c(0.5, 0.5),
paste0("lab", k),
paste0("prod", k)
)
}
dst.consumer <- node_new(
"util",
type = "FUNC",
last.a = rep(1, np),
func = function(p) {
value.marginal.utility <- beta.consumer / p
ratio <- value.marginal.utility / mean(value.marginal.utility)
a <- dst.consumer$last.a
a <- prop.table(a * ratio_adjust(ratio, 0.15))
dst.consumer$last.a <- a
a
},
paste0("prod", 1:np)
)
ge <- sdm2(
A = c(dstl.firm, dst.consumer),
B = B,
S0Exg = S,
names.commodity = c(paste0("prod", 1:np), paste0("lab", 1:np)),
names.agent = c(paste0("firm", 1:np), "consumer"),
numeraire = "lab1",
ts = TRUE,
priceAdjustmentVelocity = 0.1
)
ge$p
ge$z
ge$D
ge$S
growth_rate(ge$p[1:np])
growth_rate(ge$p[(np + 1):(2 * np)])
# }
Run the code above in your browser using DataLab