
Nashpyを使ってレプリケータダイナミクス
タイトルの通り、Nashpyを使ってレプリケータダイナミクスを見ます。
利得表はゲーム理論(岡田章, 1999)から引用しています。
参考
Nashpy
前提
Python3.9で実行しています。
グラフ描写用に
matplotlibがインストールされていること。
python -m pip install matplotlib
numpyがインストールされていること。
python -m pip install numpy
nashpyがインストールされていること。
python -m pip install nashpy
サンプルコード
タカハト
import matplotlib.pyplot as plt
import numpy as np
import nashpy as nash
# ゲームのペイオフ行列を定義
# | 戦略1 | 戦略2
# -------------------
# 戦略1 | -5, -5 | 10, 0
# 戦略2 | 0, 10 | 5, 5
A = np.array([[-5, 10],
[0, 5]])
B = np.array([[-5, 0],
[10, 5]])
# プレイヤー1とプレイヤー2のペイオフ行列を使用してゲームを定義
game = nash.Game(A, B)
# 初期分布
init_distribution = np.array([0.01, 0.99])
timepoints = np.linspace(0, 10, 1000)
# 描画用
time = np.linspace(0, 999, 1000)
x_values = []
y_values = []
# 実行
result = game.replicator_dynamics(y0=init_distribution, timepoints=timepoints)
for t in time:
x_values.append(result[int(t)][0])
y_values.append(result[int(t)][1])
# 結果のプロット
plt.plot(timepoints, x_values, label='Strategy 1 Frequency')
plt.plot(timepoints, y_values, label='Strategy 2 Frequency')
plt.xlabel('Time')
plt.ylabel('Strategy Frequency')
plt.legend()
plt.title('Replicator Dynamics')
plt.show()
実行結果

技術標準の選択ゲーム
import matplotlib.pyplot as plt
import numpy as np
import nashpy as nash
# ゲームのペイオフ行列を定義
# | 戦略1 | 戦略2
# -------------------
# 戦略1 | 2, 2 | 0, 0
# 戦略2 | 0, 0 | 4, 4
A = np.array([[2, 0],
[0, 4]])
B = np.array([[2, 0],
[0, 4]])
# プレイヤー1とプレイヤー2のペイオフ行列を使用してゲームを定義
game = nash.Game(A, B)
# 初期分布
init_distribution = np.array([0.67, 0.33]) #Strategy1 が優勢になる
#init_distribution = np.array([0.66, 0.34]) #Strategy2 が優勢になる
timepoints = np.linspace(0, 10, 1000)
# 描画用
time = np.linspace(0, 999, 1000)
x_values = []
y_values = []
# 実行
result = game.replicator_dynamics(y0=init_distribution, timepoints=timepoints)
for t in time:
x_values.append(result[int(t)][0])
y_values.append(result[int(t)][1])
# 結果のプロット
plt.plot(timepoints, x_values, label='Strategy 1 Frequency')
plt.plot(timepoints, y_values, label='Strategy 2 Frequency')
plt.xlabel('Time')
plt.ylabel('Strategy Frequency')
plt.legend()
plt.title('Replicator Dynamics')
plt.show()
実行結果
初期分布について, 戦略1の分布が2/3を超えてる場合は戦略1優勢となる

初期分布について, 戦略1の分布が2/3を下回ってる場合は戦略2優勢となる

いいなと思ったら応援しよう!
