the aspect of a formula

Hi there.
I have a program which calculates the parameters of liniar regression . The problem is that I do not know how to represent sigma in my formula.
I don't know off the top of my head which formula you speak of, but sigma represents summation (as I assume you know?). And summing over some range entails a loop.

For example, the sum of i from i = 1 to i = N would look like:
1
2
3
4
5
int sum = 0;
for (int i = 1; i <= N; i++)
{
    sum += i;
}


So to do a summation regardless of what's on the inside, you need to use a loop.
Tutorial: https://cplusplus.com/doc/tutorial/control/
Last edited on
the other side, if you are asking.. you can print a sigma; its in the 'standard' extended ascii table as unsigned char = 228 and certainly in unicode somewhere.

its a bit more than most people would have off the top of their head, and there are multiple approaches to finding the line beyond the simple one.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <valarray>
using namespace std;


double Sigma( const valarray<double> &V )
{
   return V.sum();
}


void regression( const valarray<double> X, const valarray<double> Y, double &m, double &c )
{
   int N = X.size();
   double Sx = Sigma( X ), Sy = Sigma( Y );
   m = ( N * Sigma( X * Y ) - Sx * Sy ) / ( N * Sigma( X * X ) - Sx * Sx );  // slope
   c = ( Sy - m * Sx ) / N;                                                  // intercept
}


int main()
{
   valarray<double> X = {  2,  4,  6,  8, 10 };
   valarray<double> Y = { 17, 27, 37, 47, 57 };
   double m, c;
   regression( X, Y, m, c );
   cout << "y = " << m << "x + " << c << '\n';
}


y = 5x + 7

Topic archived. No new replies allowed.