@@ -14,6 +14,8 @@ Generalized Linear Regression (GLR) model with objective function:
14
14
15
15
where `L` is a loss function, `P` a penalty, `y` is the vector of observed
16
16
response, `X` is the feature matrix and `θ` the vector of parameters.
17
+ If `scale_penalty_with_samples = true` (default) the penalty is automatically
18
+ scaled with the number of samples.
17
19
18
20
Special cases include:
19
21
@@ -28,6 +30,7 @@ Special cases include:
28
30
penalty:: P = NoPenalty () # P(θ)
29
31
fit_intercept:: Bool = true # add intercept ? def=true
30
32
penalize_intercept:: Bool = false
33
+ scale_penalty_with_samples:: Bool = true
31
34
end
32
35
33
36
const GLR = GeneralizedLinearRegression
@@ -48,44 +51,59 @@ LinearRegression(; fit_intercept::Bool=true) = GLR(fit_intercept=fit_intercept)
48
51
"""
49
52
$SIGNATURES
50
53
51
- Objective function: ``|Xθ - y|₂²/2 + λ|θ|₂²/2``.
54
+ Objective function: ``|Xθ - y|₂²/2 + n⋅λ|θ|₂²/2``,
55
+ where ``n`` is the number of samples `size(X, 1)`.
56
+ With `scale_penalty_with_samples = false` the objective function is
57
+ ``|Xθ - y|₂²/2 + λ|θ|₂²/2``.
52
58
"""
53
59
function RidgeRegression (λ:: Real = 1.0 ; lambda:: Real = λ, fit_intercept:: Bool = true ,
54
- penalize_intercept:: Bool = false )
60
+ penalize_intercept:: Bool = false ,
61
+ scale_penalty_with_samples:: Bool = true )
55
62
check_pos (lambda)
56
63
GLR (penalty= lambda* L2Penalty (),
57
64
fit_intercept= fit_intercept,
58
- penalize_intercept= penalize_intercept)
65
+ penalize_intercept= penalize_intercept,
66
+ scale_penalty_with_samples= scale_penalty_with_samples)
59
67
end
60
68
61
69
62
70
"""
63
71
$SIGNATURES
64
72
65
- Objective function: ``|Xθ - y|₂²/2 + λ|θ|₁``.
73
+ Objective function: ``|Xθ - y|₂²/2 + n⋅λ|θ|₁``,
74
+ where ``n`` is the number of samples `size(X, 1)`.
75
+ With `scale_penalty_with_samples = false` the objective function is
76
+ ``|Xθ - y|₂²/2 + λ|θ|₁``
66
77
"""
67
78
function LassoRegression (λ:: Real = 1.0 ; lambda:: Real = λ, fit_intercept:: Bool = true ,
68
- penalize_intercept:: Bool = false )
79
+ penalize_intercept:: Bool = false ,
80
+ scale_penalty_with_samples:: Bool = true )
69
81
check_pos (lambda)
70
82
GLR (penalty= lambda* L1Penalty (),
71
83
fit_intercept= fit_intercept,
72
- penalize_intercept= penalize_intercept)
84
+ penalize_intercept= penalize_intercept,
85
+ scale_penalty_with_samples= scale_penalty_with_samples)
73
86
end
74
87
75
88
76
89
"""
77
90
$SIGNATURES
78
91
79
- Objective function: ``|Xθ - y|₂²/2 + λ|θ|₂²/2 + γ|θ|₁``.
92
+ Objective function: ``|Xθ - y|₂²/2 + n⋅λ|θ|₂²/2 + n⋅γ|θ|₁``,
93
+ where ``n`` is the number of samples `size(X, 1)`.
94
+ With `scale_penalty_with_samples = false` the objective function is
95
+ ``|Xθ - y|₂²/2 + λ|θ|₂²/2 + γ|θ|₁``
80
96
"""
81
97
function ElasticNetRegression (λ:: Real = 1.0 , γ:: Real = 1.0 ;
82
98
lambda:: Real = λ, gamma:: Real = γ,
83
99
fit_intercept:: Bool = true ,
84
- penalize_intercept:: Bool = false )
100
+ penalize_intercept:: Bool = false ,
101
+ scale_penalty_with_samples:: Bool = true )
85
102
check_pos .((lambda, gamma))
86
103
GLR (penalty= lambda* L2Penalty ()+ gamma* L1Penalty (),
87
104
fit_intercept= fit_intercept,
88
- penalize_intercept= penalize_intercept)
105
+ penalize_intercept= penalize_intercept,
106
+ scale_penalty_with_samples= scale_penalty_with_samples)
89
107
end
90
108
91
109
@@ -114,14 +132,18 @@ end
114
132
"""
115
133
$SIGNATURES
116
134
117
- Objective function: ``L(y, Xθ) + λ|θ|₂²/2 + γ|θ|₁`` where `L` is either the
118
- logistic loss in the binary case or the multinomial loss otherwise.
135
+ Objective function: ``L(y, Xθ) + n⋅λ|θ|₂²/2 + n⋅γ|θ|₁`` where `L` is either the
136
+ logistic loss in the binary case or the multinomial loss otherwise and
137
+ ``n`` is the number of samples `size(X, 1)`.
138
+ With `scale_penalty_with_samples = false` the objective function is
139
+ ``L(y, Xθ) + λ|θ|₂²/2 + γ|θ|₁``.
119
140
"""
120
141
function LogisticRegression (λ:: Real = 1.0 , γ:: Real = 0.0 ;
121
142
lambda:: Real = λ, gamma:: Real = γ,
122
143
penalty:: Symbol = iszero (gamma) ? :l2 : :en ,
123
144
fit_intercept:: Bool = true ,
124
145
penalize_intercept:: Bool = false ,
146
+ scale_penalty_with_samples:: Bool = true ,
125
147
multi_class:: Bool = false ,
126
148
nclasses:: Integer = 0 )
127
149
penalty = _l1l2en (lambda, gamma, penalty, " Logistic regression" )
@@ -134,14 +156,18 @@ function LogisticRegression(λ::Real=1.0, γ::Real=0.0;
134
156
GLR (loss= loss,
135
157
penalty= penalty,
136
158
fit_intercept= fit_intercept,
137
- penalize_intercept= penalize_intercept)
159
+ penalize_intercept= penalize_intercept,
160
+ scale_penalty_with_samples= scale_penalty_with_samples)
138
161
end
139
162
140
163
"""
141
164
$SIGNATURES
142
165
143
- Objective function: ``L(y, Xθ) + λ|θ|₂²/2 + γ|θ|₁`` where `L` is the
144
- multinomial loss.
166
+ Objective function: ``L(y, Xθ) + n⋅λ|θ|₂²/2 + n⋅γ|θ|₁`` where `L` is the
167
+ multinomial loss and
168
+ ``n`` is the number of samples `size(X, 1)`.
169
+ With `scale_penalty_with_samples = false` the objective function is
170
+ ``L(y, Xθ) + λ|θ|₂²/2 + γ|θ|₁``.
145
171
"""
146
172
MultinomialRegression (a... ; kwa... ) =
147
173
LogisticRegression (a... ; multi_class= true , kwa... )
@@ -152,74 +178,94 @@ MultinomialRegression(a...; kwa...) =
152
178
"""
153
179
$SIGNATURES
154
180
155
- Objective function: ``∑ρ(Xθ - y) + λ|θ|₂² + γ|θ|₁`` where ρ is a given function
156
- on the residuals.
181
+ Objective function: ``∑ρ(Xθ - y) + n⋅λ|θ|₂² + n⋅γ|θ|₁`` where ρ is a given function
182
+ on the residuals and
183
+ ``n`` is the number of samples `size(X, 1)`.
184
+ With `scale_penalty_with_samples = false` the objective function is
185
+ ``∑ρ(Xθ - y) + λ|θ|₂² + γ|θ|₁``.
157
186
"""
158
187
function RobustRegression (ρ:: RobustRho = HuberRho (0.1 ), λ:: Real = 1.0 , γ:: Real = 0.0 ;
159
188
rho:: RobustRho = ρ, lambda:: Real = λ, gamma:: Real = γ,
160
189
penalty:: Symbol = iszero (gamma) ? :l2 : :en ,
161
190
fit_intercept:: Bool = true ,
191
+ scale_penalty_with_samples:: Bool = true ,
162
192
penalize_intercept:: Bool = false )
163
193
penalty = _l1l2en (lambda, gamma, penalty, " Robust regression" )
164
194
GLR (loss= RobustLoss (rho),
165
195
penalty= penalty,
166
196
fit_intercept= fit_intercept,
167
- penalize_intercept= penalize_intercept)
197
+ penalize_intercept= penalize_intercept,
198
+ scale_penalty_with_samples= scale_penalty_with_samples)
168
199
end
169
200
170
201
"""
171
202
$SIGNATURES
172
203
173
204
Huber Regression with objective:
174
205
175
- ``∑ρ(Xθ - y) + λ|θ|₂²/2 + γ|θ|₁``
206
+ ``∑ρ(Xθ - y) + n⋅ λ|θ|₂²/2 + n⋅ γ|θ|₁``
176
207
177
208
Where `ρ` is the Huber function `ρ(r) = r²/2`` if `|r|≤δ` and
178
- `ρ(r)=δ(|r|-δ/2)` otherwise.
209
+ `ρ(r)=δ(|r|-δ/2)` otherwise and
210
+ ``n`` is the number of samples `size(X, 1)`.
211
+ With `scale_penalty_with_samples = false` the objective function is
212
+ ``∑ρ(Xθ - y) + λ|θ|₂²/2 + γ|θ|₁``.
179
213
"""
180
214
function HuberRegression (δ:: Real = 0.5 , λ:: Real = 1.0 , γ:: Real = 0.0 ;
181
215
delta:: Real = δ, lambda:: Real = λ, gamma:: Real = γ,
182
216
penalty:: Symbol = iszero (gamma) ? :l2 : :en ,
183
217
fit_intercept:: Bool = true ,
218
+ scale_penalty_with_samples:: Bool = true ,
184
219
penalize_intercept:: Bool = false )
185
220
return RobustRegression (HuberRho (delta), lambda, gamma;
186
221
penalty= penalty, fit_intercept= fit_intercept,
187
- penalize_intercept= penalize_intercept)
222
+ penalize_intercept= penalize_intercept,
223
+ scale_penalty_with_samples= scale_penalty_with_samples)
188
224
end
189
225
190
226
"""
191
227
$SIGNATURES
192
228
193
229
Quantile Regression with objective:
194
230
195
- ``∑ρ(Xθ - y) + λ|θ|₂²/2 + γ|θ|₁``
231
+ ``∑ρ(Xθ - y) + n⋅ λ|θ|₂²/2 + n⋅ γ|θ|₁``
196
232
197
- Where `ρ` is the check function `ρ(r) = r(δ - 1(r < 0))`.
233
+ Where `ρ` is the check function `ρ(r) = r(δ - 1(r < 0))` and
234
+ ``n`` is the number of samples `size(X, 1)`.
235
+ With `scale_penalty_with_samples = false` the objective function is
236
+ ``∑ρ(Xθ - y) + λ|θ|₂²/2 + γ|θ|₁``.
198
237
"""
199
238
function QuantileRegression (δ:: Real = 0.5 , λ:: Real = 1.0 , γ:: Real = 0.0 ;
200
239
delta:: Real = δ, lambda:: Real = λ, gamma:: Real = γ,
201
240
penalty:: Symbol = iszero (gamma) ? :l2 : :en ,
202
241
fit_intercept:: Bool = true ,
242
+ scale_penalty_with_samples:: Bool = true ,
203
243
penalize_intercept:: Bool = false )
204
244
return RobustRegression (QuantileRho (delta), lambda, gamma;
205
245
penalty= penalty, fit_intercept= fit_intercept,
206
- penalize_intercept= penalize_intercept)
246
+ penalize_intercept= penalize_intercept,
247
+ scale_penalty_with_samples= scale_penalty_with_samples)
207
248
end
208
249
209
250
"""
210
251
$SIGNATURES
211
252
212
253
Least Absolute Deviation regression with objective:
213
254
214
- ``|Xθ - y|₁ + λ|θ|₂²/2 + γ|θ|₁``
255
+ ``|Xθ - y|₁ + n⋅λ|θ|₂²/2 + n⋅γ|θ|₁``
256
+ where ``n`` is the number of samples `size(X, 1)`.
257
+ With `scale_penalty_with_samples = false` the objective function is
258
+ ``|Xθ - y|₁ + λ|θ|₂²/2 + γ|θ|₁``.
215
259
216
260
This is a specific type of Quantile Regression with `δ=0.5` (median).
217
261
"""
218
262
function LADRegression (λ:: Real = 1.0 , γ:: Real = 0.0 ;
219
263
lambda:: Real = λ, gamma:: Real = γ,
220
264
penalty:: Symbol = iszero (gamma) ? :l2 : :en ,
265
+ scale_penalty_with_samples:: Bool = true ,
221
266
fit_intercept:: Bool = true , penalize_intercept:: Bool = false )
222
267
return QuantileRegression (0.5 , lambda, gamma;
223
268
penalty= penalty, fit_intercept= fit_intercept,
224
- penalize_intercept= penalize_intercept)
269
+ penalize_intercept= penalize_intercept,
270
+ scale_penalty_with_samples= scale_penalty_with_samples)
225
271
end
0 commit comments