Module: Numo::GSL::Fit

Defined in:
ext/numo/gsl/fit/gsl_fit.c

Class Method Summary collapse

Class Method Details

.linear(x, y) ⇒ GSL::Fit::LinearResult

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 (Correlation), it does not depend on the fit.

Parameters:

  • x (DFloat)

    (input array)

  • y (DFloat)

    (input array)

Returns:

  • (GSL::Fit::LinearResult)

    result Struct with members: c0, c1, cov00, cov01, cov11, sumsq.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'ext/numo/gsl/fit/gsl_fit.c', line 73

static VALUE
fit_s_linear(VALUE mod, VALUE v1, VALUE v2)
{
    VALUE r, result;
    ndfunc_arg_in_t ain[2] = {{cDF,1},{cDF,1}};
    ndfunc_arg_out_t aout[6] = {{cDF,0},{cDF,0},{cDF,0},{cDF,0},{cDF,0},{cDF,0}};
    ndfunc_t ndf = { iter_fit_s_linear, NO_LOOP|NDF_EXTRACT,
                     2, 6, ain, aout };
    narray_t *x, *y;

    GetNArray(v1,x);
    GetNArray(v2,y);
    CHECK_GE_1D(x);
    CHECK_GE_1D(y);
    CHECK_SIZE_EQ(VEC_SIZE(x),VEC_SIZE(y),"x size does not match y size");

    r = na_ndloop(&ndf, 2, v1, v2);
    result = rb_class_new_instance(6, RARRAY_PTR(r), cLinearResult);
    RB_GC_GUARD(r);
    return result;
}

.linear_est(x, linear_result) ⇒ [DFloat,DFloat]

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.

Parameters:

  • x (DFloat)
  • linear_result ((L|Wl)inearResult)
    Result of GSL::Fit.linear wlinear

Returns:

  • ([DFloat,DFloat])

    array of (y, y_err).



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'ext/numo/gsl/fit/gsl_fit.c', line 215

static VALUE
fit_s_linear_est(VALUE mod, VALUE v1, VALUE v2)
{
    ndfunc_arg_in_t ain[6] = {{cDF,0},{cDF,0},{cDF,0},{cDF,0},{cDF,0},{cDF,0}};
    ndfunc_arg_out_t aout[2] = {{cDF,0},{cDF,0}};
    ndfunc_t ndf = { iter_fit_s_linear_est, STRIDE_LOOP_NIP|NDF_EXTRACT,
                     6, 2, ain, aout };
    VALUE c0, c1, cov00, cov01, cov11;

    c0 = RSTRUCT_GET(v2,0);
    c1 = RSTRUCT_GET(v2,1);
    cov00 = RSTRUCT_GET(v2,2);
    cov01 = RSTRUCT_GET(v2,3);
    cov11 = RSTRUCT_GET(v2,4);
    return na_ndloop(&ndf, 6, v1, c0, c1, cov00, cov01, cov11);
}

.mul(x, y) ⇒ GSL::Fit::MulResult

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.

Parameters:

  • x (DFloat)

    (input array)

  • y (DFloat)

    (input array)

Returns:

  • (GSL::Fit::MulResult)

    result Struct with members: c1, cov11, sumsq.



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'ext/numo/gsl/fit/gsl_fit.c', line 271

static VALUE
fit_s_mul(VALUE mod, VALUE v1, VALUE v2)
{
    VALUE r, result;
    ndfunc_arg_in_t ain[2] = {{cDF,1},{cDF,1}};
    ndfunc_arg_out_t aout[3] = {{cDF,0},{cDF,0},{cDF,0}};
    ndfunc_t ndf = { iter_fit_s_mul, STRIDE_LOOP_NIP|NDF_EXTRACT,
                     2, 3, ain, aout };
    narray_t *x,*y;

    GetNArray(v1,x);
    GetNArray(v2,y);
    CHECK_GE_1D(x);
    CHECK_GE_1D(y);
    CHECK_SIZE_EQ(VEC_SIZE(x),VEC_SIZE(y),"x size does not match y size");

    r = na_ndloop(&ndf, 2, v1, v2);
    result = rb_class_new_instance(3, RARRAY_PTR(r), cMulResult);
    RB_GC_GUARD(r);
    return result;
}

.mul_est(x, mul_result) ⇒ [DFloat,DFloat]

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.

Parameters:

  • x (DFloat)
  • mul_result ((M|Wm)ulResult)
    Result of GSL::Fit.mul wmul

