How to get Balance for 8.5?

Hello, I currently have a program to give me a line for the best fit of the y(Balance) graph. But I am also trying to find how much the Balance is on week 8.5. Below is my code along with the data table for the weeks and their balances.
Week Balance
1 490
2 720
3 970
4 1265
5 1210
6 1521
7 1165
8 1365
9 1850
10 2050
11 1350
12 1550
13 2350
14 2450
15 2650
16 1550
17 1665
18 1800
19 2465
20 2165
21 2900
22 3600

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* This program computes computes a linear model for a list of weekly balances. */
#include <iostream>		//Required for cin, cout
#include <fstream>		//Required for ifstream
#include <string>		//Required for string
using namespace std;
int
main ()
{				// Declare and initialize objects.
  int count (0), k;
  double Week, Balance, first, last, sumWeek (0), sumBalance (0),
    sumWeek2 (0), sumWeekBalance (0), denominator, m, b;
  string filename;
  ifstream TimeBal;		// read a file and use for getting data in
  ofstream report;		// create and write to a file
// first we will create a data file

  cout << "Enter the name of the data file you want";	// Prompt user for name of input file.
  cin >> filename;

  report.open (filename.c_str ());
  cout << " enter the week along with its corresponding balance." << endl;
  cin >> Week >> Balance;	// get the values from keypad
  report << Week << " " << Balance << "  " << endl;	// send values to data file

  for (k = 1; k <= 21; k = k + 1)
    {				//How many times we want it to ask
      cout << " enter the week along with its corresponding balance." << endl;
      cin >> Week >> Balance;
      report << Week << " " << Balance << "  " << endl;
    }
  cout << "Enter the name of the data file you want to analyse";	// Prompt user for name of input file.
  cin >> filename;
  report.close ();		// close created data file

  TimeBal.open (filename.c_str ());	// Open file and check if it exists.
  if (TimeBal.fail ())
    {
      cerr << "Error opening input file\n";
      exit (1);
    }
  report.open ("MonaesReport");	//Open Report File
  TimeBal >> Week >> Balance;	// While not at the end of the file, read and accumulate information
  while (!TimeBal.eof ())
    {
      ++count;
      if (count == 1)
	first = Week;
      sumWeek += Week;
      sumBalance += Balance;
      sumWeek2 += Week * Week;
      sumWeekBalance += Week * Balance;
      TimeBal >> Week >> Balance;
    }
  last = Week;
  denominator = sumWeek * sumWeek - count * sumWeek2;	// Compute slope and y-intercept.
  m = (sumWeek * sumBalance - count * sumWeekBalance) / denominator;	//Slope
  b = (sumWeek * sumWeekBalance - sumWeek2 * sumBalance) / denominator;	//Y-intercept
// Set format flags
  cout.setf (ios::fixed);
  cout.precision (2);
  report.setf (ios::fixed);
  report.setf (ios::showpoint);
  report.precision (2);
// Print summary information.
  cout << "The Change in Balances from week: \n";
  cout << first << " to week " << last << endl << endl;
  cout << "Linear model: \n";
  cout << " The added or subtracted funds (slope) is:\n " << m <<
    "and the accumulated  amount each week (y-intercept) is: " << b << endl;
  cout << "The algebraic linear equation is " << "y" << "=" << m <<
    "*" << "x" << "+" << b << endl;
  cout << "The balance for week 8.5 is:";
//Report
  report << "The Change in Balances: \n";
  report << first << " to " << last << endl << endl;
  report << "Linear model: \n";
  report << " added or subtracted funds " << m <<
    " accumulated  amount each week " << b << endl;
  report << "The algebraic linear equation is " << "y" << "=" << m <<
    "*" << "x" << "+" << b << endl;
  report << "The balance for week 8.5 is:";

// Close file and exit program.
  report.close ();
  TimeBal.close ();
  return 0;
}				// end of main  
Last edited on
https://www.mathsisfun.com/data/least-squares-regression.html is my guide. Using your data, it might be a bit clearer as follows:

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main ()
{
    string filename("weekly_balance.txt"); // HARD CODE FOR TESTING
    ifstream TimeBal{filename};
    
    double y{0}; // BALANCE
    double x{0}; // WEEK
    
    double sum_x{0}, sum_y{0};
    double sum_x2{0}, sum_y2{0};
    double sum_xy{0};
    
    int N{0};
    if (TimeBal.is_open())
    {
        while ( TimeBal >> x >> y ) // eof() IS NOT A GOOD CHOICE
        {
            N++;
            cout << x << ' ' << y << '\n';
            
            sum_x += x; sum_y += y;
            sum_x2 += (x*x); sum_y2 += (y*y);
            sum_xy += (x*y);
            
        }
        TimeBal.close();
    }
    else
    {
        cout << "Unable to open file";
        exit(1);
    }
    
    cout << "No. of records: " << N << '\n';
    
    double m = (N * sum_xy - sum_x*sum_y)/(N*sum_x2 - sum_x * sum_x); // SLOPE <--
    double b = (sum_y - m*sum_x)/N; // INTERCEPT
    
    cout
    << "Linear best fit is y = " << m << "x + " << b << '\n'
    << " where y is the weekly balance and x is the week number\n"
    << "\n\n"
    << "Balance at 8.5 weeks: " << m * 8.5 + b << '\n';
    
    return 0;
}


