国际贸易对经济增长的影响模拟:构建一个开放经济模型,包含国内消费、投资、政府支出和进出口。消费函数为 C = a + b(Y - T),投资函数为 I = c + d(r),政府支出 G 为外生变量,出口函数为 X = e - fP(e 为自发出口,f 为出口对国内价格 P 的敏感度),进口函数为 M = g + hY(g 为自发进口,h 为进口对总产出的敏感度)。设定初始参数 a = 100,b = 0.8,c = 50,d = -200,r = 0.05,T = 100,G = 200,e = 80,f = 5,g = 30,h = 0.1,P = 1。模拟国际贸易政策变化(如提高进口关税使进口函数变为 M = g + 0.08Y,或促进出口使出口函数变为 X = e - 3P)对总产出、贸易余额和经济增长的影响,分析不同政策下的经济动态变化。
import tkinter as tk from tkinter import ttk import matplotlib.pyplot as plt from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg import numpy as np from matplotlib.figure import Figure # 设置matplotlib支持中文显示 plt.rcParams["font.family"] = ["SimHei", "WenQuanYi Micro Hei", "Heiti TC"] plt.rcParams["axes.unicode_minus"] = False # 解决负号显示问题 class EconomyModel: def __init__(self): # 模型参数 self.a = 100 # 自发消费 self.b = 0.8 # 边际消费倾向 self.c = 50 # 自发投资 self.d = -200 # 投资对利率的敏感度 self.r = 0.05 # 利率 self.T = 100 # 税收 self.G = 200 # 政府支出 self.e = 80 # 自发出口 self.f = 5 # 出口对价格的敏感度 self.g = 30 # 自发进口 self.h = 0.1 # 进口对收入的敏感度 self.P = 1 # 价格水平 # 模拟参数 self.time_steps = 30 self.current_step = 0 self.policy = "baseline" # baseline, tariff, export_promotion # 存储结果 self.Y_history = [] self.NX_history = [] self.GDP_growth_history = [] # 初始化模型 self.initialize_model() def initialize_model(self): # 计算初始均衡 self.calculate_equilibrium() # 初始化历史记录 self.Y_history = [self.calculate_equilibrium()] self.NX_history = [self.calculate_net_exports()] self.GDP_growth_history = [0] # 初始增长率为0 def calculate_equilibrium(self): # 计算均衡产出 Y = C + I + G + NX # NX = X - M # 代入消费函数和净出口函数 # Y = a + b(Y-T) + c + d*r + G + (e - f*P - (g + h*Y)) # 整理后得到 Y = [a + c + d*r + G + e - f*P - g - b*T] / (1 - b + h) numerator = self.a + self.c + self.d * self.r + self.G + self.e - self.f * self.P - self.g - self.b * self.T denominator = 1 - self.b + self.h return numerator / denominator def calculate_consumption(self): # 计算消费 C = a + b(Y-T) return self.a + self.b * (self.Y_history[-1] - self.T) def calculate_investment(self): # 计算投资 I = c + d*r return self.c + self.d * self.r def calculate_net_exports(self): # 计算净出口 NX = X - M exports = self.e - self.f * self.P # 根据当前政策调整进口函数 if self.policy == "tariff": imports = self.g + 0.08 * self.Y_history[-1] # 关税政策减少边际进口倾向 else: imports = self.g + self.h * self.Y_history[-1] # 根据当前政策调整出口函数 if self.policy == "export_promotion": exports = self.e - 3 * self.P # 出口促进政策降低价格敏感度 return exports - imports def update_policy(self, policy): self.policy = policy self.current_step = 0 self.initialize_model() def step(self): if self.current_step >= self.time_steps: return False # 计算当前步骤的均衡产出 Y = self.calculate_equilibrium() self.Y_history.append(Y) # 计算净出口 NX = self.calculate_net_exports() self.NX_history.append(NX) # 计算GDP增长率 if len(self.Y_history) >= 2: growth_rate = (Y / self.Y_history[-2] - 1) * 100 # 百分比 else: growth_rate = 0 self.GDP_growth_history.append(growth_rate) self.current_step += 1 return True class EconomySimulationApp: def __init__(self, root): self.root = root self.root.title("开放经济模型模拟") self.root.geometry("1200x600") self.model = EconomyModel() # 创建左侧图表区域 self.graph_frame = ttk.Frame(root, width=800, height=600) self.graph_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) # 创建右侧信息区域 self.info_frame = ttk.Frame(root, width=400, height=600, padding=10) self.info_frame.pack(side=tk.RIGHT, fill=tk.BOTH, expand=False) # 创建图表 self.create_charts() # 创建控制面板 self.create_control_panel() # 创建信息面板 self.create_info_panel() # 初始化图表 self.update_charts() def create_charts(self): # 创建三个图表 self.fig = Figure(figsize=(8, 10), dpi=100) # 总产出图表 self.ax1 = self.fig.add_subplot(311) self.ax1.set_title('总产出 (GDP)') self.ax1.set_xlabel('时间') self.ax1.set_ylabel('产出') self.ax1.grid(True) # 贸易余额图表 self.ax2 = self.fig.add_subplot(312) self.ax2.set_title('贸易余额') self.ax2.set_xlabel('时间') self.ax2.set_ylabel('净出口') self.ax2.grid(True) # GDP增长率图表 self.ax3 = self.fig.add_subplot(313) self.ax3.set_title('GDP增长率 (%)') self.ax3.set_xlabel('时间') self.ax3.set_ylabel('增长率') self.ax3.grid(True) # 将图表放置在Tkinter窗口中 self.canvas = FigureCanvasTkAgg(self.fig, master=self.graph_frame) self.canvas.draw() self.canvas.get_tk_widget().pack(fill=tk.BOTH, expand=True) def create_control_panel(self): control_frame = ttk.LabelFrame(self.info_frame, text="政策控制", padding=10) control_frame.pack(fill=tk.X, pady=10) # 政策选择 ttk.Label(control_frame, text="选择政策:").pack(anchor=tk.W) self.policy_var = tk.StringVar(value="baseline") policies = [ ("基准情景", "baseline"), ("提高进口关税", "tariff"), ("促进出口", "export_promotion") ] for text, value in policies: ttk.Radiobutton( control_frame, text=text, variable=self.policy_var, value=value, command=self.change_policy ).pack(anchor=tk.W, padx=10, pady=5) # 模拟控制按钮 button_frame = ttk.Frame(control_frame) button_frame.pack(fill=tk.X, pady=10) ttk.Button(button_frame, text="单步模拟", command=self.step_simulation).pack(side=tk.LEFT, padx=5) ttk.Button(button_frame, text="运行全部", command=self.run_simulation).pack(side=tk.LEFT, padx=5) ttk.Button(button_frame, text="重置", command=self.reset_simulation).pack(side=tk.LEFT, padx=5) def create_info_panel(self): # 当前经济指标 self.stats_frame = ttk.LabelFrame(self.info_frame, text="当前经济指标", padding=10) self.stats_frame.pack(fill=tk.X, pady=10) # 政策效果分析 self.analysis_frame = ttk.LabelFrame(self.info_frame, text="政策效果分析", padding=10) self.analysis_frame.pack(fill=tk.BOTH, expand=True, pady=10) self.update_info_panel() def update_info_panel(self): # 清空现有信息 for widget in self.stats_frame.winfo_children(): widget.destroy() for widget in self.analysis_frame.winfo_children(): widget.destroy() # 显示当前经济指标 if self.model.Y_history: current_step = len(self.model.Y_history) - 1 ttk.Label(self.stats_frame, text=f"时间: {current_step}").pack(anchor=tk.W) ttk.Label(self.stats_frame, text=f"总产出: {self.model.Y_history[-1]:.2f}").pack(anchor=tk.W) ttk.Label(self.stats_frame, text=f"贸易余额: {self.model.NX_history[-1]:.2f}").pack(anchor=tk.W) ttk.Label(self.stats_frame, text=f"GDP增长率: {self.model.GDP_growth_history[-1]:.2f}%").pack(anchor=tk.W) # 显示消费、投资、政府支出和净出口的构成 ttk.Separator(self.stats_frame).pack(fill=tk.X, pady=5) ttk.Label(self.stats_frame, text="支出构成:").pack(anchor=tk.W) ttk.Label(self.stats_frame, text=f"消费: {self.model.calculate_consumption():.2f}").pack(anchor=tk.W) ttk.Label(self.stats_frame, text=f"投资: {self.model.calculate_investment():.2f}").pack(anchor=tk.W) ttk.Label(self.stats_frame, text=f"政府支出: {self.model.G:.2f}").pack(anchor=tk.W) ttk.Label(self.stats_frame, text=f"净出口: {self.model.NX_history[-1]:.2f}").pack(anchor=tk.W) # 显示政策效果分析 if len(self.model.Y_history) > 1 and self.model.policy != "baseline": baseline = self.model.Y_history[0] current = self.model.Y_history[-1] change = current - baseline percent_change = (change / baseline) * 100 ttk.Label(self.analysis_frame, text=f"政策: {self.get_policy_name(self.model.policy)}").pack(anchor=tk.W) ttk.Label(self.analysis_frame, text=f"总产出变化: {change:.2f} ({percent_change:.2f}%)").pack(anchor=tk.W) baseline_nx = self.model.NX_history[0] current_nx = self.model.NX_history[-1] nx_change = current_nx - baseline_nx ttk.Label(self.analysis_frame, text=f"贸易余额变化: {nx_change:.2f}").pack(anchor=tk.W) # 分析政策影响 ttk.Separator(self.analysis_frame).pack(fill=tk.X, pady=5) ttk.Label(self.analysis_frame, text="政策影响分析:").pack(anchor=tk.W) if self.model.policy == "tariff": analysis = [ "提高进口关税减少了进口倾向,", "使得国内需求更多地转向国内产品,", "从而刺激了总产出增长。", "贸易余额通常会改善,因为进口减少。", "然而,这可能导致贸易伙伴的报复,", "长期来看可能损害经济效率。" ] elif self.model.policy == "export_promotion": analysis = [ "出口促进政策提高了出口竞争力,", "增加了国外对本国产品的需求,", "从而推动总产出增长。", "贸易余额通常会改善,因为出口增加。", "但可能面临贸易伙伴的反倾销措施。" ] for line in analysis: ttk.Label(self.analysis_frame, text=line).pack(anchor=tk.W) def get_policy_name(self, policy_code): policy_names = { "baseline": "基准情景", "tariff": "提高进口关税", "export_promotion": "促进出口" } return policy_names.get(policy_code, policy_code) def update_charts(self): # 清空图表 self.ax1.clear() self.ax2.clear() self.ax3.clear() # 设置标题和标签 self.ax1.set_title('总产出 (GDP)') self.ax1.set_xlabel('时间') self.ax1.set_ylabel('产出') self.ax1.grid(True) self.ax2.set_title('贸易余额') self.ax2.set_xlabel('时间') self.ax2.set_ylabel('净出口') self.ax2.grid(True) self.ax3.set_title('GDP增长率 (%)') self.ax3.set_xlabel('时间') self.ax3.set_ylabel('增长率') self.ax3.grid(True) # 绘制数据 if self.model.Y_history: time_steps = list(range(len(self.model.Y_history))) # 总产出图表 self.ax1.plot(time_steps, self.model.Y_history, 'b-', linewidth=2) # 贸易余额图表 self.ax2.plot(time_steps, self.model.NX_history, 'g-', linewidth=2) self.ax2.axhline(y=0, color='r', linestyle='--') # 添加零水平线 # GDP增长率图表 self.ax3.plot(time_steps, self.model.GDP_growth_history, 'r-', linewidth=2) self.ax3.axhline(y=0, color='k', linestyle='--') # 添加零水平线 # 调整布局 self.fig.tight_layout() # 刷新画布 self.canvas.draw() def change_policy(self): policy = self.policy_var.get() self.model.update_policy(policy) self.update_charts() self.update_info_panel() def step_simulation(self): if self.model.step(): self.update_charts() self.update_info_panel() else: print("已完成所有模拟步骤") def run_simulation(self): while self.model.step(): self.update_charts() self.update_info_panel() self.root.update_idletasks() # 更新UI self.root.after(100) # 暂停100毫秒,使动画可见 def reset_simulation(self): policy = self.policy_var.get() self.model.update_policy(policy) self.update_charts() self.update_info_panel() if __name__ == "__main__": root = tk.Tk() # 设置中文字体 default_font = tk.font.nametofont("TkDefaultFont") default_font.configure(family="SimHei", size=10) root.option_add("*Font", default_font) app = EconomySimulationApp(root) root.mainloop()