Least Square Regression Line | C # program to find the regression line ; Function to calculate b ; Sum of array x ; Sum of array y ; For sum of product of x and y ; Sum of square of x ; Function to find the least regression line ; Finding b ; Calculating a ; Printing regression line ; Driver code ; Statistical data
using System ; using System . Linq ; class GFG { private static double calculateB ( int [ ] x , int [ ] y ) { int n = x . Length ; int sx = x . Sum ( ) ; int sy = y . Sum ( ) ; int sxsy = 0 ; int sx2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sxsy += x [ i ] * y [ i ] ; sx2 += x [ i ] * x [ i ] ; } double b = ( double ) ( n * sxsy - sx * sy ) / ( n * sx2 - sx * sx ) ; return b ; } public static void leastRegLine ( int [ ] X , int [ ] Y ) { double b = calculateB ( X , Y ) ; int n = X . Length ; int meanX = X . Sum ( ) / n ; int meanY = Y . Sum ( ) / n ; double a = meanY - b * meanX ; Console . WriteLine ( " Regression ▁ line : " ) ; Console . Write ( " Y ▁ = ▁ " ) ; Console . Write ( " { 0 : F3 } " , a ) ; Console . Write ( " ▁ + ▁ " ) ; Console . Write ( " { 0 : F3 } " , b ) ; Console . Write ( " * X " ) ; } public static void Main ( String [ ] args ) { int [ ] X = { 95 , 85 , 80 , 70 , 60 } ; int [ ] Y = { 90 , 80 , 70 , 65 , 60 } ; leastRegLine ( X , Y ) ; } }