forked from qiyangku/FHMC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_bimodal.m
273 lines (215 loc) · 6.99 KB
/
main_bimodal.m
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
function main_bimodal(option,varargin)
switch option
case 'plot_only'
plot_bimodal(varargin{1},[2]);
return
case 'analysis_only'
post_process_bimodal(varargin{1});
return
case 'sample_path'
gen_sample_path_bimodal;
otherwise
exit_time;
end
end
function gen_sample_path_bimodal(~)
%p.ntrials = 8*3; %for parallel processing,use multiples of 8
p.target = 'bimodal';
p.sigma = 0.32;
p.dt = 1e-3; %1e-2 is no good for calculating Riesz derivative
T = 1e4;
%AIM: 1. Sample trajectory. 2. histogram
a = [1.2 2];
p.location = 2.5; %modal location
solver = {'Hamiltonian2','Langevin'}; %'Hamiltonian','Underdamped'};
%for accurate results don't need the other two... too slow
X = zeros(T/p.dt,length(solver),length(a)); %save 1 example is ok
disp('Generating raw data...')
for i = 1:length(a) %group fractional/nonfractional together as this is less familiar to people
aa = a(i);
tic
for j = 1:length(solver)
p.methods.solver = solver{j};
disp('Simulating...')
%parfor k = 1:p.ntrials
[temp,t] = fHMC(T,aa,p);
%end
clc
fprintf('Solver: %u/%u\n',j,length(solver))
fprintf('alpha: %u/%u\n',i,length(a))
toc
X(:,j,i) = temp;
end
end
n = floor(T/p.dt); %number of samples
t = (0:n-1)*p.dt;
save main_bimodal_raw_data_sample_path.mat T a solver X t p
end
function exit_time()
%x0 = linspace(0.5,2.5,21); %locations of bimodal distribution
%a = [1.2 2];
%p.T = 1e3;
x0 = linspace(0.5,1.5,21);
a = [2]; %do gaussian only
p.T = 1e4; %do more time since time is longer
solver = {'Hamiltonian2','Langevin'};
p.target = 'bimodal';
p.ntrials = 8*3;
p.dt = 1e-3;
p.sigma = 0.32;
flag = true(size(a));
tic
binedge = linspace(-5,5,101);
dx = (binedge(2)-binedge(1))*0.5;
bin = binedge(2:end) - dx;
H = zeros(length(bin), length(x0),length(solver),length(a)); %histogram
T = zeros(length(x0),length(solver),length(a),p.ntrials);
tic
disp('Beginning simulation...')
for m = 1:length(solver)
p.methods.solver = solver{m};
for i = 1:length(x0) %modal separation
p.location = x0(i); %2.5;
for k = 1:length(a)
if ~flag(k)
T(i,m,k,:) = NaN;
continue
end
aa = a(k);
TT = p.T;
h = zeros(length(bin), p.ntrials);
texit = zeros(1,p.ntrials);
parfor j = 1:p.ntrials
[X,t] = fHMC(TT,aa,p);
h(:,j) = histcounts(X,binedge,'normalization','pdf');
try
texit(j) = mean([TrapTime(X>0);TrapTime(X<0)]); %mean exit time
catch ME
texit(j) = NaN;
disp('Unknown error!')
end
end
H(:,i,m,k) = mean(h,2);
T(i,m,k,:) = texit;
if all(isnan(texit)) %T(k,i,j)>1e5 %if mean trap time is too long, skip subsequent simulations
%nan => TrapTime output empty array
flag(k) = false;
end
clc
fprintf('Solver: %u/%u\n',m,length(solver))
fprintf('alpha: %u/%u\n',k,length(a))
fprintf('Mode location: %u/%u\n',i,length(x0))
toc
end
end
end
%save main_bimodal_mean_exit_time.mat H bin T p x0 a solver
save main_bimodal_mean_exit_time_gaussian.mat H bin T p x0 a solver
end
function plot_bimodal(Q,flag)
close all
clc
if any(flag==0)
load main_bimodal_mean_exit_time_gaussian.mat T x0 p
%load exit_time_HMC_vs_FHMC_corrected.mat T x0
T(T==0)=nan;
T = mean(T,4)*p.dt;
x0 = x0*2;x0=x0(:);
xx = linspace(x0(1),x0(end),101);
f0 = myfigure;
for i =2:-1:1
indx = ~isnan(T(:,i));
linestyle = {'--','-'};
markerstyle = {'x','o'};
try
f = fit(x0(indx),T(indx,i),'exp1');
yy = f(xx);
plot(xx(yy<100),yy(yy<100),linestyle{i},'color',mycolor(1,i+2),'HandleVisibility','off','linewidth',1)
hold on
catch
end
plot(x0(indx),T(indx,i),markerstyle{i},'color',mycolor(1,i+2),'linewidth',1);
end
load main_bimodal_mean_exit_time.mat T x0 p
T(T==0)=nan;
T = mean(T,4)*p.dt;
x0 = x0*2;x0=x0(:);
xx = linspace(x0(1),x0(end),101);
for i =2:-1:1
f = fit(x0(:),T(:,i,1),'poly1');
linestyle = {'--','-'};
markerstyle = {'x','o'};
plot(xx,f(xx),linestyle{i},'color',mycolor(1,i),'HandleVisibility','off','linewidth',1)
hold on
%indx = 1:2:length(x0);
plot(x0,T(:,i,1),markerstyle{i},'color',mycolor(1,i),'linewidth',1);
end
subplotmod;
xlim([1 5])
ylim([0 100])
xlabel('Modal Separation')
ylabel('Mean Exit Time')
legend('LD','HD','FLD','FHD','box','off','location','ne')
offsetAxes;
export_fig(f0,'figures/fig_main_bimodal_met.pdf','-pdf','-nocrop','-transparent','-painters');
end
if any(flag==1)
%load main_bimodal_raw_data_sample_path.mat
f1 = myfigure;
ttl = {'Fractional Hamiltonian Dynamics','Fractional Langevin Dynamics','Hamiltonian Dynamics','Langevin Dynamics'};
Tmax = 2e2;
tindx = (1:40:(Tmax/Q.p.dt))+2e4;
for k = 1:4
subplot(4,1,k)
[j,i]=ind2sub([2 2],k);
plot(Q.t(tindx)-Q.t(tindx(1)),Q.X(tindx,j,i),'.','color',mycolor(1,k),'markersize',1);
title(ttl(k))
if k<4
set(gca,'xtick',[],'XColor','none');
else
xlabel('t')
end
ylabel('x_t')
%subplotmod;
box off
set(gca,'TickDir','out')
%hold on
%plot(xx,yy,'k--','linewidth',1.5)
ylim([-4 4])
xlim([0 Tmax])
set(gca,'linewidth',1);
end
pos =get(f1,'Position');
set(gcf,'position',[pos(1) pos(2) pos(3) 600])
export_fig(f1,'figures/fig_main_bimodal_sample_path.pdf','-pdf','-nocrop','-transparent','-painters');
end
if any(flag ==2) %Histogram
f2 = myfigure;
gs1 = makedist('normal','mu',2.5,'sigma',0.32);
gs2 = makedist('normal','mu',-2.5,'sigma',0.32);
xx = linspace(-4,4,101);
yy = 0.5*pdf(gs1,xx)+0.5*pdf(gs2,xx);
binedge = linspace(-4,4,101);
bin = (binedge(2)-binedge(1))*0.5 + binedge(1:end-1);
for k = 1:4
subplot(4,1,k)
[j,i]=ind2sub([2 2],k);
hc = histcounts(Q.X(:,j,i),binedge,'normalization','pdf');
%bar(Q.bin,Q.H(:,j,i),1,'FaceColor',mycolor(1,k),'EdgeColor',mycolor(1,k),'FaceAlpha',0.5);
bar(bin,hc,1,'FaceColor',mycolor(1,k),'EdgeColor',mycolor(1,k),'FaceAlpha',0.5);hold on
set(gca,'xtick',[])
set(gca,'ytick',[])
hold on
plot(xx,yy,'k','linewidth',1)
if k<0;%k <2.5
ylim([0 1.5/2])
else
ylim([0 1.5])
end
xlim([-4 4])
set(gcf,'position',[100 100 200 600])
end
%export_fig(f2,'figures/fig_main_unimodal_postprocess_histogram.pdf','-pdf','-nocrop','-transparent','-painters');
print(gcf, '-dpdf', 'figures\fig_main_bimodal_histogram.pdf');
end
end