@@ -3,27 +3,46 @@ export RobustLoss,
33 BisquareRho, Bisquare, LogisticRho, Logistic,
44 FairRho, Fair, TalwarRho, Talwar, QuantileRho, Quantile
55
6+ #=
7+ In the non-penalised case:
8+
9+ β⋆ = arg min ∑ ρ(yᵢ - ⟨xᵢ, β⟩)
10+
11+ where ρ is a weighing function such as, for instance, the pinball loss for
12+ the quantile regression.
13+
14+ It is useful to define the following quantities:
15+
16+ ψ(r) = ρ'(r) (first derivative)
17+ ϕ(r) = ψ'(r) (second derivative)
18+ ω(r) = ψ(r)/r (weighing function used for IWLS), a threshold can be passed
19+ to clip weights
20+
21+ Some refs:
22+ - https://josephsalmon.eu/enseignement/UW/STAT593/QuantileRegression.pdf
23+ =#
24+
625abstract type RobustRho end
726
8- abstract type RobustRho1P{δ} <: RobustRho end # one parameter
27+ # robust rho with only one parameter
28+ abstract type RobustRho1P{δ} <: RobustRho end
929
1030struct RobustLoss{ρ} <: AtomicLoss where ρ <: RobustRho
1131 rho: :ρ
1232end
1333
14- (rl:: RobustLoss )(x :: AVR , y:: AVR ) = rl (x .- y )
15- (rl:: RobustLoss )(r:: AVR ) = rl. rho (r)
34+ (rl:: RobustLoss )(Xβ :: AVR , y:: AVR ) = rl (y .- Xβ )
35+ (rl:: RobustLoss )(r:: AVR ) = rl. rho (r)
1636
17- # ψ(r) = ρ'(r) (first derivative)
18- # ω(r) = ψ(r)/r (weighing function) a thresh can be passed to clip weights
19- # ϕ(r) = ψ'(r) (second derivative)
2037
2138"""
2239$TYPEDEF
2340
2441Huber weighing of the residuals corresponding to
2542
2643``ρ(z) = z²/2`` if `|z|≤δ` and `ρ(z)=δ(|z|-δ/2)` otherwise.
44+
45+ Note: symmetric weighing.
2746"""
2847struct HuberRho{δ} <: RobustRho1P{δ}
2948 HuberRho (δ:: Real = 1.0 ; delta:: Real = δ) = new {delta} ()
@@ -33,7 +52,7 @@ Huber(δ::Real=1.0; delta::Real=δ) = HuberRho(delta)
3352(:: HuberRho{δ} )(r:: AVR ) where δ = begin
3453 ar = abs .(r)
3554 w = ar .<= δ
36- return sum ( r . ^2 / 2 .* w .+ δ . * (ar . - δ/ 2 ) .* . ! w )
55+ return sum ( @. ifelse (w, r ^ 2 / 2 , δ * (ar - δ/ 2 ) ) )
3756end
3857
3958ψ (:: Type{HuberRho{δ}} ) where δ = (r, w) -> r * w + δ * sign (r) * (1.0 - w)
@@ -47,6 +66,8 @@ $TYPEDEF
4766Andrews weighing of the residuals corresponding to
4867
4968``ρ(z) = -cos(πz/δ)/(π/δ)²`` if `|z|≤δ` and `ρ(δ)` otherwise.
69+
70+ Note: symmetric weighing.
5071"""
5172struct AndrewsRho{δ} <: RobustRho1P{δ}
5273 AndrewsRho (δ:: Real = 1.0 ; delta:: Real = δ) = new {delta} ()
@@ -58,7 +79,7 @@ Andrews(δ::Real=1.0; delta::Real=δ) = AndrewsRho(delta)
5879 w = ar .<= δ
5980 c = π/ δ
6081 κ = (δ/ π)^ 2
61- return sum ( - cos . (c . * r) . * κ .* w .+ κ .* . ! w )
82+ return sum ( @. ifelse (w, - cos (c * r) * κ, κ) )
6283end
6384
6485# Note, sinc(x) = sin(πx)/πx, well defined everywhere
@@ -74,6 +95,8 @@ $TYPEDEF
7495Bisquare weighing of the residuals corresponding to
7596
7697``ρ(z) = δ²/6 (1-(1-(z/δ)²)³)`` if `|z|≤δ` and `δ²/6` otherwise.
98+
99+ Note: symmetric weighing.
77100"""
78101struct BisquareRho{δ} <: RobustRho1P{δ}
79102 BisquareRho (δ:: Real = 1.0 ; delta:: Real = δ) = new {delta} ()
@@ -84,7 +107,7 @@ Bisquare(δ::Real=1.0; delta::Real=δ) = BisquareRho(delta)
84107 ar = abs .(r)
85108 w = ar .<= δ
86109 κ = δ^ 2 / 6
87- return sum ( κ * (1.0 . - (1.0 . - (r . / δ). ^ 2 ). ^ 3 ) .* w + κ .* . ! w )
110+ return sum ( @. ifelse (w, κ * (1 - (1 - (r / δ)^ 2 )^ 3 ), κ) )
88111end
89112
90113ψ (:: Type{BisquareRho{δ}} ) where δ = (r, w) -> w * r * (1.0 - (r / δ)^ 2 )^ 2
@@ -97,14 +120,16 @@ $TYPEDEF
97120Logistic weighing of the residuals corresponding to
98121
99122``ρ(z) = δ² log(cosh(z/δ))``
123+
124+ Note: symmetric weighing.
100125"""
101126struct LogisticRho{δ} <: RobustRho1P{δ}
102127 LogisticRho (δ:: Real = 1.0 ; delta:: Real = δ) = new {delta} ()
103128end
104129Logistic (δ:: Real = 1.0 ; delta:: Real = δ) = LogisticRho (delta)
105130
106131(:: LogisticRho{δ} )(r:: AVR ) where δ = begin
107- return sum ( δ^ 2 . * log . (cosh . (r . / δ)) )
132+ return sum ( @. δ^ 2 * log (cosh (r / δ)) )
108133end
109134
110135# similar to sinc, to avoid NaNs if tanh(0)/0 (lim is 1.0)
@@ -121,15 +146,17 @@ $TYPEDEF
121146Fair weighing of the residuals corresponding to
122147
123148``ρ(z) = δ² (|z|/δ - log(1+|z|/δ))``
149+
150+ Note: symmetric weighing.
124151"""
125152struct FairRho{δ} <: RobustRho1P{δ}
126153 FairRho (δ:: Real = 1.0 ; delta:: Real = δ) = new {delta} ()
127154end
128155Fair (δ:: Real = 1.0 ; delta:: Real = δ) = FairRho (delta)
129156
130157(:: FairRho{δ} )(r:: AVR ) where δ = begin
131- sr = abs . (r) . / δ
132- return sum ( δ^ 2 . * (sr . - log1p . (sr)) )
158+ sr = @. abs (r) / δ
159+ return sum ( @. δ^ 2 * (sr - log1p (sr)) )
133160end
134161
135162ψ (:: Type{FairRho{δ}} ) where δ = (r, _) -> δ * r / (abs (r) + δ)
@@ -143,15 +170,17 @@ $TYPEDEF
143170Talwar weighing of the residuals corresponding to
144171
145172``ρ(z) = z²/2`` if `|z|≤δ` and `ρ(z)=ρ(δ)` otherwise.
173+
174+ Note: symmetric weighing.
146175"""
147176struct TalwarRho{δ} <: RobustRho1P{δ}
148177 TalwarRho (δ:: Real = 1.0 ; delta:: Real = δ) = new {delta} ()
149178end
150179Talwar (δ:: Real = 1.0 ; delta:: Real = δ) = TalwarRho (delta)
151180
152181(:: TalwarRho{δ} )(r:: AVR ) where δ = begin
153- w = abs . (r) . <= δ
154- return sum ( r . ^2 . / 2 .* w .+ δ^ 2 / 2 .* . ! w )
182+ w = @. abs (r) <= δ
183+ return sum ( @. ifelse (w, r ^ 2 / 2 , δ^ 2 / 2 ) )
155184end
156185
157186ψ (:: Type{TalwarRho{δ}} ) where δ = (r, w) -> w * r
@@ -164,7 +193,11 @@ $TYPEDEF
164193
165194Quantile regression weighing of the residuals corresponding to
166195
167- ``ρ(z) = z(δ - 1(z<0))``
196+ ``ρ(z) = -z(δ - 1(z>=0))``
197+
198+ Note: asymetric weighing, the "-" sign is because similar libraries like
199+ quantreg for instance define the residual as `y-Xθ` while we do the opposite
200+ (out of convenience for gradients etc).
168201"""
169202struct QuantileRho{δ} <: RobustRho1P{δ}
170203 QuantileRho (δ:: Real = 1.0 ; delta:: Real = δ) = new {delta} ()
173206Quantile (δ:: Real = 1.0 ; delta:: Real = δ) = QuantileRho (delta)
174207
175208(:: QuantileRho{δ} )(r:: AVR ) where δ = begin
176- return sum ( r . * (δ . - (r .<= 0. 0 )) )
209+ return sum ( @. - r * (δ - (r >= 0 )) )
177210end
178211
179- ψ (:: Type{QuantileRho{δ}} ) where δ = (r, _) -> (δ - (r < = 0.0 ))
180- ω (:: Type{QuantileRho{δ}} , τ) where δ = (r, _) -> (δ - (r < = 0.0 )) / clip (r, τ)
212+ ψ (:: Type{QuantileRho{δ}} ) where δ = (r, _) -> ((r > = 0.0 ) - δ )
213+ ω (:: Type{QuantileRho{δ}} , τ) where δ = (r, _) -> ((r > = 0.0 ) - δ ) / clip (- r, τ)
181214ϕ (:: Type{QuantileRho{δ}} ) where δ = (_, _) -> error (" Newton(CG) not available for Quantile Reg." )
0 commit comments