Forecasting the future with simple linear regression

While writing this book, I was using simple linear regression to estimate the approximate finish date. From time to time, I recorded the total number of pages I had written up to that moment, and then incorporated the data into linear regression. The number of pages here is a feature and the date is a label.

Linear trend is a useful feature for any application that deals with some gradually progressing processes or tasks, especially in the combination with charts. Later in this chapter, we will learn how to build non-linear trend lines, namely polynomial.

Let's make some forecasts:

let xVec: [Double] = [2,3,4,5] 
let yVec: [Double] = [10,20,30,40] 
 
let regression = SimpleLinearRegression() 
regression.train(xVec: xVec, yVec: yVec, learningRate: 0.1, maxSteps: 31) 
 
regression.slope 
regression.intercept 
 
regression.predict(x: 7) 
regression.cost(trueVec: yVec, predictedVec: regression.predict(xVec: xVec))