Beispiel #1
0
void regression(point *points, size_t point_count, transform_t t,
        double *slope, double *intercept) {
    assert(slope);
    assert(intercept);

    double mx = 0;
    double my = 0;
    if (!calc_means(points, point_count, t, &mx, &my)) {
        *slope = EMPTY_VALUE;
        *intercept = EMPTY_VALUE;
        return;
    }

    double num = calc_num(points, point_count, t, mx, my);
    double den = calc_den(points, point_count, t, mx);
    *slope = num / den;
    *intercept = my - *slope * mx;

    LOG(1, "-- slope: %g, intercept: %g\n", *slope, *intercept);
}
Beispiel #2
0
/* Performs LSE of consequent parameters for all branches in the network
 *
 * PARAMETERS:	A ---> predictor variables matrix
 *		y ---> expected results for this sample set  (P in total)
 *
 * PRE: A != NULL
 *	y != NULL
 *
 * POS: result != NULL   &&   best fit consequent parameters vector returned
 *	or
 *	result == NULL
 */
static gsl_vector *
anfis_fit_linear (const gsl_matrix *A, const gsl_vector *y, size_t P, size_t M)
{
	gsl_matrix  *S =    NULL,	/* Covariance matrix */
		    *Snew = NULL,
		    *Saux = NULL;
	gsl_vector  *X = NULL,		/* Future best fit parameters */
		    *Xnew = NULL;
	unsigned int i = 0;
	double den = 0.0, factor = 0.0;
	
	assert (A != NULL);
	assert (y != NULL);
	
	/* Generating necessary workspace */
	S = gsl_matrix_alloc (M,M);
	if (S == NULL)
		goto exit_point;
	Snew = gsl_matrix_calloc (M,M);
	if (Snew == NULL)
		goto exit_point;
	Saux = gsl_matrix_calloc (M,M);
	if (Saux == NULL)
		goto exit_point;
	Xnew = gsl_vector_alloc (M);
	if (Xnew == NULL)
		goto exit_point;
	X = gsl_vector_calloc (M);
	if (X == NULL)
		goto exit_point;
	
	/* S = γ*Id , where γ is a large number */
	gsl_matrix_set_identity  (S);
	gsl_matrix_scale (S, _gamma);
	
	/* Performing Least Square Estimation */
	for (i=0 ; i < P ; i++) {
		/* Matrix A i-th row (row At_i+1 in Jang's paper) */
		gsl_vector_const_view A_i = gsl_matrix_const_row (A, i);
		
		/* Snew = S(i) * A_i+1 * At_i+1 * S(i) */
		calc_num (S, &(A_i.vector), Snew, Saux, Xnew, M);
		/* scale = At_i+1 * S(i) * A_i+1 */
		den = calc_den (S, &(A_i.vector), Xnew);
		/* Snew = Snew / (1+scale) */
		gsl_matrix_scale (Snew, 1.0 / (1.0+den));
		/* S(i+1) = S(i) - Snew */
		gsl_matrix_sub (S, Snew);
		
		/* factor = At_i+1 * X(i) */
		gsl_blas_ddot (&(A_i.vector), X, &factor);
		/* factor = yt_i+1 - factor */
		factor = gsl_vector_get (y, i) - factor;
		/* Xnew = S(i+1) * A_i+1 */
		gsl_blas_dgemv (CblasNoTrans, 1.0, S, &(A_i.vector), 0.0, Xnew);
		/* Xnew = Xnew * factor */
		gsl_vector_scale (Xnew, factor);
		/* X(i+1) = X(i) + Xnew */
		gsl_vector_add (X, Xnew);
	}
	
	exit_point:
		if (S != NULL)    { gsl_matrix_free (S);    S    = NULL;}
		if (Snew != NULL) { gsl_matrix_free (Snew); Snew = NULL;}
		if (Saux != NULL) { gsl_matrix_free (Saux); Saux = NULL;}
		if (Xnew != NULL) { gsl_vector_free (Xnew); Xnew = NULL;}
		
		return X;
}