Algorithm Fine Solution: the least squares C implementation

Source: Internet
Author: User
Tags pow

the direct purpose of econometrics research is to determine the total regression function Yi=b1+b2xi+ui, however, only the observed values from a number of samples can be obtained, and the sample regression function established with sample information is as close as possible to "To estimate the total regression function. Therefore, it is possible to determine the criteria for establishing a regression function from different angles, and there are many methods to estimate the parameters of regression model.

The minimum two multiplication estimation method is used to determine the estimated values of B1 and B0 in the function y (x) = b1x + B0.

Y (x) is n points (x0,y0), ... Best Fit Line (Xn-1, Yn-1).

B1 = (n * Sigma (xi Yi)-Singma (xi) *singma (Yi))/(N*singma (POW (xi))-Pow ((Singma (xi)));

B0 = (Sigma (Yi)-B1 * Singma (Xi))/n;

After the values B0 and B1 are calculated, the corresponding values can be obtained by substituting y (x) = B1 + B0.

Next Write an example:

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #define NR (x) sizeof (x)/sizeof (x[0])//least squares implementation void LSQE (const double *x, const double *y, int n, double *b1, double *b0) {int  I;doub Le  sumx,sumy,sumx2,sumxy;sumx = 0.0;sumy = 0.0;SUMX2 = 0.0;sumxy = 0.0;//computes n times for (i = 0; i < n; i++) {  //will be horizontal The X value to be accumulated       sumx = sumx + x[i];      The Y value of the ordinate direction is accumulated     Sumy = Sumy + y[i];    SUMX2 = sumx2 + POW (x[i], 2.0);   Sumxy = Sumxy + (x[i] * y[i]);} Solve the values of B1 and B0 according to the formula *B1 = (Sumxy-((SUMX * sumy)/(double) n))/(Sumx2-(POW (sumx,2.0)/(double) n)); *b0 = (Sumy-((*B1) * sumx) )/(double) N;return;}  int main (void) {double x[] = {1.1, 1.2, 1.3, 1.4, 1.5, 1.6}; double y[] = {4.1, 4.2, 4.3, 4.4, 4.5, 4.6}; double B0, B1; Lsqe (X,y,nr (x), &b0,&b1);  printf ("%lf,%lf\n", b0,b1);    return 0;}
Operation Result:

1.000000, 3.00000

Algorithm Fine Solution: the least squares C implementation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.