Returns:

  • ([DFloat,DFloat])

    array of [y,y_err]



402
403
404
405
406
407
408
409
410
411
412
413
414
# File 'ext/numo/gsl/fit/gsl_fit.c', line 402

static VALUE
fit_s_mul_est(VALUE mod, VALUE v1, VALUE v2)
{
    ndfunc_arg_in_t ain[3] = {{cDF,0},{cDF,0},{cDF,0}};
    ndfunc_arg_out_t aout[2] = {{cDF,0},{cDF,0}};
    ndfunc_t ndf = { iter_fit_s_mul_est, STRIDE_LOOP_NIP|NDF_EXTRACT,
                     3, 2, ain, aout };
    VALUE c1, cov11;

    c1 = RSTRUCT_GET(v2,0);
    cov11 = RSTRUCT_GET(v2,1);
    return na_ndloop(&ndf, 3, v1, c1, cov11);
}

.wlinear(x, w, y) ⇒ GSL::Fit::WlinearResult

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.

Parameters:

  • x (DFloat)

    (input array)

  • w (DFloat)

    (input array)

  • y (DFloat)

    (input array)

Returns:

  • (GSL::Fit::WlinearResult)

    result Struct with members: c0, c1, cov00, cov01, cov11, chisq.



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'ext/numo/gsl/fit/gsl_fit.c', line 144

static VALUE
fit_s_wlinear(VALUE mod, VALUE v1, VALUE v2, VALUE v3)
{
    ndfunc_arg_in_t ain[3] = {{cDF,1},{cDF,1},{cDF,1}};
    ndfunc_arg_out_t aout[6] = {{cDF,0},{cDF,0},{cDF,0},{cDF,0},{cDF,0},{cDF,0}};
    ndfunc_t ndf = { iter_fit_s_wlinear, STRIDE_LOOP_NIP|NDF_EXTRACT,
                     3, 6, ain, aout };
    narray_t *x, *y, *w;
    VALUE r, result;

    GetNArray(v1,x);
    GetNArray(v2,y);
    GetNArray(v3,w);
    CHECK_GE_1D(x);
    CHECK_GE_1D(y);
    CHECK_GE_1D(w);
    CHECK_SIZE_EQ(VEC_SIZE(x),VEC_SIZE(y),"y size does not match x size");
    CHECK_SIZE_EQ(VEC_SIZE(x),VEC_SIZE(w),"w size does not match x size");

    r = na_ndloop(&ndf, 3, v1, v2, v3);
    result = rb_class_new_instance(6, RARRAY_PTR(r), cWlinearResult);
    RB_GC_GUARD(r);
    return result;
}

.wmul(x, w, y) ⇒ GSL::Fit::WmulResult

This function computes the best-fit linear regression coefficient c1 of the model Y = c_1 X for the weighted datasets (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 variance of the parameter c1 is computed using the weights and returned via the parameter cov11. The weighted sum of squares of the residuals from the best-fit line, \chi^2, is returned in chisq.

Parameters:

  • x (DFloat)

    (input array)

  • w (DFloat)

    (input array)

  • y (DFloat)

    (input array)

Returns:

  • (GSL::Fit::WmulResult)

    result Struct with members: c1, cov11, sumsq.



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'ext/numo/gsl/fit/gsl_fit.c', line 338

static VALUE
fit_s_wmul(VALUE mod, VALUE v1, VALUE v2, VALUE v3)
{
    VALUE r, result;
    ndfunc_arg_in_t ain[3] = {{cDF,1},{cDF,1},{cDF,1}};
    ndfunc_arg_out_t aout[3] = {{cDF,0},{cDF,0},{cDF,0}};
    ndfunc_t ndf = { iter_fit_s_wmul, STRIDE_LOOP_NIP|NDF_EXTRACT,
                     3, 3, ain, aout };
    narray_t *x,*y,*w;

    GetNArray(v1,x);
    GetNArray(v2,y);
    GetNArray(v3,w);
    CHECK_GE_1D(x);
    CHECK_GE_1D(y);
    CHECK_GE_1D(w);
    CHECK_SIZE_EQ(VEC_SIZE(x),VEC_SIZE(y),"y size does not match x size");
    CHECK_SIZE_EQ(VEC_SIZE(x),VEC_SIZE(w),"w size does not match x size");

    r = na_ndloop(&ndf, 3, v1, v2, v3);
    result = rb_class_new_instance(3, RARRAY_PTR(r), cWmulResult);
    RB_GC_GUARD(r);
    return result;
}