odu_solutions-checkpoint


quant-econ Solutions: Search with Unknown Offer Distribution


In [1]:
%matplotlib inline

In [2]:
import numpy as np
import matplotlib.pyplot as plt

from quantecon import SearchProblem, compute_fixed_point

Exercise 1

This code solves the "Offer Distribution Unknown" model by iterating on a guess of the reservation wage function. You should find that the run time is much shorter than that of the value function approach in odu_vfi.py


In [3]:
sp = SearchProblem(pi_grid_size=50)

phi_init = np.ones(len(sp.pi_grid)) 
w_bar = compute_fixed_point(sp.res_wage_operator, phi_init)

fig, ax = plt.subplots(figsize=(9, 7))
ax.plot(sp.pi_grid, w_bar, linewidth=2, color='black')
ax.set_ylim(0, 2)
ax.grid(axis='x', linewidth=0.25, linestyle='--', color='0.25')
ax.grid(axis='y', linewidth=0.25, linestyle='--', color='0.25')
ax.fill_between(sp.pi_grid, 0, w_bar, color='blue', alpha=0.15)
ax.fill_between(sp.pi_grid, w_bar, 2, color='green', alpha=0.15)
ax.text(0.42, 1.2, 'reject')
ax.text(0.7, 1.8, 'accept')


Computed iterate 1 with error 0.426161
Computed iterate 2 with error 0.127050
Computed iterate 3 with error 0.076090
Computed iterate 4 with error 0.046400
Computed iterate 5 with error 0.028295
Computed iterate 6 with error 0.018182
Computed iterate 7 with error 0.013566
Computed iterate 8 with error 0.009611
Computed iterate 9 with error 0.007113
Computed iterate 10 with error 0.005174
Computed iterate 11 with error 0.003732
Computed iterate 12 with error 0.002657
Computed iterate 13 with error 0.001876
Computed iterate 14 with error 0.001348
Computed iterate 15 with error 0.000965
Out[3]:
<matplotlib.text.Text at 0x36f0290>

The next piece of code is not one of the exercises from quant-econ, it's just a fun simulation to see what the effect of a change in the underlying distribution on the unemployment rate is.

At a point in the simulation, the distribution becomes significantly worse. It takes a while for agents to learn this, and in the meantime they are too optimistic, and turn down too many jobs. As a result, the unemployment rate spikes.

The code takes a few minutes to run.


In [4]:
from scipy import interp
# Set up model and compute the function w_bar
sp = SearchProblem(pi_grid_size=50, F_a=1, F_b=1)
pi_grid, f, g, F, G = sp.pi_grid, sp.f, sp.g, sp.F, sp.G
phi_init = np.ones(len(sp.pi_grid)) 
w_bar_vals = compute_fixed_point(sp.res_wage_operator, phi_init)
w_bar = lambda x: interp(x, pi_grid, w_bar_vals)


class Agent:
    """
    Holds the employment state and beliefs of an individual agent.
    """

    def __init__(self, pi=1e-3):
        self.pi = pi
        self.employed = 1

    def update(self, H):
        "Update self by drawing wage offer from distribution H."
        if self.employed == 0:
            w = H.rvs()
            if w >= w_bar(self.pi):
                self.employed = 1
            else:
                self.pi = 1.0 / (1 + ((1 - self.pi) * g(w)) / (self.pi * f(w)))


num_agents = 5000
separation_rate = 0.025  # Fraction of jobs that end in each period 
separation_num = int(num_agents * separation_rate)
agent_indices = range(num_agents)
agents = [Agent() for i in range(num_agents)]
sim_length = 600
H = G  # Start with distribution G
change_date = 200  # Change to F after this many periods

unempl_rate = []
for i in range(sim_length):
    print "date = ", i
    if i == change_date:
        H = F
    # Randomly select separation_num agents and set employment status to 0
    np.random.shuffle(agent_indices)
    separation_list = agent_indices[:separation_num]
    for agent_index in separation_list:
        agents[agent_index].employed = 0
    # Update agents
    for agent in agents:
        agent.update(H)
    employed = [agent.employed for agent in agents]
    unempl_rate.append(1 - np.mean(employed))

