## GORegression

GORegression

## Synopsis

    GORegressionResult  (*GORegressionFunction)             (double *x,
                                                             double *params,
                                                             double *f);
    GORegressionResult  (*GORegressionFunctionl)            (long double *x,
                                                             long double *params,
                                                             long double *f);
    enum                GORegressionResult;
    #define             GORegressionStat
    #define             GORegressionStatl
    #define             GO_LOGFIT_C_ACCURACY
    #define             GO_LOGFIT_C_RANGE_FACTOR
    #define             GO_LOGFIT_C_STEP_FACTOR
    GORegressionResult  go_exponential_regression           (double **xss,
                                                             int dim,
                                                             const double *ys,
                                                             int n,
                                                             gboolean affine,
                                                             double *res,
                                                             go_regression_stat_t *stat_);
    GORegressionResult  go_exponential_regression_as_log    (double **xss,
                                                             int dim,
                                                             const double *ys,
                                                             int n,
                                                             gboolean affine,
                                                             double *res,
                                                             go_regression_stat_t *stat_);
    GORegressionResult  go_exponential_regression_as_logl   (long double **xss,
                                                             int dim,
                                                             const long double *ys,
                                                             int n,
                                                             gboolean affine,
                                                             long double *res,
                                                             go_regression_stat_tl *stat_);
    GORegressionResult  go_exponential_regressionl          (long double **xss,
                                                             int dim,
                                                             const long double *ys,
                                                             int n,
                                                             gboolean affine,
                                                             long double *res,
                                                             go_regression_stat_tl *stat_);
    GORegressionResult  go_linear_regression                (double **xss,
                                                             int dim,
                                                             const double *ys,
                                                             int n,
                                                             gboolean affine,
                                                             double *res,
                                                             go_regression_stat_t *stat_);
    GORegressionResult  go_linear_regressionl               (long double **xss,
                                                             int dim,
                                                             const long double *ys,
                                                             int n,
                                                             gboolean affine,
                                                             long double *res,
                                                             go_regression_stat_tl *stat_);
    GORegressionResult  go_linear_solve                     (double *const *const A,
                                                             const double *b,
                                                             int n,
                                                             double *res);
    GORegressionResult  go_linear_solvel                    (long double *const *const A,
                                                             const long double *b,
                                                             int n,
                                                             long double *res);
    GORegressionResult  go_logarithmic_fit                  (double *xs,
                                                             const double *ys,
                                                             int n,
                                                             double *res);
    GORegressionResult  go_logarithmic_fitl                 (long double *xs,
                                                             const long double *ys,
                                                             int n,
                                                             long double *res);
    GORegressionResult  go_logarithmic_regression           (double **xss,
                                                             int dim,
                                                             const double *ys,
                                                             int n,
                                                             gboolean affine,
                                                             double *res,
                                                             go_regression_stat_t *stat_);
    GORegressionResult  go_logarithmic_regressionl          (long double **xss,
                                                             int dim,
                                                             const long double *ys,
                                                             int n,
                                                             gboolean affine,
                                                             long double *res,
                                                             go_regression_stat_tl *stat_);
    double              go_matrix_determinant               (double *const *const A,
                                                             int n);
    long double         go_matrix_determinantl              (long double *const * const A,
                                                             int n);
    gboolean            go_matrix_invert                    (double **A,
                                                             int n);
    gboolean            go_matrix_invertl                   (long double **A,
                                                             int n);
    GORegressionResult  go_non_linear_regression            (GORegressionFunction f,
                                                             double **xvals,
                                                             double *par,
                                                             double *yvals,
                                                             double *sigmas,
                                                             int x_dim,
                                                             int p_dim,
                                                             double *chi,
                                                             double *errors);
    GORegressionResult  go_non_linear_regressionl           (GORegressionFunctionl f,
                                                             long double **xvals,
                                                             long double *par,
                                                             long double *yvals,
                                                             long double *sigmas,
                                                             int x_dim,
                                                             int p_dim,
                                                             long double *chi,
                                                             long double *errors);
    GORegressionResult  go_power_regression                 (double **xss,
                                                             int dim,
                                                             const double *ys,
                                                             int n,
                                                             gboolean affine,
                                                             double *res,
                                                             go_regression_stat_t *stat_);
    GORegressionResult  go_power_regressionl                (long double **xss,
                                                             int dim,
                                                             const long double *ys,
                                                             int n,
                                                             gboolean affine,
                                                             long double *res,
                                                             go_regression_stat_tl *stat_);
    void                go_regression_stat_destroy          (go_regression_stat_t *stat_);
    void                go_regression_stat_destroyl         (go_regression_stat_tl *stat_);
    go_regression_stat_t * go_regression_stat_new           (void);
    go_regression_stat_tl * go_regression_stat_newl         (void);
                        go_regression_stat_t;
                        go_regression_stat_tl;

