当前位置:网站首页>Least square linear fitting and its code implementation (C language)
Least square linear fitting and its code implementation (C language)
2022-07-20 09:52:00 【Continuous progress】
List of articles
Preface
Least square method is the most commonly used data fitting method . This paper starts with the least square method Concept Starting with , The method of linear fitting is introduced Mechanism ; After through C Language Algorithm implementation ; Finally, it expounds the application of least square linear fitting Shortcomings and shortcomings .
One 、 Least square method
Least square method ( Also known as the least square method ) It's a mathematical optimization technique . It finds the best function matching of data by minimizing the square sum of errors .
With the simplest y y y About x x x The linear equation of : y = β 0 + β 1 x y=β_0+β_1x y=β0+β1x For example . Suppose the coordinates of some given points are :( x 1 , y 1 x_1 , y_1 x1,y1 ) , ( x 2 , y 2 x_2 , y_2 x2,y2) ⋯( x j , y j x_j , y_j xj,yj) ⋯( x n , y n x_n , y_n xn,yn); Define ordinates y y y Residual of R e s i d u a l Residual Residual Is the difference between the estimated value and the observed value , The formula is as follows :
R e s i d u a l = y i − ( β 0 + β 1 x ) Residual = y_i - (β_0+β_1x) Residual=yi−(β0+β1x);
β 0 = n Σ y i ‾ − n a Σ x i ‾ β_0 = ^{\underline{Σ{y_i}}}_{n}- ^{\underline{aΣ{x_i}}}_{n} β0=nΣyi−naΣxi
β 1 = n Σ x i 2 − ( Σ x i ) 2 n Σ x i y i − Σ x i Σ y i ‾ β_1 = ^{\underline{nΣ{x_iy_i}-Σ{x_i}Σ{y_i}}}_{nΣ{x_i}^2-(Σ{x_i})^2} β1=nΣxi2−(Σxi)2nΣxiyi−ΣxiΣyi
The purpose of the general least square method is to make the fitting error ( Residual sum ) Minimum , That is, the least square method is to find the parameters of a group of lines , Minimize the objective function .
Two 、 Code implementation (C Language )
The code is as follows :
/**************************************************************/
// Least square linear fitting (y = β0 + β1x)
// notes : This procedure only considers dependent variables y There are errors , The argument is given as X_sample
/**************************************************************/
float β1;
float β0;
const int16_t X_sample[100] =
{
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,
21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,
41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,
61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,
81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100};
// Sum of squares
float Square_sum(int16_t* data, uint16_t len)
{
uint16_t i;
float z = 0;
for (i = 0; i < len; i++)
{
z = z + (*data)*(*data);
data++;
}
return z;
}
// Vector product
float X_By_Y(int16_t* X, int16_t* Y, uint16_t len)
{
uint16_t i;
float z = 0;
for (i = 0; i < len; i++)
{
z = z + X[i] * Y[i];
}
return z;
}
// Least square linear fitting
float Line_fit(int16_t *data, uint16_t len)
{
float X_square = Square_sum((int16_t*)X_sample, len);
float X_multiply_Y = X_By_Y((int16_t*)X_sample, data, len);
float X_sum = SFLeaSum16s((int16_t*)X_sample, len);
float Y_sum = SFLeaSum16s(data, len);
β0= (X_square*Y_sum - X_sum*X_multiply_Y)/(len*X_square - X_sum*X_sum);
β1= (len*X_multiply_Y - Y_sum*X_sum)/(len*X_square - X_sum*X_sum);
return β1;
}
float Line_return(int16_t X)
{
return β0 + β1*X;
}
3、 ... and 、 Defects and deficiencies
1. Sensitive to outliers
Observe the result of its algebraic solution , The least square method only uses the mean and variance information of points , Therefore, it is only applicable to the case of ordinary noise , When there are outliers , The general least square method is very sensitive to outliers , May change the final result , It needs to be solved by other methods .
2. The error of independent variables is not considered
Only when there is no deviation in the independent variable , Or the deviation of the independent variable can be ignored within a certain range without timing , The least square method is more applicable . Because the general least square method only considers the dependent variable y y y There are errors , Independent variables are not considered x x x The error of the , Therefore, its application conditions have certain limitations .
3. There are unsolvable cases
When the line to be fitted is perpendicular or nearly perpendicular to x x x Axial time , The slope is infinite , The least square method is not applicable .
summary
1) Least square method is the most commonly used data fitting method , By minimizing the sum of squares of errors, the optimal function matching of the data is found ;
2) The least square method only uses the mean and variance information of points , Sensitive to outliers ;
3) The least square method only considers the dependent variable y y y There are errors , But when the horizontal and vertical coordinates of the point have errors , And can not be ignored , The general least square method is not applicable ;
4) The least square method cannot be solved ( The fitted line is perpendicular or nearly perpendicular to x x x Axis ).
The above is the whole content of this article , I hope this paper can help you understand and use the least square method for linear fitting .
Of course , If there is any mistake or imprecision in the content of this article , Please also point out in time , thank you !
Reference resources
Linear regression —— Least square fitting - David Xu 2014 - Blog Garden
Linear fitting 1- Least square method _ningzian The blog of -CSDN Blog
边栏推荐
- Connaissance de la technologie des tunnels d'infiltration Intranet
- 单电源运放和双电源运放及其供电方式选择与转换的注意事项
- The Debian system is ported with USBWiFi rtl8192eu driver and set to start automatically
- 【Markdown】关于Markdown我想说这些~
- [mindspore] [Lite end-to-side training reasoning] mindspore lit runs the lenet training example code according to the instructions and reports an error
- Hard core strength! Feiling Ti Sitara am62x series-335x classic continuation
- 【BOM】初识BOM~
- Depth evaluation of Ruixin micro rk3568 development board
- Qualcomm snpe
- 【复旦微】国产MCU学习(持续更新)
猜你喜欢
【LoRa&NB-IoT】现状分析
EIM总线如何测试可用性及稳定性
Construction of virtual host (multiple sites)
瑞芯微RK3568开发板深度评测
UltraEdit自动换行/制表符设置
关于浮点数的剪不断理还乱
Mindscore "implementing a picture classification application" runs incorrectly
【mindspore】【import erro】 undefined symbol _ Z14DlogErrorInneriPK
线性卷积、循环卷积、周期卷积的定义、计算方法及三者之间的关系
Interpretation of semi supervised semantic segmentation with error localization network
随机推荐
How to test the availability and stability of EIM bus
The end result of printf - where is the data printed
stm32f4 PWM捕获 (上升沿/下降沿/高低电平时间)详解(含代码)
vulnhub 靶机 GOLDENEYE: 1
Mysql45 talking about reading notes global lock and table lock (6)
Mid value overflow of binary search
SSH协议中隧道与代理的用法详解
1人天搞定9人天的日志接入开发——基于指令集物联网操作系统的项目开发实践
"Embedded intelligence" constantly empowers medical devices
TypeScript 之泛型
How to understand the freezing network parameters in the mindscore official website tutorial? Can you explain it?
【DOM】初识DOM
Xilinx FPGA key resource evaluation
This paper interprets DNA binding site prediction using a deep learning method
Automatic nucleic acid extraction instrument based on ARM core board
Vit structure
Hard core strength! Feiling Ti Sitara am62x series-335x classic continuation
Spin process
Vivado2018.2 version with PS side configuration (BD) when calling Modelsim simulation: (vlog-13006) could not find the package (sc_util_v1_0_3_pkg)
Verilog HDL语言总结(全)