-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlewisModelPygame.py
More file actions
329 lines (271 loc) · 9.63 KB
/
lewisModelPygame.py
File metadata and controls
329 lines (271 loc) · 9.63 KB
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
import pygame
import math
##################
# pygame setup
##################
pygame.init()
screen = pygame.display.set_mode((1280, 780))
clock = pygame.time.Clock()
running = True
dt = 0
# Fonts
my_font = pygame.font.SysFont('Comic Sans MS', 14)
font_color = (0, 0, 0)
soft_blue = pygame.Color(173, 216, 230) # R: 173, G: 216, B: 230
##################
##################
# Matplotlib setup
# Within pygame, see article here:
# https://stackoverflow.com/questions/48093361/using-matplotlib-in-pygame
##################
import matplotlib
matplotlib.use("Agg")
import matplotlib.backends.backend_agg as agg
import matplotlib.pyplot as plt
##################
##################
# economy setup
##################
scenario_name = "Lewis Baseline"
Y1 = 1 # Output in sector 1 (traditional)
Y2 = 1 # Output in sector 2 (modern)
L1 = 1 # employment in sector 1
L2 = 1 # employment in sector 2
w1 = 1 # subsistence real wage sector 1 (baseline)
w2 = 1 # real wage sector 2
K = 10 # capital stock (only in sector 2)
P2 = 1 # profits in sector 2
# Set fixed parameter values
alpha = 0.7 # labour elasticity of output, sector 1
rho = 1 # wage premium
L = 20 # total labour supply (exogenous)
gamma = 0.2 # labour supply coefficient, sector 2
lambda_val = 10 # employment at which MPL in sector 1 becomes zero
beta = 0.7 # labour elasticity of output, sector 2
# Set baseline parameter values
# w1 = np.ones((S, T)) # subsistence real wage sector 1 (baseline)
# # Set parameter values for different scenarios
# w1[1, s:T] = 0.9 # scenario 2: fall in subsistence wage
# Initialise such that there is surplus labour (L1 > lambda)
L1 = 0.9 * L
L2 = L - L1
##################
##################
# Show graph of the economy
# https://macrosimulation.org/a_neoclassical_synthesis_model_is_lm_as_ad#directed-graph
##################
def iterate_economy(L1, lambda_val, alpha, gamma, L2, w1, rho, beta, K, P2, w2, L):
# Model equations
# Output sector1 and wages sector 2
Y1 = lambda_val ** alpha
w2 = w1 + rho
if L1 < lambda_val:
Y1 = L1 ** alpha
w2 = gamma * L2
# Output sector 2
Y2 = (L2 ** beta) * (K ** (1 - beta))
# Capital accumulation sector 2
K = K + P2
# Profits sector 2
P2 = Y2 - w2 * L2
# Employment sector 2
L2 = (beta * Y2) / w2
# Employment sector 1
L1 = L - L2
return Y1, w2, Y2, K, P2, L2, L1
# Enable this to automatically iterate
AUTO_ITERATIOM = True
if (AUTO_ITERATIOM):
# Set an automatic iteration event every 500 miliseconds
pygame.time.set_timer(pygame.USEREVENT, 500)
# Maximum number of iterations to display in each graph
MAX_PLOT_LENGTH = 100
# Count number of iterations for this simulation
iteration_count = 0
is_iter = False
# Used for plotting the simulation over time
Y1_time = []
Y2_time = []
L1_time = []
L2_time = []
P2_time = []
# Append initial value
Y1_time.append(Y1)
Y2_time.append(Y2)
L1_time.append(L1)
L2_time.append(L2)
P2_time.append(P2)
# graph images to render
Y1_surf = None
Y2_surf = None
L1_surf = None
L2_surf = None
P2_surf = None
# Current profit share
PS = P2 / (Y1 + Y2)
while running:
# poll for events
# pygame.QUIT event means the user clicked X to close your window
for event in pygame.event.get():
if event.type == pygame.USEREVENT:
is_iter = True
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
print("Space bar pressed!")
is_iter = True
if event.key == pygame.K_w:
print("w pressed!")
w1 += 0.1
if event.key == pygame.K_s:
print("s pressed!")
w1 -= 0.1
# Player has triggered an iteration
if is_iter:
# Run economy updates
Y1, w2, Y2, K, P2, L2, L1 = iterate_economy(L1, lambda_val, alpha, gamma, L2, w1, rho, beta, K, P2, w2, L)
# Calculate profit share of sector 2
PS = P2 / (Y1 + Y2)
Y1_time.append(Y1)
Y2_time.append(Y2)
L1_time.append(L1)
L2_time.append(L2)
P2_time.append(P2)
iteration_count += 1
###########################
plot_min = max(0, iteration_count - MAX_PLOT_LENGTH)
plot_max = iteration_count
# Rerender the graph images
# # Plot output 1
plt.plot(Y1_time, color='black', linewidth=2, linestyle='-')
plt.xlabel("Time")
plt.ylabel("Y1")
plt_title = scenario_name + ": Output Y1"
plt.title(plt_title, fontsize=15)
plt.xlim((plot_min, plot_max))
fig = plt.figure(plt_title)
fig.set_figwidth(5)
fig.set_figheight(4)
canvas = agg.FigureCanvasAgg(fig)
canvas.draw()
renderer = canvas.get_renderer()
raw_data = renderer.tostring_rgb()
size = canvas.get_width_height()
Y1_surf = pygame.image.fromstring(raw_data, size, "RGB")
Y1_surf = pygame.transform.scale(Y1_surf, (400, 360))
# Clear the plot
plt.clf()
# Plot output 2
plt.plot(Y2_time, color='black', linewidth=2, linestyle='-')
plt.xlabel("Time")
plt.ylabel("Y2")
plt_title = scenario_name + ": Output Y2"
plt.title(plt_title, fontsize=15)
plt.xlim((plot_min, plot_max))
fig = plt.figure(plt_title)
fig.set_figwidth(5)
fig.set_figheight(4)
canvas = agg.FigureCanvasAgg(fig)
canvas.draw()
renderer = canvas.get_renderer()
raw_data = renderer.tostring_rgb()
size = canvas.get_width_height()
Y2_surf = pygame.image.fromstring(raw_data, (size), "RGB")
Y2_surf = pygame.transform.scale(Y2_surf, (400, 360))
# Clear the plot
plt.clf()
# Plot labor 1
plt.plot(L1_time, color='black', linewidth=2, linestyle='-')
plt.xlabel("Time")
plt.ylabel("Labor 1")
plt_title = scenario_name + ": Labor 1"
plt.title(plt_title, fontsize=15)
plt.xlim((plot_min, plot_max))
fig = plt.figure(plt_title)
fig.set_figwidth(5)
fig.set_figheight(4)
canvas = agg.FigureCanvasAgg(fig)
canvas.draw()
renderer = canvas.get_renderer()
raw_data = renderer.tostring_rgb()
size = canvas.get_width_height()
L1_surf = pygame.image.fromstring(raw_data, size, "RGB")
L1_surf = pygame.transform.scale(L1_surf, (400, 360))
# Clear the plot
plt.clf()
# Plot labor 2
plt.plot(L2_time, color='black', linewidth=2, linestyle='-')
plt.xlabel("Time")
plt.ylabel("Labor 2")
plt_title = scenario_name + ": Labor 2"
plt.title(plt_title, fontsize=15)
plt.xlim((plot_min, plot_max))
fig = plt.figure(plt_title)
fig.set_figwidth(5)
fig.set_figheight(4)
canvas = agg.FigureCanvasAgg(fig)
canvas.draw()
renderer = canvas.get_renderer()
raw_data = renderer.tostring_rgb()
size = canvas.get_width_height()
L2_surf = pygame.image.fromstring(raw_data, size, "RGB")
L2_surf = pygame.transform.scale(L2_surf, (400, 360))
# Clear the plot
plt.clf()
# Plot employment level
plt.plot(P2_time, color='black', linewidth=2, linestyle='-')
plt.xlabel("Time")
plt.ylabel("Profits 2")
plt_title = scenario_name + ": Profits 2"
plt.title(plt_title, fontsize=15)
plt.xlim((plot_min, plot_max))
fig = plt.figure(plt_title)
fig.set_figwidth(5)
fig.set_figheight(4)
canvas = agg.FigureCanvasAgg(fig)
canvas.draw()
renderer = canvas.get_renderer()
raw_data = renderer.tostring_rgb()
size = canvas.get_width_height()
P2_surf = pygame.image.fromstring(raw_data, size, "RGB")
P2_surf = pygame.transform.scale(P2_surf, (400, 360))
# Clear the plot
plt.clf()
##########################
# Wait for next iteration from player
is_iter = False
# fill the screen with a color to wipe away anything from last frame
screen.fill(soft_blue)
#########################
# Update economic visuals
#########################
# Add simple text
iterate_text_surface = my_font.render('Press the space bar to iterate the economy', True, font_color)
sim_text_surface = my_font.render(scenario_name, True, font_color)
PS_text_surface = my_font.render('Sector 2 profit share: ' + str(PS), True, font_color)
w1_text_surface = my_font.render('subsistence real wage: ' + str(w1), True, font_color)
w2_text_surface = my_font.render('luxury real wage: ' + str(w2), True, font_color)
iter_text_surface = my_font.render('Iteration number: ' + str(iteration_count), True, font_color)
# Render text on the page at the specified positions
screen.blit(iterate_text_surface, (20, 10))
screen.blit(sim_text_surface, (20, 50))
screen.blit(PS_text_surface, (20, 80))
screen.blit(w1_text_surface, (20, 110))
screen.blit(w2_text_surface, (20, 140))
screen.blit(iter_text_surface, (20, 400))
if (iteration_count > 0):
#add the graph images to the screen
screen.blit(Y1_surf, (400,0))
screen.blit(Y2_surf, (800,0))
screen.blit(L1_surf, (400,360))
screen.blit(L2_surf, (800,360))
screen.blit(P2_surf, (0,420))
##########################
# flip() the display to put your work on screen
pygame.display.flip()
# limits FPS to 30
# dt is delta time in seconds since last frame, used for framerate-
# independent physics.
dt = clock.tick(30) / 1000
pygame.quit()