1
+ function main_unimodal(option ,varargin )
2
+
3
+ switch option
4
+ case ' plot_only'
5
+ plot_unimodal(varargin{1 },[0 2 ]);
6
+ return
7
+ case ' analysis_only'
8
+ post_process_unimodal(varargin{1 });
9
+ return
10
+ otherwise
11
+ gen_raw_data_unimodal ;
12
+ end
13
+ end
14
+
15
+
16
+ function gen_raw_data_unimodal(~)
17
+
18
+ p.ntrials = 8 * 3 ; % for parallel processing,use multiples of 8
19
+ p.target = ' unimodal' ;
20
+ p.sigma = 1 ;
21
+ p.dt = 1e-3 ; % 1e-2 is no good for calculating Riesz derivative
22
+ T = 1e3 ;
23
+
24
+ % AIM: 1. Sample trajectory. 2. histogram 2. ACF. 3. MSE
25
+ % For FHMC,HMC,
26
+
27
+ a = [1.2 2 ];
28
+ solver = {' Hamiltonian2' ,' Langevin' }; % 'Hamiltonian','Underdamped'};
29
+ % for accurate results don't need the other two... too slow
30
+
31
+ X = zeros(T / p .dt ,p .ntrials ,length(solver ),length(a ));
32
+
33
+ disp(' Generating raw data...' )
34
+ for i = 1 : length(a ) % group fractional/nonfractional together as this is less familiar to people
35
+ aa = a(i );
36
+ tic
37
+ for j = 1 : length(solver )
38
+ p.methods.solver = solver{j };
39
+ temp = zeros(T / p .dt ,p .ntrials );
40
+ parfor k = 1 : p .ntrials
41
+ [temp(: ,k ),t ] = fHMC(T ,aa ,p );
42
+ end
43
+ clc
44
+ fprintf(' Solver: %u /%u\n ' ,j ,length(solver ))
45
+ fprintf(' alpha: %u /%u\n ' ,i ,length(a ))
46
+ toc
47
+ X(: ,: ,j ,i ) = temp ;
48
+ end
49
+
50
+
51
+
52
+ end
53
+
54
+ n = floor(T / p .dt ); % number of samples
55
+ t = (0 : n - 1 )*p .dt ;
56
+
57
+ save main_unimodal_raw_data.mat T a solver X t p
58
+ end
59
+
60
+ function post_process_unimodal(Z )
61
+ n= 50 ;
62
+ binedge = linspace(-5 ,5 ,n + 1 );
63
+ bin = binedge(1 : end - 1 ) + 0.5 *(binedge(2 )-binedge(1 ));
64
+ disp(' Evaluating histogram, ACF, and MSE' )
65
+ H = zeros(n ,length(Z .solver ),length(Z .a ));
66
+
67
+ m = 10 / Z .p .dt ;% 1e4;
68
+ tau = (0 : m )*Z .p .dt ;
69
+ ACF = zeros(length(tau ),length(Z .solver ),length(Z .a ));
70
+ MSE = zeros(length(tau ),length(Z .solver ),length(Z .a ));
71
+
72
+ t = (0 : 5e4 )*Z .p .dt ;
73
+ X = zeros(length(t ),length(Z .solver ),length(Z .a )); % sample X for visualization
74
+
75
+ tic
76
+ for i = 1 : length(Z .a )
77
+ for j = 1 : length(Z .solver )
78
+ x = Z .X(: ,: ,j ,i ); x = x(: );
79
+ H(: ,j ,i ) = histcounts(x ,binedge ,' normalization' ,' pdf' );
80
+ temp_acf = 0 ;
81
+ temp_mse = 0 ;
82
+ for k = 1 : Z .p .ntrials
83
+ temp_acf = temp_acf + autocorr(Z .X(: ,k ,j ,i ),m );
84
+ temp_mse = temp_mse + myMSE(Z .X(: ,k ,j ,i ),m );
85
+ end
86
+ ACF(: ,j ,i ) = temp_acf / Z .p .ntrials ;
87
+ MSE(: ,j ,i ) = temp_mse / Z .p .ntrials ;
88
+ X(: ,j ,i ) = Z .X(1 : length(t ),1 ,j ,i );
89
+ clc
90
+ toc
91
+ fprintf(' %u / %u\n ' ,i ,length(Z .a ))
92
+ fprintf(' %u / %u\n ' ,j ,length(Z .solver ))
93
+ end
94
+ end
95
+ save main_unimodal_post_process.mat H ACF MSE tau bin binedge t X
96
+ end
97
+
98
+
99
+ function plot_unimodal(Q ,index )
100
+
101
+ close all
102
+ if any(index == 0 ) % sample trajectory
103
+ f0 = myfigure ;
104
+ ttl = {' Fractional Hamiltonian Monte Carlo' ,' Fractional Langevin Monte Carlo' ,' Hamiltonian Monte Carlo' ,' Langevin Monte Carlo' };
105
+ for k = 1 : 4
106
+ subplot(4 ,1 ,k )
107
+ [j ,i ]=ind2sub([2 2 ],k );
108
+ plot(Q .t(1 : 10 : end ),Q .X(1 : 10 : end ,j ,i ),' .' ,' color' ,mycolor(1 ,k ),' markersize' ,1 );
109
+ title(ttl(k ))
110
+ if k < 4
111
+ set(gca ,' xtick' ,[],' XColor' ,' none' );
112
+ else
113
+ xlabel(' t' )
114
+ end
115
+ ylabel(' x_t' )
116
+ % subplotmod;
117
+ box off
118
+ set(gca ,' TickDir' ,' out' )
119
+ % hold on
120
+ % plot(xx,yy,'k--','linewidth',1.5)
121
+ ylim([-4 4 ])
122
+ xlim([Q .t(1 ) Q .t(end )])
123
+ end
124
+ pos = get(f0 ,' Position' );
125
+ set(gcf ,' position' ,[pos(1 ) pos(2 ) pos(3 ) 600 ])
126
+ export_fig(f0 ,' figures/fig_main_unimodal_postprocess_sample_path.pdf' ,' -pdf' ,' -nocrop' ,' -transparent' ,' -painters' );
127
+ end
128
+
129
+ if any(index == 1 ) % ACF
130
+ f1 = myfigure ;
131
+ % plot(Q.tau,Q.ACF(:,1,1),'-','color',mycolor(1,2),'linewidth',1.5)
132
+ % hold on
133
+ % plot(Q.tau,Q.ACF(:,2,1),'-','color',mycolor(1,1),'linewidth',1.5)
134
+ % plot(Q.tau,Q.ACF(:,1,2),'--','color',mycolor(1,2) ,'linewidth',1.5)
135
+ % plot(Q.tau,Q.ACF(:,2,2),'--','color',mycolor(1,1),'linewidth',1.5)
136
+
137
+ % color scheme default
138
+ plot(Q .tau ,Q .ACF(: ,1 : 2 ,1 ),Q .tau ,Q .ACF(: ,1 : 2 ,2 ),' --' ,' linewidth' ,1.5 )
139
+ hold on
140
+ plot(Q .tau([1 end ]),[0 0 ],' color' ,[0.4 0.4 0.4 ],' HandleVisibility' ,' off' )
141
+
142
+ xlim([0 8 ])
143
+ xlabel(' Time Lag' )
144
+ ylabel(' Autocorrelation function' )
145
+ legend(' FHMC' ,' FLMC' ,' HMC' ,' LMC' ,' box' ,' off' )
146
+ subplotmod ;
147
+ offsetAxes ;
148
+
149
+ export_fig(f1 ,' figures/fig_main_unimodal_postprocess_acf.pdf' ,' -pdf' ,' -nocrop' ,' -transparent' ,' -painters' );
150
+ end
151
+ if any(index == 2 )
152
+ f2 = myfigure ;
153
+ gs = makedist(' normal' );
154
+ xx = linspace(-3 ,3 ,51 );
155
+ yy = pdf(gs ,xx );
156
+
157
+ for k = 1 : 4
158
+ subplot(4 ,1 ,k )
159
+ [j ,i ]=ind2sub([2 2 ],k );
160
+ bar(Q .bin ,Q .H(: ,j ,i ),1 ,' FaceColor' ,mycolor(1 ,k ),' EdgeColor' ,mycolor(1 ,k ),' FaceAlpha' ,0.5 );
161
+ % bar(bins,hc,1,'FaceColor',mycolor(2),'EdgeColor',mycolor(0));hold on
162
+ set(gca ,' xtick' ,[])
163
+ set(gca ,' ytick' ,[])
164
+ hold on
165
+ plot(xx ,yy ,' k--' ,' linewidth' ,1.5 )
166
+ ylim([0 0.5 ])
167
+ xlim([-4 4 ])
168
+ set(gcf ,' position' ,[100 100 200 600 ])
169
+ end
170
+ % export_fig(f2,'figures/fig_main_unimodal_postprocess_histogram.pdf','-pdf','-nocrop','-transparent','-painters');
171
+ print(gcf , ' -dpdf' , ' figures\f ig_main_unimodal_postprocess_histogram.pdf' );
172
+ end
173
+
174
+
175
+ end
0 commit comments