' predictions of the population ' https://keisan.casio.com/exec/system/14059932254941 ' Dim x(9), y(9) ' s = 17108069 m7 = s Mod 7 MsgBox "m7 is" MsgBox m7 ' m = m7 + 3 ' x(1) = 0 x(2) = 1 x(3) = 2 x(4) = 3 x(5) = 4 x(6) = 5 x(7) = 6 x(8) = 7 x(9) = 8 ' y(1) = 263 y(2) = 254 y(3) = 243 y(4) = 231 y(5) = 228 y(6) = 201 y(7) = 188 y(8) = 162 y(9) = 142 ' 'y(1) = Exp(x(1)) 'y(2) = Exp(x(2)) 'y(3) = Exp(x(3)) 'y(4) = Exp(x(4)) 'y(5) = Exp(x(5)) 'y(6) = Exp(x(6)) 'y(7) = Exp(x(7)) 'y(8) = Exp(x(8)) 'y(9) = Exp(x(9)) ' ' Exponential case: ' xsum = 0 For j = 1 To m xsum = xsum + x(j) Next j ' x2sum = 0 For j = 1 To m x2sum = x2sum + (x(j)) ^ 2 Next j ' xlogysum = 0 For j = 1 To m xlogysum = xlogysum + x(j) * Log(y(j)) Next j ' ylogsum = 0 For j = 1 To m ylogsum = ylogsum + Log(y(j)) Next j ' a = (ylogsum * x2sum - xsum * xlogysum) / (m * x2sum - xsum ^ 2) b = (m * xlogysum - xsum * ylogsum) / (m * x2sum - xsum ^ 2) ' 'MsgBox a ABIG = Exp(a) 'MsgBox ABIG 'MsgBox b error2 = 0 For j = 1 To m error2 = error2 + (y(j) - ABIG * Exp(b * x(j))) ^ 2 Next j 'MsgBox error2 p2200 = Fix(ABIG * Exp(b * 19)) 'p2200 = ABIG * Exp(b * 19) MsgBox "exponential prediction of population in year 2200 is" MsgBox p2200 MsgBox "exponential approximation will never predict zero" 'MsgBox p2200 ' http://mathworld.wolfram.com/LeastSquaresFittingExponential.html ' https://en.wikipedia.org/wiki/List_of_countries_by_future_population_(United_Nations,_low_fertility_variant) ' Linear case: sx = 0 For j = 1 To m sx = sx + x(j) Next j sy = 0 For j = 1 To m sy = sy + y(j) Next j sxy = 0 For j = 1 To m sxy = sxy + x(j) * y(j) Next j sx2 = 0 For j = 1 To m sx2 = sx2 + x(j) ^ 2 Next j g = (m * sxy - sx * sy) / (m * sx2 - sx ^ 2) i = (sy - g * sx) / m 'MsgBox g 'MsgBox i error2linear = 0 For j = 1 To m error2linear = error2linear + (y(j) - g * x(j) - i) ^ 2 Next j di = Abs(error2linear - error2) 'MsgBox di 'MsgBox error2linear Linearp2200 = Fix(g * 19 + i) 'Linearp2200 = g * 19 + i MsgBox "Linear prediction of population in year 2200 is" MsgBox Linearp2200 Linerzeropopulationtime = Fix(2020 - 10 * i / g) 'Linerzeropopulationtime = 2020 - 10 * i / g MsgBox "Linear approximation predicts zero population in the year" MsgBox Linerzeropopulationtime ' Quadratic case: ' https://www.azdhs.gov/documents/preparedness/state-laboratory/lab-licensure-certification/technical-resources/calibration-training/12-quadratic-least-squares-regression-calib.pdf ' https://keisan.casio.com/exec/system/14059932254941 a = -1 b = -8 c = 263 m = 3 '------ a = 0.75 b = -8.45 c = 263.05 m = 4 ' ------ a = 0.785714 b = -12.4429 c = 263.971 m = 5 ' ------ a = -1.03571 b = -6.25 c = 261.786 m = 6 ' ----- a = -0.9762 b = -6.5 c = 261.905 m = 7 ' ----- a = -2.25 b = -5.131 c = 261.083 m = 8 ' ----- a = -1.1905 b = -5.476 c = 261.33 m = 9 '------ error2quadratic = 0 For j = 1 To m error2quadratic = error2quadratic + (y(j) - a * x(j) ^ 2 - b * x(j) - c) ^ 2 Next j 'MsgBox error2quadratic quadratic2200 = Fix(a * 19 ^ 2 + b * 19 + c) 'quadratic2200 = a * 19 ^ 2 + b * 19 + c MsgBox "quadratic prediction of population in year 2200 is" MsgBox quadratic2200 discrmminant = b ^ 2 - 4 * a * c If discrmminant < 0 Then MsgBox "no real roots for quadratic case": GoTo 22 x1 = (-b + Sqr(discrmminant)) / (2 * a) x2 = (-b - Sqr(discrmminant)) / (2 * a) 'MsgBox "quadratic root 1 is" 'MsgBox x1 If x1 > 0 Then MsgBox "quadratic prediction of zero population is in year": MsgBox Fix(2020 + 10 * x1) If x2 > 0 Then MsgBox "quadratic prediction of zero population is in year": MsgBox Fix(2020 + 10 * x2) 'MsgBox "quadratic root 2 is" 'MsgBox x2 22 jkhjkh = 78678