1 | // $Id: test_regression_linear.cc 212 2004-11-03 15:07:00Z jari $ |
---|
2 | |
---|
3 | // C++ tools include |
---|
4 | //////////////////// |
---|
5 | #include "RegressionLinear.h" |
---|
6 | #include "vector.h" |
---|
7 | |
---|
8 | |
---|
9 | // Standard includes |
---|
10 | //////////////////// |
---|
11 | #include <gsl/gsl_fit.h> |
---|
12 | #include <iostream> |
---|
13 | |
---|
14 | |
---|
15 | using namespace std; |
---|
16 | |
---|
17 | int main() |
---|
18 | |
---|
19 | { |
---|
20 | bool ok=true; |
---|
21 | |
---|
22 | theplu::gslapi::vector x(4); |
---|
23 | x(0)=1970; |
---|
24 | x(1)=1980; |
---|
25 | x(2)=1990; |
---|
26 | x(3)=2000; |
---|
27 | theplu::gslapi::vector y(4); |
---|
28 | y(0)=12; |
---|
29 | y(1)=11; |
---|
30 | y(2)=14; |
---|
31 | y(3)=13; |
---|
32 | theplu::gslapi::vector w(4); |
---|
33 | w(0)=0.1; |
---|
34 | w(1)=0.2; |
---|
35 | w(2)=0.3; |
---|
36 | w(3)=0.4; |
---|
37 | |
---|
38 | double c0, c1, cov00, cov01, cov11, chisq; |
---|
39 | int n=4; |
---|
40 | int i=4; |
---|
41 | gsl_fit_wlinear(x.gsl_vector_pointer()->data, x.gsl_vector_pointer()->stride, |
---|
42 | w.gsl_vector_pointer()->data, w.gsl_vector_pointer()->stride, |
---|
43 | y.gsl_vector_pointer()->data, y.gsl_vector_pointer()->stride, |
---|
44 | x.gsl_vector_pointer()->size, |
---|
45 | &c0, &c1, |
---|
46 | &cov00, &cov01, &cov11, &chisq); |
---|
47 | |
---|
48 | printf("# best fit: Y = %g + %g X\n", c0, c1); |
---|
49 | printf("# covariance matrix:\n"); |
---|
50 | printf("# [ %g, %g\n# %g, %g]\n", cov00, cov01, cov01, cov11); |
---|
51 | printf("# chisq = %g\n", chisq); |
---|
52 | |
---|
53 | for (i = 0; i < n; i++) |
---|
54 | printf("data: %g %g %g\n", x[i], y[i], 1/sqrt(w[i])); |
---|
55 | |
---|
56 | printf("\n"); |
---|
57 | |
---|
58 | for (i = -30 ; i < 130 ; i++) |
---|
59 | { |
---|
60 | double xf = x[0] + (i/100.0) * (x[n-1] - x[0]); |
---|
61 | double yf, yf_err; |
---|
62 | |
---|
63 | gsl_fit_linear_est (xf, c0, c1, cov00, cov01, cov11, &yf, &yf_err); |
---|
64 | |
---|
65 | printf("fit: %g %g\n", xf, yf); |
---|
66 | printf("hi : %g %g\n", xf, yf + yf_err); |
---|
67 | printf("lo : %g %g\n", xf, yf - yf_err); |
---|
68 | } |
---|
69 | |
---|
70 | |
---|
71 | if (ok) |
---|
72 | return 0; |
---|
73 | return -1; |
---|
74 | } |
---|