LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
kpcfit_sub_bcfit.m
1function [E1j,E3j,f]=kpcfit_sub_bcfit(E,SCVj,G2j,BC,BCLags,MaxIterBC, MaxRunsBC)
2%% optimization parameters
3MaxTimeBC = 100;
4NumMAPs=length(SCVj);
5TOL=1e-9;
6EPSTOL=10*TOL;
7VERBOSE=0;
8
9
10%% other variables
11NBC=norm(BC,2); % normalizing constants for bicovariances
12
13% truncate bicorrelations when it gets negative
14tag=[];
15for index=1:size(BCLags,1)
16 if find(diff(BCLags(index,:))<0)
17 tag(end+1,1)=index;
18 end
19end
20BCLags(tag,:)=[];
21BC(tag)=[];
22
23E1j=E(1).^(1/NumMAPs)*ones(1,NumMAPs);
24t=E(1)*rand;
25fold=0;
26
27xparamset = {}; % record all the possible x params
28fset = []; % record the objective function values for each param set
29xparam_best = [];
30f_best = inf;
31for index = 1:NumMAPs
32 E2j(index)=(1+SCVj(index))*E1j(index)^2;
33 E3j(index)=(3/2+t)*E2j(index)^2/E1j(index);
34end
35x0=[E1j(:);E3j(:)]';
36x0base=x0;
37
38
39%fprintf(1,'bcfit: maximum number of solver iterations per run is MaxIterBC = %d\n', MaxIterBC);
40for ind = 1:MaxRunsBC
41 tstart = tic();
42 if VERBOSE ~= 0
43 fprintf(1,'bcfit: run %d of %d ',ind, MaxRunsBC);
44 end
45 tic;
46
47 options = optimset('LargeScale','off', ...
48 'MaxIter',MaxIterBC, ...
49 'MaxFunEvals',1e10, ...
50 'MaxSQPIter',500, ...
51 'TolCon',TOL, ...
52 'Display','off', ...
53 'DiffMinChange',1e-10, ...
54 'OutputFcn',@(x,optimValues,state) kpcfit_sub_bcfit_outfun(x,optimValues,state,MaxIterBC,MaxTimeBC,tstart,f_best));
55
56 [x,f,exitflag,output]=fmincon(@objfun,x0,[],[],[],[],0*x0+EPSTOL,[],@nnlcon,options);
57
58 if VERBOSE ~= 0
59 fprintf(1,'(%3.3f sec, %d iter) f=%f',toc, output.iterations, f);
60 end
61
62 xparamset{end+1} = x;
63 fset(1,end+1) = f;
64 if f < f_best
65 if VERBOSE ~= 0
66 fprintf(1,'**best**',toc);
67 end
68 f_best = f;
69 xparam_best = x;
70 end
71
72 if VERBOSE ~= 0
73 fprintf(1,'\n',toc);
74 end
75
76 x0 = x0base .* [(0.25+1.75*rand(NumMAPs,1)); ones(NumMAPs,1)]';
77 t = rand*E(1);
78 for index = 1:NumMAPs
79 E2j(index)=(1+SCVj(index))*x0(index)^2;
80 x0(NumMAPs+index)=(3/2+t)*E2j(index)^2/x0(index);
81 end
82end
83
84[E1j,E3j]=xtopar(xparam_best);
85E1j(1)=E(1)/prod(E1j(2:end));
86prod(E1j);
87f = f_best;
88
89
90 function [E1j,E3j]=xtopar(x)
91 E1j=x(1:NumMAPs);
92 E3j=x((NumMAPs+1):end);
93 E1j(1)=E(1)/prod(E1j(2:end));
94 end
95 function [c,ceq]=nnlcon(x)
96 [E1j,E3j]=xtopar(x);
97 c=[];
98 ceq=[];
99 for j=2:NumMAPs
100 c(end+1)=(2+EPSTOL)*E1j(j)^2-E2j(j); % E2j(j) > 2*E1j(j)^2
101 c(end+1)=(3/2+EPSTOL)*E2j(j)^2/E1j(j)-E3j(j);
102 % E3j(j) >
103 % 3*E2j(j)^2/(2*E1j(j))
104 end
105 if SCVj(1)>1
106 % if SCV for the first MMPP2 is > 1
107 % add the constraint that E2 > 2*E1^2
108 % and the constraint that E3 > 3*E2^2/(2*E1)
109 c(end+1)=(2+EPSTOL)*E1j(1)^2-E2j(1);
110 c(end+1)=(3/2+EPSTOL)*E2j(1)^2/E1j(1)-E3j(1);
111 end
112 temp = prod(E3j);
113 temp = temp/(factorial(3))^(NumMAPs-1);
114 c(end+1) = temp/E(3) -2;
115 c(end+1) = 0.5 - temp/E(3);
116 for j = 2:NumMAPs
117 c(end+1) = 1/3*E1j(j)/(E2j(j)-2*E1j(j)^2)*E3j(j)-1/2*E2j(j)^2/(E2j(j)-2*E1j(j)^2) - 1/(1e+16);
118 c(end+1) = 1e-16 - 1/3*E1j(j)/(E2j(j)-2*E1j(j)^2)*E3j(j)-1/2*E2j(j)^2/(E2j(j)-2*E1j(j)^2);
119
120 end
121
122 end
123 function f=objfun(x)
124 [E1j,E3j]=xtopar(x);
125 BCj=ones(1,size(BCLags,1));
126
127 [compMAP, subMaps, err] = kpcfit_sub_compose(E1j, SCVj, E3j, G2j);
128 if err ~= 0
129 f=max(2*fold, 10^6);
130 return
131 end
132
133 % scale MAP to match E(1)
134 compMAP=map_scale(compMAP, E(1));
135
136 for indexL=1:size(BCLags,1)
137 BCj(indexL)=(map_joint(compMAP,BCLags(indexL,:),[1,1,1]));
138 end
139
140 f=norm(BC-BCj,2)/NBC;
141
142 if isnan(f)
143 f=2*fold;
144 else
145 fold=f;
146 end
147 end
148
149end