Processing math: 100%

Sunday, August 19, 2012

Partial Derivative Logistic Regression Cost Function

Logistic regression is used for classification problems.  As Andrew said, it's a bit confusing given the "regression" in the name.

LR cost function is given by:
Cost(hθ(x),y)={log(hθ(x))if y=1log(1hθ(x))if y=0


where hθ(x)=11+eθTx is the logistic function.

Since y{0,1} only, we can reduce the cost function to an equivalent, single equation.
Cost(hθ(x),y)=ylog(hθ(x))(1y)log(1hθ(x))


This leads to the overall cost function for the logistic regression:
J(θ)=1m[mi=1y(i)loghθ(x(i))+(1y(i))log(1hθ(x))]


Our goal is to find minθJ(θ). To do so, we use gradient descent, but we first need to find the partial derivatives θjJ(θ).

We're going to make use of a neat property of the logistic function:
g(z)=ddz11+ez=1(1+ez)2ez=1+ez1(1+ez)2=11+ez1(1+ez)2=11+ez(111+ez)=g(z)(1g(z))


So for our with our cost function:
θjJ(θ)=1m[θjmi=1y(i)loghθ(x(i))+(1y(i))log(1hθ(x))]=1m[mi=1y(i)1hθ(x(i))θjhθ(x(i))+(1y(i))11hθ(x(i))(θjhθ(x(i)))]



using the chain rule and the logistic regression derivative, we see that
θjJ(θ)=1m[mi=1y(i)x(i)jhθ(x(i))hθ(x(i))(1hθ(x(i)))(1y(i))x(i)j1hθ(x(i))hθ(x)(1hθ(x(i)))]=1m[mi=1y(i)x(i)j(1hθ(x(i)))(1y(i))x(i)jhθ(x(i)))]=1m[mi=1y(i)x(i)jxjhθ(x(i))]θjJ(θ)=1mmi=1(hθ(x(i))y(i))x(i)j

This formula can now be used in gradient descent.


13 comments:

  1. Thank you! It is very useful for me!

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Fuck me, I don't understand the calculus :(

    ReplyDelete
    Replies
    1. which part of the calculus is unclear?

      Delete
    2. the d/dx thing, isn't it 1/x if the d gets canceled out?

      Delete
  4. its really good work, thank you so much

    ReplyDelete
  5. Truly appreciate your effort , well formatted and explained. Thank you!

    ReplyDelete
  6. Can someone explain how chain rule is implemented?

    ReplyDelete
  7. Awesome, Thanks you guys. I was confused on this for a while , it's really a great help to me.

    ReplyDelete
  8. hello, Thanks for this post, How did you come about the Xj ?

    ReplyDelete