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