Chewing over logistic regression

Chewing over logistic regression

In this article, we will analyze the theoretical foundations of transformation. the linear regression function. downward API support (simultaneously with this in the inverse logit transformation function (also referred to as the logistic response function).Then, using the arsenal of the maximum likelihood method,according to the logistic regression model, we will derive the loss function. Logistic Loss,or in other words, we will determine the function through which the parameters of the weight vector are fitted in the logistic regression model. Chewing over logistic regression.

Article Outline:

  1. Let's revisit the linear relationship between two variables.
  2. We will identify the need for transformation of the linear regression function. Chewing over logistic regression downward API support (simultaneously with this in the logistic response function. Chewing over logistic regression
  3. We will perform transformations and derive the logistic response function.
  4. We will try to understand why the least squares method is inadequate for parameter fitting Chewing over logistic regression functions Logistic Loss,
  5. We use maximum likelihood method to determine the parameter fitting function. Chewing over logistic regression:

    5.1. Case 1: function Logistic Loss, for objects with class labels. 0 and 1:

    Chewing over logistic regression

    5.2. Case 2: function Logistic Loss, for objects with class labels. -1 and +1:

    Chewing over logistic regression


The article is filled with simple examples where all calculations can be easily performed mentally or on paper; in some cases, a calculator may be needed. So be prepared 🙂

This article is primarily aimed at data scientists with a basic understanding of machine learning fundamentals.

The article will also provide code for plotting graphs and calculations. All code is written in the python 2.7language. I will clarify in advance the 'novelty' of the version used—this is one of the conditions for passing a well-known course from Yandex, on a well-known online education platform, Courseraand, as can be assumed, the material was prepared based on this course.

01. Linear dependence

It is quite reasonable to ask the question—what is the connection between linear dependence and logistic regression?

It's simple! Logistic regression is one of the models that fall under the linear classifier category. In simple terms, the task of a linear classifier is to predict target values Chewing over logistic regression from variables (regressors). Chewing over logistic regressionIt is assumed that the relationship between the features Chewing over logistic regression and the target values is Chewing over logistic regression linear. Hence the name of the classifier—it is linear. If we generalize significantly, the basis of the logistic regression model rests on the assumption of a linear relationship between the features. Chewing over logistic regression and the target values is Chewing over logistic regressionHere it is—the connection.

In the studio, the first example is indeed about the linear dependency of the studied quantities. While preparing the article, I stumbled upon an example that has become rather clichĂ©d — the dependency of current on voltage. (“Applied Regression Analysis,” N. Draper, G. Smith). Here, we will also consider it.

In accordance with Ohm's law:

Chewing over logistic regression, where Chewing over logistic regression — current, Chewing over logistic regression — voltage, Chewing over logistic regression — resistance.

If we didn’t know Ohm's law, we could find the dependency empirically, changing Chewing over logistic regression and measuring Chewing over logistic regression, keeping Chewing over logistic regression fixed. Then we would see that the graph of the dependency Chewing over logistic regression from Chewing over logistic regression gives a more or less straight line passing through the origin. We said 'more or less,' since although the dependency is actually exact, our measurements may contain small errors, and thus the points on the graph may not fall strictly on the line but be scattered around it randomly.

Graph 1: ‘Dependency Chewing over logistic regression from Chewing over logistic regression»

Chewing over logistic regression

Graph Rendering Code

import matplotlib.pyplot as plt
%matplotlib inline

import numpy as np

import random

R = 13.75

x_line = np.arange(0,220,1)
y_line = []
for i in x_line:
    y_line.append(i/R)
    
y_dot = []
for i in y_line:
    y_dot.append(i+random.uniform(-0.9,0.9))


fig, axes = plt.subplots(figsize = (14,6), dpi = 80)
plt.plot(x_line,y_line,color = 'purple',lw = 3, label = 'I = U/R')
plt.scatter(x_line,y_dot,color = 'red', label = 'Actual results')
plt.xlabel('I', size = 16)
plt.ylabel('U', size = 16)
plt.legend(prop = {'size': 14})
plt.show()

02. The Necessity of Transformations of the Linear Regression Equation

Let’s consider another example. Imagine that we work in a bank, and our task is to determine the probability of a borrower repaying a loan based on certain factors. To simplify the task, we will consider just two factors: the borrower’s monthly salary and the monthly payment amount for loan repayment.

The task is very conditional, but with this example, we will understand why applying the linear regression function., and we will also learn what transformations need to be made to the function.

Returning to the example. It is understood that the higher the salary, the more the borrower can allocate monthly for loan repayment. At the same time, for a certain salary range, this relationship will be quite linear. For instance, let’s take a salary range from 60,000 RUB to 200,000 RUB and assume that within this salary range, the relationship between the size of the monthly payment and the size of the salary is linear. Let’s suppose that it has been determined for this salary range that the ratio of salary to payment should not fall below 3, and the borrower should also have 5,000 RUB in reserve. Only in this case will we consider that the borrower will repay the loan to the bank. Then, the linear regression equation will look like this:

Chewing over logistic regression

where Chewing over logistic regression, Chewing over logistic regression, Chewing over logistic regression, Chewing over logistic regression — the borrower's salary, Chewing over logistic regressionthe loan payment Chewing over logistic regression — of the borrower. Chewing over logistic regressionSubstituting the salary and loan payment into the equation with fixed parameters

a decision can be made to approve or refuse the loan. Chewing over logistic regression Looking ahead, we note that, with the given parameters,

the linear regression function Chewing over logistic regression , used inthe logistic response function will yield high values, which complicate calculations for determining the probabilities of loan repayment. Therefore, it is suggested to reduce our coefficients, let’s say, by 25,000 times. This transformation in coefficients will not change the decision regarding the loan issuance. Let’s remember this point for the future, and now, to clarify what we are talking about, let’s consider the situation with three potential borrowers. Table 1 "Potential Borrowers"

Code to generate the table

Chewing over logistic regression

import pandas as pdr = 25000.0 w_0 = -5000.0/r w_1 = 1.0/r w_2 = -3.0/rdata = {'The borrower':np.array(['Vasya', 'Fedya', 'Lesha']), 'Salary':np.array([120000,180000,210000]), 'Payment':np.array([3000,50000,70000])}df = pd.DataFrame(data)df['f(w,x)'] = w_0 + df['Salary']*w_1 + df['Payment']*w_2decision = [] for i in df['f(w,x)']: if i > 0: dec = 'Approved' decision.append(dec) else: dec = 'Refusal' decision.append(dec) df['Decision'] = decisiondf[['The borrower', 'Salary', 'Payment', 'f(w,x)', 'Decision']]

According to the data in the table, Vasya, with a salary of 120,000 RUB, wants to take a loan that he will repay at 3,000 RUB monthly. We have determined that for the loan to be approved, Vasya's salary must be three times greater than the payment, and he should still have 5,000 RUB left. This requirement is satisfied by Vasya:

According to the data in the table, Vasya, with a salary of 120,000 RUB, wants to obtain a loan such that his monthly repayment is 3,000 RUB. We have determined that to approve the loan, Vasya's salary must exceed three times the payment amount and still leave him with 5,000 RUB. Vasya meets this requirement: Chewing over logistic regressionThere remains even 106,000 R. Despite the fact that we reduced the coefficients Chewing over logistic regression by 25,000 times, we obtained the same result — the loan can be approved. Fedya will also receive a loan, while Lesha, despite earning the most, will have to moderate his appetites. Chewing over logistic regression Let's draw a graph on this occasion.

Graph 2 "Classification of Borrowers"

Code to draw the graph

Chewing over logistic regression

salary = np.arange(60000,240000,20000) payment = (-w_0-w_1*salary)/w_2fig, axes = plt.subplots(figsize=(14,6), dpi=80) plt.plot(salary, payment, color='grey', lw=2, label='$f(w,x_i)=w_0 + w_1x_{i1} + w_2x_{i2}$') plt.plot(df[df['Decision'] == 'Approved']['Salary'], df[df['Decision'] == 'Approved']['Payment'], 'o', color='green', markersize=12, label='Decision - Loan approved') plt.plot(df[df['Decision'] == 'Refusal']['Salary'], df[df['Decision'] == 'Refusal']['Payment'], 's', color='red', markersize=12, label='Decision - Loan refusal') plt.xlabel('Salary', size=16) plt.ylabel('Payment', size=16) plt.legend(prop={'size': 14}) plt.show()

So, our line, built according to the function

, separates "bad" borrowers from "good" ones. Borrowers whose desires do not match their capabilities are above the line (Lesha), while those capable of repaying the loan according to our model parameters are below the line (Vasya and Fedya). In other words, our line divides borrowers into two classes. We will denote them as follows: we will assign to the class Chewing over logistic regressionthose borrowers who are most likely to repay the loan, and to the class Chewing over logistic regression those borrowers who are most likely unable to repay the loan. Chewing over logistic regression or Chewing over logistic regression Let's generalize the conclusions from this simple example. Let's take a point

and, by substituting the coordinates of the point into the corresponding equation of the line Chewing over logistic regression , consider three options: Chewing over logistic regressionIf the point is below the line, and we classify it as belonging to class

  1. , then the value of the function Chewing over logistic regressionwill be positive from Chewing over logistic regression . Thus, we can consider that the probability of loan repayment is within Chewing over logistic regression up to Chewing over logistic regression. The higher the value of the function, the greater the probability. Chewing over logistic regressionIf the point is above the line and we classify it as belonging to class
  2. , the value of the function will be negative from Chewing over logistic regression or Chewing over logistic regression. Then we will consider that the probability of repayment is within Chewing over logistic regression up to Chewing over logistic regressionand the greater the absolute value of the function, the higher our confidence. Chewing over logistic regression The point lies on the line, on the boundary between the two classes. In this case, the value of the function
  3. will be equal to Chewing over logistic regression and the probability of loan repayment is Chewing over logistic regression Now, imagine that we have not two factors, but dozens, and instead of three borrowers, we have thousands. Then instead of a line, we will have Chewing over logistic regression.

Now, let’s imagine that we have not two factors, but dozens, and instead of three borrowers, we have thousands. Then instead of a line, we will have Logit plane and coefficients Chewing over logistic regression we will use values derived from well-established rules based on accumulated data about borrowers, whether they repaid or defaulted on their loans. Indeed, notice that we are currently selecting borrowers using already known coefficients Chewing over logistic regression. The task of the logistic regression model is precisely to determine the parameters Chewing over logistic regression, at which the loss function value Logistic Loss, will tend to a minimum. But how the vector is calculated Chewing over logistic regression, we will learn in Section 5 of the article. For now, let's return to the Promised Land — to our banker and his three clients.

Thanks to the function Chewing over logistic regression we know who can be granted a loan and who should be denied. But we cannot approach the director with such information, because they wanted us to provide the probability of each borrower repaying the loan. What to do? The answer is simple — we need to somehow transform the function Chewing over logistic regression, with values lying in the range Chewing over logistic regression to a function that will have values in the range Chewing over logistic regression. Such a function exists; it is called the logistic response function or the inverse logit transformation. Meet:

Chewing over logistic regression

Let's look at the steps of how the logistic response functionis obtained. Note that we will proceed in reverse, i.e., we will assume that we know the probability value, which lies within the limits of Chewing over logistic regression up to Chewing over logistic regression , and then we will 'unwind' this value across the entire number range from Chewing over logistic regression up to Chewing over logistic regression.

03. We derive the logistic response function.

Step 1. We will translate the probability values into the range Chewing over logistic regression

During the function transformation, Chewing over logistic regression downward API support (simultaneously with this in the logistic response function. Chewing over logistic regression we will leave our credit analyst alone and instead take a look at the bookmakers. No, of course, we won't be placing bets; all we are interested in is the meaning of the expression, for example, a chance of 4 to 1. The odds, familiar to all bettors, represent a ratio of 'successes' to 'failures.' From a probability perspective, odds are the probability of an event occurring divided by the probability that the event does not occur. Let's write the formula for the odds of an event occurring: Chewing over logistic regression:

Chewing over logistic regression

, where Chewing over logistic regression — the probability of the event occurring, Chewing over logistic regression — the probability of the event NOT occurring

For example, if the probability of a young, strong, and agile horse nicknamed 'Wind' outpacing an old, flabby mare named 'Matilda' is Chewing over logistic regression, the odds of 'Wind' succeeding will be Chewing over logistic regression to Chewing over logistic regression Chewing over logistic regression , and conversely, knowing the odds allows us to easily calculate the probability Chewing over logistic regression:

Chewing over logistic regression

. Thus, we have learned to 'translate' probability into odds, which can take values from Chewing over logistic regression up to Chewing over logistic regression. Let's take another step and learn to 'translate' probability across the entire number line from Chewing over logistic regression up to Chewing over logistic regression.

Step 2. We will translate probability values into the range Chewing over logistic regression

This step is very simple — we will logarithmically transform the odds with base Euler's number Chewing over logistic regression and obtain:

Chewing over logistic regression

Now we know that if Chewing over logistic regression, calculating the value of Chewing over logistic regression will be very straightforward and, moreover, it should be positive: Chewing over logistic regression. Indeed, it is.

Out of curiosity, let's check what happens if Chewing over logistic regression, then we expect to see a negative value Chewing over logistic regression. Let's verify: Chewing over logistic regression. That's correct.

Now we know how to translate the probability value from Chewing over logistic regression up to Chewing over logistic regression across the entire number line from Chewing over logistic regression up to Chewing over logistic regression. In the next step, we will do everything in reverse.

For now, let's note that according to the rules of logarithmization, knowing the value of the function Chewing over logistic regression, we can calculate the odds:

Chewing over logistic regression

This method of determining odds will be useful in the next step.

Step 3. Let's derive the formula for determining Chewing over logistic regression

So, we have learned to find the values of the function Chewing over logistic regression, knowing that Chewing over logistic regression. However, what we actually need is precisely the opposite — knowing the value of Chewing over logistic regression to find Chewing over logistic regression. For this, we will turn to the concept of the inverse odds function, according to which:

Chewing over logistic regression

In this article, we will not derive the aforementioned formula, but will check with the numbers from the earlier example. We know that with odds of 4 to 1 (Chewing over logistic regression), the probability of the event occurring is 0.8 (Chewing over logistic regression). Let's substitute: Chewing over logistic regression. This matches our calculations performed earlier. Let's move forward.

In the previous step, we derived that Chewing over logistic regression, so we can make a substitution in the inverse odds function. We get:

Chewing over logistic regression

Dividing both the numerator and denominator by Chewing over logistic regression, then:

Chewing over logistic regression

Just in case, to ensure we haven't made any mistakes, let's perform one more small check. In Step 2, we determined that Chewing over logistic regression . Then, substituting the value Chewing over logistic regressioninto the logistic response function, we expect to get Chewing over logistic regression . Substituting it in, we get: Chewing over logistic regression. Chewing over logistic regression

Congratulations, dear reader, we have just derived and tested the logistic function. Let's take a look at the function graph.

Figure 3 "Logistic Function"

Chewing over logistic regression

salary = np.arange(60000,240000,20000) payment = (-w_0-w_1*salary)/w_2fig, axes = plt.subplots(figsize=(14,6), dpi=80) plt.plot(salary, payment, color='grey', lw=2, label='$f(w,x_i)=w_0 + w_1x_{i1} + w_2x_{i2}$') plt.plot(df[df['Decision'] == 'Approved']['Salary'], df[df['Decision'] == 'Approved']['Payment'], 'o', color='green', markersize=12, label='Decision - Loan approved') plt.plot(df[df['Decision'] == 'Refusal']['Salary'], df[df['Decision'] == 'Refusal']['Payment'], 's', color='red', markersize=12, label='Decision - Loan refusal') plt.xlabel('Salary', size=16) plt.ylabel('Payment', size=16) plt.legend(prop={'size': 14}) plt.show()

import math

def logit(f):
    return 1/(1+math.exp(-f))

f = np.arange(-7,7,0.05)
p = []

for i in f:
    p.append(logit(i))

fig, axes = plt.subplots(figsize=(14,6), dpi=80)
plt.plot(f, p, color='grey', label='$ 1 / (1+e^{-w^Tx_i})$')
plt.xlabel('$f(w,x_i) = w^Tx_i$', size=16)
plt.ylabel('$p_{i+}$', size=16)
plt.legend(prop={'size': 14})
plt.show()

In the literature, this function can also be referred to as the sigmoid function. The graph clearly shows that the main change in the probability of an object belonging to a class occurs within a relatively small range Chewing over logistic regression, somewhere from Chewing over logistic regression up to Chewing over logistic regression.

I suggest we return to our credit analyst and help him calculate the probability of loan repayments, otherwise he risks not receiving his bonus 🙂

Table 2 "Potential Borrowers"

Chewing over logistic regression

import pandas as pdr = 25000.0 w_0 = -5000.0/r w_1 = 1.0/r w_2 = -3.0/rdata = {'The borrower':np.array(['Vasya', 'Fedya', 'Lesha']), 'Salary':np.array([120000,180000,210000]), 'Payment':np.array([3000,50000,70000])}df = pd.DataFrame(data)df['f(w,x)'] = w_0 + df['Salary']*w_1 + df['Payment']*w_2decision = [] for i in df['f(w,x)']: if i > 0: dec = 'Approved' decision.append(dec) else: dec = 'Refusal' decision.append(dec) df['Decision'] = decisiondf[['The borrower', 'Salary', 'Payment', 'f(w,x)', 'Decision']]

proba = []
for i in df['f(w,x)']:
    proba.append(round(logit(i), 2))
    
df['Probability'] = proba

df[['The borrower', 'Salary', 'Payment', 'f(w,x)', 'Decision', 'Probability']]

So, we have determined the probability of loan repayment. Overall, it seems to hold true.

Indeed, the probability that Vasya will be able to pay the bank 3,000 R per month with a salary of 120,000 R is close to 100%. By the way, we need to understand that the bank can also issue a loan to Lesha if the bank's policies allow, for example, lending to clients with a probability of loan repayment greater than, let's say, 0.3. In such a case, the bank would simply create a larger reserve for potential losses.

It should also be noted that the ratio of salary to payment being no less than 3, and with a buffer of 5,000 R was somewhat arbitrary. Therefore, we couldn’t use the weight vector in its original form. Chewing over logistic regressionWe needed to significantly reduce the coefficients, and in this scenario, we divided each coefficient by 25,000, essentially adjusting the result. This was done specifically to simplify the understanding of the material at the initial stage. In real life, we will need to find the coefficients, not concoct and adjust them. In the following sections of the article, we will derive equations that help in selecting parameters. Chewing over logistic regression.

04. The Method of Least Squares for Determining the Weight Vector Chewing over logistic regression in the logistic function

We are already familiar with such a method for selecting the weight vector Chewing over logistic regressionas a the method of least squares (OLS) So, why shouldn’t we use it in binary classification tasks? Indeed, nothing prevents us from using it. LSMHowever, this method yields less accurate results in classification tasks than. Logistic Loss,There is a theoretical justification for this. Let’s start by looking at a simple example.

Let’s assume our models (using MSE and Logistic Loss,) have already begun weight vector selection. Chewing over logistic regression And we stopped the calculation at some step. It doesn’t matter whether it’s in the middle, at the end, or at the beginning; what’s important is that we already have some weight vector values. Let’s suppose that at this step, the weight vectors Chewing over logistic regression for both models are identical. In that case, let’s take the obtained weights and substitute them into the logistic response function. (Chewing over logistic regression) for some object that belongs to the class Chewing over logistic regression. We will explore two scenarios: when, based on the selected weight vector, our model makes significant errors, and conversely, when the model is very confident that the object belongs to class Chewing over logistic regression. Let’s see what penalties are assigned when using LSM and Logistic Loss,.

Code for calculating penalties based on the chosen loss function.

# Đșласс ĐŸĐ±ŃŠĐ”Đșта
y = 1
# ĐČĐ”Ń€ĐŸŃŃ‚ĐœĐŸŃŃ‚ŃŒ ĐŸŃ‚ĐœĐ”ŃĐ”ĐœĐžŃ ĐŸĐ±ŃŠĐ”Đșта Đș Đșлассу ĐČ ŃĐŸĐŸŃ‚ĐČДтстĐČОО с ĐżĐ°Ń€Đ°ĐŒĐ”Ń‚Ń€Đ°ĐŒĐž w
proba_1 = 0.01

MSE_1 = (y - proba_1)**2
print 'йтраф MSE про ĐłŃ€ŃƒĐ±ĐŸĐč ĐŸŃˆĐžĐ±ĐșĐ” =', MSE_1

# ĐœĐ°ĐżĐžŃˆĐ”ĐŒ Ń„ŃƒĐœĐșцою ĐŽĐ»Ń ĐČŃ‹Ń‡ĐžŃĐ»Đ”ĐœĐžŃ f(w,x) про ОзĐČĐ”ŃŃ‚ĐœĐŸĐč ĐČĐ”Ń€ĐŸŃŃ‚ĐœĐŸŃŃ‚Đž ĐŸŃ‚ĐœĐ”ŃĐ”ĐœĐžŃ ĐŸĐ±ŃŠĐ”Đșта Đș Đșлассу +1 (f(w,x)=ln(odds+))
def f_w_x(proba):
    return math.log(proba/(1-proba)) 

LogLoss_1 = math.log(1+math.exp(-y*f_w_x(proba_1)))
print 'йтраф Log Loss про ĐłŃ€ŃƒĐ±ĐŸĐč ĐŸŃˆĐžĐ±ĐșĐ” =', LogLoss_1

proba_2 = 0.99

MSE_2 = (y - proba_2)**2
LogLoss_2 = math.log(1+math.exp(-y*f_w_x(proba_2)))

print '**************************************************************'
print 'йтраф MSE про ŃĐžĐ»ŃŒĐœĐŸĐč уĐČĐ”Ń€Đ”ĐœĐœĐŸŃŃ‚Đž =', MSE_2
print 'йтраф Log Loss про ŃĐžĐ»ŃŒĐœĐŸĐč уĐČĐ”Ń€Đ”ĐœĐœĐŸŃŃ‚Đž =', LogLoss_2

The case of a gross error — the model classifies the object as belonging to class Chewing over logistic regression with a probability of 0.01.

The penalty when using LSM will be:
Chewing over logistic regression

The penalty when using Logistic Loss, will be:
Chewing over logistic regression

The case of strong confidence — the model classifies the object as belonging to class Chewing over logistic regression with a probability of 0.99.

The penalty when using LSM will be:
Chewing over logistic regression

The penalty when using Logistic Loss, will be:
Chewing over logistic regression

This example illustrates well that in the case of a gross error, the loss function Log Loss penalizes the model much more heavily than. MSENow let’s understand the theoretical prerequisites for using the loss function Log Loss in classification tasks.

05. Maximum Likelihood Method and Logistic Regression

As promised at the beginning, the article abounds with simple examples. Here’s another example with our old guests — bank borrowers: Vasya, Fedia, and Lesha.

Just in case, before developing the example, let me remind you that in real life we deal with a training dataset of thousands or millions of objects with dozens or hundreds of features. However, the numbers here are chosen to be easily manageable for a novice data scientist.

Let's return to the example. Imagine that the bank director decided to grant loans to everyone in need, despite the algorithm suggesting not to issue one to Alex. Time has passed, and we know who among the three heroes repaid the loan and who did not. As expected, Vasya and Fedya repaid the loan, while Alex did not. Now, let’s consider that this outcome will serve as our new training sample, while all data regarding the factors influencing loan repayment probability (borrower's salary, size of the monthly payment) have seemingly disappeared. Intuitively, we might assume that every third borrower does not return the loan or, in other words, the probability of the next borrower repaying the loan. Chewing over logistic regressionThis intuitive assumption has theoretical confirmation and is based on the method of maximum likelihood, often referred to in literature as the principle of maximum likelihood..

First, let's get acquainted with the conceptual apparatus.

The likelihood of the sample is the probability of obtaining exactly this sample, obtaining exactly such observations/results, i.e., the product of the probabilities of obtaining each of the results of the sample (for example, whether the loan was repaid or not by Vasya, Fedya, and Alex simultaneously).

The likelihood function links the sample likelihood with the values of the distribution parameters.

In our case, the training sample represents a generalized Bernoulli scheme, in which the random variable takes only two values: Chewing over logistic regression or Chewing over logistic regression. Therefore, the likelihood of the sample can be expressed as a likelihood function of the parameter Chewing over logistic regression as follows:

Chewing over logistic regression
Chewing over logistic regression

The above expression can be interpreted as follows. The joint probability that Vasya and Fedya will repay the loan is equal to Chewing over logistic regression, the probability that Alex did NOT repay the loan is equal to Chewing over logistic regression (since it was specifically the case of NOT repaying the loan), thus the joint probability of all three events is equal to Chewing over logistic regression.

Maximum Likelihood Method — this is a method for estimating an unknown parameter by maximizing the likelihood function. In our case, it is necessary to find such a value Chewing over logistic regression, at which Chewing over logistic regression achieves its maximum.

Where does the idea of searching for the value of an unknown parameter, at which the likelihood function reaches its maximum, come from? The origins of this idea stem from the understanding that a sample is the only source of knowledge we have about the population. Everything we know about the population is represented in the sample. Therefore, all we can say is that the sample is the most accurate reflection of the population available to us. Consequently, we need to find such a parameter at which the given sample becomes the most probable.

It is evident that we are dealing with an optimization problem in which we need to find the extremum point of the function. To find this extremum point, it is necessary to consider the first-order condition, that is, to equate the derivative of the function to zero and solve the equation for the unknown parameter. However, searching for the derivative of a product involving many factors can be a lengthy process; to avoid this, there is a special technique — transitioning to the logarithm. the likelihood function. Why is such a transition possible? Let's note that we are not looking for the extremum of the function itself,Chewing over logistic regression, but rather the extremum point, that is, the value of the unknown parameter Chewing over logistic regression, at which Chewing over logistic regression that reaches its maximum. When transitioning to the logarithm, the extremum point does not change (although the extremum itself will differ) since the logarithm is a monotonic function.

Let us continue to develop our example with loans from Vasya, Fedya, and Lesha, following the above. Let's transition to the logarithm of the likelihood function.:

Chewing over logistic regression

Now we can easily differentiate the expression with respect to Chewing over logistic regression:

Chewing over logistic regression

And finally, let us consider the first-order condition — we set the derivative of the function to zero:

Chewing over logistic regression

Thus, our intuitive estimate of the probability of loan repayment Chewing over logistic regression has been theoretically justified.

Great, but what do we do with this information now? If we assume that every third borrower will not repay the bank, it will inevitably go bankrupt. It sounds reasonable, but when estimating the probability of loan repayment as equal to Chewing over logistic regression We did not consider the factors affecting the loan repayment: the borrower's salary and the size of the monthly payment. Recall that earlier we calculated the probability of loan repayment for each client, taking these very factors into account. It is logical that our probabilities turned out to be different from the constant value of Chewing over logistic regression.

Let's define the likelihood of the samples:

Code for calculating the likelihood of the samples

from functools import reduce

def likelihood(y,p):
    line_true_proba = []
    for i in range(len(y)):
        ltp_i = p[i]**y[i]*(1-p[i])**(1-y[i])
        line_true_proba.append(ltp_i)
    likelihood = []
    return reduce(lambda a, b: a*b, line_true_proba)
        
    
y = [1.0,1.0,0.0]
p_log_response = df['Probability']
const = 2.0/3.0
p_const = [const, const, const]


print 'Sample likelihood at constant p=2/3:', round(likelihood(y,p_const),3)

print '****************************************************************************************************'

print 'Sample likelihood at calculated p value:', round(likelihood(y,p_log_response),3)

The likelihood of the sample at a constant value Chewing over logistic regression:

Chewing over logistic regression

The likelihood of the sample when calculating the loan repayment probability considering the factors Chewing over logistic regression:

Chewing over logistic regression
Chewing over logistic regression

The likelihood of the sample, with the probability calculated based on the factors, turned out to be higher than the likelihood at the constant probability value. What does this mean? It means that knowledge of the factors allowed for a more accurate selection of the loan repayment probability for each client. Therefore, when issuing another loan, it would be more appropriate to use the model for estimating repayment probability proposed at the end of Section 3 of the article.

But then, if we need to maximize the likelihood function of the sample, why not use some algorithm that would provide probabilities for Vasya, Fedya, and Lesha, for example, equal to 0.99, 0.99, and 0.01, respectively? Such an algorithm might perform well on the training sample, as it would bring the value of the sample likelihood closer to Chewing over logistic regression, but, firstly, such an algorithm is likely to have difficulties with generalization ability; secondly, this algorithm will definitely not be linear. And if methods for combating overfitting (as well as weak generalization ability) are clearly not part of this article's plan, then let’s delve into the second point in more detail. To do this, it's enough to answer a simple question. Can the probability of loan repayment for Vasya and Fedya be the same, considering the factors we know? From a commonsense perspective, of course not, it cannot. Thus, Vasya will be paying 2.5% of his salary monthly towards the loan, while Fedya will be paying almost 27.8%. Also, in Figure 2 'Customer Classification,' we see that Vasya is significantly further from the decision boundary than Fedya. Finally, we know that the function Chewing over logistic regression for Vasya and Fedya takes different values: 4.24 for Vasya and 1.0 for Fedya. If, for example, Fedya earned an order of magnitude more or asked for a smaller loan, then the probabilities of loan repayment for Vasya and Fedya would be similar. In other words, you can't deceive a linear dependence. And if we had indeed calculated the coefficients Chewing over logistic regression, rather than pulled them out of thin air, we could confidently say that our values Chewing over logistic regression are best suited to estimate the probability of loan repayment by each borrower; however, since we have agreed to assume that the determination of the coefficients Chewing over logistic regression was carried out according to all the rules, we will maintain that our coefficients provide the best estimate of the probability 🙂

However, we got sidetracked. In this section, we need to figure out how the weight vector is determined Chewing over logistic regression, which is necessary for assessing the probability of loan return by each borrower.

In short, let's summarize what arsenal we are bringing to the search for coefficients Chewing over logistic regression:

1. We assume that the relationship between the target variable (predicted value) and the factor influencing the result is linear. For this reason, a model of the type , used in is applied, the line of which divides the objects (clients) into classes Chewing over logistic regression(clients capable of repaying the loan and those who are not). In our case, the equation looks like Chewing over logistic regression and Chewing over logistic regression or Chewing over logistic regression 2. We use Chewing over logistic regression.

the inverse logit transformation function to determine the probability of an object belonging to a class is applied, the line of which divides the objects (clients) into classes Chewing over logistic regression 3. We consider our training sample as a realization of the generalized Chewing over logistic regression.

3. We consider our training sample as the realization of a generalized Bernoulli schemes, meaning that for each object a random variable is generated, which with a probability of Chewing over logistic regression (its own for each object) takes the value of 1 and with a probability of Chewing over logistic regression – 0.

4. We know that we need to maximize the likelihood function of the sample given the accepted factors so that the existing sample becomes the most plausible. In other words, we need to find parameters such that the sample will be the most plausible. In our case, the parameter being optimized is the probability of loan repayment Chewing over logistic regression, which in turn depends on unknown coefficients Chewing over logistic regression. Therefore, we need to find such a weight vector Chewing over logistic regression, for which the likelihood of the sample will be maximized.

5. We know that to maximize the likelihood function of the sample you can use maximum likelihood method. And we know all the clever tricks for working with this method.

This is quite a complex situation 🙂

Now let’s remember that at the very beginning of the article we wanted to derive two types of loss functions Logistic Loss, depending on how the object classes are designated. It is customary in classification tasks with two classes to denote the classes as Chewing over logistic regression and Chewing over logistic regression or Chewing over logistic regression. Depending on the designation, the corresponding loss function will be produced.

Case 1. Classification of objects into Chewing over logistic regression and Chewing over logistic regression

Previously, when determining the sample's likelihood, where the probability of default by the borrower was calculated based on factors and given coefficients Chewing over logistic regression, we applied the formula:

Chewing over logistic regression

In fact, Chewing over logistic regression — this is the value of the logistic response function Chewing over logistic regression given a specified weight vector Chewing over logistic regression

Then nothing prevents us from writing the likelihood function of the sample as follows:

Chewing over logistic regression

Sometimes it happens that some novice analysts find it difficult to immediately understand how this function works. Let's consider 4 brief examples that will clarify everything:

1. If Chewing over logistic regression (i.e., according to the training sample, the object belongs to class +1), while our algorithm Chewing over logistic regression determines the probability of the object belonging to the class Chewing over logistic regression as 0.9, then this piece of the sample's likelihood will be calculated as follows:

Chewing over logistic regression

2. If Chewing over logistic regression, and Chewing over logistic regression, then the calculation will be as follows:

Chewing over logistic regression

3. If Chewing over logistic regression, and Chewing over logistic regression, then the calculation will be as follows:

Chewing over logistic regression

4. If Chewing over logistic regression, and Chewing over logistic regression, then the calculation will be as follows:

Chewing over logistic regression

It is obvious that the likelihood function will be maximized in cases 1 and 3, or in general — when the probabilities of an object belonging to the class are correctly guessed. Chewing over logistic regression.

Due to the fact that when determining the probability of assigning an object to a class Chewing over logistic regression We only do not know the coefficients Chewing over logistic regression, so we will look for them. As mentioned above, this is an optimization task, where initially we need to find the derivative of the likelihood function with respect to the weight vector Chewing over logistic regression. However, it makes sense to simplify the task beforehand: we will seek the derivative of the logarithm the likelihood function.

Chewing over logistic regression

Why after logarithmization, in the logistic loss function, we changed the sign from Chewing over logistic regression to Chewing over logistic regression. It's simple, because in tasks involving model quality assessment it is customary to minimize the value of the function, so we multiplied the right side of the expression by Chewing over logistic regression and accordingly, instead of maximizing, we now minimize the function.

Actually, right now, in front of your eyes, a loss function has been painfully derived — Logistic Loss, for the training sample with two classes: Chewing over logistic regression and Chewing over logistic regression.

Now, to find the coefficients, we just need to find the derivative the logistic loss function and then, using numerical optimization methods like gradient descent or stochastic gradient descent, find the most optimal coefficients Chewing over logistic regression. But, considering the already significant volume of the article, it is suggested to perform differentiation independently or, perhaps, this will be the topic for the next article with a lot of arithmetic without such detailed examples.

Case 2. Classification of objects in Chewing over logistic regression and Chewing over logistic regression

The approach here will be the same as with classes Chewing over logistic regression and Chewing over logistic regression, but the path to deriving the loss function Logistic Loss,, will be more convoluted. Let's get started. We will use the operator for the likelihood function "if..., then...". That is, if Chewing over logistic regressionthe object belongs to class Chewing over logistic regression, then for calculating the likelihood of the sample we use the probability Chewing over logistic regression, if the object belongs to class Chewing over logistic regression, then we substitute Chewing over logistic regressioninto the likelihood. This is what the likelihood function looks like:

Chewing over logistic regression

In simple terms, let's outline how this works. Consider 4 cases:

1. If Chewing over logistic regression and Chewing over logistic regression, then in the likelihood of the sample it will be Chewing over logistic regression

2. If Chewing over logistic regression and Chewing over logistic regression, then in the likelihood of the sample it will be Chewing over logistic regression

3. If Chewing over logistic regression and Chewing over logistic regression, then in the likelihood of the sample it will be Chewing over logistic regression

4. If Chewing over logistic regression and Chewing over logistic regression, then in the likelihood of the sample it will be Chewing over logistic regression

It is obvious that in cases 1 and 3, when the probabilities were correctly defined by the algorithm, the likelihood function will be maximized, which is exactly what we wanted to achieve. However, this approach is quite cumbersome, and we will consider a more compact notation afterwards. But first, let's logarithmically transform the likelihood function by changing the sign since we will minimize it now.

Chewing over logistic regression

Instead of substituting Chewing over logistic regression the expression Chewing over logistic regression:

Chewing over logistic regression

Let's simplify the right summand under the logarithm using basic arithmetic techniques and get:

Chewing over logistic regression

Now it's time to get rid of the operator "if..., then...". Note that when an object Chewing over logistic regression belongs to the class Chewing over logistic regression, in the expression under the logarithm, in the denominator, Chewing over logistic regression is raised to the power Chewing over logistic regression, if the object belongs to class Chewing over logistic regression, then $e$ is raised to the power Chewing over logistic regression. Therefore, the power expression can be simplified — we can combine both cases into one: Chewing over logistic regression. Thus, the logistic loss function will take the form:

Chewing over logistic regression

According to the logarithmic rules, we will invert the fraction and take the "Chewing over logistic regression" (minus) out of the logarithm, resulting in:

Chewing over logistic regression

Before you is the loss function logistic Loss, which is applied in the training dataset with objects belonging to the classes: Chewing over logistic regression and Chewing over logistic regression.

Well, at this point, I bid you farewell and we conclude the article.

Chewing over logistic regression The author's previous work — "Bringing the Linear Regression Equation into Matrix Form"

Supplementary materials

1. Literature

1) Applied Regression Analysis / N. Draper, G. Smith – 2nd ed. – Moscow: Finance and Statistics, 1986 (translation from English)

2) Probability Theory and Mathematical Statistics / V.E. Gmurman — 9th ed. — Moscow: Higher School, 2003

