当前位置:网站首页>[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 .
边栏推荐
- NewH3C——ACL
- 小程序容器技术与物联网 IoT 可以碰撞出什么样的火花
- Newh3c - network address translation (NAT)
- Awk from entry to earth (7) conditional statements
- DM8 database recovery based on point in time
- Need help resetting PHP counters - PHP
- Flutter 集成 amap_flutter_location
- SQL statement view SQL Server 2005 version number
- Comparison between sentinel and hystrix
- How college students choose suitable computers
猜你喜欢

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

Educational Codeforces Round 115 (Rated for Div. 2)

随机事件的关系与运算

High order phase difference such as smear caused by myopic surgery

Famous blackmail software stops operation and releases decryption keys. Most hospital IOT devices have security vulnerabilities | global network security hotspot on February 14

FOC control

Codeforces Round #793 (Div. 2)(A-D)

From scratch, use Jenkins to build and publish pipeline pipeline project

C#,数值计算(Numerical Recipes in C#),线性代数方程的求解,Gauss-Jordan消去法,源代码

DM8 tablespace backup and recovery
随机推荐
Educational Codeforces Round 119 (Rated for Div. 2)
SSRF vulnerability exploitation - attack redis
awk从入门到入土(14)awk输出重定向
C#实现一个万物皆可排序的队列
Codeforces Round #793 (Div. 2)(A-D)
Basic discipline formula and unit conversion
Guanghetong's high-performance 4g/5g wireless module solution comprehensively promotes an efficient and low-carbon smart grid
What sparks can applet container technology collide with IOT
随机事件的关系与运算
What if I forget the router password
How to send pictures to the server in the form of file stream through the upload control of antd
swatch
Leetcode topic [array] -136- numbers that appear only once
deno debugger
Four essential material websites for we media people to help you easily create popular models
Redis 哨兵机制
ES6 summary
DM8 tablespace backup and recovery
The upper layer route cannot Ping the lower layer route
Famous blackmail software stops operation and releases decryption keys. Most hospital IOT devices have security vulnerabilities | global network security hotspot on February 14