Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
def solve_model_time_iter(model, # Class with model information
σ, # Initial condition
def solve_model_time_iter(model, # 含有模型信息的类
σ, # 初始条件
tol=1e-4,
max_iter=1000,
verbose=True,
print_skip=25):

# Set up loop
# 设置迭代循环
i = 0
error = tol + 1

Expand All @@ -14,12 +14,12 @@ def solve_model_time_iter(model, # Class with model information
error = np.max(np.abs(σ - σ_new))
i += 1
if verbose and i % print_skip == 0:
print(f"Error at iteration {i} is {error}.")
print(f"{i} 次迭代的误差为 {error}")
σ = σ_new

if error > tol:
print("Failed to converge!")
print("未能收敛!")
elif verbose:
print(f"\nConverged in {i} iterations.")
print(f"\n在 {i} 次迭代后收敛。")

return σ_new
4 changes: 2 additions & 2 deletions lectures/_static/lecture_specific/optgrowth/cd_analytical.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

def v_star(y, α, β, μ):
"""
True value function
真实价值函数
"""
c1 = np.log(1 - α * β) / (1 - β)
c2 = (μ + α * np.log(α * β)) / (1 - α)
Expand All @@ -11,7 +11,7 @@ def v_star(y, α, β, μ):

def σ_star(y, α, β):
"""
True optimal policy
真实最优策略
"""
return (1 - α * β) * y

12 changes: 6 additions & 6 deletions lectures/_static/lecture_specific/optgrowth/solve_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ def solve_model(og,
verbose=True,
print_skip=25):
"""
Solve model by iterating with the Bellman operator.
通过迭代贝尔曼算子求解

"""

# Set up loop
v = og.u(og.grid) # Initial condition
# 设置迭代循环
v = og.u(og.grid) # 初始条件
i = 0
error = tol + 1

Expand All @@ -18,12 +18,12 @@ def solve_model(og,
error = np.max(np.abs(v - v_new))
i += 1
if verbose and i % print_skip == 0:
print(f"Error at iteration {i} is {error}.")
print(f"{i} 次迭代的误差为 {error}")
v = v_new

if error > tol:
print("Failed to converge!")
print("未能收敛!")
elif verbose:
print(f"\nConverged in {i} iterations.")
print(f"\n在 {i} 次迭代后收敛。")

return v_greedy, v_new
26 changes: 13 additions & 13 deletions lectures/_static/lecture_specific/optgrowth_fast/ogm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
from numba.experimental import jitclass

opt_growth_data = [
('α', float64), # Production parameter
('β', float64), # Discount factor
('μ', float64), # Shock location parameter
('s', float64), # Shock scale parameter
('grid', float64[:]), # Grid (array)
('shocks', float64[:]) # Shock draws (array)
('α', float64), # 生产参数
('β', float64), # 折现因子
('μ', float64), # 冲击的均值参数
('s', float64), # 冲击的尺度参数
('grid', float64[:]), # 网格(数组)
('shocks', float64[:]) # 冲击样本(数组)
]

@jitclass(opt_growth_data)
Expand All @@ -25,32 +25,32 @@ def __init__(self,

self.α, self.β, self.μ, self.s = α, β, μ, s

# Set up grid
# 设置网格
self.grid = np.linspace(1e-5, grid_max, grid_size)

# Store shocks (with a seed, so results are reproducible)
# 存储冲击(设置随机种子以确保结果可重复)
np.random.seed(seed)
self.shocks = np.exp(μ + s * np.random.randn(shock_size))


def f(self, k):
"The production function"
"生产函数"
return k**self.α


def u(self, c):
"The utility function"
"效用函数"
return np.log(c)

def f_prime(self, k):
"Derivative of f"
"生产函数的一阶导数"
return self.α * (k**(self.α - 1))


def u_prime(self, c):
"Derivative of u"
"效用函数的一阶导数"
return 1/c

def u_prime_inv(self, c):
"Inverse of u'"
"效用函数一阶导数的反函数"
return 1/c
30 changes: 15 additions & 15 deletions lectures/_static/lecture_specific/optgrowth_fast/ogm_crra.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
from numba.experimental import jitclass

opt_growth_data = [
('α', float64), # Production parameter
('β', float64), # Discount factor
('μ', float64), # Shock location parameter
('γ', float64), # Preference parameter
('s', float64), # Shock scale parameter
('grid', float64[:]), # Grid (array)
('shocks', float64[:]) # Shock draws (array)
('α', float64), # 生产参数
('β', float64), # 折现因子
('μ', float64), # 冲击的均值参数
('γ', float64), # 偏好参数
('s', float64), # 冲击的尺度参数
('grid', float64[:]), # 网格(数组)
('shocks', float64[:]) # 冲击样本(数组)
]

@jitclass(opt_growth_data)
Expand All @@ -27,29 +27,29 @@ def __init__(self,

self.α, self.β, self.γ, self.μ, self.s = α, β, γ, μ, s

# Set up grid
# 设置网格
self.grid = np.linspace(1e-5, grid_max, grid_size)

# Store shocks (with a seed, so results are reproducible)
# 存储冲击(设置随机种子以确保结果可重复)
np.random.seed(seed)
self.shocks = np.exp(μ + s * np.random.randn(shock_size))


def f(self, k):
"The production function."
"生产函数"
return k**self.α

def u(self, c):
"The utility function."
"效用函数"
return c**(1 - self.γ) / (1 - self.γ)

def f_prime(self, k):
"Derivative of f."
"生产函数的一阶导数"
return self.α * (k**(self.α - 1))

def u_prime(self, c):
"Derivative of u."
"效用函数的一阶导数"
return c**(-self.γ)

def u_prime_inv(c):
def u_prime_inv(self, c):
"效用函数一阶导数的反函数"
return c**(-1 / self.γ)
Loading