## Object Hierarchy

      GBoxed
       +----go_regression_stat_t

      GBoxed
       +----go_regression_stat_tl

## Description

## Details

### GORegressionFunction ()

    GORegressionResult  (*GORegressionFunction)             (double *x,
                                                             double *params,
                                                             double *f);

### GORegressionFunctionl ()

    GORegressionResult  (*GORegressionFunctionl)            (long double *x,
                                                             long double *params,
                                                             long double *f);

### enum GORegressionResult

    typedef enum {
    	GO_REG_ok,
    	GO_REG_invalid_dimensions,
    	GO_REG_invalid_data,
    	GO_REG_not_enough_data,
    	GO_REG_near_singular_good, /* Probably good result */
    	GO_REG_near_singular_bad, 	/* Probably bad result */
    	GO_REG_singular
    } GORegressionResult;

`GO_REG_ok` | success.
`GO_REG_invalid_dimensions` | invalid dimensions.
`GO_REG_invalid_data` | invalid data:
`GO_REG_not_enough_data` | not enough data.
`GO_REG_near_singular_good` | probably good result.
`GO_REG_near_singular_bad` | probably bad result.
`GO_REG_singular` | singularity found.

### GORegressionStat

    #define GORegressionStat go_regression_stat_t

### GORegressionStatl

    #define GORegressionStatl go_regression_stat_tl

### GO_LOGFIT_C_ACCURACY

    #define GO_LOGFIT_C_ACCURACY 0.000001

### GO_LOGFIT_C_RANGE_FACTOR

    #define GO_LOGFIT_C_RANGE_FACTOR 100

### GO_LOGFIT_C_STEP_FACTOR

    #define GO_LOGFIT_C_STEP_FACTOR 0.05

### go_exponential_regression ()

    GORegressionResult  go_exponential_regression           (double **xss,
                                                             int dim,
                                                             const double *ys,
                                                             int n,
                                                             gboolean affine,
                                                             double *res,
                                                             go_regression_stat_t *stat_);

Performs one-dimensional linear regressions on the input points. Fits to "y = b * m1^x1 * ... * md^xd " or equivalently to "log y = log b + x1 * log m1 + ... + xd * log md".

`xss` : | x-vectors (i.e. independent data)
`dim` : | number of x-vectors
`ys` : | y-vector (dependent data)
`n` : | number of data points
`affine` : | if `TRUE`, a non-one multiplier is allowed
`res` : | output place for constant[0] and root1[1], root2[2],... There will be dim+1 results.
`stat_` : | non-NULL storage for additional results.
Returns : | GORegressionResult as above.

### go_exponential_regression_as_log ()

    GORegressionResult  go_exponential_regression_as_log    (double **xss,
                                                             int dim,
                                                             const double *ys,
                                                             int n,
                                                             gboolean affine,
                                                             double *res,
                                                             go_regression_stat_t *stat_);

Performs one-dimensional linear regressions on the input points as go_exponential_regression, but returns the logarithm of the coefficients instead or the coefficients themselves. Fits to "y = b * exp (m1*x1) * ... * exp (md*xd) " or equivalently to "ln y = ln b + x1 * m1 + ... + xd * md".

