-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex1-refl.jl
304 lines (275 loc) · 11.7 KB
/
ex1-refl.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
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
include(joinpath(@__DIR__, "src", "bp.jl"));
using Cubature;
s = ArgParseSettings();
@add_arg_table! s begin
"--C1", "-a"
help = "coefficient 1"
arg_type = Float64
default = 1.0
"--C2", "-b"
help = "coefficient 2"
arg_type = Float64
default = 3.0
"--x0", "-X"
help = "initial configuration"
arg_type = String
default = "(::Any) -> 0.0"
"--force", "-f"
help = "applied force"
arg_type = Float64
default = 0.0
"--umbrella-b"
help = "pseudopotential 'b'"
arg_type = Float64
default = 0.5
"--umbrella-C"
help = "pseudopotential 'C'"
arg_type = Float64
default = 1.0
end
default_options = src_include("default_options.jl");
ArgParse.import_settings!(s, default_options);
pargs = src_include("parse_args.jl");
@everywhere function mcmc(nsteps::Int, pargs)
kT = pargs["kT"];
a = pargs["C1"];
b = pargs["C2"];
f = pargs["force"];
wt = pargs["weight"];
U = (x) -> a*x^4 - b*x^2 - f*x;
x = pargs["x0"](pargs);
xstep = pargs["dx"];
dx_dist = Uniform(-xstep, xstep);
orbf = (pargs["orbit"]) ? x -> rand([-1; 1])*x : x -> x;
wt_curr = wt(x);
inv_wt_total = 1 / wt_curr;
xtotal = x / wt_curr;
xrolling = Float64[];
Ucurr = U(x);
Utotal = Ucurr / wt_curr;
Urolling = Float64[];
x2total = x*x / wt_curr;
x2rolling = Float64[];
xstd_rolling = Float64[];
U2total = Ucurr*Ucurr / wt_curr;
U2rolling = Float64[];
Ustd_rolling = Float64[];
stepout = pargs["stepout"];
rolls = Int[];
traj_file = if pargs["do-trajectory"]
mkpath(pargs["outdir"]);
open(joinpath(pargs["outdir"], "trajectory.csv"), "w");
else
nothing;
end
natt = 0;
nacc = 0;
nacc_total = 0;
start = time();
last_update = start;
for s = 1:nsteps
xtrial = orbf(x + rand(dx_dist));
Utrial = U(xtrial);
wt_trial = wt(xtrial);
if rand() <= ( exp(-(Utrial - Ucurr) / kT) * wt_trial / wt_curr )
x = xtrial;
Ucurr = Utrial;
wt_curr = wt_trial;
nacc += 1;
nacc_total += 1;
end
natt += 1;
inv_wt_total += 1 / wt_curr;
xtotal += x / wt_curr;
Utotal += Ucurr / wt_curr;
x2total += x*x / wt_curr;
U2total += Ucurr*Ucurr / wt_curr;
ar = nacc_total / s; # rolling acceptance ratio
if s % stepout == 0
push!(rolls, s);
push!(xrolling, xtotal / inv_wt_total);
push!(Urolling, Utotal / inv_wt_total);
push!(x2rolling, x2total / inv_wt_total);
push!(U2rolling, U2total / inv_wt_total);
push!(xstd_rolling, sqrt(max(0.0, x2total / inv_wt_total
- (xtotal / inv_wt_total)^2)));
push!(Ustd_rolling, sqrt(max(0.0, U2total / inv_wt_total -
(Utotal / inv_wt_total)^2)));
if pargs["do-trajectory"]
writedlm(traj_file, [x Ucurr x*x Ucurr*Ucurr ar s]);
end
end
if time() - last_update > pargs["update-freq"]
@info "elapsed: $(time() - start)";
@info "step: $s / $nsteps";
last_update = time();
end
if (
pargs["step-adjust-scale"] != 1.0 &&
pargs["steps-per-adjust"] != 0 &&
s % pargs["steps-per-adjust"] == 0
) # adjust step size?
ar = nacc / natt;
if (ar > pargs["step-adjust-ub"])
nacc = 0;
natt = 0;
xstep *= pargs["step-adjust-scale"];
@info "acceptance ratio is high; increasing step size", xstep;
elseif ar < pargs["step-adjust-lb"]
nacc = 0;
natt = 0;
xstep /= pargs["step-adjust-scale"];
@info "acceptance ratio is low; decreasing step size", xstep;
end
dx_dist = Uniform(-xstep, xstep);
end
end
if pargs["do-trajectory"]
close(traj_file);
end
return Dict(:xavg => xtotal / inv_wt_total,
:Uavg => Utotal / inv_wt_total,
:xrolling => copy(xrolling), :Urolling => copy(Urolling),
:x2avg => x2total / inv_wt_total,
:U2avg => U2total / inv_wt_total,
:x2rolling => copy(x2rolling), :U2rolling => copy(U2rolling),
:xstd_rolling => copy(xstd_rolling),
:Ustd_rolling => copy(Ustd_rolling),
:rolls => rolls,
:ar => nacc / natt,
:AR => nacc_total / nsteps
);
end
src_include("wrap_main_runs.jl");
results_std, results_polya = wrap_main_runs(pargs);
uC, ub = pargs["umbrella-C"], pargs["umbrella-b"];
pargs_umb = copy(pargs);
pargs_umb["weight"] = x -> uC*exp(-ub*x^2);
pargs_umb["outdir"] = joinpath(pargs["outdir"], "umb");
results_umb_std, results_umb_polya = wrap_main_runs(pargs_umb);
kT = pargs["kT"];
a = pargs["C1"];
b = pargs["C2"];
f = pargs["force"];
wt = pargs["weight"];
U = (x) -> a*x^4 - b*x^2 - f*x;
meq = 25000;
ℓ = 4*sqrt(b / a);
(Zquad, Zquad_err) = (
collect(hquadrature(t -> -exp(-U(1/t)/kT) / t^2, 0.0, -1/ℓ; maxevals=meq)) +
collect(hquadrature(x -> exp(-U(x)/kT), -ℓ, ℓ; maxevals=meq)) +
collect(hquadrature(t -> -exp(-U(1/t)/kT) / t^2, 1/ℓ, 0.0; maxevals=meq))
);
(xquad, xquad_err) = (f == 0) ? [0.0; 0.0] : (
(
collect(hquadrature(t -> -(1/t)*exp(-U(1/t)/kT) / t^2 / Zquad, 0.0, -1/ℓ; maxevals=meq)) +
collect(hquadrature(x -> x*exp(-U(x)/kT) / Zquad, -ℓ, ℓ; maxevals=meq)) +
collect(hquadrature(t -> -(1/t)*exp(-U(1/t)/kT) / t^2 / Zquad, 1/ℓ, 0.0; maxevals=meq))
)
);
(Uquad, Uquad_err) = (
collect(hquadrature(t -> -U(1/t)*exp(-U(1/t)/kT) / t^2 / Zquad, 0.0, -1/ℓ; maxevals=meq)) +
collect(hquadrature(x -> U(x)*exp(-U(x)/kT) / Zquad, -ℓ, ℓ; maxevals=meq)) +
collect(hquadrature(t -> -U(1/t)*exp(-U(1/t)/kT) / t^2 / Zquad, 1/ℓ, 0.0; maxevals=meq))
);
(x2quad, x2quad_err) = (
collect(hquadrature(t -> -(1/t)^2*exp(-U(1/t)/kT) / t^2 / Zquad, 0.0, -1/ℓ; maxevals=meq)) +
collect(hquadrature(x -> x^2*exp(-U(x)/kT) / Zquad, -ℓ, ℓ; maxevals=meq)) +
collect(hquadrature(t -> -(1/t)^2*exp(-U(1/t)/kT) / t^2 / Zquad, 1/ℓ, 0.0; maxevals=meq))
);
(U2quad, U2quad_err) = (
collect(hquadrature(t -> -(U(1/t))^2*exp(-U(1/t)/kT) / t^2 / Zquad, 0.0, -1/ℓ; maxevals=meq)) +
collect(hquadrature(x -> (U(x))^2*exp(-U(x)/kT) / Zquad, -ℓ, ℓ; maxevals=meq)) +
collect(hquadrature(t -> -(U(1/t))^2*exp(-U(1/t)/kT) / t^2 / Zquad, 1/ℓ, 0.0; maxevals=meq))
);
idx = rand(1:pargs["num-runs"]);
@info "std ar = $(results_std[idx][:ar])";
@info "polya ar = $(results_polya[idx][:ar])";
@info "umb std ar = $(results_umb_std[idx][:ar])";
@info "umb polya ar = $(results_umb_polya[idx][:ar])";
@info "Z via quadrature = $Zquad; error = $Zquad_err";
@info "<x> via quadrature = $xquad; error = $xquad_err";
@info "<x> via std = $(results_std[idx][:xavg])";
@info "<x> via polya = $(results_polya[idx][:xavg])";
@info "<x> via umb std = $(results_umb_std[idx][:xavg])";
@info "<x> via umb polya = $(results_umb_polya[idx][:xavg])";
@info "<U> via quadrature = $Uquad; error = $Uquad_err";
@info "<U> via std = $(results_std[idx][:Uavg])";
@info "<U> via polya = $(results_polya[idx][:Uavg])";
@info "<U> via umb std = $(results_umb_std[idx][:Uavg])";
@info "<U> via umb polya = $(results_umb_polya[idx][:Uavg])";
@info "<x^2> via quadrature = $x2quad; error = $x2quad_err";
@info "<x^2> via std = $(results_std[idx][:x2avg])";
@info "<x^2> via polya = $(results_polya[idx][:x2avg])";
@info "<x^2> via umb std = $(results_umb_std[idx][:x2avg])";
@info "<x^2> via umb polya = $(results_umb_polya[idx][:x2avg])";
@info "<U^2> via quadrature = $U2quad; error = $U2quad_err";
@info "<U^2> via std = $(results_std[idx][:U2avg])";
@info "<U^2> via polya = $(results_polya[idx][:U2avg])";
@info "<U^2> via umb std = $(results_umb_std[idx][:U2avg])";
@info "<U^2> via umb polya = $(results_umb_polya[idx][:U2avg])";
#@info "<Urolling> via std = $(results_std[idx][:Urolling])";
#@info "<Urolling> via polya = $(results_polya[idx][:Urolling])";
#@info "<Urolling> via umb std = $(results_umb_std[idx][:Urolling])";
#@info "<Urolling> via umb polya = $(results_umb_polya[idx][:Urolling])";
L1_error_std, L1_error_polya = post_process_main_runs(results_std,
results_polya,
pargs;
xrolling=xquad,
Urolling=Uquad,
x2rolling=x2quad,
U2rolling=U2quad);
L1_err_umb_std, L1_err_umb_polya = post_process_main_runs(results_umb_std,
results_umb_polya,
pargs_umb;
xrolling=xquad,
Urolling=Uquad,
x2rolling=x2quad,
U2rolling=U2quad);
if pargs["do-csvs"]
for k in keys(L1_error_std)
writedlm(joinpath(pargs["outdir"], "L1_$k.csv"), hcat(
results_std[1][:rolls],
L1_error_std[k],
L1_error_polya[k],
L1_err_umb_std[k],
L1_err_umb_polya[k]
), ',');
end
end
if pargs["do-conv-rates"]
println("αs via std = $(convergence_rates(L1_error_std, results_std[1][:rolls]))");
println("αs via polya = $(convergence_rates(L1_error_polya, results_std[1][:rolls]))");
println("αs via umb = $(convergence_rates(L1_err_umb_std, results_std[1][:rolls]))");
println("αs via gu = $(convergence_rates(L1_err_umb_polya, results_std[1][:rolls]))");
end
if pargs["do-plots"]
idx = rand(1:pargs["num-runs"]);
p = plot(results_std[idx][:rolls], results_std[idx][:xrolling]; label="std mcmc",
xlabel = "step", ylabel = "\$\\langle x \\rangle\$");
plot!(results_polya[idx][:rolls], results_polya[idx][:xrolling]; label="polya");
plot!(results_umb_std[idx][:rolls], results_umb_std[idx][:xrolling]; label="umb");
plot!(results_umb_polya[idx][:rolls], results_umb_polya[idx][:xrolling]; label="umb + polya");
display(p);
println();
println("Press RETURN to exit...");
readline();
p = plot(results_std[idx][:rolls], results_std[idx][:Urolling]; label="std mcmc",
xlabel = "step", ylabel = "\$\\langle U \\rangle\$");
plot!(results_polya[idx][:rolls], results_polya[idx][:Urolling]; label="polya");
plot!(results_umb_std[idx][:rolls], results_umb_std[idx][:Urolling]; label="umb");
plot!(results_umb_polya[idx][:rolls], results_umb_polya[idx][:Urolling]; label="umb + polya");
display(p);
println();
println("Press RETURN to exit...");
readline();
p = plot(results_std[idx][:rolls], results_std[idx][:x2rolling]; label="std mcmc",
xlabel = "step", ylabel = "\$\\langle x^2 \\rangle\$");
plot!(results_polya[idx][:rolls], results_polya[idx][:x2rolling]; label="polya");
plot!(results_umb_std[idx][:rolls], results_umb_std[idx][:x2rolling]; label="umb");
plot!(results_umb_polya[idx][:rolls], results_umb_polya[idx][:x2rolling]; label="umb + polya");
display(p);
println();
println("Press RETURN to exit...");
readline();
end