# \donttest{
#### a time-circle pure exchange economy with two-period-lived consumers.
# In this example, each agent sells some labor to the previous
# generation and buys some labor from the next generation.
# Here np can tend to infinity.
np <- 4 # the number of planning periods and consumers (i.e. laborers).
lab.age1 <- 1 # labor endowment of age1
lab.age2 <- 0 # labor endowment of age2
S <- lab.age1 * diag(np) + lab.age2 * diag(np)[, c(2:np, 1)]
# Suppose each consumer has a utility function log(c1) + log(c2).
beta.consumer <- c(0.5, 0.5)
index <- c(1:np, 1)
dstl.consumer <- list()
for (k in 1:np) {
dstl.consumer[[k]] <- node_new(
"util",
type = "CD", alpha = 1,
beta = beta.consumer,
paste0("lab", index[k]), paste0("lab", index[k + 1])
)
}
ge <- sdm2(
A = dstl.consumer,
B = matrix(0, np, np, TRUE),
S0Exg = S,
names.commodity = paste0("lab", 1:np),
numeraire = "lab1"
)
ge$p
ge$D
ge$S
## Introduce population growth into the above pure exchange economy.
GRExg <- 0.03 # the population growth rate
S <- S %*% diag((1 + GRExg)^(0:(np - 1)))
S[1, np] <- S[1, np] * (1 + GRExg)^-np
dstl.consumer[[np]] <- node_new(
"util",
type = "CD", alpha = 1,
beta = beta.consumer,
paste0("lab", np), "cc1"
)
node_set(dstl.consumer[[np]],
"cc1",
type = "Leontief", a = (1 + GRExg)^(-np), # a discounting factor
"lab1"
)
ge <- sdm2(
A = dstl.consumer,
B = matrix(0, np, np, TRUE),
S0Exg = S,
names.commodity = paste0("lab", 1:np),
numeraire = "lab1"
)
ge$p
ge$D
ge$DV
ge$S
#### a time-circle pure exchange economy with three-period-lived consumers
# Suppose each consumer has a utility function log(c1) + log(c2) + log(c3).
# See gemOLGPureExchange_2_2.
np <- 5 # the number of planning periods and consumers (i.e. laborers).
lab.age1 <- 1 # labor endowment of age1
lab.age2 <- 1
lab.age3 <- 0
S <- lab.age1 * diag(np) + lab.age2 * diag(np)[, c(2:np, 1)] + lab.age3 * diag(np)[, c(3:np, 1:2)]
index <- c(1:np, 1, 2)
dstl.consumer <- list()
beta.consumer <- c(1 / 3, 1 / 3, 1 / 3)
for (k in 1:np) {
dstl.consumer[[k]] <- node_new(
"util",
type = "CD", alpha = 1,
beta = beta.consumer,
paste0("lab", index[k]), paste0("lab", index[k + 1]), paste0("lab", index[k + 2])
)
}
ge <- sdm2(
A = dstl.consumer,
B = matrix(0, np, np, TRUE),
S0Exg = S,
names.commodity = paste0("lab", 1:np),
numeraire = "lab1"
)
ge$p
ge$D
## Introduce population growth into the above pure exchange economy.
GRExg <- 0.03 # the population growth rate
S <- S %*% diag((1 + GRExg)^(0:(np - 1)))
S[1, np - 1] <- S[1, np - 1] * (1 + GRExg)^-np
S[1:2, np] <- S[1:2, np] * (1 + GRExg)^-np
dstl.consumer[[np - 1]] <- node_new(
"util",
type = "CD", alpha = 1,
beta = beta.consumer,
paste0("lab", np - 1), paste0("lab", np), "cc1"
)
node_set(dstl.consumer[[np - 1]], "cc1",
type = "Leontief", a = (1 + GRExg)^(-np), # a discounting factor
"lab1"
)
dstl.consumer[[np]] <- node_new(
"util",
type = "CD", alpha = 1,
beta = beta.consumer,
paste0("lab", np), "cc1", "cc2"
)
node_set(dstl.consumer[[np]], "cc1",
type = "Leontief", a = (1 + GRExg)^(-np), # a discounting factor
"lab1"
)
node_set(dstl.consumer[[np]], "cc2",
type = "Leontief", a = (1 + GRExg)^(-np), # a discounting factor
"lab2"
)
ge <- sdm2(
A = dstl.consumer,
B = matrix(0, np, np, TRUE),
S0Exg = S,
names.commodity = paste0("lab", 1:np),
numeraire = "lab1"
)
ge$p
growth_rate(ge$p) + 1 # 1 / (1 + GRExg)
ge$D
ge$DV
## Suppose each consumer has a Leontief-type utility function min(c1, c2, c3).
discount.factor <- c(1, 1 / (1 + GRExg), 1 / (1 + GRExg)^2)
beta.consumer <- prop.table(discount.factor)
lapply(dstl.consumer, function(dst) dst$beta <- beta.consumer)
ge <- sdm2(
A = dstl.consumer,
B = matrix(0, np, np, TRUE),
S0Exg = S,
names.commodity = paste0("lab", 1:np),
numeraire = "lab1"
)
ge$p
growth_rate(ge$p) + 1 # 1 / (1 + GRExg)
ge$D
ge$DV
#### a time-circle model with production and two-period-lived consumers
# Suppose each consumer has a utility function log(c1) + log(c2).
np <- 5 # the number of planning periods, consumers and firms.
S <- matrix(NA, 2 * np, 2 * np)
S.lab.consumer <- diag(1, np)
S[(np + 1):(2 * np), (np + 1):(2 * np)] <- S.lab.consumer
B <- matrix(0, 2 * np, 2 * np)
B[1:np, 1:np] <- diag(np)[, c(2:np, 1)]
dstl.firm <- list()
beta.firm <- c(1 / 3, 2 / 3)
for (k in 1:np) {
dstl.firm[[k]] <- node_new(
"prod",
type = "CD", alpha = 5,
beta = beta.firm,
paste0("lab", k), paste0("prod", k)
)
}
index <- c(1:np, 1)
beta.consumer <- c(1 / 2, 1 / 2)
dstl.consumer <- list()
for (k in 1:np) {
dstl.consumer[[k]] <- node_new(
"util",
type = "CD", alpha = 1,
beta = beta.consumer,
paste0("prod", index[k]), paste0("prod", index[k + 1])
)
}
ge <- sdm2(
A = c(dstl.firm, dstl.consumer),
B = B,
S0Exg = S,
names.commodity = c(paste0("prod", 1:np), paste0("lab", 1:np)),
names.agent = c(paste0("firm", 1:np), paste0("consumer", 1:np)),
numeraire = "lab1"
)
ge$p
ge$D
## Introduce population growth into the above economy.
GRExg <- 0.03 # the population growth rate
S[(np + 1):(2 * np), (np + 1):(2 * np)] <- diag((1 + GRExg)^(0:(np - 1)), np)
B[1,np] <- (1 + GRExg)^(-np)
dstl.consumer[[np]] <- node_new(
"util",
type = "CD", alpha = 1,
beta = beta.consumer,
paste0("prod", np),"cc1"
)
node_set(dstl.consumer[[np]], "cc1",
type = "Leontief", a = (1 + GRExg)^(-np), # a discounting factor
"prod1"
)
ge <- sdm2(
A = c(dstl.firm, dstl.consumer),
B = B,
S0Exg = S,
names.commodity = c(paste0("prod", 1:np), paste0("lab", 1:np)),
names.agent = c(paste0("firm", 1:np), paste0("consumer", 1:np)),
numeraire = "lab1",
policy = makePolicyMeanValue(30),
maxIteration = 1,
numberOfPeriods = 600,
ts = TRUE
)
ge$p
growth_rate(ge$p[1:np]) + 1 # 1 / (1 + GRExg)
growth_rate(ge$p[(np + 1):(2 * np)]) + 1 # 1 / (1 + GRExg)
ge$D
ge$DV
#### a time-circle model with production and one-period-lived consumers.
# These consumers also can be regarded as infinite-lived agents maximizing
# their per period utility subject to their disposable income per period.
np <- 5 # the number of planning periods, consumers and firms.
GRExg <- 0.03 # the population growth rate
discount.factor.last.output <- (1 + GRExg)^(-np)
S <- matrix(NA, 2 * np, 2 * np)
S.lab.consumer <- diag((1 + GRExg)^(0:(np - 1)), np)
S[(np + 1):(2 * np), (np + 1):(2 * np)] <- S.lab.consumer
B <- matrix(0, 2 * np, 2 * np)
B[1:np, 1:np] <- diag(np)[, c(2:np, 1)]
B[1, np] <- discount.factor.last.output
beta.firm <- c(1 / 3, 2 / 3)
beta.consumer <- c(1 / 2, 1 / 2)
dstl.firm <- list()
for (k in 1:np) {
dstl.firm[[k]] <- node_new(
"prod",
type = "CD", alpha = 5,
beta = beta.firm,
paste0("lab", k), paste0("prod", k)
)
}
dstl.consumer <- list()
for (k in 1:np) {
dstl.consumer[[k]] <- node_new(
"util",
type = "CD", alpha = 1,
beta = beta.consumer,
paste0("lab", k), paste0("prod", k)
)
}
ge <- sdm2(
A = c(dstl.firm, dstl.consumer),
B = B,
S0Exg = S,
names.commodity = c(paste0("prod", 1:np), paste0("lab", 1:np)),
names.agent = c(paste0("firm", 1:np), paste0("consumer", 1:np)),
numeraire = "lab1",
policy = makePolicyMeanValue(30),
maxIteration = 1,
numberOfPeriods = 600,
ts = TRUE
)
ge$p
growth_rate(ge$p[1:np]) + 1 # 1 / (1 + GRExg)
growth_rate(ge$p[(np + 1):(2 * np)]) + 1 # 1 / (1 + GRExg)
ge$D
## the reduced form of the above model.
dst.firm <- node_new(
"prod",
type = "CD", alpha = 5,
beta = beta.firm,
"lab", "prod"
)
dst.consumer <- node_new(
"util",
type = "CD", alpha = 1,
beta = beta.consumer,
"lab", "prod"
)
ge2 <- sdm2(
A = list(
dst.firm,
dst.consumer
),
B = matrix(c(
1, 0,
0, 0
), 2, 2, TRUE),
S0Exg = matrix(c(
NA, NA,
NA, 1
), 2, 2, TRUE),
names.commodity = c("prod", "lab"),
names.agent = c("firm", "consumer"),
numeraire = "lab",
GRExg = 0.03
)
ge2$p
ge2$D
#### a time-circle model with production and three-period-lived consumers.
# Suppose each consumer has a utility function log(c1) + log(c2) + log(c3).
np <- 6 # the number of planning periods, consumers and firms
GRExg <- 0.03 # the population growth rate
S <- matrix(NA, 2 * np, 2 * np)
S.lab.consumer <- rbind(diag((1 + GRExg)^(0:(np - 1)), np), 0) +
rbind(0, diag((1 + GRExg)^(0:(np - 1)), np))
S.lab.consumer <- S.lab.consumer[1:np, ]
S.lab.consumer[1, np] <- (1 + GRExg)^-1
S[(np + 1):(2 * np), (np + 1):(2 * np)] <- S.lab.consumer
B <- matrix(0, 2 * np, 2 * np)
B[1:np, 1:np] <- diag(np)[, c(2:np, 1)]
B[1, np] <- (1 + GRExg)^(-np)
beta.firm <- c(2 / 5, 3 / 5)
beta.consumer <- c(1 / 3, 1 / 3, 1 / 3)
dstl.firm <- list()
for (k in 1:np) {
dstl.firm[[k]] <- node_new(
"prod",
type = "CD", alpha = 5,
beta = beta.firm,
paste0("lab", k), paste0("prod", k)
)
}
dstl.consumer <- list()
for (k in 1:(np - 2)) {
dstl.consumer[[k]] <- node_new(
"util",
type = "CD", alpha = 1,
beta = beta.consumer,
paste0("prod", k), paste0("prod", k + 1), paste0("prod", k + 2)
)
}
dstl.consumer[[np - 1]] <- node_new(
"util",
type = "CD", alpha = 1,
beta = beta.consumer,
paste0("prod", np - 1), paste0("prod", np), "cc1"
)
node_set(dstl.consumer[[np - 1]], "cc1",
type = "Leontief", a = (1 + GRExg)^(-np), # a discounting factor
"prod1"
)
dstl.consumer[[np]] <- node_new(
"util",
type = "CD", alpha = 1,
beta = beta.consumer,
paste0("prod", np), "cc1", "cc2"
)
node_set(dstl.consumer[[np]], "cc1",
type = "Leontief", a = (1 + GRExg)^(-np), # a discounting factor
"prod1"
)
node_set(dstl.consumer[[np]], "cc2",
type = "Leontief", a = (1 + GRExg)^(-np), # a discounting factor
"prod2"
)
ge <- sdm2(
A = c(dstl.firm, dstl.consumer),
B = B,
S0Exg = S,
names.commodity = c(paste0("prod", 1:np), paste0("lab", 1:np)),
names.agent = c(paste0("firm", 1:np), paste0("consumer", 1:np)),
numeraire = "lab1",
policy = makePolicyMeanValue(30),
maxIteration = 1,
numberOfPeriods = 600,
ts = TRUE
)
ge$p
growth_rate(ge$p[1:np]) + 1 # 1 / (1 + GRExg)
growth_rate(ge$p[(np + 1):(2 * np)]) + 1 # 1 / (1 + GRExg)
ge$D
ge$DV
# }
Run the code above in your browser using DataLab