`xss` : | x-vectors (i.e. independent data)
`dim` : | number of x-vectors
`ys` : | y-vector (dependent data)
`n` : | number of data points
`affine` : | if `TRUE`, a non-one multiplier is allowed
`res` : | output place for constant[0] and root1[1], root2[2],... There will be dim+1 results.
`stat_` : | non-NULL storage for additional results.
Returns : | GORegressionResult as above.

### go_exponential_regression_as_logl ()

    GORegressionResult  go_exponential_regression_as_logl   (long double **xss,
                                                             int dim,
                                                             const long double *ys,
                                                             int n,
                                                             gboolean affine,
                                                             long double *res,
                                                             go_regression_stat_tl *stat_);

### go_exponential_regressionl ()

    GORegressionResult  go_exponential_regressionl          (long double **xss,
                                                             int dim,
                                                             const long double *ys,
                                                             int n,
                                                             gboolean affine,
                                                             long double *res,
                                                             go_regression_stat_tl *stat_);

### go_linear_regression ()

    GORegressionResult  go_linear_regression                (double **xss,
                                                             int dim,
                                                             const double *ys,
                                                             int n,
                                                             gboolean affine,
                                                             double *res,
                                                             go_regression_stat_t *stat_);

Performs multi-dimensional linear regressions on the input points. Fits to "y = b + a1 * x1 + ... ad * xd".

`xss` : | x-vectors (i.e. independent data)
`dim` : | number of x-vectors.
`ys` : | y-vector. (Dependent data.)
`n` : | number of data points.
`affine` : | if true, a non-zero constant is allowed.
`res` : | output place for constant[0] and slope1[1], slope2[2],... There will be dim+1 results.
`stat_` : | non-NULL storage for additional results.
Returns : | GORegressionResult as above.

### go_linear_regressionl ()

    GORegressionResult  go_linear_regressionl               (long double **xss,
                                                             int dim,
                                                             const long double *ys,
                                                             int n,
                                                             gboolean affine,
                                                             long double *res,
                                                             go_regression_stat_tl *stat_);

### go_linear_solve ()

    GORegressionResult  go_linear_solve                     (double *const *const A,
                                                             const double *b,
                                                             int n,
                                                             double *res);

### go_linear_solvel ()

    GORegressionResult  go_linear_solvel                    (long double *const *const A,
                                                             const long double *b,
                                                             int n,
                                                             long double *res);

### go_logarithmic_fit ()

    GORegressionResult  go_logarithmic_fit                  (double *xs,
                                                             const double *ys,
                                                             int n,
                                                             double *res);

Performs a two-dimensional non-linear fitting on the input points. Fits to "y = a + b * ln (sign * (x - c))", with sign in {-1, +1}. The graph is a logarithmic curve moved horizontally by c and possibly mirrored across the y-axis (if sign = -1).

Fits c (and sign) by iterative trials, but seems to be fast enough even for automatic recomputation.

Adapts c until a local minimum of squared residuals is reached. For each new c tried out the corresponding a and b are calculated by linear regression. If no local minimum is found, an error is returned. If there is more than one local minimum, the one found is not necessarily the smallest (i.e., there might be cases in which the returned fit is not the best possible). If the shape of the point cloud is to different from ``logarithmic'', either sign can not be determined (error returned) or no local minimum will be found.

(Requires: at least 3 different x values, at least 3 different y values.)

`xs` : | x-vector (i.e. independent data)
`ys` : | y-vector (dependent data)
`n` : | number of data points
`res` : | output place for sign[0], a[1], b[2], c[3], and sum of squared residuals[4].
Returns : | GORegressionResult as above.

### go_logarithmic_fitl ()

    GORegressionResult  go_logarithmic_fitl                 (long double *xs,
                                                             const long double *ys,
                                                             int n,
                                                             long double *res);

