-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulate_DeBacker.py
95 lines (59 loc) · 2.44 KB
/
simulate_DeBacker.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
import numpy as np
import time
import subprocess
from SCEconomy_give_A import Economy, split_shock
import numba as nb
from markov import calc_trans
import pickle
if __name__ == '__main__':
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
input_path = './input_data/'
num_core = 4
### additional info
agrid2 = curvedspace(0., 200., 2., 40)
kapgrid2 = curvedspace(0., 2., 2., 20)
zgrid2 = np.load('./input_data/zgrid.npy') ** 2.0
prob = np.load('./DeBacker/prob_epsz.npy')
#generate shock sequence
num_pop = 100_000
sim_time = 2_000
data_i_s = np.ones((num_pop, sim_time), dtype = int)
#need to set initial state for zp
data_i_s[:, 0] = 7
data_rand = np.random.rand(num_pop, sim_time)
calc_trans(data_i_s, data_rand, prob)
np.save(input_path + 'data_i_s_tmp.npy', data_i_s[:,-1000:])
split_shock(input_path + 'data_i_s_tmp', 100_000, num_core)
path_to_data_i_s = input_path + 'data_i_s_tmp'
# prices
w_, p_, rc_ = 3.1529396907242555, 0.7362549279771939, 0.06176895413151351
###end defining additional parameters###
print('Solving the model with the given prices...')
print('Do not simulate more thn one maodels at the same time...')
econ = Economy(agrid = agrid2, kapgrid = kapgrid2, zgrid = zgrid2, prob = prob, path_to_data_i_s = path_to_data_i_s)
econ.set_prices(w = w_, p = p_, rc = rc_)
with open('econ.pickle', mode='wb') as f: pickle.dump(econ, f)
t0 = time.time()
result = subprocess.run(['mpiexec', '-n', str(num_core), 'python', 'SCEconomy_give_A.py'], stdout=subprocess.PIPE)
t1 = time.time()
with open('econ.pickle', mode='rb') as f: econ = pickle.load(f)
w = econ.w
p = econ.p
rc = econ.rc
moms = econ.moms
dist = np.sqrt(moms[0]**2.0 + moms[1]**2.0 + moms[2]**2.0)
if w != w_ or p != p_ or rc != rc_:
print('err: input prices and output prices do not coincide.')
print('w = ', w, ', w_ = ', w_)
print('p = ', p, ', p_ = ', p_)
print('rc = ', rc, ', rc_ = ', rc_)
econ.calc_moments()
###calculate other important variables###
econ.calc_sweat_eq_value()
econ.calc_age()
econ.simulate_other_vars()
econ.save_result()