LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
map2_fit.m
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"
5if nargin==3
6 g2=e3;
7 e3=-1;
8end
9ERR=0;
10r1=e1; r2=e2/2;
11h2=(r2-r1^2)/r1^2;
12if e3==-1
13 % select e3 that maximizes the range of correlations
14 scv=(e2-e1^2)/e1^2;
15 if 1<=scv && scv<3
16 if g2<0
17 h3=h2-h2^2; %b=0
18 e3=12*e1^3*h2+6*e1^3*h3+6*e1^3*(1+h2^2);
19 else
20 e3=(3/2+1e-3)*e2^2/e1;
21 end
22 elseif 3<=scv
23 e3=(3/2+1e-3)*e2^2/e1;
24 elseif 0<scv && scv<1
25 e3=(1+1e-10)*(12*e1^3*h2+6*e1^3*(h2*(1-h2-2*sqrt(-h2)))+6*e1^3*(1+h2^2));
26 end
27end
28if e3==-2
29 % select the minimum e3
30 scv=(e2-e1^2)/e1^2;
31 if 1<=scv
32 e3=(3/2+1e-6)*e2^2/e1;
33 elseif 0<scv && scv<1
34 h3=h2*(1-h2-2*sqrt(-h2));
35 e3=6*e1^3*(h2^2 + h3);
36 end
37end
38if e3==-3
39 % select the maximum e3
40 scv=(e2-e1^2)/e1^2;
41 if 1<=scv
42 e3=10^6;
43 elseif 0<scv && scv<1
44 h3=(-h2)^2;
45 e3=6*e1^3*(h2^2 + h3);
46 end
47end
48if e3==-4
49 % select a random e3
50 scv=(e2-e1^2)/e1^2;
51 r = rand;
52 if 1<=scv
53 e3=r*(3/2+1e-6)*e2^2/e1+(1-r)*10^6;
54 elseif 0<scv && scv<1
55 h3=r*(-h2)^2+(1-r)*h2*(1-h2-2*sqrt(-h2));
56 e3=6*e1^3*(h2^2 + h3);
57 end
58end
59if e3>-1 && e3<0
60 % use a custom random e3
61 scv=(e2-e1^2)/e1^2;
62 r = abs(e3);
63 if 1<=scv
64 e3=r*(3/2+1e-6)*e2^2/e1+(1-r)*10^6;
65 elseif 0<scv && scv<1
66 h3=r*h2*(1-h2-2*sqrt(-h2))+(1-r)*(-h2)^2;
67 e3=6*e1^3*(h2^2 + h3);
68 end
69end
70r3=e3/6;
71h3=(r3*r1-r2^2)/r1^4;
72b=h3+h2^2-h2;
73c=sqrt(b^2+4*h2^3);
74
75if r1<=0
76 MAP={};
77 ERR=10; % mean out of bounds
78 return
79end
80
81if h2==0
82 if h3==0 && g2==0
83 MAP=map_exponential(e1);
84 ERR=0;
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.
88 else
89 MAP={};
90 ERR=20; % correlated exponential
91 return
92 end
93end
94
95if h2>0 && h3>0
96 MAP=map_fit_hyper();
97 if length(MAP)>0 && map_isfeasible(MAP)==0
98 ERR=-1;
99 end
100 return
101elseif -1/4<=h2 && h2<0 && h2*(1-h2-2*sqrt(-h2))<=h3 && h3<=-h2^2
102 MAP=map_fit_hypo();
103 if length(MAP)>0 && map_isfeasible(MAP)==0
104 ERR=-1;
105 end
106 return
107else
108 if ~(-1/4<=h2 && h2<0 && h2*(1-h2-2*sqrt(-h2))<=h3 && h3<=-h2^2)
109 ERR=30; % h2 out of bounds
110 MAP={};
111 return
112 elseif (h2>0 && h3<0) || h2*(1-h2-2*sqrt(-h2))>h3 || h3<=-h2^2
113 ERR=40; % h3 out of bounds
114 MAP={};
115 return
116 else
117 error('I lost an error')
118 end
119end
120
121 function MAP=map_fit_hyper()
122 if b>=0
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))];
126 return
127 else
128 ERR=51; % g2 out of bounds
129 MAP={};
130 return
131 end
132 elseif b<0
133 if 0<=g2 && g2<1
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))];
136 return
137 elseif -(h3+h2^2)/h2 <= g2 && g2<0
138 a=(h3+h2^2)/h2;
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)];
143 return
144 else
145 ERR=52; % g2 out of bounds
146 MAP={};
147 return
148 end
149 end
150 end
151 function MAP=map_fit_hypo()
152 if g2>=0
153 if g2<=-(h2+sqrt(-h3))^2/h2
154 a=(2*h2+b-c)*(h2+sqrt(-h3))/(2*h2*sqrt(-h3));
155 c=-c;
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)];
160 else
161 ERR=53; % g2 out of bounds
162 MAP={};
163 return
164 end
165 elseif g2<0
166 if g2 >= -(h3+h2^2)/h2
167 a=(h3+h2^2)/h2;
168 c=-c;
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)];
173 else
174 ERR=54; % g2 out of bounds
175 MAP={};
176 return
177 end
178 end
179 end
180end