-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdev.jl
175 lines (100 loc) · 3.8 KB
/
dev.jl
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
import Dolo
import DoloYAML
import Dolo: getprecision
# model_source = DoloYAML.ModelSource("examples/integration_A.yaml")
model_ = DoloYAML.yaml_import("examples/integration_A.yaml")
using StaticArrays: SVector
s = SVector((Dolo.calibrated(model_, :states)[3:4])...)
x = Dolo.calibrated(model_, :controls)
m = Dolo.calibrated(model_, :exogenous)
ss = SVector(m..., s...)
# p = Dolo.calibrated(model_, :parameters)
# @time Dolo.arbitrage(model_, m,s,x,m,s,x,p)
# @time Dolo.transition(model_, m,s,x,m,p)
# @time Dolo.arbitrage(model_, m,s,x,m,s,x)
# @time Dolo.transition(model_, m,s,x,m)
@time Dolo.transition(model_, ss,x,m)
@time Dolo.arbitrage(model_, ss,x,ss,x)
dmodel_ = Dolo.discretize(model_)
wk_ = Dolo.time_iteration_workspace(dmodel_)
@time Dolo.F!(wk_.r0, dmodel_, wk_.x0, wk_.φ)
Dolo.time_iteration(dmodel)
####
model = Dolo.convert_precision(Float32, model_)
dmodel = Dolo.discretize(model)
s = dmodel.grid[1+im]
x0 = Dolo.calibrated(model, :controls)
[Dolo.τ(dmodel, s, x0)...]
wk = Dolo.time_iteration_workspace(dmodel);
@time Dolo.F!(wk.r0, dmodel, wk.x0, wk.φ);
@time Dolo.time_iteration(dmodel, wk)
# @time Dolo.time_iteration(model, improve=true)
using StaticArrays
# Dolo.time_iteration(model; improve=true)
s0 = Dolo.calibrated(model, :states)
x0 = Dolo.calibrated(model, :controls)
m0 = Dolo.calibrated(model, :exogenous)
p0 = Dolo.calibrated(model, :parameters)
vals = (;s0, x0, m0, p0)
ss = Dolo.QP((5,200), s0)
ss0 = Dolo.QP((5, 200), Dolo.calibrated(model, :states))
x0 = Dolo.calibrated(model, :controls)
ϕ0 = Dolo.Policy(model.states, model.controls, u->x0)
Dolo.F(dmodel, ss0, x0, ϕ0)
xvec = Dolo.GVector(dmodel.grid, [x0 for i=1:length(dmodel.grid)])
φ1 = Dolo.DFun(model, xvec)
res = Dolo.F(dmodel, xvec, φ1);
dres = Dolo.dF_1(dmodel, xvec, φ1);
import Dolo: CPU
res1 = res*0.0
@time begin res1 = res*0.0;(Dolo.F!(res1, dmodel, xvec, φ1);println(sum(sum(res1)))) end
@time begin res2 = res*0.0;Dolo.F!(res2, dmodel, xvec, φ1, CPU());println(sum(sum(res2))) end
function test(dmodel,ss0, x0, ϕ0)
return sum(Dolo.dF_1(dmodel,ss0, x0, ϕ0))
end
@time test(dmodel,ss0, x0, φ1)
@time Dolo.dF_1!(dres1, dmodel, xvec, φ1);
@time begin dres1 = dres*0.0;(Dolo.dF_1!(dres1, dmodel, xvec, φ1);println(sum(sum(dres1)))) end
@time begin dres2 = dres*0.0;Dolo.dF_1!(dres2, dmodel, xvec, φ1, CPU());println(sum(sum(dres2))) end
@time Dolo.time_iteration(model;)
@time Dolo.time_iteration(model; engine=:cpu)
# x0 = Dolo.GVector(dmodel.grid, [x0 for i=1:length(dmodel.grid)])
(;φ) = Dolo.time_iteration_workspace(dmodel; )
# ϕ0 = Dolo.Policy(model.states, model.controls, s->x0)
function test(dmodel, vals, φ)
model = dmodel.model
(;m0, s0, x0, p0) = vals
# r = Dolo.arbitrage(model.source, m0, s0, x0, m0, s0, x0, p0) # ok
# s = Dolo.transition(model.source, m0, s0, x0, m0, p0) # ok
ss = Dolo.QP((5,200), s0)
# SS = Dolo.QP((5,SVector(s0[3],s0[4])), s0)
Dolo.F(dmodel, ss, x0, φ) # ok
# if abs(r)>-0.1
# return nothing
# else
# return "No"
# end
# return sum(r)
end
test(dmodel, vals, φ)
@time test(dmodel, vals, φ);
function test2(dmodel, φ, xvec, rvec) # OK!
model = dmodel.model
Dolo.F!(rvec, dmodel, xvec, φ)
end
xvec = Dolo.GVector(dmodel.grid, [x0 for i=1:length(dmodel.grid)])
rvec = deepcopy(xvec)
test2(dmodel, φ, xvec, rvec)
@time test2(dmodel, φ, xvec, rvec)
model = DoloYAML.yaml_import("integration_B.yaml")
dmodel = Dolo.discretize(model)
Dolo.time_iteration(model)
s0 = Dolo.QP((5, 4001), Dolo.calibrated(model, :states))
x0 = Dolo.calibrated(model, :controls)
ϕ0 = Dolo.Policy(model.states, model.controls, s->x0)
Dolo.F(dmodel, s0, x0, ϕ0)
function testalloc(dmodel, s0, x0,ϕ0)
r = Dolo.F(dmodel, s0, x0, ϕ0)
return sum(r)
end
@time testalloc(dmodel, s0, x0, ϕ0)