# C1_W1_Optional Lab Cost Function: cost_sum = 0?

Greetings! I have a question about the codes in the function compute_cost . Specifically, why cost_sum is equal to 0 first, then cost_sum = cost + cost_sum later in the loop ?

Is there a mathematical reason for that, or is it for the loop’s sake? And which part of the lecture covers or relates to this code?

Hi there

So in that cell, if you look at the two cells above, you can see the math behind calculating the cost function.
J(w,b) = \frac{1}{2m} \sum\limits_{i = 0}^{m-1} (f_{w,b}(x^{(i)}) - y^{(i)})^2 \tag{1}

As you can see, the total cost is a sum. Now all that the loop does is calculate that sum.

  # number of training examples
m = x.shape[0]     # take the number of examples in the training data.

cost_sum = 0     # initialize the sum to zero (to store the result of the sum)
for i in range(m):
f_wb = w * x[i] + b     # this is the prediction of our model for each x, given w and b
cost = (f_wb - y[i]) ** 2     # this is the squared error for that sample. (remember that our cost is the Mean Squared Error i.e. MSE)
cost_sum = cost_sum + cost      # add the cost of this sample, to the sum
total_cost = (1 / (2 * m)) * cost_sum     # now we have the sum. we devide by (2 * m) as shown in the equation.