1 490
2 720
3 970
4 1265

... blah blah
20 2165
21 2900
22 3600
No. of records: 22
Linear best fit is y = 96.476x + 667.844
 where y is the weekly balance and x is the week number


Balance at 8.5 weeks: 1487.89
Program ended with exit code: 0


Then proceed to outputting to the report file. If you use any of the above make sure you check it.

Last edited on
Are you sure that you don't want to use interpolation rather than regression? That data is quite a long way away from a linear relationship.
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>
using namespace std;

//======================================================================

void regression( const vector<double> &x, const vector<double> &y, double &m, double &c, double &rho )
{
   int N = x.size();
   double Sx = 0, Sy = 0, Sxx = 0, Syy = 0, Sxy = 0;
   for ( int i = 0; i < N; i++ )
   {
      Sx  += x[i];
      Sy  += y[i];
      Sxx += x[i] * x[i];
      Syy += y[i] * y[i];
      Sxy += x[i] * y[i];
   }
   double varxx = Sxx - Sx * Sx / N, varyy = Syy - Sy * Sy / N, varxy = Sxy - Sx * Sy / N;
   m = varxy / varxx;
   c = ( Sy - m * Sx ) / N;
   rho = varxy / sqrt( varxx * varyy );
}

//======================================================================

double interpolation( const vector<double> &x, const vector<double> &y, double xval )    // x sorted and increasing
{
   int i = 0, N = x.size();
   if ( xval >= x[N-1] )
      i = N - 2;
   else if ( xval > x[0] )
      while( x[i+1] < xval ) i++;

   return y[i] + ( xval - x[i] ) * ( y[i+1] - y[i] ) / ( x[i+1] - x[i] );
}

//======================================================================

int main()
{
   vector<double> x, y;
   ifstream in( "input.txt" );
   for ( double xpt, ypt; in >> xpt >> ypt; )
   {
      x.push_back( xpt );
      y.push_back( ypt );
   }

   double m, c, rho;
   regression( x, y, m, c, rho );
   cout << "Linear regression line y = mx + c\n"
        << "   m = " << m << '\n'
        << "   c = " << c << '\n'
        << "Correlation coefficient rho = " << rho << "\n\n";

   double test = 8.5;
   cout << "At test value " << test << '\n'
        << "   linear regression gives y = " << m * test + c << '\n'
        << "   interpolation     gives y = " << interpolation( x, y, test ) << '\n';
}


Linear regression line y = mx + c
   m = 96.476
   c = 667.844
Correlation coefficient rho = 0.837577

At test value 8.5
   linear regression gives y = 1487.89
   interpolation     gives y = 1607.5

Topic archived. No new replies allowed.