fig, ax = plt.subplots(figsize=(9, 7))
ax.plot(unempl_rate, lw=2, alpha=0.8, label='unemployment rate')
ax.axvline(change_date, color="red")
ax.legend()


Computed iterate 1 with error 0.426161
Computed iterate 2 with error 0.127050
Computed iterate 3 with error 0.076090
Computed iterate 4 with error 0.046400
Computed iterate 5 with error 0.028295
Computed iterate 6 with error 0.018182
Computed iterate 7 with error 0.013566
Computed iterate 8 with error 0.009611
Computed iterate 9 with error 0.007113
Computed iterate 10 with error 0.005174
Computed iterate 11 with error 0.003732
Computed iterate 12 with error 0.002657
Computed iterate 13 with error 0.001876
Computed iterate 14 with error 0.001348
Computed iterate 15 with error 0.000965
date =  0
date =  1
date =  2
date =  3
date =  4
date =  5
date =  6
date =  7
date =  8
date =  9
date =  10
date =  11
date =  12
date =  13
date =  14
date =  15
date =  16
date =  17
date =  18
date =  19
date =  20
date =  21
date =  22
date =  23
date =  24
date =  25
date =  26
date =  27
date =  28
date =  29
date =  30
date =  31
date =  32
date =  33
date =  34
date =  35
date =  36
date =  37
date =  38
date =  39
date =  40
date =  41
date =  42
date =  43
date =  44
date =  45
date =  46
date =  47
date =  48
date =  49
date =  50
date =  51
date =  52
date =  53
date =  54
date =  55
date =  56
date =  57
date =  58
date =  59
date =  60
date =  61
date =  62
date =  63
date =  64
date =  65
date =  66
date =  67
date =  68
date =  69
date =  70
date =  71
date =  72
date =  73
date =  74
date =  75
date =  76
date =  77
date =  78
date =  79
date =  80
date =  81
date =  82
date =  83
date =  84
date =  85
date =  86
date =  87
date =  88
date =  89
date =  90
date =  91
date =  92
date =  93
date =  94
date =  95
date =  96
date =  97
date =  98
date =  99
date =  100
date =  101
date =  102
date =  103
date =  104
date =  105
date =  106
date =  107
date =  108
date =  109
date =  110
date =  111
date =  112
date =  113
date =  114
date =  115
date =  116
date =  117
date =  118
date =  119
date =  120
date =  121
date =  122
date =  123
date =  124
date =  125
date =  126
date =  127
date =  128
date =  129
date =  130
date =  131
date =  132
date =  133
date =  134
date =  135
date =  136
date =  137
date =  138
date =  139
date =  140
date =  141
date =  142
date =  143
date =  144
date =  145
date =  146
date =  147
date =  148
date =  149
date =  150
date =  151
date =  152
date =  153
date =  154
date =  155
date =  156
date =  157
date =  158
date =  159
date =  160
date =  161
date =  162
date =  163
date =  164
date =  165
date =  166
date =  167
date =  168
date =  169
date =  170
date =  171
date =  172
date =  173
date =  174
date =  175
date =  176
date =  177
date =  178
date =  179
date =  180
date =  181
date =  182
date =  183
date =  184
date =  185
date =  186
date =  187
date =  188
date =  189
date =  190
date =  191
date =  192
date =  193
date =  194
date =  195
date =  196
date =  197
date =  198
date =  199
date =  200
date =  201
date =  202
date =  203
date =  204
date =  205
date =  206
date =  207
date =  208
date =  209
date =  210
date =  211
date =  212
date =  213
date =  214
date =  215
date =  216
date =  217
date =  218
date =  219
date =  220
date =  221
date =  222
date =  223
date =  224
date =  225
date =  226
date =  227
date =  228
date =  229
date =  230
date =  231
date =  232
date =  233
date =  234
date =  235
date =  236
date =  237
date =  238
date =  239
date =  240
date =  241
date =  242
date =  243
date =  244
date =  245
date =  246
date =  247
date =  248
date =  249
date =  250
date =  251
date =  252
date =  253
date =  254
date =  255
date =  256
date =  257
date =  258
date =  259
date =  260
date =  261
date =  262
date =  263
date =  264
date =  265
date =  266
date =  267
date =  268
date =  269
date =  270
date =  271
date =  272
date =  273
date =  274
date =  275
date =  276
date =  277
date =  278
date =  279
date =  280
date =  281
date =  282
date =  283
date =  284
date =  285
date =  286
date =  287
date =  288
date =  289
date =  290
date =  291
date =  292
date =  293
date =  294
date =  295
date =  296
date =  297
date =  298
date =  299
date =  300
date =  301
date =  302
date =  303
date =  304
date =  305
date =  306
date =  307
date =  308
date =  309
date =  310
date =  311
date =  312
date =  313
date =  314
date =  315
date =  316
date =  317
date =  318
date =  319
date =  320
date =  321
date =  322
date =  323
date =  324
date =  325
date =  326
date =  327
date =  328
date =  329
date =  330
date =  331
date =  332
date =  333
date =  334
date =  335
date =  336
date =  337
date =  338
date =  339
date =  340
date =  341
date =  342
date =  343
date =  344
date =  345
date =  346
date =  347
date =  348
date =  349
date =  350
date =  351
date =  352
date =  353
date =  354
date =  355
date =  356
date =  357
date =  358
date =  359
date =  360
date =  361
date =  362
date =  363
date =  364
date =  365
date =  366
date =  367
date =  368
date =  369
date =  370
date =  371
date =  372
date =  373
date =  374
date =  375
date =  376
date =  377
date =  378
date =  379
date =  380
date =  381
date =  382
date =  383
date =  384
date =  385
date =  386
date =  387
date =  388
date =  389
date =  390
date =  391
date =  392
date =  393
date =  394
date =  395
date =  396
date =  397
date =  398
date =  399
date =  400
date =  401
date =  402
date =  403
date =  404
date =  405
date =  406
date =  407
date =  408
date =  409
date =  410
date =  411
date =  412
date =  413
date =  414
date =  415
date =  416
date =  417
date =  418
date =  419
date =  420
date =  421
date =  422
date =  423
date =  424
date =  425
date =  426
date =  427
date =  428
date =  429
date =  430
date =  431
date =  432
date =  433
date =  434
date =  435
date =  436
date =  437
date =  438
date =  439
date =  440
date =  441
date =  442
date =  443
date =  444
date =  445
date =  446
date =  447
date =  448
date =  449
date =  450
date =  451
date =  452
date =  453
date =  454
date =  455
date =  456
date =  457
date =  458
date =  459
date =  460
date =  461
date =  462
date =  463
date =  464
date =  465
date =  466
date =  467
date =  468
date =  469
date =  470
date =  471
date =  472
date =  473
date =  474
date =  475
date =  476
date =  477
date =  478
date =  479
date =  480
date =  481
date =  482
date =  483
date =  484
date =  485
date =  486
date =  487
date =  488
date =  489
date =  490
date =  491
date =  492
date =  493
date =  494
date =  495
date =  496
date =  497
date =  498
date =  499
date =  500
date =  501
date =  502
date =  503
date =  504
date =  505
date =  506
date =  507
date =  508
date =  509
date =  510
date =  511
date =  512
date =  513
date =  514
date =  515
date =  516
date =  517
date =  518
date =  519
date =  520
date =  521
date =  522
date =  523
date =  524
date =  525
date =  526
date =  527
date =  528
date =  529
date =  530
date =  531
date =  532
date =  533
date =  534
date =  535
date =  536
date =  537
date =  538
date =  539
date =  540
date =  541
date =  542
date =  543
date =  544
date =  545
date =  546
date =  547
date =  548
date =  549
date =  550
date =  551
date =  552
date =  553
date =  554
date =  555
date =  556
date =  557
date =  558
date =  559
date =  560
date =  561
date =  562
date =  563
date =  564
date =  565
date =  566
date =  567
date =  568
date =  569
date =  570
date =  571
date =  572
date =  573
date =  574
date =  575
date =  576
date =  577
date =  578
date =  579
date =  580
date =  581
date =  582
date =  583
date =  584
date =  585
date =  586
date =  587
date =  588
date =  589
date =  590
date =  591
date =  592
date =  593
date =  594
date =  595
date =  596
date =  597
date =  598
date =  599
Out[4]:
<matplotlib.legend.Legend at 0x3b3ce50>

In [ ]: