Logistic Regression is a common Supervised Machine Learning Classification algorithm that attempts to estimate the probability that a given set of data can be classified as a positive example of some category. Examples of some question logistic regression can help answer include:

- What is the probability a costumer will purchase a product given their recent on-sight behavior?
- What is the probability that a recipient of a loan will default based on their most recent credit data?
- What is the probability a given wine is red based on its chemical properties?

These questions can be changed to a goal such as: “Classify this person as a purchaser/non-purchaser based on their on-sight behavior.” Logistic Regression does not do this directly. It is done by setting a classification threshold for the probability, say 0.75 (or 75%). All examples with an estimated probability of being a positive example of the classification above 75% will be classified as a positive example. What to set the threshold depends on the problem/ goal of the classification, and is not inherit to Logistic Regression. I will defer discussing this to another time.

# Why Implement It Ourselves?

There are great libraries already written in all common data science languages that already implement Logistic Regression. They are faster and much more robust than what I am going to write here. I also use them over my own code. I recommend that for everyone.

The point of implementation is partly to have a deeper understanding of the packages/libraries/algorithms, and partly for the sheer join of it. There is also a habit of mind that I want to establish for myself: rigorous understanding. Most implementations have design decisions and choice that might not be be the best choice for all problems. There are also bugs that exist that might inhibit your work. Plus, there is also the fact that there are many very good and specific machine learning methods and algorithms that are not in the common or best packages out there. You might find yourself wanting to use one, and not willing to wait.

My last reason: cost function manipulation is fun and the results can be surprising. I will also defer this to a post later in the series.

If you want to skip the theory you can jump to the __code__.

# Logistic Regression Theory (Math o’clock)

Logistic Regression is an algorithm that attempts to estimate the probability () that a given the data () is an example of given classification (). This can be written as:

where

The odds ratio for a given example is:

The Logistic Regression algorithm requires/assumes that the natural log of the odds ( ) is a linear function of the variables:

Notice that the weights () slipped into the equation. The index represents the variable in the example set . The result is now both a condition of the data and the chosen weights because we have decided to model the probability with a specific function.

The above equation than requires that the probability is represented as a Sigmoid function:

We now have a set of examples of and a model to represent the probabilities from each example. The weights or coefficients in the model are not specified. The goal now is to choose the weights that best reflect the truth presented in the data.

## Gradient Decent – How to Choose the Best Weights.

The best always depends on the definition of the problem, so we have to define it. Lets consider one example and ask what is the probability of being correct. If then the probability of being correct is:

And if , the probability of being correct is:

We because or , we write the probability/likelihood of being correct () as:

We can multiply the above equation together for each example if the assumption that each example is independent holds. The likelihood of being correct for all the examples can be written as:

Where the index is the example from our set of training examples.

We want to maximize the likelihood of being correct, we we need to choose the weights that produce the maximum likelihood of being correct. We have two problems with the current form of the likelihood that makes it difficult to solve for the weights

- Machine Precisions – the product of small numbers is a smaller number. At some point zero is the only representation of the result in the computer
- Algebriac complexity of the product rule for finding the direction to step in.

Thankfully, there is an acceptable way around these problems: by taking the natural log of the likelihood of being correct. By the property of logs, the product turns into a sum. There is also convention of taking the negative sign so that the problem is transformed from finding the weights that maximum likelihood of being correct to finding the weights the minimizes the negative log likelihood of being correct.

Before we move on to the implementation, there is one last math value we need to derive, and that is the gradient of the negative log-likelihood function from above.

where

As before the indexes and represent the variable in the training example.

Combining these two equations leads to:

Because we have a cost function ( ) we want to minimize, we can use gradient decent.

The algorithm follows:

- Set initial weight/coefficient values
- While the change cost function is larger than some threshold
- Find the gradient of the cost function
- Change the weight values in the direction of the gradient

Though simply described, there is a lot of nuance in practice. One is that we must decide the right amount to change the weights in the direction of the gradient. We will use a constant proportion of the gradient, known as the learning rate (), for our implementation.

The final weight update equation than becomes:

# Pseudo-Code Logistic Regression

Before we get to the python code, lets write out the steps we are planning to implement.

- Setup
- Set stopping criteria
- Add an incept () to the training data

- Run Gradient Decent
- Get Log Likelihood Gradient
- Update Weights
- Get new Log Likelihood Value
- Stop if difference between old and new Log Likelihood is below threshold

- Use Model

Now lets implement Logistic Regression in python in the next post.

**Thank You**

I appreciate you taking the time to read this post. I hope you gained something from it. Feel free to reach out with any questions in the comments or directly through the email link in the menu.

Hey Bryan

Very thorough article! I thought dividing your work into two separate portions – theory and example – was useful.

I had two comments:

1. Explain we can’t find the optimal weights analytically – thus, we find them numerically.

2. This pertains to the 2nd article – provide a quick background on overfitting and thus the need for CV techniques.

Great work! It’s obvious you spent a considerable amount of time on it.

Hi Jimmy,

I really appreciate the feedback and suggestions.

I agree that I glossed over the fact the weights can’t be found analytically for logistic regressions.

Cross validation is a really useful/necessary process in practice, but I do not think it is inherit to logistic regression.

I have post plans where I will show that the sampling data from distributions can lead to cost functions that do not have the same minimum as the cost function based on the underlying distributions

minimum. I will also show how cross validation helps finding minimums closer to the true minimum.

That post, however, is planned outside the context of logistic regression.

Hi Bryan, thanks for sharing. But …. I can’t see any images (equations, formula) on this webpage. Nor do my friends. Is it possible get it to fixed?

Thanks for letting me know. I have selected a new theme, and I believe that fixed the issue. Let me know if you still can’t see the equations.

That works！Thanks!!!