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 else
85 MAP={};
86 ERR=20; % correlated exponential
87 return
88 end
89end
90
91if h2>0 && h3>0
92 MAP=map_fit_hyper();
93 if length(MAP)>0 && map_isfeasible(MAP)==0
94 ERR=-1;
95 end
96 return
97elseif -1/4<=h2 && h2<0 && h2*(1-h2-2*sqrt(-h2))<=h3 && h3<=-h2^2
98 MAP=map_fit_hypo();
99 if length(MAP)>0 && map_isfeasible(MAP)==0
100 ERR=-1;
101 end
102 return
103else
104 if ~(-1/4<=h2 && h2<0 && h2*(1-h2-2*sqrt(-h2))<=h3 && h3<=-h2^2)
105 ERR=30; % h2 out of bounds
106 MAP={};
107 return
108 elseif (h2>0 && h3<0) || h2*(1-h2-2*sqrt(-h2))>h3 || h3<=-h2^2
109 ERR=40; % h3 out of bounds
110 MAP={};
111 return
112 else
113 error('I lost an error')
114 end
115end
116
117 function MAP=map_fit_hyper()
118 if b>=0
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))];
122 return
123 else
124 ERR=51; % g2 out of bounds
125 MAP={};
126 return
127 end
128 elseif b<0
129 if 0<=g2 && g2<1
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))];
132 return
133 elseif -(h3+h2^2)/h2 <= g2 && g2<0
134 a=(h3+h2^2)/h2;
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)];
139 return
140 else
141 ERR=52; % g2 out of bounds
142 MAP={};
143 return
144 end
145 end
146 end
147 function MAP=map_fit_hypo()
148 if g2>=0
149 if g2<=-(h2+sqrt(-h3))^2/h2
150 a=(2*h2+b-c)*(h2+sqrt(-h3))/(2*h2*sqrt(-h3));
151 c=-c;
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)];
156 else
157 ERR=53; % g2 out of bounds
158 MAP={};
159 return
160 end
161 elseif g2<0
162 if g2 >= -(h3+h2^2)/h2
163 a=(h3+h2^2)/h2;
164 c=-c;
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)];
169 else
170 ERR=54; % g2 out of bounds
171 MAP={};
172 return
173 end
174 end
175 end
176end