14
14
default_laglen (lx:: Int ) = min (lx- 1 , round (Int,10 * log10 (lx)))
15
15
check_lags (lx:: Int , lags:: AbstractVector ) = (maximum (lags) < lx || error (" lags must be less than the sample length." ))
16
16
17
- function demean_col! (z:: RealVector , x:: RealMatrix , j:: Int , demean:: Bool )
17
+ function demean_col! (z:: AbstractVector , x:: AbstractMatrix , j:: Int , demean:: Bool )
18
18
T = eltype (z)
19
19
m = size (x, 1 )
20
20
@assert m == length (z)
43
43
44
44
default_autolags (lx:: Int ) = 0 : default_laglen (lx)
45
45
46
- _autodot (x:: AbstractVector{<:RealFP} , lx:: Int , l:: Int ) = dot (x, 1 : (lx- l), x, (1 + l): lx)
47
- _autodot (x:: RealVector , lx:: Int , l:: Int ) = dot (view (x, 1 : (lx- l)), view (x, (1 + l): lx))
46
+ _autodot (x:: AbstractVector , lx:: Int , l:: Int ) = dot (view (x, 1 : (lx- l)), view (x, (1 + l): lx))
48
47
49
48
50
49
# # autocov
@@ -61,34 +60,32 @@ where each column in the result will correspond to a column in `x`.
61
60
62
61
The output is not normalized. See [`autocor!`](@ref) for a method with normalization.
63
62
"""
64
- function autocov! (r:: RealVector , x:: RealVector , lags:: IntegerVector ; demean:: Bool = true )
63
+ function autocov! (
64
+ r:: AbstractVector ,
65
+ x:: AbstractVector ,
66
+ lags:: AbstractVector{<:Integer} ,
67
+ z= zeros (typeof (zero (eltype (x)) / 1 ), length (x));
68
+ demean:: Bool = true
69
+ )
65
70
lx = length (x)
66
71
m = length (lags)
67
72
length (r) == m || throw (DimensionMismatch ())
68
73
check_lags (lx, lags)
69
-
70
74
T = typeof (zero (eltype (x)) / 1 )
71
- z:: Vector{T} = demean ? x .- mean (x) : x
75
+ z .= x
76
+ demean && z .= z .- mean (z)
72
77
for k = 1 : m # foreach lag value
73
78
r[k] = _autodot (z, lx, lags[k]) / lx
74
79
end
75
80
return r
76
81
end
77
82
78
- function autocov! (r:: RealMatrix , x:: RealMatrix , lags:: IntegerVector ; demean:: Bool = true )
79
- lx = size (x, 1 )
80
- ns = size (x, 2 )
81
- m = length (lags)
82
- size (r) == (m, ns) || throw (DimensionMismatch ())
83
- check_lags (lx, lags)
84
-
85
- T = typeof (zero (eltype (x)) / 1 )
86
- z = Vector {T} (undef, lx)
87
- for j = 1 : ns
88
- demean_col! (z, x, j, demean)
89
- for k = 1 : m
90
- r[k,j] = _autodot (z, lx, lags[k]) / lx
91
- end
83
+ function autocov! (
84
+ r:: AbstractMatrix , x:: AbstractMatrix , lags:: AbstractVector{<:Integer} ; demean:: Bool = true
85
+ )
86
+ z = zeros (typeof (zero (eltype (x))/ 1 ), size (x, 1 ))
87
+ for n in 1 : size (x, 2 )
88
+ autocov! (view (r, :, n), view (x, :, n), lags, z; demean)
92
89
end
93
90
return r
94
91
end
@@ -110,18 +107,18 @@ When left unspecified, the lags used are the integers from 0 to
110
107
111
108
The output is not normalized. See [`autocor`](@ref) for a function with normalization.
112
109
"""
113
- function autocov (x:: RealVector , lags:: IntegerVector ; demean:: Bool = true )
110
+ function autocov (x:: AbstractVector , lags:: AbstractVector{<:Integer} ; demean:: Bool = true )
114
111
out = Vector {float(eltype(x))} (undef, length (lags))
115
- autocov! (out, x, lags; demean= demean )
112
+ autocov! (out, x, lags; demean)
116
113
end
117
114
118
- function autocov (x:: RealMatrix , lags:: IntegerVector ; demean:: Bool = true )
115
+ function autocov (x:: AbstractMatrix , lags:: AbstractVector{<:Integer} ; demean:: Bool = true )
119
116
out = Matrix {float(eltype(x))} (undef, length (lags), size (x,2 ))
120
- autocov! (out, x, lags; demean= demean )
117
+ autocov! (out, x, lags; demean)
121
118
end
122
119
123
- autocov (x:: AbstractVecOrMat{<:Real} ; demean:: Bool = true ) =
124
- autocov (x, default_autolags (size (x,1 )); demean= demean )
120
+ autocov (x:: AbstractVecOrMat ; demean:: Bool = true ) =
121
+ autocov (x, default_autolags (size (x,1 )); demean)
125
122
126
123
# # autocor
127
124
@@ -139,36 +136,27 @@ where each column in the result will correspond to a column in `x`.
139
136
The output is normalized by the variance of `x`, i.e. so that the lag 0
140
137
autocorrelation is 1. See [`autocov!`](@ref) for the unnormalized form.
141
138
"""
142
- function autocor! (r:: RealVector , x:: RealVector , lags:: IntegerVector ; demean:: Bool = true )
143
- lx = length (x)
144
- m = length (lags)
145
- length (r) == m || throw (DimensionMismatch ())
146
- check_lags (lx, lags)
147
-
148
- T = typeof (zero (eltype (x)) / 1 )
149
- z:: Vector{T} = demean ? x .- mean (x) : x
150
- zz = dot (z, z)
151
- for k = 1 : m # foreach lag value
152
- r[k] = _autodot (z, lx, lags[k]) / zz
153
- end
139
+ function autocor! (
140
+ r:: AbstractVector ,
141
+ x:: AbstractVector ,
142
+ lags:: AbstractVector{<:Integer} ,
143
+ z= zeros (typeof (zero (eltype (x)) / 1 ), length (x));
144
+ demean:: Bool = true
145
+ )
146
+ autocov! (view (r, 1 : 1 ), x, 0 : 0 , z; demean)
147
+ zz = r[1 ]
148
+ autocov! (r, x, lags, z; demean)
149
+ r ./= zz
154
150
return r
155
151
end
156
152
157
- function autocor! (r:: RealMatrix , x:: RealMatrix , lags:: IntegerVector ; demean:: Bool = true )
158
- lx = size (x, 1 )
159
- ns = size (x, 2 )
160
- m = length (lags)
161
- size (r) == (m, ns) || throw (DimensionMismatch ())
162
- check_lags (lx, lags)
163
-
164
- T = typeof (zero (eltype (x)) / 1 )
165
- z = Vector {T} (undef, lx)
166
- for j = 1 : ns
167
- demean_col! (z, x, j, demean)
168
- zz = dot (z, z)
169
- for k = 1 : m
170
- r[k,j] = _autodot (z, lx, lags[k]) / zz
171
- end
153
+ function autocor! (
154
+ r:: AbstractMatrix , x:: AbstractMatrix , lags:: AbstractVector{<:Integer} ; demean:: Bool = true
155
+ )
156
+ T = typeof (zero (eltype (x))/ 1 )
157
+ z = Vector {T} (undef, size (x, 1 ))
158
+ for n in 1 : size (x, 2 )
159
+ autocor! (view (r, :, n), view (x, :, n), lags, z; demean)
172
160
end
173
161
return r
174
162
end
@@ -191,18 +179,18 @@ When left unspecified, the lags used are the integers from 0 to
191
179
The output is normalized by the variance of `x`, i.e. so that the lag 0
192
180
autocorrelation is 1. See [`autocov`](@ref) for the unnormalized form.
193
181
"""
194
- function autocor (x:: RealVector , lags:: IntegerVector ; demean:: Bool = true )
182
+ function autocor (x:: AbstractVector , lags:: AbstractVector{<:Integer} ; demean:: Bool = true )
195
183
out = Vector {float(eltype(x))} (undef, length (lags))
196
- autocor! (out, x, lags; demean= demean )
184
+ autocor! (out, x, lags; demean)
197
185
end
198
186
199
- function autocor (x:: RealMatrix , lags:: IntegerVector ; demean:: Bool = true )
187
+ function autocor (x:: AbstractMatrix , lags:: AbstractVector{<:Integer} ; demean:: Bool = true )
200
188
out = Matrix {float(eltype(x))} (undef, length (lags), size (x,2 ))
201
- autocor! (out, x, lags; demean= demean )
189
+ autocor! (out, x, lags; demean)
202
190
end
203
191
204
- autocor (x:: AbstractVecOrMat{<:Real} ; demean:: Bool = true ) =
205
- autocor (x, default_autolags (size (x,1 )); demean= demean )
192
+ autocor (x:: AbstractVecOrMat ; demean:: Bool = true ) =
193
+ autocor (x, default_autolags (size (x,1 )); demean)
206
194
207
195
208
196
# ######################################
@@ -213,14 +201,7 @@ autocor(x::AbstractVecOrMat{<:Real}; demean::Bool=true) =
213
201
214
202
default_crosslags (lx:: Int ) = (l= default_laglen (lx); - l: l)
215
203
216
- function _crossdot (x:: AbstractVector{T} , y:: AbstractVector{T} , lx:: Int , l:: Int ) where {T<: RealFP }
217
- if l >= 0
218
- dot (x, 1 : (lx- l), y, (1 + l): lx)
219
- else
220
- dot (x, (1 - l): lx, y, 1 : (lx+ l))
221
- end
222
- end
223
- function _crossdot (x:: RealVector , y:: RealVector , lx:: Int , l:: Int )
204
+ function _crossdot (x:: AbstractVector , y:: AbstractVector , lx:: Int , l:: Int )
224
205
if l >= 0
225
206
dot (view (x, 1 : (lx- l)), view (y, (1 + l): lx))
226
207
else
@@ -246,7 +227,7 @@ three-dimensional array of size `(length(lags), size(x, 2), size(y, 2))`.
246
227
247
228
The output is not normalized. See [`crosscor!`](@ref) for a function with normalization.
248
229
"""
249
- function crosscov! (r:: RealVector , x:: RealVector , y:: RealVector , lags:: IntegerVector ; demean:: Bool = true )
230
+ function crosscov! (r:: AbstractVector , x:: AbstractVector , y:: AbstractVector , lags:: AbstractVector{<:Integer} ; demean:: Bool = true )
250
231
lx = length (x)
251
232
m = length (lags)
252
233
(length (y) == lx && length (r) == m) || throw (DimensionMismatch ())
@@ -262,7 +243,7 @@ function crosscov!(r::RealVector, x::RealVector, y::RealVector, lags::IntegerVec
262
243
return r
263
244
end
264
245
265
- function crosscov! (r:: RealMatrix , x:: RealMatrix , y:: RealVector , lags:: IntegerVector ; demean:: Bool = true )
246
+ function crosscov! (r:: AbstractMatrix , x:: AbstractMatrix , y:: AbstractVector , lags:: AbstractVector{<:Integer} ; demean:: Bool = true )
266
247
lx = size (x, 1 )
267
248
ns = size (x, 2 )
268
249
m = length (lags)
@@ -282,7 +263,7 @@ function crosscov!(r::RealMatrix, x::RealMatrix, y::RealVector, lags::IntegerVec
282
263
return r
283
264
end
284
265
285
- function crosscov! (r:: RealMatrix , x:: RealVector , y:: RealMatrix , lags:: IntegerVector ; demean:: Bool = true )
266
+ function crosscov! (r:: AbstractMatrix , x:: AbstractVector , y:: AbstractMatrix , lags:: AbstractVector{<:Integer} ; demean:: Bool = true )
286
267
lx = length (x)
287
268
ns = size (y, 2 )
288
269
m = length (lags)
@@ -302,7 +283,7 @@ function crosscov!(r::RealMatrix, x::RealVector, y::RealMatrix, lags::IntegerVec
302
283
return r
303
284
end
304
285
305
- function crosscov! (r:: AbstractArray{<:Real ,3} , x:: RealMatrix , y:: RealMatrix , lags:: IntegerVector ; demean:: Bool = true )
286
+ function crosscov! (r:: AbstractArray{U ,3} where U , x:: AbstractMatrix , y:: AbstractMatrix , lags:: AbstractVector{<:Integer} ; demean:: Bool = true )
306
287
lx = size (x, 1 )
307
288
nx = size (x, 2 )
308
289
ny = size (y, 2 )
@@ -356,27 +337,27 @@ When left unspecified, the lags used are the integers from
356
337
357
338
The output is not normalized. See [`crosscor`](@ref) for a function with normalization.
358
339
"""
359
- function crosscov (x:: RealVector , y:: RealVector , lags:: IntegerVector ; demean:: Bool = true )
340
+ function crosscov (x:: AbstractVector , y:: AbstractVector , lags:: AbstractVector{<:Integer} ; demean:: Bool = true )
360
341
out = Vector {float(Base.promote_eltype(x, y))} (undef, length (lags))
361
342
crosscov! (out, x, y, lags; demean= demean)
362
343
end
363
344
364
- function crosscov (x:: RealMatrix , y:: RealVector , lags:: IntegerVector ; demean:: Bool = true )
345
+ function crosscov (x:: AbstractMatrix , y:: AbstractVector , lags:: AbstractVector{<:Integer} ; demean:: Bool = true )
365
346
out = Matrix {float(Base.promote_eltype(x, y))} (undef, length (lags), size (x,2 ))
366
347
crosscov! (out, x, y, lags; demean= demean)
367
348
end
368
349
369
- function crosscov (x:: RealVector , y:: RealMatrix , lags:: IntegerVector ; demean:: Bool = true )
350
+ function crosscov (x:: AbstractVector , y:: AbstractMatrix , lags:: AbstractVector{<:Integer} ; demean:: Bool = true )
370
351
out = Matrix {float(Base.promote_eltype(x, y))} (undef, length (lags), size (y,2 ))
371
352
crosscov! (out, x, y, lags; demean= demean)
372
353
end
373
354
374
- function crosscov (x:: RealMatrix , y:: RealMatrix , lags:: IntegerVector ; demean:: Bool = true )
355
+ function crosscov (x:: AbstractMatrix , y:: AbstractMatrix , lags:: AbstractVector{<:Integer} ; demean:: Bool = true )
375
356
out = Array {float(Base.promote_eltype(x, y)),3} (undef, length (lags), size (x,2 ), size (y,2 ))
376
357
crosscov! (out, x, y, lags; demean= demean)
377
358
end
378
359
379
- crosscov (x:: AbstractVecOrMat{<:Real} , y:: AbstractVecOrMat{<:Real} ; demean:: Bool = true ) =
360
+ crosscov (x:: AbstractVecOrMat , y:: AbstractVecOrMat ; demean:: Bool = true ) =
380
361
crosscov (x, y, default_crosslags (size (x,1 )); demean= demean)
381
362
382
363
@@ -397,7 +378,7 @@ three-dimensional array of size `(length(lags), size(x, 2), size(y, 2))`.
397
378
The output is normalized by `sqrt(var(x)*var(y))`. See [`crosscov!`](@ref) for the
398
379
unnormalized form.
399
380
"""
400
- function crosscor! (r:: RealVector , x:: RealVector , y:: RealVector , lags:: IntegerVector ; demean:: Bool = true )
381
+ function crosscor! (r:: AbstractVector , x:: AbstractVector , y:: AbstractVector , lags:: AbstractVector{<:Integer} ; demean:: Bool = true )
401
382
lx = length (x)
402
383
m = length (lags)
403
384
(length (y) == lx && length (r) == m) || throw (DimensionMismatch ())
@@ -414,7 +395,7 @@ function crosscor!(r::RealVector, x::RealVector, y::RealVector, lags::IntegerVec
414
395
return r
415
396
end
416
397
417
- function crosscor! (r:: RealMatrix , x:: RealMatrix , y:: RealVector , lags:: IntegerVector ; demean:: Bool = true )
398
+ function crosscor! (r:: AbstractMatrix , x:: AbstractMatrix , y:: AbstractVector , lags:: AbstractVector{<:Integer} ; demean:: Bool = true )
418
399
lx = size (x, 1 )
419
400
ns = size (x, 2 )
420
401
m = length (lags)
@@ -436,7 +417,7 @@ function crosscor!(r::RealMatrix, x::RealMatrix, y::RealVector, lags::IntegerVec
436
417
return r
437
418
end
438
419
439
- function crosscor! (r:: RealMatrix , x:: RealVector , y:: RealMatrix , lags:: IntegerVector ; demean:: Bool = true )
420
+ function crosscor! (r:: AbstractMatrix , x:: AbstractVector , y:: AbstractMatrix , lags:: AbstractVector{<:Integer} ; demean:: Bool = true )
440
421
lx = length (x)
441
422
ns = size (y, 2 )
442
423
m = length (lags)
@@ -458,7 +439,7 @@ function crosscor!(r::RealMatrix, x::RealVector, y::RealMatrix, lags::IntegerVec
458
439
return r
459
440
end
460
441
461
- function crosscor! (r:: AbstractArray{<:Real ,3} , x:: RealMatrix , y:: RealMatrix , lags:: IntegerVector ; demean:: Bool = true )
442
+ function crosscor! (r:: AbstractArray{U ,3} where U , x:: AbstractMatrix , y:: AbstractMatrix , lags:: AbstractVector{<:Integer} ; demean:: Bool = true )
462
443
lx = size (x, 1 )
463
444
nx = size (x, 2 )
464
445
ny = size (y, 2 )
@@ -517,27 +498,27 @@ When left unspecified, the lags used are the integers from
517
498
The output is normalized by `sqrt(var(x)*var(y))`. See [`crosscov`](@ref) for the
518
499
unnormalized form.
519
500
"""
520
- function crosscor (x:: RealVector , y:: RealVector , lags:: IntegerVector ; demean:: Bool = true )
501
+ function crosscor (x:: AbstractVector , y:: AbstractVector , lags:: AbstractVector{<:Integer} ; demean:: Bool = true )
521
502
out = Vector {float(Base.promote_eltype(x, y))} (undef, length (lags))
522
503
crosscor! (out, x, y, lags; demean= demean)
523
504
end
524
505
525
- function crosscor (x:: RealMatrix , y:: RealVector , lags:: IntegerVector ; demean:: Bool = true )
506
+ function crosscor (x:: AbstractMatrix , y:: AbstractVector , lags:: AbstractVector{<:Integer} ; demean:: Bool = true )
526
507
out = Matrix {float(Base.promote_eltype(x, y))} (undef, length (lags), size (x,2 ))
527
508
crosscor! (out, x, y, lags; demean= demean)
528
509
end
529
510
530
- function crosscor (x:: RealVector , y:: RealMatrix , lags:: IntegerVector ; demean:: Bool = true )
511
+ function crosscor (x:: AbstractVector , y:: AbstractMatrix , lags:: AbstractVector{<:Integer} ; demean:: Bool = true )
531
512
out = Matrix {float(Base.promote_eltype(x, y))} (undef, length (lags), size (y,2 ))
532
513
crosscor! (out, x, y, lags; demean= demean)
533
514
end
534
515
535
- function crosscor (x:: RealMatrix , y:: RealMatrix , lags:: IntegerVector ; demean:: Bool = true )
516
+ function crosscor (x:: AbstractMatrix , y:: AbstractMatrix , lags:: AbstractVector{<:Integer} ; demean:: Bool = true )
536
517
out = Array {float(Base.promote_eltype(x, y)),3} (undef, length (lags), size (x,2 ), size (y,2 ))
537
518
crosscor! (out, x, y, lags; demean= demean)
538
519
end
539
520
540
- crosscor (x:: AbstractVecOrMat{<:Real} , y:: AbstractVecOrMat{<:Real} ; demean:: Bool = true ) =
521
+ crosscor (x:: AbstractVecOrMat , y:: AbstractVecOrMat ; demean:: Bool = true ) =
541
522
crosscor (x, y, default_crosslags (size (x,1 )); demean= demean)
542
523
543
524
0 commit comments