# NOT RUN {
#### a time-circle pure exchange economy with three-period-lived consumers
## See gemOLGPureExchange_2_2.
n <- 5 # the number of periods and consumers (i.e. laborers).
lab.age1 <- 1 # labor endowment of age1
lab.age2 <- 1
lab.age3 <- 0
S <- lab.age1 * diag(n) + lab.age2 * diag(n)[, c(2:n, 1)] + lab.age3 * diag(n)[, c(3:n, 1:2)]
index <- c(1:n, 1, 2)
dstl.consumer <- list()
for (k in 1:n) {
dstl.consumer[[k]] <- node_new(
"util",
type = "CD", alpha = 1,
beta = c(1 / 6, 2 / 6, 3 / 6),
paste0("lab", index[k]), paste0("lab", index[k + 1]), paste0("lab", index[k + 2])
)
}
ge <- sdm2(
A = dstl.consumer,
B = matrix(0, n, n, TRUE),
S0Exg = S,
names.commodity = paste0("lab", 1:n),
numeraire = "lab1"
)
ge$p
ge$D
#### Introduce population growth into the above economy.
n <- 5 # the number of periods and consumers.
lab.age1 <- 1
lab.age2 <- 1
lab.age3 <- 0
S <- lab.age1 * diag(n) + lab.age2 * diag(n)[, c(2:n, 1)] + lab.age3 * diag(n)[, c(3:n, 1:2)]
GRExg <- 0.03 # the population growth rate
S <- S %*% diag((1 + GRExg)^(0:(n - 1)))
S[1, n - 1] <- S[1, n - 1] * (1 + GRExg)^-n
S[1:2, n] <- S[1:2, n] * (1 + GRExg)^-n
dstl.consumer <- list()
beta.consumer <- c(1 / 6, 2 / 6, 3 / 6)
for (k in 1:(n - 2)) {
dstl.consumer[[k]] <- node_new(
"util",
type = "CD", alpha = 1,
beta = beta.consumer,
paste0("lab", k), paste0("lab", k + 1), paste0("lab", k + 2)
)
}
dstl.consumer[[n - 1]] <- node_new(
"util",
type = "CD", alpha = 1,
beta = beta.consumer,
paste0("lab", n - 1), paste0("lab", n), "cc1"
)
node_set(dstl.consumer[[n - 1]], "cc1",
type = "Leontief", a = (1 + GRExg)^(-n), # a discounting factor
"lab1"
)
dstl.consumer[[n]] <- node_new(
"util",
type = "CD", alpha = 1,
beta = beta.consumer,
paste0("lab", n), "cc1", "cc2"
)
node_set(dstl.consumer[[n]], "cc1",
type = "Leontief", a = (1 + GRExg)^(-n), # a discounting factor
"lab1"
)
node_set(dstl.consumer[[n]], "cc2",
type = "Leontief", a = (1 + GRExg)^(-n), # a discounting factor
"lab2"
)
ge <- sdm2(
A = dstl.consumer,
B = matrix(0, n, n, TRUE),
S0Exg = S,
names.commodity = paste0("lab", 1:n),
numeraire = "lab1"
)
ge$p
diff(log(ge$p)) # -log(1 + GRExg)
ge$D
#### a time-circle model with production and two-period-lived consumers
n <- 5 # the number of periods, consumers and firms.
S <- matrix(NA, 2 * n, 2 * n)
S.lab.consumer <- diag(1, n)
S[(n + 1):(2 * n), (n + 1):(2 * n)] <- S.lab.consumer
B <- matrix(0, 2 * n, 2 * n)
B[1:n, 1:n] <- diag(n)[, c(2:n, 1)]
dstl.firm <- list()
for (k in 1:n) {
dstl.firm[[k]] <- node_new(
"prod",
type = "CD", alpha = 5,
beta = c(1 / 3, 2 / 3),
paste0("lab", k), paste0("prod", k)
)
}
index <- c(1:n, 1)
dstl.consumer <- list()
for (k in 1:n) {
dstl.consumer[[k]] <- node_new(
"util",
type = "CD", alpha = 1,
beta = c(1 / 3, 2 / 3),
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:n), paste0("lab", 1:n)),
names.agent = c(paste0("firm", 1:n), paste0("consumer", 1:n)),
numeraire = "lab1"
)
ge$p
ge$D
#### Introduce population growth into the above economy.
n <- 5 # the number of periods, consumers and firms.
GRExg <- 0.03 # the population growth rate
S <- matrix(NA, 2 * n, 2 * n)
S.lab.consumer <- diag((1 + GRExg)^(0:(n - 1)), n)
S[(n + 1):(2 * n), (n + 1):(2 * n)] <- S.lab.consumer
B <- matrix(0, 2 * n, 2 * n)
B[1:n, 1:n] <- diag(n)[, c(2:n, 1)]
B[1,n] <- (1 + GRExg)^(-n)
beta.firm <- c(3 / 5, 2 / 5)
beta.consumer <- c(1 / 3, 2 / 3)
dstl.firm <- list()
for (k in 1:n) {
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:(n-1)) {
dstl.consumer[[k]] <- node_new(
"util",
type = "CD", alpha = 1,
beta = beta.consumer,
paste0("prod", k), paste0("prod", k + 1)
)
}
dstl.consumer[[n]] <- node_new(
"util",
type = "CD", alpha = 1,
beta = beta.consumer,
paste0("prod", n),"cc1"
)
node_set(dstl.consumer[[n]], "cc1",
type = "Leontief", a = (1 + GRExg)^(-n), # a discounting factor
"prod1"
)
ge <- sdm2(
A = c(dstl.firm, dstl.consumer),
B = B,
S0Exg = S,
names.commodity = c(paste0("prod", 1:n), paste0("lab", 1:n)),
names.agent = c(paste0("firm", 1:n), paste0("consumer", 1:n)),
numeraire = "lab1",
policy = makePolicyMeanValue(30),
maxIteration = 1,
numberOfPeriods = 600,
ts = TRUE
)
ge$p
diff(log(ge$p[1:n]))
diff(log(ge$p[(n + 1):(2 * n)]))
ge$D
#### a time-circle model with production and one-period-lived consumers
## These consumers also can be regarded as infinite-lived carpe diem agents,
## who have endowments in each period, maximize the instantaneous utility and do not
## consider the future.
n <- 5 # the number of periods, consumers and firms.
GRExg <- 0.03 # the population growth rate
discount.factor.last.output <- (1 + GRExg)^(-n)
S <- matrix(NA, 2 * n, 2 * n)
S.lab.consumer <- diag((1 + GRExg)^(0:(n - 1)), n)
S[(n + 1):(2 * n), (n + 1):(2 * n)] <- S.lab.consumer
B <- matrix(0, 2 * n, 2 * n)
B[1:n, 1:n] <- diag(n)[, c(2:n, 1)]
B[1, n] <- discount.factor.last.output
beta.firm <- c(3 / 5, 2 / 5)
beta.consumer <- c(1 / 3, 2 / 3)
dstl.firm <- list()
for (k in 1:n) {
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:n) {
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:n), paste0("lab", 1:n)),
names.agent = c(paste0("firm", 1:n), paste0("consumer", 1:n)),
numeraire = "lab1",
policy = makePolicyMeanValue(30),
maxIteration = 1,
numberOfPeriods = 600,
ts = TRUE
)
ge$p
diff(log(ge$p[1:n]))
diff(log(ge$p[(n + 1):(2 * n)]))
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
n <- 6 # the number of periods, consumers and firms.
GRExg <- 0.03 # the population growth rate
S <- matrix(NA, 2 * n, 2 * n)
S.lab.consumer <- rbind(diag((1 + GRExg)^(0:(n - 1)), n), 0) +
rbind(0, diag((1 + GRExg)^(0:(n - 1)), n))
S.lab.consumer <- S.lab.consumer[1:n, ]
S.lab.consumer[1, 6] <- (1 + GRExg)^-1
S[(n + 1):(2 * n), (n + 1):(2 * n)] <- S.lab.consumer
B <- matrix(0, 2 * n, 2 * n)
B[1:n, 1:n] <- diag(n)[, c(2:n, 1)]
B[1, n] <- (1 + GRExg)^(-n)
beta.firm <- c(2 / 5, 3 / 5)
beta.consumer <- c(1 / 6, 3 / 6, 2 / 6)
dstl.firm <- list()
for (k in 1:n) {
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:(n - 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[[n - 1]] <- node_new(
"util",
type = "CD", alpha = 1,
beta = beta.consumer,
paste0("prod", n - 1), paste0("prod", n), "cc1"
)
node_set(dstl.consumer[[n - 1]], "cc1",
type = "Leontief", a = (1 + GRExg)^(-n), # a discounting factor
"prod1"
)
dstl.consumer[[n]] <- node_new(
"util",
type = "CD", alpha = 1,
beta = beta.consumer,
paste0("prod", n), "cc1", "cc2"
)
node_set(dstl.consumer[[n]], "cc1",
type = "Leontief", a = (1 + GRExg)^(-n), # a discounting factor
"prod1"
)
node_set(dstl.consumer[[n]], "cc2",
type = "Leontief", a = (1 + GRExg)^(-n), # a discounting factor
"prod2"
)
ge <- sdm2(
A = c(dstl.firm, dstl.consumer),
B = B,
S0Exg = S,
names.commodity = c(paste0("prod", 1:n), paste0("lab", 1:n)),
names.agent = c(paste0("firm", 1:n), paste0("consumer", 1:n)),
numeraire = "lab1",
policy = makePolicyMeanValue(30),
maxIteration = 1,
numberOfPeriods = 600,
ts = TRUE
)
ge$p
diff(log(ge$p[1:n]))
diff(log(ge$p[(n + 1):(2 * n)]))
ge$D
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab