1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.hipparchus.optim.nonlinear.vector.leastsquares;
24
25 import java.util.ArrayList;
26
27 import org.hipparchus.UnitTestUtils;
28 import org.hipparchus.UnitTestUtils.SimpleRegression;
29 import org.hipparchus.analysis.MultivariateMatrixFunction;
30 import org.hipparchus.analysis.MultivariateVectorFunction;
31 import org.hipparchus.analysis.UnivariateFunction;
32
33
34
35
36
37
38
39
40
41
42
43
44
45 class StraightLineProblem {
46
47 private final ArrayList<double[]> points;
48
49 private final double sigma;
50
51
52
53
54 public StraightLineProblem(double error) {
55 points = new ArrayList<double[]>();
56 sigma = error;
57 }
58
59 public void addPoint(double px, double py) {
60 points.add(new double[] { px, py });
61 }
62
63
64
65
66 public double[] x() {
67 final double[] v = new double[points.size()];
68 for (int i = 0; i < points.size(); i++) {
69 final double[] p = points.get(i);
70 v[i] = p[0];
71 }
72
73 return v;
74 }
75
76
77
78
79 public double[] y() {
80 final double[] v = new double[points.size()];
81 for (int i = 0; i < points.size(); i++) {
82 final double[] p = points.get(i);
83 v[i] = p[1];
84 }
85
86 return v;
87 }
88
89 public double[] target() {
90 return y();
91 }
92
93 public double[] weight() {
94 final double weight = 1 / (sigma * sigma);
95 final double[] w = new double[points.size()];
96 for (int i = 0; i < points.size(); i++) {
97 w[i] = weight;
98 }
99
100 return w;
101 }
102
103 public MultivariateVectorFunction getModelFunction() {
104 return new MultivariateVectorFunction() {
105 public double[] value(double[] params) {
106 final Model line = new Model(params[0], params[1]);
107
108 final double[] model = new double[points.size()];
109 for (int i = 0; i < points.size(); i++) {
110 final double[] p = points.get(i);
111 model[i] = line.value(p[0]);
112 }
113
114 return model;
115 }
116 };
117 }
118
119 public MultivariateMatrixFunction getModelFunctionJacobian() {
120 return new MultivariateMatrixFunction() {
121 public double[][] value(double[] point) {
122 return jacobian(point);
123 }
124 };
125 }
126
127
128
129
130
131 public double[] solve() {
132 final UnitTestUtils.SimpleRegression regress = new UnitTestUtils.SimpleRegression();
133 for (double[] d : points) {
134 regress.addData(d[0], d[1]);
135 }
136
137 final double[] result = { regress.getSlope(), regress.getIntercept() };
138 return result;
139 }
140
141 private double[][] jacobian(double[] params) {
142 final double[][] jacobian = new double[points.size()][2];
143
144 for (int i = 0; i < points.size(); i++) {
145 final double[] p = points.get(i);
146
147 jacobian[i][0] = p[0];
148
149 jacobian[i][1] = 1;
150 }
151
152 return jacobian;
153 }
154
155
156
157
158 public static class Model implements UnivariateFunction {
159 final double a;
160 final double b;
161
162 public Model(double a,
163 double b) {
164 this.a = a;
165 this.b = b;
166 }
167
168 public double value(double x) {
169 return a * x + b;
170 }
171 }
172 }