-
Notifications
You must be signed in to change notification settings - Fork 0
/
combined_model_general.py
431 lines (342 loc) · 18.3 KB
/
combined_model_general.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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import numpy as np
from dataclasses import dataclass, field
from typing import List
from scipy.stats import poisson, norm
import random as rand
import matplotlib as mp
import matplotlib.pyplot as plt
@dataclass
class Parameters:
e_b: float
e_g: float
e_b_h: float # defined as proportion of e_b that is h (prob h given e_b)
e_b_m: float
e_g_h: float
e_g_m: float
# tao: float
number_of_blue: int = 1
number_of_green: int = 1
total_n: int = number_of_blue + number_of_green
h_b: float = 1
h_g: float = 1
w_min: float = 0.0
# Alternatively, ref_dist can be 'normal on normal'
ref_distribution: str = "Poisson"
value_distribution: str = "vh vm vl"
vh : float = 1
vm: float = 0.5
vl : float = 0.0
alpha: float = 0.5
alpha_b: float = alpha
alpha_g: float = alpha
# 0.2549378627974277
vh_freq: float = 1/3
vm_freq: float = 1/3
b_vh_freq: float = vh_freq
g_vh_freq: float = vh_freq
b_vm_freq: float = vm_freq
g_vm_freq: float = vm_freq
value_mean: float = vh_freq * vh + vm_freq * vm + (1- vh_freq - vm_freq) * vl
value_variance: float = (vh ** 2) * vh_freq + (vm ** 2)* vm_freq + (vl ** 2) * (1-vh_freq-vm_freq) - (value_mean ** 2)
b_value_mean: float = value_mean
g_value_mean: float = value_mean
b_value_variance: float = value_variance
g_value_variance: float = value_variance
b_value_sigma : float = b_value_variance ** (0.5)
g_value_sigma : float = g_value_variance ** (0.5)
prob_b : float = number_of_blue / total_n
prob_b_h : float = prob_b * b_vh_freq
prob_b_m : float = prob_b * b_vm_freq
prob_b_l : float = prob_b * (1 - b_vh_freq - b_vm_freq)
prob_g : float = number_of_green / total_n
prob_g_h : float = prob_g * (g_vh_freq)
prob_g_m : float = prob_g * g_vm_freq
prob_g_l : float = prob_g * (1 - g_vh_freq - g_vm_freq)
# Should be equilibrium employed g_vh_freq
# gh_earning : float = (1 - e_b)*(h_g)*(alpha_g*g_vh_freq)
r: float = 1.0
def calculate_threshold(self):
self.b_vl_freq = 1 - self.b_vm_freq -self.b_vh_freq
self.g_vl_freq = 1 - self.g_vm_freq -self.g_vh_freq
p = self
e_b = p.e_b
e_b_h = p.e_b_h
e_b_m = p.e_b_m
e_b_l = 1 - e_b_h - e_b_m
e_g_h = p.e_g_h
e_g_m = p.e_g_m
e_g_l = 1 - e_g_h - e_g_m
e_g = p.e_g
# figure somehting out here
# self.alpha_b = self.alpha
# self.alpha_g = 0.5 + (self.alpha_g - 0.5 + p.tao * e_g)/(1+p.tao)
# print('alpha b is ')
# print(self.alpha_b)
if self.ref_distribution == "Poisson":
b_h_lambda = 1/(p.b_vh_freq * p.number_of_blue) * (
(e_b * p.h_b * ((e_b_h * p.alpha_b) + (1-p.alpha_b)*(0.5 * (e_b_l + e_b_m))) +
(1-p.h_g)*e_g * ((e_g_h * p.alpha_g) + 0.5*(e_g_l + e_g_m) * (1-p.alpha_g)))
)
b_m_lambda = 1/(p.b_vm_freq * p.number_of_blue) * (
(e_b * p.h_b * ((e_b_m * p.alpha_b) + (1-p.alpha_b)*(0.5 * (e_b_l + e_b_h))) +
(1-p.h_g)*e_g * ((e_g_m * p.alpha_g) + 0.5*(e_g_l + e_g_h) * (1-p.alpha_g)))
)
b_l_lambda = 1/(p.b_vl_freq * p.number_of_blue) * (
(e_b * p.h_b * ((e_b_l * p.alpha_b) + (1-p.alpha_b)*(0.5 * (e_b_m + e_b_h))) +
(1-p.h_g)*e_g * ((e_g_l * p.alpha_g) + 0.5*(e_g_m + e_g_h) * (1-p.alpha_g)))
)
g_h_lambda = 1/(p.g_vh_freq * p.number_of_green) * (
(e_b * (1-p.h_b) * ((e_b_h * p.alpha_b) + (1-p.alpha_b)*(0.5 * (e_b_l + e_b_m))) +
(p.h_g)*e_g * ((e_g_h * p.alpha_g) + 0.5*(e_g_l + e_g_m) * (1-p.alpha_g)))
)
g_m_lambda = 1/(p.g_vm_freq * p.number_of_green) * (
(e_b * (1-p.h_b) * ((e_b_m * p.alpha_b) + (1-p.alpha_b)*(0.5 * (e_b_l + e_b_h))) +
(p.h_g)*e_g * ((e_g_m * p.alpha_g) + 0.5*(e_g_l + e_g_h) * (1-p.alpha_g)))
)
g_l_lambda = 1/(p.g_vl_freq * p.number_of_green) * (
(e_b * (1-p.h_b) * ((e_b_l * p.alpha_b) + (1-p.alpha_b)*(0.5 * (e_b_m + e_b_h))) +
(p.h_g)*e_g * ((e_g_l * p.alpha_g) + 0.5*(e_g_m + e_g_h) * (1-p.alpha_g)))
)
p_b_h_zero = poisson.pmf(0, b_h_lambda)
p_b_m_zero = poisson.pmf(0, b_m_lambda)
p_b_l_zero = poisson.pmf(0, b_l_lambda)
p_g_h_zero = poisson.pmf(0, g_h_lambda)
p_g_m_zero = poisson.pmf(0, g_m_lambda)
p_g_l_zero = poisson.pmf(0, g_l_lambda)
prob_b = p.prob_b
prob_b_h = p.prob_b_h
prob_b_m = p.prob_b_m
prob_b_l = prob_b * (1 - p.b_vh_freq - p.b_vm_freq)
prob_g = p.prob_g
prob_g_h = prob_g * (p.g_vh_freq)
prob_g_m = prob_g * p.g_vm_freq
prob_g_l = prob_g * (1 - p.g_vh_freq - p.g_vm_freq)
l_h_s = p.w_min - 1
r_h_s = p.w_min
while abs(l_h_s - r_h_s) != 0:
l_h_s = r_h_s
m_hired = 1 if p.vm >= l_h_s else 0
r_h_s = (
(
(
(p_b_h_zero*prob_b_h + p_g_h_zero*prob_g_h)* p.vh +
((p_b_m_zero + (1-p_b_m_zero)*m_hired) * prob_b_m + (p_g_m_zero + (1-p_g_m_zero)*m_hired) * prob_g_m) * p.vm +
p.vl*(prob_b_l + prob_g_l) )
)
/
(
# Denominator
(
p_b_h_zero * prob_b_h + p_g_h_zero * prob_g_h + p_g_m_zero +
(p_b_m_zero + (1-p_b_m_zero)*m_hired) * prob_b_m + (p_g_m_zero + (1-p_g_m_zero)*m_hired) * prob_g_m +
prob_b_l + prob_g_l
)
)
)
self.v_tilda = r_h_s
self.m_hired = 1 if p.vm >= self.v_tilda else 0
threshold = max(self.v_tilda, self.w_min)
# threshold = 0.1541919477612662
self.threshold = threshold
return (threshold)
def hire_continuous(self) -> float:
self.b_vl_freq = 1 - self.b_vm_freq -self.b_vh_freq
self.g_vl_freq = 1 - self.g_vm_freq -self.g_vh_freq
p = self
e_b = p.e_b
e_b_h = p.e_b_h
e_b_m = p.e_b_m
e_b_l = 1 - e_b_h - e_b_m
e_g_h = p.e_g_h
e_g_m = p.e_g_m
e_g_l = 1 - e_g_h - e_g_m
e_g = p.e_g
assert self.threshold >= self.w_min, "Make sure to calculate threshold before hiring"
assert e_b <= 1, "Something wrong with the logic"
# assert e_b_h + e_b_m + e_b_l == 1, f"{e_b_h, e_b_m, e_b_l}"
# assert e_g_h + e_g_m + e_g_l == 1
if self.ref_distribution == "Poisson":
b_h_lambda = 1/(p.b_vh_freq * p.number_of_blue) * (
(e_b * p.h_b * ((e_b_h * p.alpha_b) + (1-p.alpha_b)*(0.5 * (e_b_l + e_b_m))) +
(1-p.h_g)*e_g * ((e_g_h * p.alpha_g) + 0.5*(e_g_l + e_g_m)*(1-p.alpha_g)))
)
b_m_lambda = 1/(p.b_vm_freq * p.number_of_blue) * (
(e_b * p.h_b * ((e_b_m * p.alpha_b) + (1-p.alpha_b)*(0.5 * (e_b_l + e_b_h))) +
(1-p.h_g)*e_g * ((e_g_m * p.alpha_g) + 0.5*(e_g_l + e_g_h) * (1-p.alpha_g)))
)
b_l_lambda = 1/(p.b_vl_freq * p.number_of_blue) * (
(e_b * p.h_b * ((e_b_l * p.alpha_b) + (1-p.alpha_b)*(0.5 * (e_b_m + e_b_h))) +
(1-p.h_g)*e_g * ((e_g_l * p.alpha_g) + 0.5*(e_g_m + e_g_h) * (1-p.alpha_g)))
)
g_h_lambda = 1/(p.g_vh_freq * p.number_of_green) * (
(e_b * (1-p.h_b) * ((e_b_h * p.alpha_b) + (1-p.alpha_b)*(0.5 * (e_b_l + e_b_m))) +
(p.h_g)*e_g * ((e_g_h * p.alpha_g) + 0.5*(e_g_l + e_g_m) * (1-p.alpha_g)))
)
g_m_lambda = 1/(p.g_vm_freq * p.number_of_green) * (
(e_b * (1-p.h_b) * ((e_b_m * p.alpha_b) + (1-p.alpha_b)*(0.5 * (e_b_l + e_b_h))) +
(p.h_g)*e_g * ((e_g_m * p.alpha_g) + 0.5*(e_g_l + e_g_h) * (1-p.alpha_g)))
)
g_l_lambda = 1/(p.g_vl_freq * p.number_of_green) * (
(e_b * (1-p.h_b) * ((e_b_l * p.alpha_b) + (1-p.alpha_b)*(0.5 * (e_b_m + e_b_h))) +
(p.h_g)*e_g * ((e_g_l * p.alpha_g) + 0.5*(e_g_m + e_g_h) * (1-p.alpha_g)))
)
p_b_h_zero = poisson.pmf(0, b_h_lambda)
p_b_m_zero = poisson.pmf(0, b_m_lambda)
p_b_l_zero = poisson.pmf(0, b_l_lambda)
p_g_h_zero = poisson.pmf(0, g_h_lambda)
p_g_m_zero = poisson.pmf(0, g_m_lambda)
p_g_l_zero = poisson.pmf(0, g_l_lambda)
# prob hired from pool - same for both men and women
# b_p_h_pool = (
# (1 -
# ((1 - p_b_h_zero) * p.prob_b_h * p.number_of_blue + (1 - p_g_h_zero) * p.prob_g_h * p.number_of_green)
# )
# /
# (
# (p_b_h_zero * p.prob_b_h + p.prob_b_l)*p.number_of_blue + (p_g_h_zero * p.prob_g_h + p.prob_g_l)*p.number_of_green
# )
# )
# Commented out p.prob_b_h because it is given if (1-p_g_h_zero)
b_p_h_pool = (
(1 -
(
((1 - p_b_h_zero)*p.b_vh_freq + (1-p_b_m_zero)*p.b_vm_freq* self.m_hired) * p.number_of_blue +
((1 - p_g_h_zero)*p.g_vh_freq + (1-p_g_m_zero)*p.g_vm_freq* self.m_hired) * p.number_of_green
)
)
/
(
(1-(1-p_b_h_zero)*p.b_vh_freq - (1-p_b_m_zero)*p.b_vm_freq * self.m_hired)*p.number_of_blue +
(1-(1-p_g_h_zero)*p.g_vh_freq - (1-p_g_m_zero)*p.g_vm_freq* self.m_hired)*p.number_of_green
)
)
a =(
((1 - p_b_h_zero)*p.b_vh_freq + (1-p_b_m_zero)*p.b_vm_freq* self.m_hired) * p.number_of_blue +
((1 - p_g_h_zero)*p.g_vh_freq + (1-p_g_m_zero)*p.g_vm_freq* self.m_hired) * p.number_of_green
)
assert b_p_h_pool >= 0, f"whoopsy doopsy {b_p_h_pool, self.m_hired, a}"
if b_p_h_pool > 1:
print('uh oh probability greater than one check logic')
b_p_h_pool = b_p_h_pool if self.v_tilda >= p.w_min else 0
# print(f'{b_p_h_pool} is bph pool')
# g_p_h_pool = b_p_h_pool
# prob hired given blue received a referral
# Prob v > threshold * 1 + Prob v< threshold and hired from pool
b_p_h_r = p.prob_b_h
g_p_h_r = p.prob_g_h
# This is an intersection of sets, we want probability given, thus converted in next step
non_norm_ebh_next = (p_b_h_zero* b_p_h_pool + (1-p_b_h_zero)) * p.number_of_blue * p.b_vh_freq
non_norm_egh_next = (p_g_h_zero * b_p_h_pool + (1-p_g_h_zero)) * p.number_of_green * p.g_vh_freq
non_norm_ebm_next = (p_b_m_zero* b_p_h_pool + (1-p_b_m_zero)* (1 - (1- b_p_h_pool) *(1-p.m_hired))) * p.number_of_blue * p.b_vm_freq
non_norm_egm_next = (p_g_m_zero* b_p_h_pool + (1-p_g_m_zero)* (1 - (1- b_p_h_pool) *(1-p.m_hired))) * p.number_of_green * p.g_vm_freq
non_norm_ebl_next = b_p_h_pool * p.number_of_blue * p.b_vl_freq
theta_b_h, theta_g_h = p.b_vh_freq*(1-p_b_h_zero), p.g_vh_freq*(1-p_g_h_zero)
theta_b_m, theta_g_m = p.b_vm_freq*(1- p_b_m_zero), p.g_vm_freq* (1- p_g_m_zero)
e_b_next = ((1 - theta_b_h - (p.m_hired)* theta_b_m) * b_p_h_pool + theta_b_h + theta_b_m * p.m_hired) * p.number_of_blue
e_g_next = ((1 - theta_g_h - (p.m_hired)* theta_g_m) * b_p_h_pool + theta_g_h + theta_g_m * p.m_hired) * p.number_of_green
# print((theta_b-theta_g)*(1-b_p_h_not_r))
ebh_next = non_norm_ebh_next/e_b_next
egh_next = non_norm_egh_next/e_g_next
ebm_next = non_norm_ebm_next/e_b_next
egm_next = non_norm_egm_next/e_g_next
return {'e_b' : e_b_next, 'e_b_h': ebh_next, 'e_b_m': ebm_next, 'e_g_m': egm_next, 'e_g_h': egh_next, 'e_g': e_g_next}
def run_periods(periods = 15, e_b = 0.8, e_g= 0.2, e_b_h = 0.5, e_g_h = 0.5, e_b_m = 0.2, e_g_m = 0.2,
n= 2.0, alpha_b= 1, alpha_g= 1, h_b= 1, h_g= 1, verbose = True ):
for period in range(periods):
p = Parameters(e_b = e_b, e_g = e_g, e_b_h = e_b_h, e_g_h = e_g_h, e_b_m = e_b_m, e_g_m = e_g_m,
number_of_blue= n, number_of_green=n, alpha_b= alpha_b, alpha_g= alpha_g,
h_b= h_b, h_g= h_g)
p.calculate_threshold()
if period == 0 and verbose:
print(f'The parameters for p are {p}')
# print(f'the skill threshold for this period is {p.threshold} ')
# if e_b <= 0.5:
# print(f'It took {period} generations for the male employment rate to equal the female employment rate')
# break
future_emp_dict = p.hire_continuous()
e_b, e_g, e_b_h, e_g_h = future_emp_dict['e_b'], future_emp_dict['e_g'], future_emp_dict['e_b_h'], future_emp_dict['e_g_h']
e_b_m, e_g_m = future_emp_dict['e_b_m'], future_emp_dict['e_g_m']
print (e_b, e_g, e_b_h, e_g_h, e_b_m, e_g_m, p.m_hired)
# vh_freq doesnt work, watch out
def run_period(e_b, e_g, e_b_h: float = 0.5, e_g_h: float = 0.5, e_b_m: float = 0.2, e_g_m: float = 0.2,
n: float = 2.0, alpha_b: float = 0.8, alpha_g: float = 0.8,
h_b: float = 0.8, h_g: float = 0.8):
p = Parameters(e_b = e_b, e_g = e_g, e_b_h = e_b_h, e_g_h = e_g_h, e_b_m = e_b_m, e_g_m = e_g_m,
number_of_blue= n, number_of_green=n, alpha_b= alpha_b, alpha_g= alpha_g,
h_b= h_b, h_g= h_g)
p.calculate_threshold()
future_emp_dict = p.hire_continuous()
e_b, e_g, e_b_h, e_g_h = future_emp_dict['e_b'], future_emp_dict['e_g'], future_emp_dict['e_b_h'], future_emp_dict['e_g_h']
e_b_m, e_g_m = future_emp_dict['e_b_m'], future_emp_dict['e_g_m']
return (e_b, e_g, e_b_h, e_g_h, e_b_m, e_g_m)
# vh_freq doesn't work cuz dataclass, will need to fix
def find_steady_state(e_b_0: float, n: float, alpha_b: float, alpha_g: float, h_b: float, h_g: float,
e_g_0: float = 0.5, e_b_h_0: float = 0.5, e_g_h_0: float = 0.5,
e_b_m_0: float = 0.2, e_g_m_0 : float = 0.2, vh_freq = 0.4,
max_iterations: int = 1000, return_iterations: bool = False):
iteration = 0
e_b = e_b_0
e_g = e_g_0
e_b_h = e_b_h_0
e_g_h = e_g_h_0
e_b_m = e_b_m_0
e_g_m = e_g_m_0
e_b_new = 0
e_g_new = 0
ebh_new = 0
egh_new = 0
ebm_new = 0
egm_new = 0
if return_iterations:
while (abs(e_b - e_b_new) != 0.0 or abs(e_b_h - ebh_new) != 0.0 or abs(e_g_h - egh_new) != 0.0) and iteration < max_iterations:
iteration +=1
e_b = e_b_new if e_b_new else e_b
e_g = e_g_new if e_g_new else e_g
e_b_h = ebh_new if ebh_new else e_b_h
e_g_h = egh_new if egh_new else e_g_h
e_b_m = ebm_new if ebm_new else e_b_m
e_g_m = egm_new if egm_new else e_g_m
e_b_new, e_g_new, ebh_new, egh_new, ebm_new, egm_new = run_period(e_b = e_b, e_g = e_g, e_b_h = e_b_h, e_g_h = e_g_h,
e_b_m = e_b_m, e_g_m = e_g_m, n = n, alpha_b=alpha_b, alpha_g=alpha_g, h_b = h_b, h_g = h_g)
else:
while (e_b != e_b_new or e_b_h != ebh_new or e_g_h != egh_new) and iteration < max_iterations:
iteration +=1
e_b = e_b_new if e_b_new else e_b
e_g = e_g_new if e_g_new else e_g
e_b_h = ebh_new if ebh_new else e_b_h
e_g_h = egh_new if egh_new else e_g_h
e_b_m = ebm_new if ebm_new else e_b_m
e_g_m = egm_new if egm_new else e_g_m
e_b_new, e_g_new, ebh_new, egh_new, ebm_new, egm_new = run_period(e_b = e_b, e_g = e_g, e_b_h = e_b_h, e_g_h = e_g_h,
e_b_m = e_b_m, e_g_m = e_g_m, n = n, alpha_b=alpha_b, alpha_g=alpha_g, h_b = h_b, h_g = h_g)
if iteration == max_iterations:
print('max iteration reached')
if return_iterations:
return iteration
else:
return (e_b_new, e_g_new, ebh_new, egh_new, ebm_new, egh_new)
else:
print(f'reached in iteration # {iteration}')
if return_iterations:
return iteration
else:
p = Parameters(e_b = e_b, e_g=e_g, e_b_h = e_b_h, e_g_h = e_g_h, e_b_m = e_b_m, e_g_m = e_g_m,
number_of_blue= n, number_of_green=n, alpha_b= alpha_b, alpha_g= alpha_g,
h_b= h_b, h_g= h_g )
# print('lambda bh')
# print(p.vh_freq, n, e_b_new, ebh_new, alpha_b, alpha_g, h_b, h_g)
# lambda_bh_n = (1/(p.vh_freq * n) * ( (e_b_new * h_b * ((ebh_new * alpha_b) + (1-alpha_b)*(1-ebh_new))) + (1-h_g)*(1-e_b_new) * ((egh_new * alpha_g) + (1-egh_new) * (1-alpha_g))))
# print(lambda_bh_n)
# print('lambda bl')
# print(1/((1-p.vh_freq) * n) * ((e_b_new * h_b * (((1-ebh_new) * alpha_b) + (1-alpha_b)*(ebh_new))) + (1-h_g)*(1-e_b_new) * (egh_new * (1-alpha_g) + (1-egh_new) * (alpha_g))))
# print('lambda gh')
# print(1/((p.vh_freq) * n) * (((1-e_b_new) * h_g * ((egh_new * alpha_b) + (1-alpha_b)*(egh_new))) + (1-h_b)*(e_b_new) * (ebh_new * alpha_b + (1-ebh_new) * (1-alpha_g))))
return (e_b_new, e_g_new, ebh_new, egh_new, ebm_new, egm_new)
# print(find_steady_state(e_b_0 = 0.5, e_g_0=0.5, e_b_h_0=0.2, e_g_h_0 = 0.2, e_b_m_0=0.1, e_g_m_0=0.1,
# n=2, alpha_b = 1.0, alpha_g = 1.0, h_b = 1.0, h_g = 1.0, return_iterations=False))
e_b = 0.8
e_g = 1 - e_b
run_periods(periods = 6, e_b = e_b, e_g =e_g, e_b_h = 2/3, e_g_h = 6/9, e_b_m = 3/9, e_g_m = 3/9, n=1.0,
alpha_b = 1, alpha_g = 1, h_b = 1, h_g = 1, verbose = False)
# run_periods()
# plot_e_b()