-
Notifications
You must be signed in to change notification settings - Fork 0
/
k2kRobustEstimate.cs
219 lines (162 loc) · 6.69 KB
/
k2kRobustEstimate.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace k2kLib
{
public class k2kRobustEstimate
{
public k2kRobustEstimate(k2kLeastSquare LeastSquare, int IterationCount)
{
this.LeastSquare = LeastSquare;
this.IterationCount = IterationCount;
}
public k2kLeastSquare LeastSquare;
public int IterationCount;
public virtual k2kEstimateResult GetMaxLikelihood(k2kObservationEquation equation)
{
throw new NotImplementedException();
}
protected virtual double[] GetResidualError(double[] Approximation, k2kObservationEquation equation)
{
int i, j;
var ret = new double[equation.Count];
double TempExplaining;
for (i = 0; i < equation.Count; ++i)
{
TempExplaining = 0;
for (j = 0; j < equation[i].Explaining.Length; ++j)
{
TempExplaining += equation[i].Explaining[j] * Approximation[j];
}
TempExplaining -= equation[i].Objective;
ret[i] = TempExplaining * TempExplaining;
}
return ret;
}
protected virtual void ResidualEvaluation(double[] ResidualErrors, k2kObservationEquation equation)
{
throw new NotImplementedException();
}
}
public class k2kEstimateResult
{
public k2kEstimateResult(double[] Approximation, double[] ResidualError)
{
this.Approximation = Approximation;
this.ResidualError = ResidualError;
}
public double[] Approximation;
public double[] ResidualError;
}
public class k2kMEstimator : k2kRobustEstimate
{
public k2kMEstimator(k2kLeastSquare LeastSquare, int IterationCount) : base(LeastSquare, IterationCount) { }
public override k2kEstimateResult GetMaxLikelihood(k2kObservationEquation equation)
{
Inlier = new k2kObservationEquation(equation);
double[] ResidualError = null;
double[] Approximation = null;
for (int i = 0; i < IterationCount; ++i)
{
if (Inlier.Count < equation[0].Length)
break;
Approximation = LeastSquare.GetSolution(Inlier);
ResidualError = GetResidualError(Approximation, equation);
ResidualEvaluation(ResidualError, equation);
}
return new k2kEstimateResult(Approximation, ResidualError);
}
public double AllowableErrorCoefficient = 1.5;
protected k2kObservationEquation Inlier;
protected override void ResidualEvaluation(double[] ResidualErrors, k2kObservationEquation equation)
{
Inlier.Clear();
int i;
var Sigma = k2kMath.GetMedian<double>(ResidualErrors) * 1.4826;
var AllowableError = Sigma * AllowableErrorCoefficient;
for (i = 0; i < equation.Count; ++i)
{
if (ResidualErrors[i] <= AllowableError)
Inlier.Add(equation[i]);
}
}
}
public class k2kRandomizedEstimate : k2kRobustEstimate
{
public k2kRandomizedEstimate(k2kLeastSquare LeastSquare, int IterationCount, int SampleCount)
: base(LeastSquare, IterationCount)
{
this.SampleCount = SampleCount;
}
public int SampleCount;
public override k2kEstimateResult GetMaxLikelihood(k2kObservationEquation equation)
{
if (SampleCount < equation[0].Length)
throw new ArgumentException();
int i;
var rand = new k2kRandom();
k2kObservationEquation sample;
double[] ResidualError = null;
double[] Approximation, BestApproximation = null;
InitBestResidualError();
for (i = 0; i < IterationCount; ++i)
{
sample = new k2kObservationEquation(rand.RandomSampling(SampleCount, equation));
Approximation = LeastSquare.GetSolution(sample);
ResidualError = GetResidualError(Approximation, equation);
ResidualEvaluation(ResidualError, equation);
if (BestResidualError)
BestApproximation = Approximation;
}
return new k2kEstimateResult(BestApproximation, ResidualError);
}
protected bool BestResidualError = false;
protected override void ResidualEvaluation(double[] ResidualErrors, k2kObservationEquation equation)
{
throw new NotImplementedException();
}
protected virtual void InitBestResidualError()
{
throw new NotImplementedException();
}
}
public class k2kLMedS : k2kRandomizedEstimate
{
public k2kLMedS(k2kLeastSquare LeastSquare, int IterationCount, int SampleCount) : base(LeastSquare, IterationCount, SampleCount) { }
protected double MedResidualError;
protected double MinMedResidualError;
protected override void InitBestResidualError()
{
MinMedResidualError = double.MaxValue;
}
protected override void ResidualEvaluation(double[] ResidualErrors, k2kObservationEquation equation)
{
double MedResidualError = k2kMath.GetMedian<double>(ResidualErrors);
BestResidualError = (MedResidualError < MinMedResidualError);
if (BestResidualError)
MinMedResidualError = MedResidualError;
}
}
public class k2kRANSAC : k2kRandomizedEstimate
{
public k2kRANSAC(k2kLeastSquare LeastSquare, int IterationCount, int SampleCount) : base(LeastSquare, IterationCount, SampleCount) { }
public double AllowableResidualError = 1.0;
protected int AllowableCount;
protected int MinCount;
protected override void InitBestResidualError()
{
MinCount = int.MaxValue;
}
protected override void ResidualEvaluation(double[] ResidualErrors, k2kObservationEquation equation)
{
AllowableCount = 0;
foreach (var err in ResidualErrors)
if (err <= AllowableResidualError)
++AllowableCount;
BestResidualError = (AllowableCount < MinCount);
if (BestResidualError)
MinCount = AllowableCount;
}
}
}