-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
brd.jl
358 lines (281 loc) · 9.85 KB
/
brd.jl
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#=
Tools for best response dynamics
=#
using StatsBase
# AbstractBRD
"""
AbstractBRD
Abstract type representing the best response dynamics model.
"""
abstract type AbstractBRD{T<:Real} end
"""
BRD
Type representing the best response dynamics model.
# Fields
- `N::Int` : The number of players.
- `player::Player{2,T}` : `Player` instance in the model.
- `num_actions::Int` : The number of actions for players.
"""
struct BRD{T<:Real} <: AbstractBRD{T}
N::Int
player::Player{2,T}
num_actions::Int
end
"""
BRD(N, payoff_array)
Create a new BRD instance.
# Arguments
- `N::Integer` : The number of players.
- `payoff_array::Matrix` : Payoff array for each player.
# Returns
- `::BRD` : The best response dynamics model.
"""
function BRD(payoff_array::Matrix{T}, N::Integer) where {T<:Real}
num_actions = size(payoff_array, 1)
if num_actions != size(payoff_array, 2)
throw(ArgumentError("Payoff array must be square"))
end
return BRD(N, Player(payoff_array), num_actions)
end
"""
KMR
Type representing the Kandori Mailath Rob model.
# Fields
- `N::Int` : The number of players.
- `player::Player` : `Player` instance in the model.
- `num_actions::Int` : The number of actions for players.
- `epsilon::Float64` : The probability of strategy flips.
"""
struct KMR{T<:Real} <: AbstractBRD{T}
N::Int
player::Player{2,T}
num_actions::Int
epsilon::Float64
end
"""
KMR(N, payoff_array, epsilon)
Create a new KMR instance.
# Arguments
- `N::Integer` : The number of players.
- `payoff_array::Matrix` : The payoff array for each player.
- `epsilon::Float64` : The probability of strategy flips.
# Returns
- `::KMR` : The Kandori Mailath Rob model.
"""
function KMR(payoff_array::Matrix{T},
N::Integer,
epsilon::Float64) where {T<:Real}
num_actions = size(payoff_array, 1)
if num_actions != size(payoff_array, 2)
throw(ArgumentError("Payoff array must be square"))
end
return KMR(N, Player(payoff_array), num_actions, epsilon)
end
"""
SamplingBRD
Type representing the sampling best response dynamics model.
# Fields
- `N::Int` : The number of players.
- `player::Player` : `Player` instance in the model.
- `num_actions::Int` : The number of actions for players.
- `k::Int` : Sample size.
"""
struct SamplingBRD{T<:Real} <: AbstractBRD{T}
N::Int
player::Player{2,T}
num_actions::Int
k::Int #sample size
end
"""
SamplingBRD(N, payoff_array, k)
Create a new SamplingBRD instance.
# Arguments
- `N::Integer` : The number of players.
- `payoff_array::Matrix` : Payoff array for a player.
- `k::Integer` : Sample size.
# Returns
- `::SamplingBRD` : The sampling best response dynamics model.
"""
function SamplingBRD(payoff_array::Matrix{T},
N::Integer,
k::Integer) where {T<:Real}
num_actions = size(payoff_array, 1)
if num_actions != size(payoff_array, 2)
throw(ArgumentError("Payoff array must be square"))
end
return SamplingBRD(N, Player(payoff_array), num_actions, k)
end
# play!
function play!(rng::AbstractRNG,
brd::BRD,
action::Integer,
action_dist::Vector{<:Integer},
options::BROptions=BROptions())
action_dist[action] -= 1
next_action = best_response(brd.player, action_dist, options)
action_dist[next_action] += 1
return action_dist
end
function play!(rng::AbstractRNG,
brd::KMR,
action::Integer,
action_dist::Vector{<:Integer},
options::BROptions=BROptions())
action_dist[action] -= 1
if rand(rng) <= brd.epsilon
next_action = rand(rng, 1:brd.num_actions)
else
next_action = best_response(brd.player, action_dist, options)
end
action_dist[next_action] += 1
return action_dist
end
function play!(rng::AbstractRNG,
brd::SamplingBRD,
action::Integer,
action_dist::Vector{<:Integer},
options::BROptions=BROptions())
action_dist[action] -= 1
actions = sample(1:brd.num_actions, Weights(action_dist), brd.k)
sample_action_dist = zeros(Int, brd.num_actions)
for a in actions
sample_action_dist[a] += 1
end
next_action = best_response(brd.player, sample_action_dist, options)
action_dist[next_action] += 1
return action_dist
end
@doc """
play!([rng=Random.GLOBAL_RNG, ]brd, action, action_dist[, options=BROptions()])
Update an action distribution given a specified action.
# Arguments
- `rng::AbstractRNG` : Random number generator used.
- `brd::AbstractBRD` : `AbstractBRD` instance.
- `action::Integer` : A specified action.
- `action_dist::Vector{<:Integer}` : The distribution of players' actions.
- `options::BROptions` : Options for `best response` method.
# Returns
- `action_dist::Vector{<:Integer}` : Updated `action_dist`.
"""
# play
"""
play([rng=Random.GLOBAL_RNG, ]brd, init_action_dist[, options=BROptions(); num_reps=1])
Return the action distribution after `num_reps` times iteration
# Arguments
- `rng::AbstractRNG` : Random number generator used.
- `brd::AbstractBRD` : `AbstractBRD` instance.
- `init_action_dist::Vector{<:Integer}` : The initial distribution of players' actions.
- `options::BROptions` : Options for `best_response` method.
- `num_reps::Integer` : The number of iterations.
# Returns
- `::Vector{<:Integer}` : The action distribution after iterations.
"""
function play(rng::AbstractRNG,
brd::AbstractBRD,
init_action_dist::Vector{<:Integer},
options::BROptions=BROptions();
num_reps::Integer=1)
if length(init_action_dist) != brd.num_actions
throw(ArgumentError("The length of init_action_dist must be the number
of actions"))
end
if sum(init_action_dist) != brd.N
throw(ArgumentError("The sum of init_action_dist must be the number of
players"))
end
player_ind_seq = rand(rng, 1:brd.N, num_reps)
for t in 1:num_reps
action = searchsortedfirst(accumulate(+, init_action_dist),
player_ind_seq[t])
init_action_dist = play!(rng, brd, action, init_action_dist, options)
end
return init_action_dist
end
play(brd::AbstractBRD, init_action_dist::Vector{<:Integer},
options::BROptions=BROptions(); num_reps::Integer=1) =
play(Random.GLOBAL_RNG, brd, init_action_dist, options, num_reps=num_reps)
# time_series!
"""
time_series!(rng, brd, out, player_ind_seq, options)
Update the matrix `out` which is used in `time_series` method given a player
index sequence.
# Arguments
- `rng::AbstractRNG` : Random number generator used.
- `brd::AbstractBRD` : Instance of the model.
- `out::Matrix{<:Integer}` : Matrix representing the time series of action
profiles.
- `player_ind_seq::Vector{<:Integer}` : The vector of player index.
- `options::BROptions` : Options for `best_response` method.
# Returns
- `out::Matrix{<:Integer}` : Updated `out`.
"""
function time_series!(rng::AbstractRNG,
brd::AbstractBRD,
out::Matrix{<:Integer},
player_ind_seq::Vector{<:Integer},
options::BROptions)
ts_length = size(out, 2)
action_dist = [out[i,1] for i in 1:brd.num_actions]
for t in 1:ts_length-1
action = searchsortedfirst(accumulate(+, action_dist), player_ind_seq[t])
action_dist = play!(rng, brd, action, action_dist, options)
for i in 1:brd.num_actions
out[i,t+1] = action_dist[i]
end
end
return out
end
# time_series
function time_series(rng::AbstractRNG,
brd::AbstractBRD,
ts_length::Integer,
init_action_dist::Vector{<:Integer},
options::BROptions=BROptions())
if length(init_action_dist) != brd.num_actions
throw(ArgumentError("The length of init_action_dist must be the number
of actions"))
end
if sum(init_action_dist) != brd.N
throw(ArgumentError("The sum of init_action_dist must be the number of
players"))
end
player_ind_seq = rand(rng, 1:brd.N, ts_length)
out = Matrix{Int}(undef, brd.num_actions, ts_length)
for i in 1:brd.num_actions
out[i, 1] = init_action_dist[i]
end
time_series!(rng, brd, out, player_ind_seq, options)
end
time_series(brd::AbstractBRD, ts_length::Integer,
init_action_dist::Vector{<:Integer},
options::BROptions=BROptions()) =
time_series(Random.GLOBAL_RNG, brd, ts_length, init_action_dist, options)
function time_series(rng::AbstractRNG,
brd::AbstractBRD,
ts_length::Integer,
options::BROptions=BROptions())
player_ind_seq = rand(rng, 1:brd.N, ts_length)
nums_actions = ntuple(i -> brd.num_actions, brd.N)
init_actions = random_pure_actions(rng, nums_actions)
action_dist = zeros(Int, brd.num_actions)
for i in 1:brd.N
action_dist[init_actions[i]] += 1
end
time_series(rng, brd, ts_length, action_dist, options)
end
time_series(brd::AbstractBRD, ts_length::Integer,
options::BROptions=BROptions()) =
time_series(Random.GLOBAL_RNG, brd, ts_length, options)
@doc """
time_series([rng=Random.GLOBAL_RNG, ]brd, ts_length, init_action_dist[, options=BROptions()])
Return the time series of action distribution.
# Arguments
- `rng::AbstractRNG` : Random number generator used.
- `brd::AbstractBRD` : `AbstractBRD` instance.
- `ts_length::Integer` : The length of time series.
- `init_action_dist::Vector{<:Integer}` : Initial action distribution. If not
provided, it is selected randomly.
- `options::BROptions` : Options for `best_response` method.
# Returns
- `::Matrix{<:Integer}` : The time series of action distributions.
"""