### go_logarithmic_regression ()

    GORegressionResult  go_logarithmic_regression           (double **xss,
                                                             int dim,
                                                             const double *ys,
                                                             int n,
                                                             gboolean affine,
                                                             double *res,
                                                             go_regression_stat_t *stat_);

This is almost a copy of linear_regression and produces multi-dimensional linear regressions on the input points after transforming xss to ln(xss). Fits to "y = b + a1 * z1 + ... ad * zd" with "zi = ln (xi)". Problems with arrays in the calling function: see comment to gnumeric_linest, which is also valid for gnumeric_logreg.

(Errors: less than two points, all points on a vertical line, non-positive x data.)

`xss` : | x-vectors (i.e. independent data)
`dim` : | number of x-vectors
`ys` : | y-vector (dependent data)
`n` : | number of data points
`affine` : | if `TRUE`, a non-zero constant is allowed
`res` : | output place for constant[0] and factor1[1], factor2[2],... There will be dim+1 results.
`stat_` : | non-NULL storage for additional results.
Returns : | GORegressionResult as above.

### go_logarithmic_regressionl ()

    GORegressionResult  go_logarithmic_regressionl          (long double **xss,
                                                             int dim,
                                                             const long double *ys,
                                                             int n,
                                                             gboolean affine,
                                                             long double *res,
                                                             go_regression_stat_tl *stat_);

### go_matrix_determinant ()

    double              go_matrix_determinant               (double *const *const A,
                                                             int n);

### go_matrix_determinantl ()

    long double         go_matrix_determinantl              (long double *const * const A,
                                                             int n);

### go_matrix_invert ()

    gboolean            go_matrix_invert                    (double **A,
                                                             int n);

### go_matrix_invertl ()

    gboolean            go_matrix_invertl                   (long double **A,
                                                             int n);

### go_non_linear_regression ()

    GORegressionResult  go_non_linear_regression            (GORegressionFunction f,
                                                             double **xvals,
                                                             double *par,
                                                             double *yvals,
                                                             double *sigmas,
                                                             int x_dim,
                                                             int p_dim,
                                                             double *chi,
                                                             double *errors);

SYNOPSIS: result = non_linear_regression (f, xvals, par, yvals, sigmas, x_dim, p_dim, &chi, errors) Non linear regression.

`f` : | the model function. [scope call]
`xvals` : | independent values.
`par` : | model parameters.
`yvals` : | dependent values.
`sigmas` : | stahdard deviations for the dependent values.
`x_dim` : | Number of data points.
`p_dim` : | Number of parameters.
`chi` : | Chi Squared of the final result. This value is not very meaningful without the sigmas.
`errors` : | MUST ALREADY BE ALLOCATED. These are the approximated standard deviation for each parameter.
Returns : | the results of the non-linear regression from the given initial values. The resulting parameters are placed back into `par`.

### go_non_linear_regressionl ()

    GORegressionResult  go_non_linear_regressionl           (GORegressionFunctionl f,
                                                             long double **xvals,
                                                             long double *par,
                                                             long double *yvals,
                                                             long double *sigmas,
                                                             int x_dim,
                                                             int p_dim,
                                                             long double *chi,
                                                             long double *errors);

SYNOPSIS: result = non_linear_regression (f, xvals, par, yvals, sigmas, x_dim, p_dim, &chi, errors) Non linear regression.

`f` : | the model function. [scope call]
`xvals` : | independent values.
`par` : | model parameters.
`yvals` : | dependent values.
`sigmas` : | stahdard deviations for the dependent values.
`x_dim` : | Number of data points.
`p_dim` : | Number of parameters.
`chi` : | Chi Squared of the final result. This value is not very meaningful without the sigmas.
`errors` : | MUST ALREADY BE ALLOCATED. These are the approximated standard deviation for each parameter.
Returns : | the results of the non-linear regression from the given initial values. The resulting parameters are placed back into `par`.

