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
//
// A rust binding for the GSL library by Guillaume Gomez (guillaume1.gomez@gmail.com)
//

/*!
Linear Regression

The functions described in this section can be used to perform least-squares fits to a straight line model, Y(c,x) = c_0 + c_1 x.
!*/

use crate::Value;

/// This function computes the best-fit linear regression coefficients (c0,c1) of the model
/// Y = c_0 + c_1 X for the dataset (x, y), two vectors of length n with strides xstride and
/// ystride.
///
/// The errors on y are assumed unknown so the variance-covariance matrix for the parameters
/// (c0, c1) is estimated from the scatter of the points around the best-fit line and returned via
/// the parameters (cov00, cov01, cov11).
///
/// The sum of squares of the residuals from the best-fit line is returned in sumsq. Note: the
/// correlation coefficient of the data can be computed using gsl_stats_correlation (see
/// [`Correlation`](http://www.gnu.org/software/gsl/manual/html_node/Correlation.html#Correlation)),
/// it does not depend on the fit.
///
/// Returns `(Value, c0, c1, cov00, cov01, cov11, sumsq)`.
#[doc(alias = "gsl_fit_linear")]
pub fn linear(
    x: &[f64],
    xstride: usize,
    y: &[f64],
    ystride: usize,
    n: usize,
) -> (Value, f64, f64, f64, f64, f64, f64) {
    let mut c0 = 0.;
    let mut c1 = 0.;
    let mut cov00 = 0.;
    let mut cov01 = 0.;
    let mut cov11 = 0.;
    let mut sumsq = 0.;
    let ret = unsafe {
        ::sys::gsl_fit_linear(
            x.as_ptr(),
            xstride,
            y.as_ptr(),
            ystride,
            n,
            &mut c0,
            &mut c1,
            &mut cov00,
            &mut cov01,
            &mut cov11,
            &mut sumsq,
        )
    };
    (Value::from(ret), c0, c1, cov00, cov01, cov11, sumsq)
}

/// This function computes the best-fit linear regression coefficients (c0,c1) of the model
/// Y = c_0 + c_1 X for the weighted dataset (x, y), two vectors of length n with strides xstride
/// and ystride.
///
/// The vector w, of length n and stride wstride, specifies the weight of each datapoint.
///
/// The weight is the reciprocal of the variance for each datapoint in y.
///
/// The covariance matrix for the parameters (c0, c1) is computed using the weights and returned via
/// the parameters (cov00, cov01, cov11).
/// The weighted sum of squares of the residuals from the best-fit line, \chi^2, is returned in chisq.
///
/// Returns `(Value, c0, c1, cov00, cov01, cov11, chisq)`.
#[doc(alias = "gsl_fit_wlinear")]
pub fn wlinear(
    x: &[f64],
    xstride: usize,
    w: &[f64],
    wstride: usize,
    y: &[f64],
    ystride: usize,
    n: usize,
) -> (Value, f64, f64, f64, f64, f64, f64) {
    let mut c0 = 0.;
    let mut c1 = 0.;
    let mut cov00 = 0.;
    let mut cov01 = 0.;
    let mut cov11 = 0.;
    let mut chisq = 0.;
    let ret = unsafe {
        ::sys::gsl_fit_wlinear(
            x.as_ptr(),
            xstride,
            w.as_ptr(),
            wstride,
            y.as_ptr(),
            ystride,
            n,
            &mut c0,
            &mut c1,
            &mut cov00,
            &mut cov01,
            &mut cov11,
            &mut chisq,
        )
    };
    (Value::from(ret), c0, c1, cov00, cov01, cov11, chisq)
}

/// This function uses the best-fit linear regression coefficients c0, c1 and their covariance
/// cov00, cov01, cov11 to compute the fitted function y and its standard deviation y_err for the
/// model Y = c_0 + c_1 X at the point x.
///
/// Returns `(Value, y, y_err)`.
#[doc(alias = "gsl_fit_linear_est")]
pub fn linear_est(
    x: f64,
    c0: f64,
    c1: f64,
    cov00: f64,
    cov01: f64,
    cov11: f64,
) -> (Value, f64, f64) {
    let mut y = 0.;
    let mut y_err = 0.;
    let ret =
        unsafe { ::sys::gsl_fit_linear_est(x, c0, c1, cov00, cov01, cov11, &mut y, &mut y_err) };
    (Value::from(ret), y, y_err)
}

/// This function computes the best-fit linear regression coefficient c1 of the model Y = c_1 X for
/// the datasets (x, y), two vectors of length n with strides xstride and ystride.
/// The errors on y are assumed unknown so the variance of the parameter c1 is estimated from the
/// scatter of the points around the best-fit line and returned via the parameter cov11.
/// The sum of squares of the residuals from the best-fit line is returned in sumsq.
///
/// Returns `(Value, c1, cov11, sumsq)`.
#[doc(alias = "gsl_fit_mul")]
pub fn mul(
    x: &[f64],
    xstride: usize,
    y: &[f64],
    ystride: usize,
    n: usize,
) -> (Value, f64, f64, f64) {
    let mut c1 = 0.;
    let mut cov11 = 0.;
    let mut sumsq = 0.;
    let ret = unsafe {
        ::sys::gsl_fit_mul(
            x.as_ptr(),
            xstride,
            y.as_ptr(),
            ystride,
            n,
            &mut c1,
            &mut cov11,
            &mut sumsq,
        )
    };
    (Value::from(ret), c1, cov11, sumsq)
}

/// Returns `(Value, c1, cov11, sumsq)`.
#[doc(alias = "gsl_fit_wmul")]
pub fn wmul(
    x: &[f64],
    xstride: usize,
    w: &[f64],
    wstride: usize,
    y: &[f64],
    ystride: usize,
    n: usize,
) -> (Value, f64, f64, f64) {
    let mut c1 = 0.;
    let mut cov11 = 0.;
    let mut sumsq = 0.;
    let ret = unsafe {
        ::sys::gsl_fit_wmul(
            x.as_ptr(),
            xstride,
            w.as_ptr(),
            wstride,
            y.as_ptr(),
            ystride,
            n,
            &mut c1,
            &mut cov11,
            &mut sumsq,
        )
    };
    (Value::from(ret), c1, cov11, sumsq)
}

/// This function uses the best-fit linear regression coefficient c1 and its covariance cov11 to
/// compute the fitted function y and its standard deviation y_err for the model Y = c_1 X at the
/// point x.
///
/// Returns `(Value, y, y_err)`.
#[doc(alias = "gsl_fit_mul_est")]
pub fn mul_est(x: f64, c1: f64, cov11: f64) -> (Value, f64, f64) {
    let mut y = 0.;
    let mut y_err = 0.;
    let ret = unsafe { ::sys::gsl_fit_mul_est(x, c1, cov11, &mut y, &mut y_err) };
    (Value::from(ret), y, y_err)
}