1function [MAP,ERR]=map2_fit(e1,e2,e3,g2)
2% [MAP,ERR]=map2_fit(e1,e2,e3,g2)
3% A. Heindl, G.Horvath, K. Gross
"Explicit inverse characterization of
4% acyclic MAPs of second order"
13 % select e3 that maximizes the range of correlations
18 e3=12*e1^3*h2+6*e1^3*h3+6*e1^3*(1+h2^2);
20 e3=(3/2+1e-3)*e2^2/e1;
23 e3=(3/2+1e-3)*e2^2/e1;
25 e3=(1+1e-10)*(12*e1^3*h2+6*e1^3*(h2*(1-h2-2*sqrt(-h2)))+6*e1^3*(1+h2^2));
29 % select the minimum e3
32 e3=(3/2+1e-6)*e2^2/e1;
34 h3=h2*(1-h2-2*sqrt(-h2));
35 e3=6*e1^3*(h2^2 + h3);
39 % select the maximum e3
45 e3=6*e1^3*(h2^2 + h3);
53 e3=r*(3/2+1e-6)*e2^2/e1+(1-r)*10^6;
55 h3=r*(-h2)^2+(1-r)*h2*(1-h2-2*sqrt(-h2));
56 e3=6*e1^3*(h2^2 + h3);
60 % use a custom random e3
64 e3=r*(3/2+1e-6)*e2^2/e1+(1-r)*10^6;
66 h3=r*h2*(1-h2-2*sqrt(-h2))+(1-r)*(-h2)^2;
67 e3=6*e1^3*(h2^2 + h3);
77 ERR=10; % mean out of bounds
83 MAP=map_exponential(e1);
85 return % without
this the Poisson answer just built fell through to
86 % the h2 out-of-bounds branch below, which overwrote it with
87 % MAP={} and ERR=30. Map2_fit.java reproduces the same fall-through.
90 ERR=20; % correlated exponential
97 if length(MAP)>0 && map_isfeasible(MAP)==0
101elseif -1/4<=h2 && h2<0 && h2*(1-h2-2*sqrt(-h2))<=h3 && h3<=-h2^2
103 if length(MAP)>0 && map_isfeasible(MAP)==0
108 if ~(-1/4<=h2 && h2<0 && h2*(1-h2-2*sqrt(-h2))<=h3 && h3<=-h2^2)
109 ERR=30; % h2 out of bounds
112 elseif (h2>0 && h3<0) || h2*(1-h2-2*sqrt(-h2))>h3 || h3<=-h2^2
113 ERR=40; % h3 out of bounds
117 error(
'I lost an error')
121 function MAP=map_fit_hyper()
123 if (b-c)/(b+c)<=g2 && g2<1
124 MAP{1}=(1/(2*r1*h3))*[-(2*h2+b-c),0;0,-(2*h2+b+c)];
125 MAP{2}=(1/(4*r1*h3))*[(2*h2+b-c)*(1-b/c+g2*(1+b/c)),(2*h2+b-c)*(1+b/c)*(1-g2); (2*h2+b+c)*(1-b/c)*(1-g2),(2*h2+b+c)*(1+b/c+g2*(1-b/c))];
128 ERR=51; % g2 out of bounds
134 MAP{1}=(1/(2*r1*h3))*[-(2*h2+b-c),0;0,-(2*h2+b+c)];
135 MAP{2}=(1/(4*r1*h3))*[(2*h2+b-c)*(1-b/c+g2*(1+b/c)),(2*h2+b-c)*(1+b/c)*(1-g2); (2*h2+b+c)*(1-b/c)*(1-g2),(2*h2+b+c)*(1+b/c+g2*(1-b/c))];
137 elseif -(h3+h2^2)/h2 <= g2 && g2<0
139 d1=((1-a)*(2*h2*g2+b-c)+g2*(b+c)-(b-c))/((1-a)*(2*h2+b-c)+2*c);
140 d2=((g2-1)*(b-c))/((1-a)*(2*h2+b-c)+2*c);
141 MAP{1}=(1/(2*r1*h3))*[-(2*h2+b-c),(2*h2+b-c)*(1-a);0,-(2*h2+b+c)];
142 MAP{2}=(1/(2*r1*h3))*[(2*h2+b-c)*d1,(2*h2+b-c)*(a-d1); (2*h2+b+c)*d2, (2*h2+b+c)*(1-d2)];
145 ERR=52; % g2 out of bounds
151 function MAP=map_fit_hypo()
153 if g2<=-(h2+sqrt(-h3))^2/h2
154 a=(2*h2+b-c)*(h2+sqrt(-h3))/(2*h2*sqrt(-h3));
156 d1=((1-a)*(2*h2*g2+b-c)+g2*(b+c)-(b-c))/((1-a)*(2*h2+b-c)+2*c);
157 d2=((g2-1)*(b-c))/((1-a)*(2*h2+b-c)+2*c);
158 MAP{1}=(1/(2*r1*h3))*[-(2*h2+b-c),(2*h2+b-c)*(1-a);0,-(2*h2+b+c)];
159 MAP{2}=(1/(2*r1*h3))*[(2*h2+b-c)*d1,(2*h2+b-c)*(a-d1); (2*h2+b+c)*d2, (2*h2+b+c)*(1-d2)];
161 ERR=53; % g2 out of bounds
166 if g2 >= -(h3+h2^2)/h2
169 d1=((1-a)*(2*h2*g2+b-c)+g2*(b+c)-(b-c))/((1-a)*(2*h2+b-c)+2*c);
170 d2=((g2-1)*(b-c))/((1-a)*(2*h2+b-c)+2*c);
171 MAP{1}=(1/(2*r1*h3))*[-(2*h2+b-c),(2*h2+b-c)*(1-a);0,-(2*h2+b+c)];
172 MAP{2}=(1/(2*r1*h3))*[(2*h2+b-c)*d1,(2*h2+b-c)*(a-d1); (2*h2+b+c)*d2, (2*h2+b+c)*(1-d2)];
174 ERR=54; % g2 out of bounds