-
Notifications
You must be signed in to change notification settings - Fork 0
/
KMR.py
162 lines (120 loc) · 4.86 KB
/
KMR.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
from __future__ import division
import numpy as np
from scipy.stats import binom
import matplotlib.pyplot as plt
from mc_tools import mc_compute_stationary, mc_sample_path
"""
This file is based on D.Oyama's class file, KMR_2x2.
"""
"""
Parameters for KMR game.
N; number of players.
p; number of threshhold to take an action 1 for best response.
epsilon; probability of random choice (experiment, mutation).
x; number of agents playing action 1.
"""
"""
(1) Simultenious revisions.
"""
def KMR_2x2_P_simultaneous(N, p, epsilon):
P = np.empty((N+1, N+1), dtype=float)
for x in range(N+1):
P[x,:] = /
(x/N < p) * binom.pmf(range(N+1), N, epsilon/2) + /
(x/N == p) * binom.pmf(range(N+1), N, 1/2) + /
(x/N > p) * binom.pmf(range(N+1), N, 1-epsilon/2) +/
return P
"""
(2) Sequential revisions
I allow each agent to play the game with himself.
"""
def KMR_2x2_P_sequential(N, p, epsilon):
P = np.zeros((N+1, N+1), dtype=float)
P[0, 0], P[0, 1] = 1 - epsilon * (1/2), epsilon * (1/2)
P[N, N-1], P[N, N] = epsilon * (1/2), 1 - epsilon * (1/2)
for x in range(1, N):
P[x, x-1] = /
(x/N) * (
(x/N >p)*epsilon * (1/2)
(x/N == p) * (1/2))
(x/N < p)*(1 - epsilon*(1/2))
)
P[x, x+1] = /
(1-(x/N)) * (
(x/N >p)*(1 - epsilon*(1/2))
(x/N == p) * (1/2))
(x/N < p)*epsilon * (1/2)
)
P[x, x] = 1 - P[x, x-1] - P[x, x+1]
return P
class KMR_2x2:
def __init__(self, N, p, epsilon, move='simultaneous'):
self._epsilon = epsilon
self.N, self.p, self.move = N, p, move
self.set_P()
def get_epsilon(self):
return self._epsilon
def set_epsilon(self, new_value):
self._epsilon = new_value
self.set_P()
epsilon = property(get_epsilon, set_epsilon)
def set_P(self):
if self.move == 'sequential':
self.P = KMR_2x2_P_sequential(self.N, self.p, self._epsilon)
else:
self.P = KMR_2x2_P_simultaneous(self.N, self.p, self._epsilon)
def simulate(self, T=100000, x0=0):
"""
Generates a NumPy array containing a sample path of length T
with initial state x0 = 0
"""
self.s = mc_sample_path(self.P, x0, T)
def get_sample_path(self):
return self.s
def plot_sample_path(self, ax=None, show=True):
if show:
fig, ax = plt.subplots()
ax.plot(self.s, alpha=0.5)
ax.set_ylim(0, self.N)
ax.set_title(r'Sample path: $\\varepsilon = {0}$'.format(self._epsilon))
ax.set_xlabel('time')
ax.set_ylabel('state space')
if show:
plt.show()
def plot_emprical_dist(self, ax=None, show=True):
if show:
fig, ax = plt.subplots()
hist, bins = np.histogram(self.s, self.N+1)
ax.bar(range(self.N+1), hist, align='center')
ax.set_title(r'Emprical distribution: $\\varepsilon = {0}$'.format(self._epsilon))
ax.set_xlim(-0.5, self.N+0.5)
ax.set_xlabel('state space')
ax.set_ylabel('frequency')
@if show:
plt.show()
def compute_stationary_dist(self):
"""
Generates a NumPy array containing the stationary distribution
"""
self.mu = mc_compute_stationary(self.P)
def get_stationary_dist(self):
return self.mu
def plot_stationary_dist(self, ax=None, show=True):
if show:
fig, ax = plt.subplots()
ax.bar(range(self.N+1), self.mu, align='center')
ax.set_xlim(-0.5, self.N+0.5)
ax.set_ylim(0, 1)
ax.set_title(r'Stationary distribution: $\\varepsilon = {0}$'.format(self._epsilon))
ax.set_xlabel('state space')
ax.set_ylabel('probability')
if show:
plt.show()
if __name__ == '__main__':
p = 1/3
N = 10
epsilon = 0.03
T = 300000
kmr = KMR_2x2(N, p, epsilon)
kmr.simulate(T)
kmr.plot_sample_path()