当前位置:网站首页>[untitled] forwarding least square method
[untitled] forwarding least square method
2022-07-04 08:47:00 【Ruoshui】
Recently, a project of the company needs to be calculated TVDI(Temperature Vegetation Dryness Index , Temperature Vegetation Drought Index ) ,TVDI The calculation formula of is as follows ( The specific principle is self-contained ):
among , Is the surface temperature of any pixel ;
For a NDVI Corresponding minimum surface temperature , Corresponding to wet edge
;
For a NDVI Corresponding maximum surface temperature , Corresponding to the dry side
;a,b Wet edged fitting Equation coefficient ,c,d Is the fitting equation coefficient of the dry edge .
In the process of fitting dry and wet edges , It is necessary to use the least square method to effectively NDVI and Lst Data for linear fitting . therefore , This paper is used in work C++ The least square fitting line realized , The key is to understand the basic principle of least square fitting line , It's easy to implement . The specific least square principle will not be elaborated too much , There are a lot of introduction materials on the Internet , Here we only give the shape as Linear regression calculation of a,b Coefficient and r^2 The final calculation formula of , The relevant code is as follows :
[cpp] view plain copy
- /*************************************************************************
- The least square method is used to fit the straight line ,y = a*x + b; n Group data ; r- The correlation coefficient [-1,1],fabs(r)->1, explain x,y The linear relationship between them is good ,fabs(r)->0,x,y There is no linear relationship between , Fitting is meaningless
- a = (n*C - B*D) / (n*A - B*B)
- b = (A*D - B*C) / (n*A - B*B)
- r = E / F
- among :
- A = sum(Xi * Xi)
- B = sum(Xi)
- C = sum(Xi * Yi)
- D = sum(Yi)
- E = sum((Xi - Xmean)*(Yi - Ymean))
- F = sqrt(sum((Xi - Xmean)*(Xi - Xmean))) * sqrt(sum((Yi - Ymean)*(Yi - Ymean)))
- **************************************************************************/
- void LineFitLeastSquares(float *data_x, float *data_y, int data_n, vector<float> &vResult)
- {
- float A = 0.0;
- float B = 0.0;
- float C = 0.0;
- float D = 0.0;
- float E = 0.0;
- float F = 0.0;
- for (int i=0; i<data_n; i++)
- {
- A += data_x[i] * data_x[i];
- B += data_x[i];
- C += data_x[i] * data_y[i];
- D += data_y[i];
- }
- // Calculate slope a And intercept b
- float a, b, temp = 0;
- if( temp = (data_n*A - B*B) )// Judge whether the denominator is 0
- {
- a = (data_n*C - B*D) / temp;
- b = (A*D - B*C) / temp;
- }
- else
- {
- a = 1;
- b = 0;
- }
- // Calculate the correlation coefficient r
- float Xmean, Ymean;
- Xmean = B / data_n;
- Ymean = D / data_n;
- float tempSumXX = 0.0, tempSumYY = 0.0;
- for (int i=0; i<data_n; i++)
- {
- tempSumXX += (data_x[i] - Xmean) * (data_x[i] - Xmean);
- tempSumYY += (data_y[i] - Ymean) * (data_y[i] - Ymean);
- E += (data_x[i] - Xmean) * (data_y[i] - Ymean);
- }
- F = sqrt(tempSumXX) * sqrt(tempSumYY);
- float r;
- r = E / F;
- vResult.push_back(a);
- vResult.push_back(b);
- vResult.push_back(r*r);
- }
In order to verify the effectiveness of the algorithm , Give the following test data , The data source is the experimental data of a paper :
[cpp] view plain copy
- float pY[25] = { 10.98, 11.13, 12.51, 8.40, 9.27,
- 8.73, 6.36, 8.50, 7.82, 9.14,
- 8.24, 12.19, 11.88, 9.57, 10.94,
- 9.58, 10.09, 8.11, 6.83, 8.88,
- 7.68, 8.47, 8.86, 10.38, 11.08 };
- float pX[25] = { 35.3, 29.7, 30.8, 58.8, 61.4,
- 71.3, 74.4, 76.6, 70.7, 57.5,
- 46.4, 28.9, 28.1, 39.1, 46.8,
- 48.5, 59.3, 70.0, 70.0, 74.5,
- 72.1, 58.1, 44.6, 33.4, 28.6 };
The data is in Excel The fitting result of is , among
.
Reprinted address http://blog.csdn.net/pl20140910/article/details/51926886
In engineering practice , We often encounter similar problems :
We did n Experiments , Got a set of data
then , We want to know x and y The functional relationship between . So we describe it in XOY In rectangular coordinates , Get the following point cloud :

