-
Notifications
You must be signed in to change notification settings - Fork 0
/
SCEconomy_LSC_ns_lifecycle.py
executable file
·2675 lines (1972 loc) · 92.8 KB
/
SCEconomy_LSC_ns_lifecycle.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#import Yuki's library in the directory ./library
import sys
sys.path.insert(0, './library/')
import numpy as np
import numba as nb
###usage
###@nb.jit(nopython = True)
#my library
#import
from orderedTableSearch import locate, hunt
from FEM import femeval, fem_peval #1D interpolation
from FEM_2D import fem2d_peval, fem2deval_mesh #2D interpolation
from markov import Stationary
from ravel_unravel_nb import unravel_index_nb
from mpi4py import MPI
comm = MPI.COMM_WORLD #retreive the communicator module
rank = comm.Get_rank() #get the rank of the process
size = comm.Get_size() #get the number of processes
import time
# %matplotlib inline
# import matplotlib as mpl
# mpl.rc("savefig",dpi=100)
# from matplotlib import pyplot as plt
class Economy:
"""
class Economy stores all the economic parameters, prices, computational parameters,
grids information for a, kappa, eps, z, and shock transition matrix
"""
def __init__(self,
alpha = None,
nu = None,
beta = None,
iota = None,
chi = None,
delk = None,
delkap = None,
eta = None,
g = None,
grate = None,
la = None,
tau_wo = None,
tau_bo = None,
mu = None,
ome = None,
phi = None,
rho = None,
tauc = None,
taud = None,
taup = None,
theta = None,
trans_retire = None,
veps = None,
vthet = None,
xnb = None,
yn = None,
zeta = None,
lbar = None,
agrid = None,
epsgrid = None,
zgrid = None,
prob = None,
prob_yo = None,
is_to_iz = None,
is_to_ieps = None,
amin = None,
sim_time = None,
num_total_pop = None,
A = None,
path_to_data_i_s = None,
path_to_data_is_o = None,
taun = None,
psin = None,
psin_fixed = None,
nbracket = None,
nbracket_fixed = None,
scaling_n = None,
taub = None,
psib = None,
psib_fixed = None,
bbracket = None,
bbracket_fixed = None,
scaling_b = None):
self.__set_default_parameters__()
#set the parameters if designated
#I don't know how to automate these lines
if alpha is not None: self.alpha = alpha
if nu is not None: self.nu = nu
if beta is not None: self.beta = beta
if iota is not None: self.iota = iota
if chi is not None: self.chi = chi
if delk is not None: self.delk = delk
if delkap is not None: self.delkap = delkap
if eta is not None: self.eta = eta
if g is not None: self.g = g
if grate is not None: self.grate = grate
if la is not None: self.la = la
if tau_wo is not None: self.tau_wo = tau_wo
if tau_bo is not None: self.tau_bo = tau_bo
if mu is not None: self.mu = mu
if ome is not None: self.ome = ome
if phi is not None: self.phi = phi
if rho is not None: self.rho = rho
if tauc is not None: self.tauc = tauc
if taud is not None: self.taud = taud
# if taum is not None: self.taum = taum
# if taun is not None: self.taun = taun
if taup is not None: self.taup = taup
if theta is not None: self.theta = theta
if trans_retire is not None: self.trans_retire = trans_retire #added
if veps is not None: self.veps = veps
if vthet is not None: self.vthet = vthet
if xnb is not None: self.xnb = xnb
if yn is not None: self.yn = yn
if zeta is not None: self.zeta = zeta
if lbar is not None: self.lbar = lbar
if agrid is not None: self.agrid = agrid
if epsgrid is not None: self.epsgrid = epsgrid
if zgrid is not None: self.zgrid = zgrid
if prob is not None: self.prob = prob
if prob_yo is not None: self.prob_yo = prob_yo
if is_to_iz is not None: self.is_to_iz = is_to_iz
if is_to_ieps is not None: self.is_to_ieps = is_to_ieps
if amin is not None: self.amin = amin
if sim_time is not None: self.sim_time = sim_time
if num_total_pop is not None: self.num_total_pop = num_total_pop
if A is not None: self.A = A
if path_to_data_i_s is not None: self.path_to_data_i_s = path_to_data_i_s
if path_to_data_is_o is not None: self.path_to_data_is_o = path_to_data_is_o
if taun is not None: self.taun = taun
if psin is not None: self.psin = psin
if psin_fixed is not None: self.psin_fixed = psin_fixed
if nbracket is not None: self.nbracket = nbracket
if nbracket_fixed is not None: self.nbracket_fixed = nbracket_fixed
if scaling_n is not None: self.scaling_n = scaling_n
if taub is not None: self.taub = taub
if psib is not None: self.psib = psib
if psib_fixed is not None: self.psib_fixed = psib_fixed
if bbracket is not None: self.bbracket = bbracket
if bbracket_fixed is not None: self.bbracket_fixed = bbracket_fixed
if scaling_b is not None: self.scaling_b = scaling_b
#c
# if self.upsilon >= 1.0:
# print('Error: upsilon must be < 1 but upsilon = ', upsilon)
self.__set_nltax_parameters__()
self.__set_implied_parameters__()
def __set_default_parameters__(self):
"""
Load the baseline value
"""
self.__is_price_set__ = False
self.alpha = 0.3
self.nu = 0.55
self.beta = 0.98
self.iota = 1.0
self.chi = 0.0 #param for borrowing constarint
self.delk = 0.05
self.delkap = 0.05
self.eta = 0.42
self.g = 0.234 #govt spending
self.grate = 0.02 #gamma, growth rate for detrending
self.la = np.inf #lambda
self.tau_wo = 0.5 #added
self.tau_bo = 0.5 #added
self.mu = 1.5
self.ome = 0.6 #omega
self.phi = np.nan
self.rho = 0.01
self.tauc = 0.06
self.taud = 0.14
self.taum = 0.20
self.taun = 0.40
self.taup = 0.30
self.theta = 0.41
self.trans_retire = 0.48
self.veps = 0.4
self.vthet = 0.4
self.xnb = 0.185
self.yn = 0.451
self.zeta = 1.0 #totally tentative
self.lbar = 1.0
#borrowing constraint for C - guys. they just can't borrow in
self.amin = 0.0
self.sim_time = 1_000
self.num_total_pop = 100_000
self.A = 1.577707121233179 #this should give yc = 1 (approx.) z^2 case
self.path_to_data_i_s = './tmp/data_i_s'
self.path_to_data_is_o = './tmp/data_is_o'
self.taub = np.array([.137, .185, .202, .238, .266, .280])
self.bbracket = np.array([0.150, 0.319, 0.824, 2.085, 2.930])
self.scaling_b = 1.0
self.psib_fixed = 0.03
self.bbracket_fixed = 2
self.psib = None
self.taun = np.array([.2930, .3170, .3240, .3430, .3900, .4050, .4080, .4190])
self.nbracket = np.array([.1760, .2196, .2710, .4432, 0.6001, 1.4566, 2.7825])
self.scaling_n = 1.0
self.psin_fixed = 0.03
self.nbracket_fixed = 5
self.psin = None
self.agrid = np.load('./input_data/agrid.npy')
self.epsgrid = np.load('./input_data/epsgrid.npy')
self.zgrid = np.load('./input_data/zgrid.npy')
#conbined exogenous states
#s = (e,z)'
#pi(t,t+1)
self.prob = np.load('./DeBacker/prob_epsz.npy') #default transition is taken from DeBakcer
self.prob_yo = np.array([[44./45., 1./45.], [3./45., 42./45.]]) #[[y -> y, y -> o], [o -> y, o ->o]]
# ####do we need this one here?
# #normalization to correct rounding error.
# for i in range(prob.shape[0]):
# prob[i,:] = prob[i,:] / np.sum(prob[i,:])
self.is_to_iz = np.load('./input_data/is_to_iz.npy')
self.is_to_ieps = np.load('./input_data/is_to_ieps.npy')
self.s_age = None
self.c_age = None
self.sind_age = None
self.cind_age = None
self.y_age = None
self.o_age = None
def __set_nltax_parameters__(self):
from LinearTax import get_consistent_phi #the name is wrong
self.bbracket = self.bbracket * self.scaling_b
#set transfer term if not provided
if self.psib is None:
self.psib = get_consistent_phi(self.bbracket, self.taub, self.psib_fixed, self.bbracket_fixed) # we need to set the last two as arguments
tmp = self.bbracket
self.bbracket = np.zeros(len(tmp)+2)
self.bbracket[0] = -np.inf
self.bbracket[-1] = np.inf
self.bbracket[1:-1] = tmp[:]
self.nbracket = self.nbracket * self.scaling_n
#set transfer term if not provided
if self.psin is None:
self.psin = get_consistent_phi(self.nbracket, self.taun, self.psin_fixed, self.nbracket_fixed) # we need to set the last two as arguments
tmp = self.nbracket
self.nbracket = np.zeros(len(tmp)+2)
self.nbracket[0] = -np.inf
self.nbracket[-1] = np.inf
self.nbracket[1:-1] = tmp[:]
def __set_implied_parameters__(self):
#length of grids
self.num_a = len(self.agrid)
self.num_eps = len(self.epsgrid)
self.num_z = len(self.zgrid)
self.num_s = self.prob.shape[0]
#implied parameters
self.bh = self.beta*(1. + self.grate)**(self.eta*(1. - self.mu)) #must be less than one.
self.varrho = (1. - self.alpha - self.nu)/(1. - self.alpha) * self.vthet / (self.vthet + self.veps)
if self.bh >= 1.0:
print('Error: bh must be in (0, 1) but bh = ', self.bh)
self.prob_st = Stationary(self.prob)
self.prob_yo_st = Stationary(self.prob_yo)
def set_prices(self, p, rc):
self.p = p
self.rc = rc
#assuming CRS technology for C-corp
self.kcnc_ratio = ((self.theta * self.A)/(self.delk + self.rc))**(1./(1. - self.theta))
self.w = (1. - self.theta)*self.A*self.kcnc_ratio**self.theta
self.__is_price_set__ = True
#implied prices
self.rbar = (1. - self.taup) * self.rc
self.rs = (1. - self.taup) * self.rc
self.xi1 = ((self.ome*self.p)/(1. - self.ome))**(1./(self.rho-1.0)) #ok
self.xi2 = (self.ome + (1. - self.ome) * self.xi1**self.rho)**(1./self.rho) #ok
self.xi3 = self.eta/(1. - self.eta) * self.ome / (1. + self.tauc) / self.xi2**self.rho #changed
self.xi8 = ((self.p*(self.nu**self.nu)*(self.alpha**(1.-self.nu))/((self.w**self.nu)*((self.rs + self.delk)**(1.-self.nu)) )))**(1./(1.-self.alpha-self.nu))
self.xi13 = self.nu/self.alpha*(self.rs + self.delk)/self.w #ns = xi13*ks
self.denom = (1. + self.p*self.xi1)*(1. + self.tauc)
self.xi7 = 1./ self.denom #changed
self.xi4 = (1. + self.rbar) / self.denom #ok
self.xi5 = (1. + self.grate) / self.denom #ok
self.xi6 = (self.yn - self.xnb) / self.denom #changed
def print_parameters(self):
print('')
print('Parameters')
print('alpha = ', self.alpha)
print('nu = ', self.nu)
print('beta = ', self.beta)
print('chi = ', self.chi)
print('delk = ', self.delk)
print('delkap = ', self.delkap)
print('eta = ', self.eta)
print('g (govt spending) = ', self.g)
print('grate (growth rate of the economy) = ', self.grate)
print('la = ', self.la)
print('mu = ', self.mu)
print('ome = ', self.ome)
print('phi = ', self.phi)
print('rho = ', self.rho)
# print('varpi = ', self.varpi)
print('tauc = ', self.tauc)
print('taud = ', self.taud)
print('taup = ', self.taup)
print('theta = ', self.theta)
print('veps = ', self.veps)
print('vthet = ', self.vthet)
print('xnb = ', self.xnb)
print('yn = ', self.yn)
print('zeta = ', self.zeta)
print('A = ', self.A)
print('')
print('nonlinear tax function')
for ib, tmp in enumerate(self.taub):
print(f'taub{ib} = {tmp}')
for ib, tmp in enumerate(self.psib):
print(f'psib{ib} = {tmp}')
for ib, tmp in enumerate(self.bbracket):
print(f'bbracket{ib} = {tmp}')
for i, tmp in enumerate(self.taun):
print(f'taun{i} = {tmp}')
for i, tmp in enumerate(self.psin):
print(f'psin{i} = {tmp}')
for i, tmp in enumerate(self.nbracket):
print(f'nbracket{i} = {tmp}')
print('')
print('Parameters specific to a lifecycle model')
print('iota = ', self.iota) #added
# print('la_tilde = ', self.la_tilde) #added
print('tau_wo = ', self.tau_wo) #added
print('tau_bo = ', self.tau_bo) #added
print('trans_retire = ', self.trans_retire)
print(f'prob_yo = {self.prob_yo[0,0]}, {self.prob_yo[0,1]}, {self.prob_yo[1,0]}, {self.prob_yo[1,1]}.') #added
print('statinary dist of prob_yo = ', self.prob_yo_st) #added
print('')
if self.__is_price_set__:
print('')
print('Prices')
print('w = ', self.w)
print('p = ', self.p)
print('rc = ', self.rc)
print('')
print('Implied prices')
print('rbar = ', self.rbar)
print('rs = ', self.rs)
print('')
print('')
print('')
print('')
print('xi1 = ', self.xi1)
print('xi2 = ', self.xi2)
print('xi3 = ', self.xi3)
print('xi4 = ', self.xi4)
print('xi5 = ', self.xi5)
print('xi6 = ', self.xi6)
print('xi7 = ', self.xi7)
print('xi8 = ', self.xi8)
print('xi13 = ', self.xi13)
else:
print('')
print('Prices not set')
print('')
print('Computational Parameters')
print('amin = ', self.amin)
print('sim_time = ', self.sim_time)
print('num_total_pop = ', self.num_total_pop)
def generate_util(self):
bh = self.bh
eta = self.eta
mu = self.mu
@nb.jit(nopython = True)
def util(c, l):
if c > 0.0 and l > 0.0 and l <= 1.0:
return (1. - bh) * (((c**eta)*(l**(1. - eta)))**(1. - mu))
else:
if mu < 1.0:
return -np.inf
else:
return np.inf
#not way to code mu = 1.0 case
return util
def generate_dc_util(self):
bh = self.bh
eta = self.eta
mu = self.mu
#this is in the original form
@nb.jit(nopython = True)
def dc_util(c, l):
if c > 0.0 and l > 0.0 and l <= 1.0:
return eta * c**(eta*(1. - mu) - 1.0) * ((l**(1. - eta)))**(1. - mu)
else:
print('dc_util at c = ', c, ', l = ', l, 'is not defined.')
print('nan will be returned.')
return np.nan #???
return dc_util
# import math
def generate_cstatic(self):
# for variable in self.__dict__ : exec(variable+'= self.'+variable)
alpha = self.alpha
beta = self.beta
chi = self.chi
delk = self.delk
delkap = self.delkap
eta = self.eta
g = self.g
grate = self.grate
la = self.la
mu = self.mu
ome = self.ome
phi = self.phi
rho = self.rho
tauc = self.tauc
taud = self.taud
taup = self.taup
theta = self.theta
veps = self.veps
vthet = self.vthet
xnb = self.xnb
yn = self.yn
zeta = self.zeta
tau_wo = self.tau_wo
trans_retire = self.trans_retire
taun = self.taun
psin = self.psin
nbracket = self.nbracket
agrid = self.agrid
epsgrid = self.epsgrid
zgrid = self.zgrid
prob = self.prob
is_to_iz = self.is_to_iz
is_to_ieps = self.is_to_ieps
amin = self.amin
num_a = self.num_a
num_eps = self.num_eps
num_z = self.num_z
nu = self.nu
bh = self.bh
w = self.w
p = self.p
rc = self.rc
rbar = self.rbar
rs = self.rs
xi1 = self.xi1
xi2 = self.xi2
xi3 = self.xi3
xi4 = self.xi4
xi5 = self.xi5
xi6 = self.xi6
xi7 = self.xi7
xi8 = self.xi8
util = self.generate_util()
@nb.jit(nopython = True)
def get_cstatic(s):
a = s[0]
an = s[1]
eps = s[2]
is_o = s[3]
u = -np.inf
cc = -1.0
cs = -1.0
cagg = -1.0
l = -1.0
n = -1.0
if is_o:
eps = tau_wo*eps #replace eps with tau_wo*eps
#is this unique?
#repeat until n falls in bracket nuber i (i=0,1,2,..,I-1)
i = 0
j = 0
num_taun = len(taun)
wepsn = 0.0
for i in range(num_taun):
n = (xi3*w*eps*(1.-taun[i]) - xi4*a + xi5*an - xi6 - xi7*(psin[i] + is_o*trans_retire))/(w*eps*(1.-taun[i])*(xi3 + xi7))
# n = (xi3*w*eps*(1.-taun[i]) - xi4*a + xi5*an - xi6 - xi7*(psin[i] + is_o*trans_retire))/(w*eps*(1.-taun[i])*(xi3 + xi7))
wepsn = w*eps*n #wageincome
j = locate(wepsn, nbracket)
if i == j:
break
obj_i = 0.
obj_i1 = 0.
#when solution is at a kink
flag = True
flag2 = False
if i == len(taun) - 1 and i != j:
flag = False
flag2 = True
for i, wepsn in enumerate(nbracket[1:-1]): #remove -inf, inf
n = wepsn/w/eps
obj_i = n - ( (xi3*w*eps*(1.-taun[i]) - xi4*a + xi5*an - xi6 - xi7*(psin[i]+is_o*trans_retire))/(w*eps*(1.-taun[i])*(xi3 + xi7)))
obj_i1 = n - ( (xi3*w*eps*(1.-taun[i+1]) - xi4*a + xi5*an - xi6 - xi7*(psin[i+1]+is_o*trans_retire))/(w*eps*(1.-taun[i+1])*(xi3 + xi7)))
# obj_i = n - ( (xi3*w*eps*(1.-taun[i]) - xi4*a + xi5*an - xi6 - xi7*(psin[i] - is_o*trans_retire))/(w*eps*(1.-taun[i])*(xi3 + xi7)))
# obj_i1 = n - ( (xi3*w*eps*(1.-taun[i+1]) - xi4*a + xi5*an - xi6 - xi7*(psin[i+1] - is_o*trans_retire))/(w*eps*(1.-taun[i+1])*(xi3 + xi7)))
if obj_i * obj_i1 < 0:
flag = True
break
# if flag2 and flag:
# print('a solution is found at a corner ')
# print('obj_i = ', obj_i)
# print('obj_i1 = ', obj_i1)
# print('a = ', a)
# print('an = ', an)
# print('eps = ', eps)
# print('i = ', i)
# # print('j = ', j)
# print('n = ', n)
# print('wepsn = ', wepsn)
# print('')
if not flag:
print('no solution find ')
print('err: cstatic: no bracket for n')
print('a = ', a)
print('an = ', an)
print('eps = ', eps)
print('is_o = ', is_o)
print('trans_retire = ', trans_retire)
print('i = ', i)
print('j = ', j)
print('n = ', n)
print('w = ', w)
print('wepsn = ', wepsn)
print('')
if n < 0.0:
n = 0.0
wepsn = w*eps*n
i = locate(wepsn, nbracket)
if n >= 0. and n <= 1.:
l = 1. - n
#cc from FOC is wrong at the corner.
cc = xi4*a - xi5*an + xi6 + xi7*((1.-taun[i])*w*eps*n + psin[i] + is_o*trans_retire)
cs = xi1*cc
cagg = xi2*cc
u = util(cagg, 1. - n)
return u, cc, cs, cagg, l ,n, -1.0e23, -1.0e23, i, taun[i], psin[i] + is_o*trans_retire
return get_cstatic
def generate_sstatic(self):
###load vars###
alpha = self.alpha
beta = self.beta
chi = self.chi
delk = self.delk
delkap = self.delkap
eta = self.eta
g = self.g
grate = self.grate
la = self.la
mu = self.mu
ome = self.ome
phi = self.phi
rho = self.rho
tauc = self.tauc
taud = self.taud
taup = self.taup
theta = self.theta
veps = self.veps
vthet = self.vthet
xnb = self.xnb
yn = self.yn
zeta= self.zeta
lbar = self.lbar
tau_bo = self.tau_bo
trans_retire = self.trans_retire
taub = self.taub
psib = self.psib
bbracket = self.bbracket
agrid = self.agrid
epsgrid = self.epsgrid
zgrid = self.zgrid
prob = self.prob
is_to_iz = self.is_to_iz
is_to_ieps = self.is_to_ieps
amin = self.amin
num_a = self.num_a
num_eps = self.num_eps
num_z = self.num_z
nu = self.nu
bh = self.bh
varrho = self.varrho
w = self.w
p = self.p
rc = self.rc
rbar = self.rbar
rs = self.rs
denom = self.denom
xi1 = self.xi1
xi2 = self.xi2
xi3 = self.xi3
xi4 = self.xi4
xi5 = self.xi5
xi6 = self.xi6
xi7 = self.xi7
xi8 = self.xi8
xi13 = self.xi13
###end loading vars###
util = self.generate_util()
@nb.jit(nopython = True)
def get_sstatic(s):
a = s[0]
an = s[1]
z = s[2]
is_o = s[3]
if is_o:
z = tau_bo*z #replace eps with tau_wo*eps
# ks = (z*p*alpha/(rs+delk))**(1./(1. - alpha))
ks = z**(1./(1. - alpha - nu))*xi8 #should be the same as above
ns = xi13*ks
if an < chi *ks: #if the working capital constraint is binding
if an > 0.:
ks = an / chi
ns = (nu*p*z*ks**alpha/w)**(1./(1.0 - nu))
else:
ks = 0.0
ns = 0.0
ys = z*(ks**alpha)*(ns**nu)
#bizinc does not depend on tax rate
bizinc = p*ys - (rs+delk)*ks - w*ns
ibracket = locate(bizinc, bbracket) #check if this is actually working
#initial
u = -np.inf
cc = -1.0
cs = -1.0
cagg = -1.0
# cc = xi4*a - xi5*an + xi6 + xi11*(p*ys - (rs+delk)*ks)
cc = xi4*a - xi5*an + (1.- taub[ibracket])/denom*bizinc + (psib[ibracket] + is_o*trans_retire + yn - xnb)/denom
# cc = xi4*a - xi5*an + (1.- taub[ibracket])*xi7*bizinc + (psib[ibracket])*xi7 + xi6
cs = xi1*cc
cagg = xi2 * cc
#adhoc feasbility check
if (cagg > 0.0):
u = util(cagg, lbar)
#else, return -np.inf
# lbar is a given constant
#mx, my, x are set to np.nan.
return u, cc, cs, cagg, lbar, -1.0e20, ks, ys, ibracket, taub[ibracket], psib[ibracket] + is_o*trans_retire, ns
return get_sstatic
def get_policy(self, max_iter = 20, max_howard_iter = 100):
# I found this magic is very dangerous.
for variable in self.__dict__ : exec(variable+'= self.'+variable, locals(), globals())
Econ = self
num_total_state = num_a* num_s
m = num_total_state // size
r = num_total_state % size
assigned_state_range = (rank*m+min(rank,r),(rank+1)*m+min(rank+1,r))
num_assigned = assigned_state_range[1] - assigned_state_range[0]
all_assigned_state_range = np.ones((size, 2))*(-2.)
all_num_assigned = ()
all_istart_assigned = ()
for irank in range(size):
all_istart_assigned += (int(irank*m+min(irank,r)), )
all_assigned_state_range[irank,0] = irank*m+min(irank,r)
all_assigned_state_range[irank,1] = (irank+1)*m+min(irank+1,r)
all_num_assigned += (int(all_assigned_state_range[irank,1] - all_assigned_state_range[irank,0]),)
###end parameters for MPI###
@nb.jit(nopython = True)
def unravel_ip(i_aggregated_state):
istate, ia = unravel_index_nb(i_aggregated_state, num_s, num_a)
#ia, ikap, istate = unravel_index_nb(i_aggregated_state, num_a, num_kap, num_s)
return istate, ia
get_cstatic = Econ.generate_cstatic()
c_supan = np.ones((num_a, num_eps, 2)) * (-2.)
#for young c-corp workers
for ia, a in enumerate(agrid):
for ieps, eps in enumerate(epsgrid):
c_supan[ia, ieps, 0] = ((1. + rbar)*a + (1. - taun[0])*w*eps + psin[0] + yn - xnb)/(1. + grate)
#for old c-corp workers
for ia, a in enumerate(agrid):
for ieps, eps in enumerate(epsgrid):
c_supan[ia, ieps, 1] = ((1. + rbar)*a + (1. - taun[0])*tau_wo*w*eps + psin[0] + trans_retire + yn - xnb)/(1. + grate)
get_sstatic = Econ.generate_sstatic()
#to solve S-optimization problem, we need the max feasible set for an [amin, sup_an]
s_supan = np.ones((num_a, num_z, 2)) * (-2.)
### ks = xi8 ##this is wrong
for iz, z in enumerate(zgrid):
for ia, a in enumerate(agrid):
for is_old in range(2):
ks = z**(1./(1. - alpha - nu))*xi8 #should be the same as above
ns = xi13*ks
ys = z*(ks**alpha)*(ns**nu)
#first tax bracket is picked. I don't have a clear reqson for it.
s_supan[ia, iz, is_old] = ((1. + rbar)*a + (1. - taub[0])*(p*ys - (rs + delk)*ks - w*ns) + psib[0] + is_old*trans_retire + yn - xnb)/(1. + grate)
del ks, ys, ns
# objective function of VFI optimization problem
@nb.jit(nopython = True)
def _obj_loop_(*args):
_an_ = args[0]
_EV_ = args[1]
_ia_ = args[2]
_istate_ = args[3]
_is_c_ = args[4]
_is_old_ = args[5] # 0 (young) or 1 (old)
u = 0.0
if _is_c_:
u = get_cstatic(np.array([agrid[_ia_], _an_, epsgrid[is_to_ieps[_istate_]], _is_old_]))[0]
else:
u = get_sstatic(np.array([agrid[_ia_], _an_, zgrid[is_to_iz[_istate_]], _is_old_]))[0]
# isn't it wrong?
# return -(u + bh*fem_peval(_an_, agrid, _EV_[0, :, _istate_])**(1. - mu))**(1./(1. - mu))
# usually _EV_ is already discounted
return -(u + fem_peval(_an_, agrid, _EV_[0, :, _istate_] ))**(1./(1. - mu))
#epsilon = np.finfo(float).eps
@nb.jit(nopython = True)
def _optimize_given_state_(_an_min_, _an_sup_, _EV_, _ia_ ,_istate_, _is_c_, _is_old_):
#arguments
ax = _an_min_
cx = _an_sup_
bx = 0.5*(ax + cx)
tol=1.0e-8
itmax=500
#parameters
CGOLD=0.3819660
ZEPS=1.0e-3*2.2204460492503131e-16
#*np.finfo(float).eps
brent = 1.0e20
xmin = 1.0e20
a=min(ax,cx)
b=max(ax,cx)
v=bx
w=v
x=v
e=0.0
# print('is_c = ',_is_c_)
fx= _obj_loop_(x, _EV_, _ia_ ,_istate_, _is_c_, _is_old_)
fv=fx
fw=fx
d = 0.0
it = 0
for it in range(itmax):
xm=0.5*(a+b)
tol1=tol*abs(x)+ZEPS
tol2=2.0*tol1
tmp1 = tol2 - 0.5*(b-a)
tmp2 = abs(x-xm)
if abs(x - xm) <= tol2 - 0.5*(b - a):
it = itmax
xmin=x
# brent=fx
if (abs(e) > tol1):
r=(x-w)*(fx-fv)
q=(x-v)*(fx-fw)
p=(x-v)*q-(x-w)*r
q=2.0*(q-r)
if (q > 0.0):
p=-p
q=abs(q)
etemp=e
e=d
if abs(p) >= abs(0.5*q*etemp) or p <= q*(a-x) or p >= q*(b-x):
#e=merge(a-x,b-x, x >= xm )
if x >= xm:
e = a-x
else:
e = b-x
d=CGOLD*e
else:
d=p/q
u=x+d