0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Mesa3.xによる経済循環モデル

Last updated at Posted at 2025-03-02

はじめに

現代貨幣理論(MMT)は、よくモデルがないと批判される。
そこで、MultiAgentでMMTに則った経済循環モデルを作成しようと思う。
MultiAgentを書くのには、Mesa/Pythonが比較的簡単のようだ。
MultiAgentのプログラムを書くには、少々癖のある書法になる。
しかも、Mesa2.xからMesa3.xにバージョンアップで書き方が少し変わってくる。
Mesa3.xに慣れる意味もあるので、企業、政府のみの簡単なモデルをMesa3.1で構築する。

なお、以下のソースは、jupyter上で作動する。

Agentの作成

まずは、Mesaをimportして、MoneyAgentを作成する。
Agentにはunique_idが自動的に付くので、=1を政府、>1を企業とする。
最初に、企業は、最初に金貨0枚で、価格が金貨1万枚の商品を持つものとし、
政府は、100万枚の金貨を持ち、ランダムに企業を選定して、商品を買い上げる。
その際、売上の1/10を売上税として徴収するので、売上から税額を引いておく。

import mesa

class MoneyAgent(mesa.Agent):
    def __init__(self, model):
        super().__init__(model)
        self.tax    = 0
        self.wealth = 0
        if self.unique_id == 1:
            self.wealth = 1000000
    
    def exec(self):
        price = 10000
        if self.unique_id == 1:
            other_agent = self.random.choice(self.model.agents.select(lambda a: a.unique_id > 1))    
            if other_agent is not None:
                other_agent.wealth += price
                tax = price // 10
                other_agent.tax    += tax
                other_agent.wealth -= tax
                self.wealth -= price
        else:
            pass

モデルの作成

次に、モデルを作成する。num_agentsの数だけAgentが作成される。
step()により、Agentで定義されたexec()を実行する。
tax()により、各企業から売上より分けられていた税額を徴収する。

class MoneyModel(mesa.Model):
    def __init__(self, n):
        super().__init__()
        self.num_agents = n
        MoneyAgent.create_agents(model=self, n=n)

    def step(self):
        self.agents.do("exec")
        
    def tax(self):
        sum_tax = 0
        for a in model.agents:
            sum_tax += a.tax
            a.tax = 0
 
        for a in model.agents.select(lambda a: a.unique_id == 1):
            a.wealth += sum_tax 

実行

まず、Agentを10個作成するので、政府は1個、企業は9個出来る。
model.step() で、政府はランダムに選んだ企業から商品を買い上げる。
model.tax() で、政府は企業から税額を吸い上げる。

model = MoneyModel(10)
for _ in range(30):
    model.step()
    
print("徴税前")
print([a.wealth for a in model.agents])    # 金貨の枚数
print([a.tax    for a in model.agents])    # 税額
print("徴税後")
model.tax()
print([a.wealth for a in model.agents])    # 金貨の枚数
print([a.tax    for a in model.agents])    # 税額

徴税前
[700000, 63000, 18000, 9000, 18000, 81000, 27000, 9000, 27000, 18000]
[0, 7000, 2000, 1000, 2000, 9000, 3000, 1000, 3000, 2000]
徴税後
[730000, 63000, 18000, 9000, 18000, 81000, 27000, 9000, 27000, 18000]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

おわりに

Mesa3.xの日本語解説が殆どないので、お役に立てただろうか。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?