-
Notifications
You must be signed in to change notification settings - Fork 0
/
SCEconomy_hy_ns_lifecycle.py
executable file
·4155 lines (2989 loc) · 154 KB
/
SCEconomy_hy_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 FEM import fem_peval #1D interpolation
from orderedTableSearch import locate, hunt
from FEM_2D import fem2d_peval, fem2deval_mesh
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,
beta = None,
iota = None, # iota \in [0, 1], which is
chi = None,
delk = None,
delkap = None,
eta = None,
g = None,
grate = None,
la = None,
la_tilde = None, #preserved sweat_capital if it is taken over
tau_wo = None, #c-productivity decline if s/he is old
tau_bo = None, #s-productiity decline is s/he is old
mu = None,
ome = None,
phi = None,
rho = None,
tauc = None,
taud = None,
taup = None,
theta = None,
trans_retire = None, #retirement benefit which is not included in tran
veps = None,
vthet = None,
xnb = None,
yn = None,
zeta = None,
agrid = None,
kapgrid = None,
epsgrid = None,
zgrid = None,
prob = None,
prob_yo = None, #young-old transition matrix
is_to_iz = None,
is_to_ieps = None,
num_suba_inner = None,
num_subkap_inner = None,
sim_time = None,
num_total_pop = None,
A = None,
upsilon = None,
varpi = 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 beta is not None: self.beta = beta
if iota is not None: self.iota = iota #added
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 la_tilde is not None: self.la_tilde = la_tilde #added
if tau_wo is not None: self.tau_wo = tau_wo #added
if tau_bo is not None: self.tau_bo = tau_bo #added
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 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 agrid is not None: self.agrid = agrid
if kapgrid is not None: self.kapgrid = kapgrid
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 num_suba_inner is not None: self.num_suba_inner = num_suba_inner
if num_subkap_inner is not None: self.num_subkap_inner = num_subkap_inner
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 upsilon is not None: self.upsilon = upsilon
if varpi is not None: self.varpi = varpi
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
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.beta = 0.98
self.iota = 1.0 #added
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 = 0.7 #lambda
self.la_tilde = 0.1 #added lambda_tilde
self.tau_wo = 0.5 #added
self.tau_bo = 0.5 #added
self.mu = 1.5
self.ome = 0.6 #omega
self.phi = 0.15
self.rho = 0.01
self.tauc = 0.06
self.taud = 0.14
self.taup = 0.30
self.theta = 0.41
self.trans_retire = 0.48 #added retirement benefit
self.veps = 0.4
self.vthet = 0.4
self.xnb = 0.185
self.yn = 0.451
self.zeta = 1.0
self.sim_time = 500
self.num_total_pop = 25_000
self.A = 1.577707121233179 #this should give yc = 1 (approx.) z^2 case
self.upsilon = 0.5
self.varpi = 0.5
# self.path_to_data_i_s = './input_data/data_i_s.npy'
self.path_to_data_i_s = './tmp/data_i_s'
self.path_to_data_is_o = './tmp/data_is_o'
#nonlinear tax parameters
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 #0.15
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 #0.15
self.nbracket_fixed = 5
self.psin = None
#grid information
self.agrid = np.load('./input_data/agrid.npy')
self.kapgrid = np.load('./input_data/kapgrid.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]]
# self.prob = np.load('./input_data/transition_matrix.npy')
# self.prob_yo = np.array([[0.5, 0.5], [0.5, 0.5]]) #[[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')
#computational parameters
self.num_suba_inner = 20
self.num_subkap_inner = 30
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 PiecewiseLinearTax import get_consistent_phi
# 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_kap = len(self.kapgrid)
self.num_eps = len(self.epsgrid)
self.num_z = len(self.zgrid)
self.num_s = self.prob.shape[0]
#implied parameters
self.nu = 1. - self.alpha - self.phi
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 or self.bh <= 0.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
#set Xi-s.
self.xi1 = ((self.ome*self.p)/(1. - self.ome))**(1./(self.rho-1.0))
self.xi2 = (self.ome + (1. - self.ome) * self.xi1**self.rho)**(1./self.rho)
self.xi3 = self.eta/(1. - self.eta) * self.ome / (1. + self.tauc) / self.xi2**self.rho #changed
self.denom = (1. + self.p*self.xi1)*(1. + self.tauc)
self.xi4 = (1. + self.rbar) / self.denom
self.xi5 = (1. + self.grate) / self.denom
self.xi6 = (self.yn - self.xnb) / self.denom #changed
self.xi7 = 1./ self.denom #changed
self.xi8 = ((self.alpha*self.p)/(self.rs + self.delk))**(1./(1.-self.alpha))
self.xi11 = self.xi7 #modified
self.xi10 = (self.p*self.xi8**self.alpha - (self.rs + self.delk)*self.xi8)/self.denom
self.xi13 = (self.nu*self.varpi*(self.rs + self.delk)/(self.alpha * self.w))**(1./(1.- self.upsilon))
self.xi14 = self.w*self.xi13*(self.xi8**(1./(1.-self.upsilon)))/self.denom
self.xi9 = (self.eta*self.ome*self.nu*(1.-self.varpi)*self.p*self.xi8**self.alpha)\
/((1.-self.eta)*(1.+self.tauc)*self.xi2**self.rho)
self.xi12 = (self.vthet/self.veps)*self.nu*(1.-self.varpi)*self.p*self.xi8**self.alpha
def print_parameters(self):
print('')
print('Parameters')
print('alpha = ', self.alpha)
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('upsilon = ', self.upsilon)
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('Implied Parameters')
print('nu = ', self.nu)
print('bh (beta_tilde) = ', self.bh)
print('varrho = ', self.varrho)
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('xi9 = ', self.xi9)
print('xi10 = ', self.xi10)
print('xi11 = ', self.xi11)
print('xi12 = ', self.xi12)
print('xi13 = ', self.xi13)
print('xi14 = ', self.xi14)
else:
print('')
print('Prices not set')
print('')
print('Computational Parameters')
print('num_suba_inner = ', self.num_suba_inner)
print('num_subkap_inner = ', self.num_subkap_inner)
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:
return -np.inf
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
def generate_cstatic(self):
#load variables
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
tau_wo = self.tau_wo
mu = self.mu
ome = self.ome
phi = self.phi
rho = self.rho
tauc = self.tauc
taud = self.taud
taup = self.taup
theta = self.theta
taun = self.taun
psin = self.psin
nbracket = self.nbracket
vthet = self.vthet
xnb = self.xnb
yn = self.yn
zeta= self.zeta
trans_retire = self.trans_retire
agrid = self.agrid
kapgrid = self.kapgrid
epsgrid = self.epsgrid
zgrid = self.zgrid
prob = self.prob
is_to_iz = self.is_to_iz
is_to_ieps = self.is_to_ieps
num_suba_inner = self.num_suba_inner
num_subkap_inner = self.num_subkap_inner
num_a = self.num_a
num_kap = self.num_kap
num_eps = self.num_eps
num_z = self.num_z
num_s = self.prob.shape[0]
nu = self.nu
bh = self.bh
varrho = self.varrho
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
xi9 = self.xi9
xi10 = self.xi10
xi11 = self.xi11
xi12 = self.xi12
#end loading
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] # if young, this is 1. if old, 0. (or True, False)
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
# print('tau_wo = ', tau_wo)
# print('trans_retire = ', trans_retire)
#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))
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: #if i is not identified above
flag = False
flag2 = True
for i, wepsn in enumerate(nbracket[1:-1]): #remove -inf, inf
#maybe it does not matter which bracket he is in?
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)))
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('i = ', i)
print('j = ', j)
print('n = ', n)
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, i, taun[i], psin[i] + is_o*trans_retire
return get_cstatic
def generate_sstatic(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
upsilon = self.upsilon
rho = self.rho
varpi = self.varpi
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_bo = self.tau_bo
trans_retire = self.trans_retire
taub = self.taub
psib = self.psib
bbracket = self.bbracket
agrid = self.agrid
kapgrid = self.kapgrid
epsgrid = self.epsgrid
zgrid = self.zgrid
prob = self.prob
is_to_iz = self.is_to_iz
is_to_ieps = self.is_to_ieps
num_suba_inner = self.num_suba_inner
num_subkap_inne = self.num_subkap_inner
num_a = self.num_a
num_kap = self.num_kap
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
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
xi9 = self.xi9
xi10 = self.xi10
xi11 = self.xi11
xi12 = self.xi12
xi13 = self.xi13
xi14 = self.xi14
util = self.generate_util()
@nb.jit(nopython = True)
def Hy(h, alp6):
tmp = (1. - alp6*h**((nu/(1.-alpha) - upsilon)*(upsilon/(1.-upsilon))-upsilon))
tmp = tmp / (1. - varpi)
tmp = (tmp**(1./upsilon))*h
return tmp
@nb.jit(nopython = True)
def g(h, alp6):
return ((h**(upsilon - nu/(1.-alpha)))*(Hy(h, alp6)**(1.-upsilon)))**(vthet/(veps+vthet))
@nb.jit(nopython = True)
def get_h_lbar(alp6):
return alp6**(1./(upsilon + (upsilon - nu/(1.-alpha))*(upsilon/(1.-upsilon))))
@nb.jit(nopython = True)
def solve_hhyhkap(s):
#return h, hy, hkap
a = s[0]
an = s[1]
kap = s[2]
kapn = s[3]
z = s[4]
is_o = s[5]
if is_o:
z = tau_bo*z #replace eps with tau_wo*eps
#case 0
if (kap == 0.0 and kapn > 0.0) or (kap > 0.0 and kap < 1.0e-9 and kapn > (1. - delkap)/(1. + grate) * kap):
#if (kap < 1.0e-10) and kapn >= 1.0e-10:
#New version which is consistent with kap>0 version in limit
alp1 = eta/(1. - eta) * ome / xi2**rho / (1. + tauc) #updated
alp2 = vthet*(xi4*a - xi5*an + xi6 + xi7*(psib + is_o*trans_retire))/(1.-taub)/veps/ ((1.+grate)*kapn/zeta)**(1./vthet)
alp3 = vthet/(veps*((1. + p*xi1)*(1. + tauc))) #updated
hk_min = 1.0e-10 #used to be 0., but hk_min**(-1) = 0.**(-1.) is not ok in python grammer. in numba, it works though.
hk_max = 1.0
####bisection start
x_lb = ((((1. + grate)*kapn - (1. - delkap)*kap)/zeta)**(1./vthet))*hk_min**(-veps/vthet)
ib_lb = locate(-x_lb, bbracket)
val_lb = alp1*(1. - hk_min) - alp2[ib_lb]*hk_min**((vthet + veps)/vthet) + alp3*hk_min
x_ub = ((((1. + grate)*kapn - (1. - delkap)*kap)/zeta)**(1./vthet))*hk_max**(-veps/vthet)
ib_ub = locate(-x_ub, bbracket)
val_ub = alp1*(1. - hk_max) - alp2[ib_ub]*hk_max**((vthet + veps)/vthet) + alp3*hk_max
if val_lb *val_ub > 0.0:
# print('warning : no bracket')
return -1., -1., -1.
sign = -1.0
if val_ub > 0.:
sign = 1.0
hk = (hk_max + hk_min)/2.
x = ((((1. + grate)*kapn - (1. - delkap)*kap)/zeta)**(1./vthet))*hk**(-veps/vthet)
ib = locate(-x, bbracket)
it = 0
tol = 1.0e-12
maxit = 200
val_m = 10000.
diff = 1.0e23
while it < maxit:
it = it + 1
val_m = alp1*(1. - hk) - alp2[ib]*hk**((vthet + veps)/vthet) + alp3*hk
if sign * val_m > 0.:
hk_max = hk
elif sign * val_m < 0.:
hk_min = hk
diff = abs((hk_max + hk_min)/2 - hk)
hk = (hk_max + hk_min)/2.
x = ((((1. + grate)*kapn - (1. - delkap)*kap)/zeta)**(1./vthet))*hk**(-veps/vthet)
ib = hunt(-x, bbracket, ib)
if diff < tol:
break
#convergence check
if it == maxit or diff >= tol:
print('err: bisection method for hmax did not converge.')
print('val_m = ', val_m)
print('hk = ', hk)
print('hk_max = ', hk_max)
print('diff = ', diff)
####bisection end
# # mx_lb = max( (alp3*vthet/(alp2*(vthet + veps)))**(vthet/veps), (alp3/alp2) ) #typo?
# # mx_lb = max( (alp3*vthet/(alp2*(vthet + veps)))**(vthet/veps), (alp3/alp2)**(vthet/veps) )
# ###start newton method
# mx = mx_lb
# # print('mx = ', mx_lb)
# it = 0
# maxit = 100 #scipy's newton use maxit = 50
# tol = 1.0e-15
# dist = 10000000.
# while it < maxit:
# it = it + 1
# res = alp1*(1. - mx) - alp2*mx**((vthet + veps)/vthet) + alp3*mx
# dist = abs(res)
# if dist < tol:
# break
# dres= -alp1 - alp2*((vthet + veps)/vthet)*mx**(veps/vthet) + alp3
# diff = res/dres
# mx = mx - res/dres
# #convergence check
# if it == maxit:
# print('err: newton method for mx did not converge.')
# print('mx = ', mx)
# ans = mx
# ###end newton method
return 0., 0., hk
#case 1
elif kap == 0.0 and kapn == 0.0:
return 0.0, 0.0, 0.0
#case 2 -- the main case--
elif kap > 0.0 and kapn > (1. - delkap)/(1. + grate) * kap:
alp1 = xi9
alp2 = (xi4*a - xi5*an + xi6 + xi7*(psib+is_o*trans_retire))/(1.-taub)/((z*kap**phi)**(1./(1.-alpha))) #updated
alp3 = xi10
alp5 = (((((1. + grate)*kapn - (1. - delkap)*kap)/zeta)**(1./vthet))/(xi12 * (z*kap**phi)**(1./(1.-alpha))))**(vthet/(vthet + veps))
alp4 = xi11 * xi12 * alp5
alp6 = varpi*(xi13**upsilon)*(xi8*(z*kap**phi)**(1./(1.-alpha)))**(upsilon/(1.-upsilon))
alp7 = xi14*(z*kap**phi)**((1./(1.-alpha))*(upsilon/(1.-upsilon))) #
h_lbar = alp6**(1./(upsilon + (upsilon - nu/(1.-alpha))*(upsilon/(1.-upsilon))))
### we can do better ###
tmp = (alp6 + 1. - varpi)
h_hbar_plus = max(tmp**(1./upsilon), tmp**(1./(upsilon + (upsilon - nu/(1.-alpha))*(upsilon/(1.-upsilon)))) )
hmax_lb = h_lbar
hmax_ub = h_hbar_plus
####bisection start
val_lb = 1. - Hy(hmax_lb, alp6) - alp5*g(hmax_lb, alp6)
val_ub = 1. - Hy(hmax_ub, alp6) - alp5*g(hmax_ub, alp6)
if val_lb * val_ub > 0.0:
print('error: no bracket')
sign = -1.0
if val_ub > 0.:
sign = 1.0
hmax = (hmax_lb + hmax_ub)/2.
it = 0
tol = 1.0e-12
maxit = 200
val_m = 10000.
diff = 1.0e23
while it < maxit:
it = it + 1
val_m = 1. - Hy(hmax, alp6) - alp5*g(hmax, alp6)
if sign * val_m > 0.:
hmax_ub = hmax
elif sign * val_m < 0.:
hmax_lb = hmax
diff = abs((hmax_lb + hmax_ub)/2 - hmax)
hmax = (hmax_lb + hmax_ub)/2.
if diff < tol:
break
#convergence check
if it == maxit or diff >= tol:
print('err: bisection method for hmax did not converge.')
print('val_m = ', val_m)
print('mymax = ', hmax)
####bisection end
####bisection start
#setting h = h_lb makes x = inf, which is problematic. no warning will show up in numba.
#in the end, x = inf makes bizinc = -inf, so we correctly infer the lowest bracket for now.
h_lb = h_lbar + 1.0e-10
h_ub = hmax
#check bracketting