Aquí deduzco todas las propiedades e identidades necesarias para que la solución sea autónoma, pero aparte de eso, esta derivación es limpia y fácil. Formalicemos nuestra notación y escribamos la función de pérdida un poco más compacta. Considere m muestras {xi,yi} tal que xi∈Rd y yi∈R . Recuerde que en la regresión logística binaria típicamente tenemos la función de hipótesis hθ ser la función logística. Formalmente
hθ(xi)=σ(ωTxi)=σ(zi)=11+e−zi,
donde ω∈Rd y zi=ωTxi . La función de pérdida (que creo que a los OP les falta un signo negativo) se define como:
l(ω)=∑i=1m−(yilogσ(zi)+(1−yi)log(1−σ(zi)))
Hay dos propiedades importantes de la función logística que obtengo aquí para referencia futura. Primero, tenga en cuenta que 1−σ(z)=1−1/(1+e−z)=e−z/(1+e−z)=1/(1+ez)=σ(−z) .
También tenga en cuenta que
∂∂zσ(z)=∂∂z(1+e−z)−1=e−z(1+e−z)−2=11+e−ze−z1+e−z=σ(z)(1−σ(z))
En lugar de tomar derivados con respecto a componentes, aquí trabajaremos directamente con vectores (puede revisar derivados con vectores aquí ). La arpillera de la función de pérdida l(ω) viene dada por ∇⃗ 2l(ω) , pero primero recuerde que ∂z∂ω=xTω∂ω=xTy∂z∂ωT=∂ωTx∂ωT=x.
Let li(ω)=−yilogσ(zi)−(1−yi)log(1−σ(zi)). Using the properties we derived above and the chain rule
∂logσ(zi)∂ωT∂log(1−σ(zi))∂ωT=1σ(zi)∂σ(zi)∂ωT=1σ(zi)∂σ(zi)∂zi∂zi∂ωT=(1−σ(zi))xi=11−σ(zi)∂(1−σ(zi))∂ωT=−σ(zi)xi
It's now trivial to show that
∇⃗ li(ω)=∂li(ω)∂ωT=−yixi(1−σ(zi))+(1−yi)xiσ(zi)=xi(σ(zi)−yi)
whew!
Our last step is to compute the Hessian
∇⃗ 2li(ω)=∂li(ω)∂ω∂ωT=xixTiσ(zi)(1−σ(zi))
For m samples we have ∇⃗ 2l(ω)=∑mi=1xixTiσ(zi)(1−σ(zi)). This is equivalent to concatenating column vectors xi∈Rd into a matrix X of size d×m such that ∑mi=1xixTi=XXT. The scalar terms are combined in a diagonal matrix D such that Dii=σ(zi)(1−σ(zi)). Finally, we conclude that
H⃗ (ω)=∇⃗ 2l(ω)=XDXT
A faster approach can be derived by considering all samples at once from the beginning and instead work with matrix derivatives. As an extra note, with this formulation it's trivial to show that l(ω) is convex. Let δ be any vector such that δ∈Rd. Then
δTH⃗ (ω)δ=δT∇⃗ 2l(ω)δ=δTXDXTδ=δTXD(δTX)T=∥δTDX∥2≥0
since D>0 and ∥δTX∥≥0. This implies H is positive-semidefinite and therefore l is convex (but not strongly convex).