-
Notifications
You must be signed in to change notification settings - Fork 0
/
SCEconomy_lifecycle_give_A.py
executable file
·3956 lines (2870 loc) · 136 KB
/
SCEconomy_lifecycle_give_A.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 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,
taum = None,
taun = None,
taup = None,
theta = None,
tran = 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,
amin = None,
num_suba_inner = None,
num_subkap_inner = None,
sim_time = None,
num_total_pop = None,
A = 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 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 tran is not None: self.tran = tran
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 amin is not None: self.amin = amin
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
self.__set_implied_parameters__()
def __set_default_parameters__(self):
"""
Load the baseline value
"""
self.__is_price_set__ = False
self.alpha = 0.4
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.5 #lambda
self.la_tilde = 0.5 #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.5
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.tran = 0.03 #psi
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 = 1000
self.num_total_pop = 100_000
self.A = 1.577707121233179 #this should give yc = 1 (approx.) z^2 case
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('./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]]
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')
#computational parameters
self.amin = 0.0
self.num_suba_inner = 20
self.num_subkap_inner = 30
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, w, p, rc):
self.w = w
self.p = p
self.rc = rc
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))
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.taun) / (1. + self.tauc) * self.w / self.xi2**self.rho
self.xi8 = (self.alpha*self.p/(self.rs + self.delk))**(1./(1. - self.alpha))
self.xi9 = self.eta / (1. - self.eta) * self.ome * self.p * self.nu * (1. - self.taum) / (1. + self.tauc) * self.xi8**self.alpha / self.xi2**self.rho
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_y = (self.tran + self.yn - self.xnb) / self.denom
self.xi6_o = (self.tran + self.trans_retire + self.yn - self.xnb) / self.denom
self.xi7 = (1. - self.taun)*self.w/self.denom
self.xi11 = (1. - self.taum) / self.denom
self.xi10 = (self.p*self.xi8**self.alpha - (self.rs + self.delk)*self.xi8)*(1. - self.taum)/self.denom
self.xi12 = self.vthet/self.veps*self.nu*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('ome = ', self.ome)
print('phi = ', self.phi)
print('rho = ', self.rho)
print('tauc = ', self.tauc)
print('taud = ', self.taud)
print('taum = ', self.taum)
print('taun = ', self.taun)
print('taup = ', self.taup)
print('theta = ', self.theta)
print('tran (transfer) = ', self.tran)
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('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) #added
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('xi6_y = ', self.xi6_y)
print('xi6_o = ', self.xi6_o)
print('xi7 = ', self.xi7)
print('xi8 = ', self.xi8)
print('xi9 = ', self.xi9)
print('xi10 = ', self.xi10)
print('xi11 = ', self.xi11)
print('xi12 = ', self.xi12)
else:
print('')
print('Prices not set')
print('')
print('Computational Parameters')
print('amin = ', self.amin)
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):
###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
taum = self.taum
taun = self.taun
taup = self.taup
theta = self.theta
tran = self.tran
veps = self.veps
vthet = self.vthet
xnb = self.xnb
yn = self.yn
zeta= self.zeta
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
amin = self.amin
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
varrho = self.varrho
w = self.w
p = self.p
rc = self.rc
rbar = self.rbar
rs = self.rs
###end loading vars###
@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):
###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
taum = self.taum
taun = self.taun
taup = self.taup
theta = self.theta
tran = self.tran
veps = self.veps
vthet = self.vthet
xnb = self.xnb
yn = self.yn
zeta= self.zeta
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
amin = self.amin
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
varrho = self.varrho
w = self.w
p = self.p
rc = self.rc
rbar = self.rbar
rs = self.rs
###end loading vars###
#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
taum = self.taum
taun = self.taun
taup = self.taup
theta = self.theta
tran = self.tran
veps = self.veps
vthet = self.vthet
xnb = self.xnb
yn = self.yn
zeta= self.zeta
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
amin = self.amin
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
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
xi6_y = self.xi6_y
xi6_o = self.xi6_o
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, an, eps, is_o] (eps is the original one)
a = s[0]
an = s[1]
eps = s[2]
is_o = s[3] # if young, this is 1. if old, 0. (or True, False)
#this is a bit dangerous since is_o can be other than 0 and 1
if is_o:
xi6 = xi6_o
eps = tau_wo*eps #replace eps with tau_wo*eps
else:
xi6 = xi6_y
u = -np.inf
cc = -1.0
cs = -1.0
cagg = -1.0
l = -1.0
n = -1.0
if eps <= 0.0: #if they have non-positive productivity, labor supply is zero.
n = 0.0
else:
n = (xi3*eps - xi4*a + xi5*an - xi6)/(eps*(xi3 + xi7))
n = max(n, 0.0)
if n >= 0. and n <= 1.:
l = 1. - n
#cc = xi3*eps*(1. - temp_n) #this is wrong at the corner.
cc = xi4*a - xi5*an + xi6 + xi7*eps*n
cs = xi1*cc
cagg = xi2*cc
u = util(cagg, 1. - n)
return u, cc, cs, cagg, l ,n
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
tau_wo = self.tau_wo
tau_bo = self.tau_bo
mu = self.mu
ome = self.ome
phi = self.phi
rho = self.rho
tauc = self.tauc
taud = self.taud
taum = self.taum
taun = self.taun
taup = self.taup
theta = self.theta
tran = self.tran
veps = self.veps
vthet = self.vthet
xnb = self.xnb
yn = self.yn
zeta= self.zeta
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
amin = self.amin
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
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
xi6_y = self.xi6_y
xi6_o = self.xi6_o
xi7 = self.xi7
xi8 = self.xi8
xi9 = self.xi9
xi10 = self.xi10
xi11 = self.xi11
xi12 = self.xi12
###end loading vars###
util = self.generate_util()
@nb.jit(nopython = True)
def obj_find_mx(*args):
mx = args[0]
alp1 = args[1]
alp2 = args[2]
alp3 = args[3]
return alp1*(1. - mx) - alp2*mx**((vthet + veps)/vthet) + alp3*mx
@nb.jit(nopython = True)
def d_obj_find_mx(*args):
mx = args[0]
alp1 = args[1]
alp2 = args[2]
alp3 = args[3]
return -alp1 - alp2*((vthet + veps)/vthet)*mx**(veps/vthet) + alp3
@nb.jit(nopython = True)
def obj_find_mymax(*args):
my = args[0]
alp5 = args[1]
return 1. - my - alp5*my**varrho
@nb.jit(nopython = True)
def d_obj_find_mymax(*args):
my = args[0]
alp5 = args[1]
return - 1.0 - varrho*alp5*my**(varrho - 1.0)
@nb.jit(nopython = True)
def obj_find_my(*args):
my = args[0]
alp1 = args[1]
alp2 = args[2]
alp3 = args[3]
alp4 = args[4]
alp5 = args[5]
return alp1*(1. - my - alp5*my**varrho) - alp2 * my**(1. - nu/(1. - alpha)) - alp3*my + alp4*my**varrho
@nb.jit(nopython = True)
def solve_mxmy(s):#return mx and my given (a, \kappa, a', \kappa'; z, is_o). z is not adjusted for tau_bo
a = s[0]
an = s[1]
kap = s[2]
kapn = s[3]
z = s[4]
is_o = s[5]
if is_o:
xi6 = xi6_o
z = tau_bo*z #replace eps with tau_wo*eps
else:
xi6 = xi6_y
if kap == 0.0 and kapn > 0.0:
alp1 = eta/(1. - eta) * ome / xi2**rho / (1. + tauc)
alp2 = vthet*(xi4*a - xi5*an + xi6)/veps/ ((1.+grate)*kapn/zeta)**(1./vthet)
alp3 = vthet/(veps*denom)
if alp2 == alp3:
return 1.0, 0.0 #in this case, utility must be -inf
if (alp2< alp3) or (alp2 <=0):
return -1., -1. #the solution does not exist
mx_lb = max( (alp3*vthet/(alp2*(vthet + veps)))**(vthet/veps), (alp3/alp2) )
# obj = lambda mx: alp1*(1. - mx) - alp2*mx**((vthet + veps)/vthet) + alp3*mx
# objprime = lambda mx: -alp1 - alp2*((vthet + veps)/vthet)*mx**(veps/vthet) + alp3
# ans = newton(obj_find_mx, mx_lb , fprime = d_obj_find_mx, args = (alp1, alp2, alp3), tol = 1.0e-15)
###start newton method
mx = mx_lb
it = 0
maxit = 100 #scipy's newton use maxit = 50
tol = 1.0e-15
dist = 10000
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.')
ans = mx
###end newton method
return ans, 0.
elif kap == 0.0 and kapn == 0.0:
#m=0.0, my = 0.0, x = 0.0
#x = 0.0 should be designated outside of this function.
return 0.0, 0.0
elif kap > 0.0 and kapn > (1. - delkap)/(1. + grate) * kap: ##>=
alp1 = xi9
alp2 = (xi4 * a - xi5*an + xi6)/((z*kap**phi)**(1./(1.-alpha)))
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
# obj_mymax = lambda my: 1. - my - alp5*my**varrho
# obj_mymax_prime = lambda my: - 1.0 - varrho*alp5*my**(varrho - 1.0)
# mymax = brentq(obj_find_mymax, 0., 1., args = (alp5,))
####bisection start
mymax_lb = 0.
mymax_ub = 1.
#check bracketting
val_lb = 1. - mymax_lb - alp5*mymax_lb**varrho
val_ub = 1. - mymax_ub - alp5*mymax_ub**varrho
if val_lb *val_ub > 0.0:
print('error: no bracket')
sign = -1.0
if val_ub > 0.:
sign = 1.0
mymax = (mymax_lb + mymax_ub)/2.
it = 0
tol = 1.0e-12
maxit = 200
val_m = 10000
while it < maxit:
it = it + 1
val_m = 1. - mymax - alp5*mymax**varrho
if sign * val_m > 0.:
mymax_ub = mymax
elif sign * val_m < 0.:
mymax_lb = mymax
diff = abs((mymax_lb + mymax_ub)/2 - mymax)
mymax = (mymax_lb + mymax_ub)/2.
if diff < tol:
break
#convergence check
if it == maxit:
print('err: bisection method for mymax did not converge.')
print('val_m = ', val_m)
print('mymax = ', mymax)
####bisection end
# if mymax <= 0.0 or mymax >= 1.0:
# do we need this part?
if mymax < 0.0 or mymax > 1.0:
print('mymax is not well-solved or a corner solution')
return -1., -1.
# obj = lambda my: alp1*(1. - my - alp5*my**varrho) - alp2 * my**(1. - nu/(1. - alpha)) - alp3*my + alp4*my**varrho
if obj_find_my(0.0, alp1, alp2, alp3, alp4, alp5) == 0.0:
#print('my = 0.0')
return 0.0, 0.0
if obj_find_my(mymax, alp1, alp2, alp3, alp4, alp5) == 0.0:
#print('my = mymax')
return alp5*mymax**varrho, mymax
if obj_find_my(mymax, alp1, alp2, alp3, alp4, alp5) > 0:
#print('my does not exist')
return -1., -1.
# ans = brentq(obj_find_my, 0., mymax, args = (alp1, alp2, alp3, alp4, alp5), xtol=1e-20)
####bisection start
my_lb = 0.
my_ub = mymax
#check bracketting
val_lb = alp1*(1. - my_lb - alp5*my_lb**varrho) - alp2 * my_lb**(1. - nu/(1. - alpha)) - alp3*my_lb + alp4*my_lb**varrho
val_ub = alp1*(1. - my_ub - alp5*my_ub**varrho) - alp2 * my_ub**(1. - nu/(1. - alpha)) - alp3*my_ub + alp4*my_ub**varrho
if val_lb *val_ub > 0.0:
print('error: no bracket')
sign = -1.0
if val_ub > 0.:
sign = 1.0
my = (my_lb + my_ub)/2.
it = 0
tol = 1.0e-12
rtol = 4.4408920985006262e-16
maxit = 400
val_m = 10000.
while it < maxit:
it = it + 1
if my > 0. and my < 1.0e-6:
tol = 1.0e-20
val_m = alp1*(1. - my - alp5*my**varrho) - alp2 * my**(1. - nu/(1. - alpha)) - alp3*my + alp4*my**varrho
if sign * val_m > 0.:
my_ub = my
elif sign * val_m < 0.:
my_lb = my
diff = abs((my_lb + my_ub)/2 - my)
my = (my_lb + my_ub)/2.
if diff < tol and abs(val_m) < rtol:
break
#convergence check
if it == maxit:
print('err: bisection method for my did not converge.')
#print('alp1 = ', alp1)
#print('alp2 = ', alp2)
#print('alp3 = ', alp3)
#print('alp4 = ', alp4)
#print('alp5 = ', alp5)
#print('val_m = ', val_m)
#print('my = ', my)
#print('mymax = ', mymax)
ans = my
####bisection end
if ans == 0.0:
print('A corner solution at 0.0 is obtianed: consider setting a smaller xtol.')
print('my = ', ans)
# if ans == mymax:
# #sometimes ans is extremely close to mymax
# #due to solver's accuracy.
return alp5*ans**varrho, ans
else:
#print('error: kap < 0 is not allowed.')
return -1., -1.
@nb.jit(nopython = True)
def get_sstatic(s):
a = s[0]
an = s[1]
kap = s[2]
kapn = s[3]
z = s[4]
is_o = s[5]
if is_o:
xi6 = xi6_o
z = tau_bo*z #replace eps with tau_wo*eps
else:
xi6 = xi6_y
u = -np.inf
mx = -1.0
my = -1.0
l = -1.0
x = -1.0