3) Probability Theory / N.I. Chernova — Novosibirsk: Novosibirsk State University, 2007

4) Business Analytics: From Data to Knowledge / Paklin N.B., Oreshkov V.I. — 2nd ed. — St. Petersburg: Peter, 2013

5) Data Science From Scratch / Joel Grus — St. Petersburg: BHV Peterburg, 2017

6) Practical Statistics for Data Science Specialists / P.Bryce, E.Bryce — St. Petersburg: BHV Peterburg, 2018

2. Lectures, courses (videos)

1) The essence of the maximum likelihood method, Boris Demeshev

2) Maximum likelihood method in the continuous case, Boris Demeshev

3) Logistic regression. Open ODS course, Yury Kashnitsky

4) Lecture 4, Evgeny Sokolov (from 47 minutes into the video)

5) Logistic regression, Vyacheslav Vorontsov

3. Internet sources

1) Linear classification and regression models

2) How to easily understand logistic regression

3) Logistic error function

4) Independent trials and Bernoulli's formula

5) Ballad about MMP

6) Maximum Likelihood Method

7) Formulas and properties of logarithms

8) Why the number Chewing over logistic regression?

9) Linear classifier

Source: habr.com

Buy reliable website hosting with DDoS protection, VPS VDS servers đŸ”„ Buy reliable website hosting with DDoS protection, VPS VDS servers | ProHoster