@@ -200,21 +200,35 @@ end
200
200
@weights ExponentialWeights false
201
201
202
202
"""
203
- ExponentialWeights
203
+ ExponentialWeights(vs)
204
204
205
- # Fields
205
+ Construct an `ExponentialWeights` vector with weight values `vs`, which must sum to 1.
206
206
207
- * `λ::Float64`: is a smoothing factor or rate paremeter between 0 .. 1.
208
- As this value approaches 0 the resulting weights will be almost equal(),
209
- while values closer to 1 will put higher weight on the end elements of the vector.
207
+ Exponential weights are a common form of temporal weights which assign exponentially
208
+ greater weight to past observations, which in this case corresponds to the tail end of
209
+ the vector.
210
+ """
211
+ function ExponentialWeights (vs:: V ) where {T<: Real , V<: AbstractVector{T} }
212
+ s = sum (vs)
213
+ s ≈ one (T) || throw (ArgumentError (" weight values do not sum to 1 (got $s )" ))
214
+ ExponentialWeights {T, T, V} (vs, s)
215
+ end
210
216
211
- When called with a desired length `n` (`Int`) a vector of length `n` will
212
- be returned, where each element is set to `λ * (1 - λ)^(1 - i)`.
217
+ """
218
+ eweights(n, λ)
213
219
214
- # Usage
220
+ Construct an [`ExponentialWeights`](@ref) vector with length `n`,
221
+ where each element in position ``i`` is set to ``λ (1 - λ)^{1 - i}``.
222
+ The entire set of weights are then normalized to sum to 1.
215
223
216
- ```julia
217
- w = ExponentialWeights(10, 0.3)
224
+ ``λ`` is a smoothing factor or rate parameter such that ``0 < λ \\ leq 1``.
225
+ As this value approaches 0, the resulting weights will be almost equal,
226
+ while values closer to 1 will put greater weight on the tail elements of the vector.
227
+
228
+ # Examples
229
+
230
+ ```julia-repl
231
+ julia> eweights(10, 0.3)
218
232
10-element ExponentialWeights{Float64,Float64,Array{Float64,1}}:
219
233
0.012458
220
234
0.0177971
@@ -228,41 +242,35 @@ w = ExponentialWeights(10, 0.3)
228
242
0.308721
229
243
```
230
244
"""
231
- function ExponentialWeights (vs:: V ) where {T<: Real , V<: AbstractVector{T} }
232
- s = sum (vs)
233
- s ≈ one (T) || throw (ArgumentError (" weight values do not sum to 1 (got $s )" ))
234
- ExponentialWeights {T, T, V} (vs, s)
235
- end
236
-
237
- function ExponentialWeights (n:: Integer , λ:: Real )
238
- n > 0 || throw (ArgumentError (" cannot construct weights of length < 1" ))
245
+ function eweights (n:: Integer , λ:: Real )
246
+ n > 0 || throw (ArgumentError (" cannot construct exponential weights of length < 1" ))
239
247
0 < λ <= 1 || throw (ArgumentError (" smoothing factor must be between 0 and 1" ))
240
248
w0 = map (i -> λ * (1 - λ)^ (1 - i), 1 : n)
241
249
s = sum (w0)
242
- ExponentialWeights (w0 / s)
250
+ w0 ./= s
251
+ ExponentialWeights {typeof(s), eltype(w0), typeof(w0)} (w0, s)
243
252
end
244
253
245
254
"""
246
- eweights(n, λ)
247
-
248
- Construct an `ExponentialWeights` vector with length `n`,
249
- where each element in position ``i`` is set to ``λ * (1 - λ)^(1 - i)``.
250
- The entire set of weights are then normalized so that they sum to 1.0
255
+ eweights(vs)
251
256
252
- ``λ`` is a smoothing factor or rate parameter between 0 and 1.
253
- As this value approaches 0 the resulting weights will be almost equal,
254
- while values closer to 1 will put higher weight on the end elements of the vector.
257
+ Construct an [`ExponentialWeights`](@ref) vector using the given array.
255
258
"""
256
- eweights (n:: Integer , λ:: Real ) = ExponentialWeights (n, λ)
259
+ eweights (v:: RealVector ) = ExponentialWeights (v)
260
+ eweights (v:: RealArray ) = ExponentialWeights (vec (v))
257
261
258
262
"""
259
263
varcorrection(w::ExponentialWeights, corrected=false)
260
264
261
265
* `corrected=true`: ``\\ frac{1}{1 - \\ sum {w^2}}``
262
- * `corrected=false`: ``1.0 ``
266
+ * `corrected=false`: ``1``
263
267
"""
264
268
@inline function varcorrection (w:: ExponentialWeights , corrected:: Bool = false )
265
- corrected ? 1 / (1 - sum (x -> x^ 2 , w)) : 1.0
269
+ if corrected
270
+ 1 / (1 - sum (abs2, w. values))
271
+ else
272
+ 1 / one (w. sum) # just 1 promoted to the same type as the other branch
273
+ end
266
274
end
267
275
268
276
# #### Equality tests #####
0 commit comments