best fit straight line

best fit straight line

Time Limit: 1000 ms  Memory Limit: 65536 KiB

Problem Description

In many cases, the data obtained from astronomical observations is a set of images containing a large number of sequential point images, each of which is defined by an x -value and a y -value. This may require drawing a best-fit curve through these points.

To avoid analyzing only individual data, an optimal curve fit is required. Consider N data points whose coordinates are (X 1 ,Y 1 ) , (X 2 ,Y 2 )... , (X N ,Y N ) . It is assumed that X of these values ​​is strictly exact and the value of Y is measured ( with some error ) .
         

For a given X, such as X 1 , there will be a difference between the corresponding value Y 1 and the corresponding Y value on the curve C. We denote this difference by D 1 , and sometimes we call this difference the bias, error, or residual, which may be positive, negative, or zero. Similarly, X 2 ..., X N , the corresponding differences are D 2 , ...., D N .

 We use D 1 + D 2 + ... + D N 2  as a measure of how "best" the curve C fits. The smaller the value, the better, and the larger the better. Therefore, we make the following definition: any type of curve, they all have a common characteristic, when ΣD i 2 is the smallest, it is called the best fit curve. Note: ∑ refers to "sum" calculation. When a curve has this characteristic, it is called "least squares fitting", and such a curve is called "least squares curve".

The calculation task this time is to fit a straight line, which is mathematically called "linear regression". The term "regression" may seem a little foreign, since there is nothing to "regress" to calculate the optimal curve, the best term is "curve fit", in the case of a straight line, "linear curve fit".

Your task is to write a program that calculates the coefficients (slope a and y-intercept b) of the following linear equation using least squares:

 y = a*x + b (4.1)

 a and b can be calculated using the following formula:
 
where N is the number of data points. Note that the above two equations have the same denominator, and ∑ refers to an item-by-item addition calculation (summation). ∑x means summing all x values, ∑y means summing all y values, and ∑(x^2) means summing all squares of x. ∑xy refers to the sum calculation of all products xy. It should be noted that ∑xy and ∑x*∑y are not the same ("sum of products" and "product of sums" are different), and similarly (∑x)^2 and ∑(x^2) are not the same ("Sum of squares" is not the same as "Sum of squares").


Input

  n sets of integers represent x i , y i , in the period |x|<=10 6 , |y|<=10 6 , n < 15  

Output

  The parameters a and b of the best fitting curve , a and b each occupy one line, and b are accurate to 3 decimal places .

Sample Input

4
1  6
2  5
3  7
4  10

Sample Output

1.400
3.500
This question can be solved in order according to the formula given in the title. The information of the title is very large, but the final foothold is in the formula. Of course, this fitting method in mathematics is still worth learning.
 
  
//package hello;

import java.util. *;

public class Main {
	public static int sum(int[]... arr) {
		int len = arr.length;
		int arrlength = arr[0].length;
		int sum = 0;
		for (int i = 0; i < arrlength; i++) {
			if (len == 1) {
				sum += arr[0][i];
			} else if (len == 2) {
				sum += arr[0][i] * arr[1][i];
			}
		}
		return sum;
	}
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int n = cin.nextInt();
		int[] x = new int[n];
		int[] y = new int[n];
		for (int i = 0; i < n; i++) {
			x[i] = cin.nextInt();
			y[i] = cin.nextInt();
		}
		int result_x = sum(x);
		int result_y = sum(y);
		int result_xx = sum(x, x);
		int result_xy = sum(x, y);
		double a = (double) (n * result_xy - result_x * result_y) / (n * result_xx - result_x * result_x);
		double b = (double) (result_y * result_xx - result_x * result_xy) / (n * result_xx - result_x * result_x);
		System.out.println(String.format("%.3f", a));
		System.out.println(String.format("%.3f", b));
		cin.close();
	}
	
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325748732&siteId=291194637