Linear Function

Overview

Linear functions are fundamental mathematical expressions used to model linear relationships between variables. They are typically expressed in the form ( p(x) = ax + b ), where ( a ) and ( b ) are coefficients. These coefficients play crucial roles in determining the characteristics of the linear function: ( a ) represents the slope of the line, while ( b ) represents the y-intercept, or the value of the function when ( x = 0 ).

Definition

A linear function is defined as a function that, when graphed, produces a straight line. The general form of a linear function is ( f(x) = ax + b ), where ( a ) and ( b ) are constants. This form is widely used in various fields, including programming and data science, to model and analyze linear relationships.

Example

An example of a linear function is ( p(x) = ax + b ). In this context, ( a ) and ( b ) are coefficients that determine the slope and intercept of the line, respectively. This function can be visualized as a straight line on a graph, where the slope ( a ) indicates the steepness and direction of the line, and the intercept ( b ) indicates where the line crosses the y-axis.

Application in Predictive Modeling

Linear functions are often used in predictive modeling to estimate outcomes based on input variables. For instance, consider a scenario where we want to predict the price of a car based on its mileage. The linear function can be expressed as ( p(x) = ax + b ), where ( x ) is the mileage, ( p ) is the predicted price, and ( a ) and ( b ) are coefficients that define the relationship between mileage and price.

[Figure 14.2](https://livebook.manning.com/book/exploring-math-for-programmers-and-data-scientists/chapter-14/1)

Figure 14.2 A schematic of a linear function predicting price p from mileage x Figure 14.2 A schematic of a linear function predicting price p from mileage x

In this example, the function acts as an imaginary machine that takes the mileage of a Toyota Prius and predicts its price. By choosing specific values for ( a ) and ( b ), such as ( a = -0.05 ) and ( b = 20,000 ), the graph of the function becomes a line starting at a price of $20,000 and decreasing by $0.05 for every additional mile driven.

[Figure 14.4](https://livebook.manning.com/book/exploring-math-for-programmers-and-data-scientists/chapter-14/1)

Figure 14.4 A pair of numbers (a, b) define a linear function that we can plot on a graph as a line. For positive values of a, the graph slopes upward. Figure 14.4 A pair of numbers (a, b) define a linear function that we can plot on a graph as a line. For positive values of a, the graph slopes upward.

Figure 14.4 illustrates how different values of ( a ) and ( b ) affect the slope and direction of the line. Positive values of ( a ) result in an upward-sloping line, which, in the context of car pricing, would imply that a car gains value as it is driven—a scenario that is unlikely in reality. The cost function associated with this model compares the predicted line to actual data, with a lower cost indicating a better fit to the data.

FAQ (Frequently asked questions)

What do the coefficients 'a' and 'b' represent in a linear function?

What does a linear function look like when graphed?

What is the general form of a linear function?