then , We found that ,x and y「 Probably 」 It's a linear relationship , Because we can roughly connect all the sample points with a straight line , Here's the picture :

therefore , We can 「 guess 」. The next question , Is to find out a and b Value .
This seems to be a very simple problem ,a and b Are two unknowns , We just need to find two sample points randomly , List the equations :
Two unknowns , Two equations , I can solve for that a and b Value .
However , It's wrong here , Or inaccurate . Why? ? because This functional relationship , It is our 「 guess 」 Of , It is not necessarily objective and correct ( Although it may be right ). So we can't solve such a simple and rough set of equations .
Then what shall I do? ? Since it is 「 guess 」 Of , Then there is an error . Then we will slightly revise this functional relationship to :
here , They are the first i The dependent variable of the experiment 、 The independent variables 、 error .
Since it is 「 guess 」, Then of course we hope to guess more accurately . How to measure accurately ? Nature and e It matters .
After the above formula is modified, you can get :
ad locum ,a and b Is the independent variable ,e Is the function value .
This is the easiest place to get confused , Why? a,b It's an independent variable , instead of x,y?
This is to mention 「 Curve fitting 」 The concept of . So-called 「 fitting 」 That is, we need to find a function , Come on 「 matching 」 The sample value we obtained in the experiment . Put it in the example above , We need to adjust a and b Value , To make this function and the data obtained in the experiment more 「 matching 」. therefore ,a and b It's just 「 Curve fitting 」 Arguments in the process .
Next , Continue the problem of how to make the error smaller .
「 Least square method 」 The ideological core of , Is to define a loss function :
obviously , If we adjust a and b, bring Q To achieve the minimum , that 「 Curve fitting 」 The error will also be minimal .
here ,Q yes a,b Function of . According to higher mathematics, only ,Q The minimum point of its derivative must be 0 The point of .
therefore , We make :
After solving the above equations, we get about a,b A system of binary quadratic equations , Therefore, we can solve a and b Value . This is the whole process of the least square method .
The last show :
(1) English name of least square method Least Squares, Actually translated into 「 The least square method 」, It's easier to understand . Its core is to define the loss function ;
(2) There is more than one method to evaluate errors , And things like etc. ( Of course, this is not the least square method );
(3) The least square method can not only be used for fitting primary functions , It can also be used for fitting higher-order functions ;
(4) The least square method is both a method of curve fitting , It can also be used for optimization .
边栏推荐
- How to play dapr without kubernetes?
- deno debugger
- swatch
- [BSP video tutorial] stm32h7 video tutorial phase 5: MDK topic, system introduction to MDK debugging, AC5, AC6 compilers, RTE development environment and the role of various configuration items (2022-
- User login function: simple but difficult
- awk从入门到入土(8)数组
- A single element in an ordered array
- 一文了解數據异常值檢測方法
- Codeforces Round #793 (Div. 2)(A-D)
- [untitled] 2022 polymerization process analysis and polymerization process simulation examination
猜你喜欢

MySQL relearn 1-centos install mysql5.7

Codeforces Round #750 (Div. 2)(A,B,C,D,F1)

How does Xiaobai buy a suitable notebook

awk从入门到入土(12)awk也可以写脚本,替代shell

Comparison between sentinel and hystrix

Azure ad domain service (II) configure azure file share disk sharing for machines in the domain service

How to re enable local connection when the network of laptop is disabled

ctfshow web255 web 256 web257

Newh3c - network address translation (NAT)

Codeforces Round #803 (Div. 2)(A-D)
随机推荐
DM8 database recovery based on point in time
1211 or chicken and rabbit in the same cage
Horizon sunrise X3 PI (I) first boot details
yolov5 xml数据集转换为VOC数据集
The upper layer route cannot Ping the lower layer route
NewH3C——ACL
@Role of pathvariable annotation
awk从入门到入土(14)awk输出重定向
Group programming ladder race - exercise set l1-006 continuity factor
Take you to master the formatter of visual studio code
What if I forget the router password
Internal learning
awk从入门到入土(8)数组
What sparks can applet container technology collide with IOT
snipaste 方便的截图软件,可以复制在屏幕上
Educational Codeforces Round 119 (Rated for Div. 2)
Sequence model
ES6 summary
力扣今日题-1200. 最小绝对差
一文了解数据异常值检测方法