-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulate_LSC_ns_lifecycle.py
149 lines (99 loc) · 4.92 KB
/
simulate_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
import numpy as np
import time
import subprocess
from SCEconomy_LSC_ns_lifecycle import Economy, split_shock
import pickle
if __name__ == '__main__':
import sys
args = sys.argv
num_core = int(args[1])
from markov import calc_trans, Stationary
#generate shock sequene
path_to_data_i_s = './tmp/data_i_s'
path_to_data_is_o = './tmp/data_is_o'
num_pop = 100_000
sim_time = 3_000
#save and split shocks for istate
# prob = np.load('./input_data/transition_matrix.npy')
prob = np.load('./DeBacker/prob_epsz.npy')
np.random.seed(0)
data_rand = np.random.rand(num_pop, sim_time)
data_i_s = np.ones((num_pop, sim_time), dtype = int)
data_i_s[:, 0] = 7 #initial state. it does not matter if simulation is long enough.
calc_trans(data_i_s, data_rand, prob)
data_i_s = data_i_s[:, 2000:]
np.save(path_to_data_i_s + '.npy' , data_i_s)
split_shock(path_to_data_i_s, num_pop, num_core)
del data_rand, data_i_s
#save and split shocks for is_old
prob_yo = np.array([[44./45., 1./45.], [3./45., 42./45.]]) #[[y -> y, y -> o], [o -> y, o ->o]]
np.random.seed(2)
data_rand = np.random.rand(num_pop, sim_time+1) #+1 is added since this matters in calculation
data_is_o = np.ones((num_pop, sim_time+1), dtype = int)
data_is_o[:, 0] = 0 #initial state. it does not matter if simulation is long enough.
calc_trans(data_is_o, data_rand, prob_yo)
data_is_o = data_is_o[:, 2000:]
np.save(path_to_data_is_o + '.npy' , data_is_o)
split_shock(path_to_data_is_o, num_pop, num_core)
del data_rand, data_is_o
# taub = np.array([0.137, 0.185, 0.202, 0.238, 0.266, 0.28]) * 0.50 #large one
# psib = np.array([0.007026139999999993, 0.02013013999999999, 0.03, 0.08398847999999996, 0.19024008000000006, 0.2648964800000001])
# taup = 0.20
def curvedspace(begin, end, curve, num=100):
import numpy as np
ans = np.linspace(0, (end - begin)**(1.0/curve), num) ** (curve) + begin
ans[-1] = end #so that the last element is exactly end
return ans
# additional info
agrid2 = curvedspace(0., 200., 2., 40)
zgrid2 = np.load('./input_data/zgrid.npy') ** 2.0
GDP_guess = 3.20
taup = 0.36 * (1.0 - 0.278)
taub = np.array([0.137, 0.185, 0.202, 0.238, 0.266, 0.28]) *(1.0 - 0.506) #large one
psib = np.array([-0.010393600000000013, 0.012646399999999981, 0.03, 0.12492479999999993, 0.3117408000000001,0.4430048000000002])
taun = np.array([0.293, 0.317, 0.324, 0.343, 0.39, 0.405, 0.408, 0.419])
psin = np.array([-0.10037472000000003, -0.08685792000000002, -0.08193888000000002, -0.06546208, 0.0011951999999999727, 0.03, 0.04398335999999975, 0.14192735999999984])
p_, rc_, ome_, theta_ = 1.5212566636733704, 0.05885423450623066, 0.4561128052733918, 0.5071181286751945
# 1.5209092097405632, 0.053192497033077685, 0.46388548260346824, 0.6002410397243539
###end defining additional parameters#
print('Solving the model with the given prices...')
print('Do not simulate more than one models at the same time...')
econ = Economy(sim_time = 1000, num_total_pop = num_pop,
agrid = agrid2, zgrid = zgrid2, rho = 0.01, prob = prob,
ome = ome_, theta = theta_,
path_to_data_i_s = path_to_data_i_s, path_to_data_is_o = path_to_data_is_o,
scaling_n = GDP_guess, scaling_b = GDP_guess, g = 0.133*GDP_guess, yn = 0.266*GDP_guess, xnb = 0.110*GDP_guess,
delk = 0.041, delkap = 0.041, veps = 0.418, vthet = 1.0 - 0.418,
tauc = 0.065, taud = 0.133,
taup = taup, taub = taub , psib = psib
)
econ.set_prices(p = p_, rc = rc_)
with open('econ.pickle', mode='wb') as f: pickle.dump(econ, f)
t0 = time.time()
#don't forget to replace import argument
result = subprocess.run(['mpiexec', '-n', str(num_core), 'python', 'SCEconomy_LSC_ns_lifecycle.py'], stdout=subprocess.PIPE)
t1 = time.time()
detailed_output_file = './log/test.txt'
f = open(detailed_output_file, 'ab') #use byte mode
f.write(result.stdout)
f.close()
with open('econ.pickle', mode='rb') as f: econ = pickle.load(f)
p = econ.p
rc = econ.rc
ome = econ.ome
theta = econ.theta
if p != p_ or rc != rc_ or ome != ome_ or theta != theta_ :
#if p != p_ or rc != rc_:
print('err: input prices and output prices do not coincide.')
print('p = ', p, ', p_ = ', p_)
print('rc = ', rc, ', rc_ = ', rc_)
print('ome = ', ome, ', ome_ = ', ome_)
print('theta = ', theta, ', theta_ = ', theta_)
#calc main moments
econ.print_parameters()
###calculate other important variables###
##econ.calc_sweat_eq_value()
econ.calc_age()
econ.simulate_other_vars()
econ.calc_moments()
econ.save_result()