0% found this document useful (0 votes)
675 views2 pages

Pseudocode For Linear Regression

The pseudocode describes the linear regression process to fit a line to data points in 7 steps: 1) Start 2) Read number of data points and values for x and y 3) Initialize sums 4) Calculate required sums of x, x^2, y, and xy 5) Calculate coefficients a and b using formulae 6) Display values of a and b 7) Stop.

Uploaded by

Jay Lumongsod
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
675 views2 pages

Pseudocode For Linear Regression

The pseudocode describes the linear regression process to fit a line to data points in 7 steps: 1) Start 2) Read number of data points and values for x and y 3) Initialize sums 4) Calculate required sums of x, x^2, y, and xy 5) Calculate coefficients a and b using formulae 6) Display values of a and b 7) Stop.

Uploaded by

Jay Lumongsod
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Pseudocode for Linear Regression

1. Start

2. Read Number of Data (n)

3. For i=1 to n:
Read Xi and Yi
Next i

4. Initialize:
sumX = 0
sumX2 = 0
sumY = 0
sumXY = 0

5. Calculate Required Sum


For i=1 to n:
sumX = sumX + Xi
sumX2 = sumX2 + Xi * Xi
sumY = sumY + Yi
sumXY = sumXY + Xi * Yi
Next i

6. Calculate Required Constant a and b of y = a + bx:


b = (n * sumXY - sumX * sumY)/(n*sumX2 - sumX * sumX)
a = (sumY - b*sumX)/n

7. Display value of a and b

8. Stop
#include<iostream>

#define S 50

using namespace std;


int main()
{
int n, i;
float x[S], y[S], sumX=0, sumX2=0, sumY=0, sumXY=0, a, b;

/* Input */
cout<<"How many data points? ";
cin>>n;

cout<<"Enter data:"<< endl;

for(i=1;i<=n;i++)
{
cout<<"x["<< i <<"] = ";
cin>>x[i];
cout<<"y["<< i <<"] = ";
cin>>y[i];
}

/* Calculating Required Sum */


for(i=1;i<=n;i++)
{
sumX = sumX + x[i];
sumX2 = sumX2 + x[i]*x[i];
sumY = sumY + y[i];
sumXY = sumXY + x[i]*y[i];
}
/* Calculating a and b */
b = (n*sumXY-sumX*sumY)/(n*sumX2-sumX*sumX);
a = (sumY - b*sumX)/n;

/* Displaying value of a and b */


cout<<"Calculated value of a is "<< a << "and b is "<< b << endl;
cout<<"Equation of best fit is: y = "<< a <<" + "<< b<<"x";

return(0);
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy