I am learning machine learning from Andrew Ng's open-class notes and coursera.org. I am trying to understand how the cost function for the logistic regression is derived. I will start with the cost function for linear regression and then get to my question with logistic regression.
(Btw a similar question was asked here, which answers the question how the derivative of cost function was derived but not the cost function itself.)
1) Linear regression uses the following hypothesis: $$ h_\theta(x) = \theta_0 + \theta_1 x$$
Accordingly, the cost function is defined as:
$$J(\theta) = \dfrac {1}{2m} \displaystyle \sum_{i=1}^m \left (h_\theta (x^{(i)}) - y^{(i)} \right)^2$$
-
2) The logistic regression uses a sigmoid/logistic function which is $ 0 \leq h_\theta (x) \leq 1 $.
Defined as :
$$ \begin{align*} & h_\theta (x) = \dfrac{1}{1 + e^{-(\theta^T x)}} \newline \newline \end{align*} $$
Accordingly, our cost function has also changed. However, instead of plugging-in the new h(x) equation directly, we used logarithm.
$$ \begin{align*} & J(\theta) = \dfrac{1}{m} \sum_{i=1}^m Cost(h_\theta(x^{(i)}),y^{(i)}) \newline & Cost(h_\theta(x),y) = -log(h_\theta(x)) \; & \text{if y = 1} \newline & Cost(h_\theta(x),y) = -log(1-h_\theta(x)) \; & \text{if y = 0} \end{align*} $$
And the new cost function is defined as:
$$ J(\theta) = - \frac{1}{m} \sum_{i=1}^m [y^{(i)}\log (h_\theta (x^{(i)})) + (1 - y^{(i)})\log (1 - h_\theta(x^{(i)}))]$$
From class notes ".. the more hypothesis is off from y, the larger the cost function output. If our hypothesis is equal to y, then our cost is 0."
It's also mentioned in the class notes that MLE (maximum-likelihood estimation) is used to derive the logs in the cost function. I can see how logs function and set penalty values until we find the right values, but I don't see how we came to choose them in the cost function.