Compare commits

...

1 Commits

Author SHA1 Message Date
sofia-samaniego 64e50c78fd Desarrollo de Log Loss 1 month ago

@ -269,6 +269,41 @@ def sigmoid(z):
This function calculates the error between the model's predicted probabilities ($p$) and the true binary labels ($y$).
Probabilities are clipped using a tiny epsilon ($\epsilon$) to prevent mathematical undefined errors (like $\log(0)$), which would break the algorithm.
### Derivation of the Log Loss (Binary Cross-Entropy) Cost Function
In binary classification models, the goal is to estimate the probability that an instance belongs to the positive class. To optimize the model, we need a cost function that heavily penalizes confident but incorrect predictions. This is achieved using the **Log Loss** (Binary Cross-Entropy), derived via Maximum Likelihood Estimation (MLE).
Here is the step-by-step mathematical derivation:
#### 1. Likelihood of a Single Instance (Bernoulli Distribution)
For a single training instance $(x^{(i)}, y^{(i)})$, the true label is binary: $y^{(i)} \in \{0, 1\}$. If our model predicts the probability $\hat{y}^{(i)}$, we can express the probability (Likelihood) of observing the true label using the Bernoulli distribution:
$$P(y^{(i)}|x^{(i)};\theta) = (\hat{y}^{(i)})^{y^{(i)}}(1-\hat{y}^{(i)})^{1-y^{(i)}}$$
This compact expression works for both possible outcomes:
* If $y^{(i)} = 1$, the probability is $\hat{y}^{(i)}$.
* If $y^{(i)} = 0$, the probability is $1 - \hat{y}^{(i)}$.
#### 2. Joint Likelihood of the Dataset
Assuming that all $m$ training instances are independent, the total likelihood of the model, $L(\theta)$, is the product of the individual probabilities:
$$L(\theta) = \prod_{i=1}^{m} P(y^{(i)}|x^{(i)};\theta)$$
During training, our objective is to find the parameters (weights) that **maximize** this likelihood.
#### 3. Log-Likelihood
Multiplying thousands of probabilities (numbers between 0 and 1) leads to computational underflow. To fix this, we apply the natural logarithm. This transforms the product into a sum and brings the exponents down as multipliers:
$$l(\theta) = \sum_{i=1}^{m} \log P(y^{(i)}|x^{(i)};\theta)$$
$$l(\theta) = \sum_{i=1}^{m} \left[ y^{(i)}\log(\hat{y}^{(i)}) + (1-y^{(i)})\log(1-\hat{y}^{(i)}) \right]$$
#### 4. Negative Log-Likelihood (The Cost Function)
Optimization algorithms like Gradient Descent are designed to **minimize** a cost function rather than maximize it. To convert this into a minimization problem, we multiply the Log-Likelihood by $-1$. Finally, we divide by the total number of samples $m$ to get the average error.
This gives us the final Log Loss equation:
$$J(\theta) = -\frac{1}{m} \sum_{i=1}^{m} \left[ y^{(i)}\log(\hat{y}^{(i)}) + (1-y^{(i)})\log(1-\hat{y}^{(i)}) \right]$$
```python
def logLoss(y, p, eps=1e-12):

Loading…
Cancel
Save