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);
86 ERR=20; % correlated exponential
93 if length(MAP)>0 && map_isfeasible(MAP)==0
97elseif -1/4<=h2 && h2<0 && h2*(1-h2-2*sqrt(-h2))<=h3 && h3<=-h2^2
99 if length(MAP)>0 && map_isfeasible(MAP)==0
104 if ~(-1/4<=h2 && h2<0 && h2*(1-h2-2*sqrt(-h2))<=h3 && h3<=-h2^2)
105 ERR=30; % h2 out of bounds
108 elseif (h2>0 && h3<0) || h2*(1-h2-2*sqrt(-h2))>h3 || h3<=-h2^2
109 ERR=40; % h3 out of bounds
113 error(
'I lost an error')
117 function MAP=map_fit_hyper()
119 if (b-c)/(b+c)<=g2 && g2<1
120 MAP{1}=(1/(2*r1*h3))*[-(2*h2+b-c),0;0,-(2*h2+b+c)];
121 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))];
124 ERR=51; % g2 out of bounds
130 MAP{1}=(1/(2*r1*h3))*[-(2*h2+b-c),0;0,-(2*h2+b+c)];
131 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))];
133 elseif -(h3+h2^2)/h2 <= g2 && g2<0
135 d1=((1-a)*(2*h2*g2+b-c)+g2*(b+c)-(b-c))/((1-a)*(2*h2+b-c)+2*c);
136 d2=((g2-1)*(b-c))/((1-a)*(2*h2+b-c)+2*c);
137 MAP{1}=(1/(2*r1*h3))*[-(2*h2+b-c),(2*h2+b-c)*(1-a);0,-(2*h2+b+c)];
138 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)];
141 ERR=52; % g2 out of bounds
147 function MAP=map_fit_hypo()
149 if g2<=-(h2+sqrt(-h3))^2/h2
150 a=(2*h2+b-c)*(h2+sqrt(-h3))/(2*h2*sqrt(-h3));
152 d1=((1-a)*(2*h2*g2+b-c)+g2*(b+c)-(b-c))/((1-a)*(2*h2+b-c)+2*c);
153 d2=((g2-1)*(b-c))/((1-a)*(2*h2+b-c)+2*c);
154 MAP{1}=(1/(2*r1*h3))*[-(2*h2+b-c),(2*h2+b-c)*(1-a);0,-(2*h2+b+c)];
155 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)];
157 ERR=53; % g2 out of bounds
162 if g2 >= -(h3+h2^2)/h2
165 d1=((1-a)*(2*h2*g2+b-c)+g2*(b+c)-(b-c))/((1-a)*(2*h2+b-c)+2*c);
166 d2=((g2-1)*(b-c))/((1-a)*(2*h2+b-c)+2*c);
167 MAP{1}=(1/(2*r1*h3))*[-(2*h2+b-c),(2*h2+b-c)*(1-a);0,-(2*h2+b+c)];
168 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)];
170 ERR=54; % g2 out of bounds