### go_power_regression ()

    GORegressionResult  go_power_regression                 (double **xss,
                                                             int dim,
                                                             const double *ys,
                                                             int n,
                                                             gboolean affine,
                                                             double *res,
                                                             go_regression_stat_t *stat_);

Performs one-dimensional linear regressions on the input points. Fits to "y = b * x1^m1 * ... * xd^md " or equivalently to "log y = log b + m1 * log x1 + ... + md * log xd".

`xss` : | x-vectors (i.e. independent data)
`dim` : | number of x-vectors
`ys` : | y-vector (dependent data)
`n` : | number of data points
`affine` : | if `TRUE`, a non-one multiplier is allowed
`res` : | output place for constant[0] and root1[1], root2[2],... There will be dim+1 results.
`stat_` : | non-NULL storage for additional results.
Returns : | GORegressionResult as above.

### go_power_regressionl ()

    GORegressionResult  go_power_regressionl                (long double **xss,
                                                             int dim,
                                                             const long double *ys,
                                                             int n,
                                                             gboolean affine,
                                                             long double *res,
                                                             go_regression_stat_tl *stat_);

### go_regression_stat_destroy ()

    void                go_regression_stat_destroy          (go_regression_stat_t *stat_);

### go_regression_stat_destroyl ()

    void                go_regression_stat_destroyl         (go_regression_stat_tl *stat_);

### go_regression_stat_new ()

    go_regression_stat_t * go_regression_stat_new           (void);

### go_regression_stat_newl ()

    go_regression_stat_tl * go_regression_stat_newl         (void);

### go_regression_stat_t

    typedef struct {
            double *se;		/* SE for each parameter estimator */
            double *t;  		/* t values for each parameter estimator */
            double sqr_r;
    	double adj_sqr_r;
            double se_y; 		/* The Standard Error of Y */
            double F;
            int    df_reg;
            int    df_resid;
            int    df_total;
            double ss_reg;
            double ss_resid;
            double ss_total;
            double ms_reg;
            double ms_resid;
    	double ybar;
    	double *xbar;
    	double var; 		/* The variance of the entire regression: sum(errors^2)/(n-xdim) */
    } go_regression_stat_t;

double *`se`; | SE for each parameter estimator.
double *`t`; | t values for each parameter estimator.
double `sqr_r`; | squared R.
double `adj_sqr_r`; |
double `se_y`; | the Standard Error of Y.
double `F`; |
int `df_reg`; |
int `df_resid`; |
int `df_total`; |
double `ss_reg`; |
double `ss_resid`; |
double `ss_total`; |
double `ms_reg`; |
double `ms_resid`; |
double `ybar`; |
double *`xbar`; |
double `var`; | the variance of the entire regression: sum(errors^2)/(n-xdim).

### go_regression_stat_tl

    typedef struct {
            long double *se; /*SE for each parameter estimator*/
            long double *t;  /*t values for each parameter estimator*/
            long double sqr_r;
    	long double adj_sqr_r;
            long double se_y; /* The Standard Error of Y */
            long double F;
            int        df_reg;
            int        df_resid;
            int        df_total;
            long double ss_reg;
            long double ss_resid;
            long double ss_total;
            long double ms_reg;
            long double ms_resid;
    	long double ybar;
    	long double *xbar;
    	long double var; /* The variance of the entire regression:
    			sum(errors^2)/(n-xdim) */
    } go_regression_stat_tl;

long double *`se`; | SE for each parameter estimator.
long double *`t`; | t values for each parameter estimator.
long double `sqr_r`; | squared R.
long double `adj_sqr_r`; |
long double `se_y`; | the Standard Error of Y.
long double `F`; |
int `df_reg`; |
int `df_resid`; |
int `df_total`; |
long double `ss_reg`; |
long double `ss_resid`; |
long double `ss_total`; |
long double `ms_reg`; |
long double `ms_resid`; |
long double `ybar`; |
long double *`xbar`; |
long double `var`; | the variance of the entire regression: sum(errors^2)/(n-xdim).