Imagine that you are the producer for a comedy show at your school. We need you to use knowledge of linear regression to make predictions as to the success of the show.
The comedy show is trying to figure out how much money to spend on advertising in the student newspaper. The newspaper tells the show that
- For every two dollars spent on advertising, three students attend the show.
- If no money is spent on advertising, no one will attend the show.
Write a linear regression function called attendance
that shows the relationship between advertising and attendance expressed by the newspaper.
def attendance(advertising):
pass
attendance(100) # 150
attendance(50) # 75
Despite what the student newspaper says, the comedy show knows from experience that they'll still have a crowd even without an advertising budget. Some of the comedians in the show have friends (believe it or not), and twenty of those friends will show up. Write a function called attendance_with_friends
that models the following:
- When the advertising budget is zero, 20 friends still attend
- For every two dollars spent on advertising, three additional people attend the show.
def attendance_with_friends(advertising):
pass
attendance_with_friends(100) # 170
attendance_with_friends(50) # 95
Let's help plot this line so you can get a sense of what your
Our x-values can be a list of initial_sample_budgets
, equal to a list of our budgets. And we can use the outputs of our attendance_with_friends
function to determine the list of attendance-values
, the attendance at each of those x-values.
initial_sample_budgets = [0, 50, 100]
attendance_values = [20, 95, 170]
First we import the necessary plotly library, and graph_obs
function, and setup plotly
to be used without uploading our plots to its website.
Finally, we plot out our regression line, using our attendance_with_friends
function. The budgets
will be our x values. For our y values, we need to use our attendance_with_friends
function to create a list of y-value attendances for every input of x.
import plotly
from plotly import graph_objs
plotly.offline.init_notebook_mode(connected=True)
trace_of_attendance_with_friends = graph_objs.Scatter(
x=initial_sample_budgets,
y=attendance_values,
)
plotly.offline.iplot([trace_of_attendance_with_friends])
trace_of_attendance_with_friends
{'type': 'scatter', 'x': [0, 50, 100], 'y': [20, 95, 170]}
Now let's write a couple functions that we can use going forward. We'll write a function called m_b_data
that given a slope of a line, x
pointing to a list of x_values
, and a key of y
that points to a list of y_values
. Each x_values
.
def m_b_data(m, b, x_values):
pass
m_b_data(1.5, 20, [0, 50, 100]) # {'x': [0, 50, 100], 'y': [20.0, 95.0, 170.0]}
Now let's write a function called m_b_trace
that uses our m_b_data
function to return a dictionary that includes keys of name
and mode
in addition to x
and y
. The values of mode
and name
are provided as arguments. When the mode
argument is not provided, it has a default value of line
and when name
is not provided, it has a default value of line function
.
def m_b_trace(m, b, x_values, mode = 'line', name = 'line function'):
pass
m_b_trace(1.5, 20, [0, 50, 100])
# {'mode': 'line', 'name': 'line function', 'x': [0, 50, 100], 'y': [20.0, 95.0, 170.0]}
The comedy show decides to use advertising with three different shows. The attendance looks like the following.
Budgets (dollars) | Attendance |
---|---|
200 | 400 |
400 | 700 |
In code, we represent the shows as the following:
first_show = {'budget': 200, 'attendance': 400}
second_show = {'budget': 400, 'attendance': 700}
Write a function called marginal_return_on_budget
that provided these two shows, returns the expected amount of increase per every dollar spent on budget. The function should use the formula for calculating the slope of a line.
def marginal_return_on_budget(first_show, second_show):
pass
marginal_return_on_budget(first_show, second_show) # 1.5
first_show
{'attendance': 400, 'budget': 200}
Let's make sure that our function properly calculates the slope of the line with different data.
imaginary_third_show = {'budget': 300, 'attendance': 500}
imaginary_fourth_show = {'budget': 600, 'attendance': 900}
marginal_return_on_budget(imaginary_third_show, imaginary_fourth_show) # 1.3333333333333333
Now we'll begin to write functions that we can use going forward. The functions will calculate attributes of lines in general, as well be applied to predicting the attendance of the comedy show.
Take the following data. The comedy show spends zero dollars on advertising for the next show. Now the attendance chart looks like the following:
Budgets (dollars) | Attendance |
---|---|
0 | 100 |
200 | 400 |
400 | 700 |
budgets = [0, 200, 400]
attendance_numbers = [100, 400, 700]
Write a function called y_intercept_provided
. Given a list of x_values
and a list of y_values
should find the point with an x_value of zero, and then return the corresponding y-value. If there is no x-value equal to zero, it returns False
.
To get you started, we'll provide a function called sort_points
that returns a list of points sorted by their x_values. The return value is a list of sorted tuples.
def sorted_points(x_values, y_values):
values = list(zip(x_values, y_values))
sorted_values = sorted(values, key=lambda value: value[0])
return sorted_values
sorted_points([4, 1, 6], [4, 6, 7])
[(1, 6), (4, 4), (6, 7)]
def y_intercept_provided(x_values, y_values):
pass
y_intercept_provided([200, 400], [400, 700]) # False
y_intercept_provided(budgets, attendance_numbers) # 100
Now write a function called slope
, that given a list of points, will use the points with the lowest and highest x-values to calculate the slope of a line.
def slope(x_values, y_values):
pass
slope([200, 400], [400, 700]) # 1.5
Now write a function called y_intercept
. It then calculates the y intercept, by drawing a line from the point with the highest x-value and the calculated slope. It's arguments are a list of x-values and a list of y-values, and it returns a dictionary with the keys of m
and b
to return the values of m
and b
.
If the values provided includes the y_intercept, it returns the provided y_intercept.
def y_intercept(x_values, y_values, m = None):
pass
y_intercept([200, 400], [400, 700]) # 100
y_intercept([0, 200, 400], [10, 400, 700]) # 10
Now write a function called build_regression_line
that given a list of x_values
and a list of y_values
returns a dictionary with a key of m
and a key of b
to return the m
and b
values of the calculated regression line. Use the slope
and y_intercept
functions to calculate the line.
def build_regression_line(x_values, y_values):
pass
build_regression_line([0, 200, 400], [10, 400, 700]) # {'b': 10.0, 'm': 1.725}
Now, let's write a function that will will return the expected attendance given previous shows, and a budget, and do so even when the previous shows do not include a value for when x is zero. The function will be called regression_line_two_points
and take inputs of an array of two shows and a budget.
first_show = {'budget': 300, 'attendance': 700}
second_show = {'budget': 400, 'attendance': 900}
shows = [first_show, second_show]
def expected_value_for_line(m, b, x_value):
pass
expected_value_for_line(1.5, 100, 100)
250.0
Now that we have built these functions, we can use them for our dataset. Uncomment and run the lines below to see how we can use our functions going forward.
first_show = {'budget': 200, 'attendance': 400}
second_show = {'budget': 400, 'attendance': 700}
third_show = {'budget': 300, 'attendance': 500}
fourth_show = {'budget': 600, 'attendance': 900}
comedy_shows = [first_show, second_show, third_show, fourth_show]
show_x_values = list(map(lambda show: show['budget'], comedy_shows))
show_y_values = list(map(lambda show: show['attendance'], comedy_shows))
def trace_values(x_values, y_values, mode = 'markers', name="data"):
return {'x': x_values, 'y': y_values, 'mode': mode, 'name': name}
def plot(traces):
plotly.offline.iplot(traces)
# comedy_show_trace = trace_values(show_x_values, show_y_values, name = 'comedy show data')
# comedy_show_trace
{'mode': 'markers',
'name': 'comedy show data',
'x': [200, 400, 300, 600],
'y': [400, 700, 500, 900]}
# show_regression_line = build_regression_line(show_x_values, show_y_values)
# show_regression_line
{'b': 150.0, 'm': 1.25}
# trace_show_line = m_b_trace(show_regression_line['m'], show_regression_line['b'], show_x_values, name = 'regression line')
# trace_show_line
{'mode': 'line',
'name': 'regression line',
'x': [200, 400, 300, 600],
'y': [400.0, 650.0, 525.0, 900.0]}
# plot([comedy_show_trace, trace_show_line])