Calculus for ML: Derivatives Part - 01
Tags: beginners, machinelearning, learning
Published: 2024-05-16 12:54:08

Do you know calculus plays an important role in ML? I am going to show you how derivates performs a vital role to make the most accurate model for you. Let's start !! 

First let's have a look at some derivatives. 

**Calculating a slope: **

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cd3ym8xi8pfaiqpns6e7.png)

slope = rise/run 
      = change in x axis/change in y axis
      = ∆x/∆y 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dw83lbef8mkdbgwiu6z4.png)
Here, the slope is going to be - 
     slope =  ∆x/∆t 
           = x(15) - x(10) / t(15) - t(10)
           = 202-122 / 15-10
           = 16 m/s 

Now, I ask you a question. At t = 12.5. what is the velocity? If not the exact velocity, find me a good estimate. 
Let's find out the slope, 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e759x1vixvkrjubqya2v.png)

                slope = ∆x/∆t
                      = (177-155)/(13-12) = 15 m/s 
But this is just a roughly value. We still don't know the exact estimate value of v at t=12.5s. To find this, we need finer and finer intervals. The more finer, the more accurate answer it is, and this is where the importance of derivate comes. 

**1. Derivatives and Tangents**
A tangent is a line that touches a curve at only one point. The line points out the slope of the tangent to that point. A derivative of a function gives you the gradient of a tangent at a certain point on a curve. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/883tz2epbteog9hmfkqg.png)
Look, how the intervals are coming closer as we are getting smaller to smaller. This gets so close that we can't even find the difference. These are called instantaneous rate of change aka the derivatives. 

**2. Derivative of a line** 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cbgnam602chjlp5w8oqs.png)
slope = ∆y/∆x 
      = rise/run
      = a(x+∆x)+b-(ax+b) / x+∆x-x 
      = a∆x/∆x = a
So, the derivative of the eq(ax+b) is the slope a. 

**3. Derivative of Quadratic Function :**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vv1zzyfu7bbz5kiyhgs8.png)

slope = ∆f/∆x = {f(x+∆x)^2 - f(x)^2} / ∆x 
Between (1,1) and (1.5, 2.25) - 
∆x = 2-1 = 1 
[∆f = 4-1 = 3 ; y2 - y1] 
∆f = (x+∆x)^2 - (x)^2 
=> (1+1)^2 - 1 => 4-1 = 3
so, slope = 3 

Between (1,1) and (1.5, 2.25)- 
∆x = 1.5 - 1 = 0.5
∆f = (x+∆x)^2 - (x)^2 
= (1+0.5)^2 - 1 = 1.25 
slope = 1.25/0.5 = 2.5 

Even smaller: (1,1) and (1.25, 1.56) - 
∆x = 1.25-1 = 0.25 
∆f = (x+∆x)^2 - (x)^2 = (1+0,25)^2 - 1 = 0.56
slope = 0.56 / 0.25 = 2.25

Again, d/dx(f) = {f(x+∆x)^2 - f(x)^2} / ∆x 
= (x^2 + 2x∆x + ∆x^2)/ ∆x = 2x + ∆x 

So, f(x) = x^2 => f'(x) = 2x 

**4. Derivative of Cubic function**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dvon4ptlvrmyu0m8jf3p.png)

slope = ∆f/∆x = {f(x+∆x)^3 - f(x)}/∆x
If you apply the cubic formula, it will end at 3x^2 + 3x∆x + ∆x^2. 
As, ∆x -> 0, so, f'(y) = 3x^2 

**5. Derivative of 1/x**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/579d843b2d1z9i1hwt9w.png)

Here, i am going to make it short - 
slope = ∆f/∆x = {f(x+∆x)^(-1) - f(x)}/∆x 

f(x) = x^(-1)
=> f'(x) = -x^(2) 

I hope you noticed that i basically followed the rule **nx^(n-1)**