@@ -3,15 +3,18 @@ Moving linear regression intercept (column 1) and slope (column 2)
3
3
4
4
`mlr_beta{Float64}(y::Array{Float64}; n::Int64=10)::Array{Float64}`
5
5
""" ->
6
- function mlr_beta {Float64} (y:: Array{Float64} ; n:: Int64 = 10 ):: Matrix{Float64}
6
+ function mlr_beta {Float64} (y:: Array{Float64} ; n:: Int64 = 10 , x :: Array{Float64} = collect ( 1.0 : n) ):: Matrix{Float64}
7
7
@assert n< length (y) && n> 0 " Argument n out of bounds."
8
+ @assert size (y,2 ) == 1
9
+ @assert size (x,1 ) == n || size (x,1 ) == size (y,1 )
10
+ const_x = size (x,1 ) == n
8
11
out = zeros (Float64, (length (y),2 ))
9
12
out[1 : n- 1 ,:] = NaN
10
- xi = collect (1.0 : n)
11
- xbar = mean (xi)
13
+ xbar = mean (x)
12
14
ybar = runmean (y, n= n, cumulative= false )
13
15
@inbounds for i = n: length (y)
14
16
yi = y[i- n+ 1 : i]
17
+ xi = const_x ? x : x[i- n+ 1 : i]
15
18
out[i,2 ] = cov (xi,yi) / var (xi)
16
19
out[i,1 ] = ybar[i] - out[i,2 ]* xbar
17
20
end
@@ -23,13 +26,16 @@ Moving linear regression slope
23
26
24
27
`mlr_slope{Float64}(y::Array{Float64}; n::Int64=10)::Array{Float64}`
25
28
""" ->
26
- function mlr_slope {Float64} (y:: Array{Float64} ; n:: Int64 = 10 ):: Array{Float64}
29
+ function mlr_slope {Float64} (y:: Array{Float64} ; n:: Int64 = 10 , x :: Array{Float64} = collect ( 1.0 : n) ):: Array{Float64}
27
30
@assert n< length (y) && n> 0 " Argument n out of bounds."
31
+ @assert size (y,2 ) == 1
32
+ @assert size (x,1 ) == n || size (x,1 ) == size (y,1 )
33
+ const_x = size (x,1 ) == n
28
34
out = zeros (y)
29
35
out[1 : n- 1 ] = NaN
30
- xi = collect (1.0 : n)
31
36
@inbounds for i = n: length (y)
32
37
yi = y[i- n+ 1 : i]
38
+ xi = const_x ? x : x[i- n+ 1 : i]
33
39
out[i] = cov (xi,yi) / var (xi)
34
40
end
35
41
return out
@@ -40,15 +46,18 @@ Moving linear regression y-intercept
40
46
41
47
`mlr_intercept{Float64}(y::Array{Float64}; n::Int64=10)::Array{Float64}`
42
48
""" ->
43
- function mlr_intercept {Float64} (y:: Array{Float64} ; n:: Int64 = 10 ):: Array{Float64}
49
+ function mlr_intercept {Float64} (y:: Array{Float64} ; n:: Int64 = 10 , x :: Array{Float64} = collect ( 1.0 : n) ):: Array{Float64}
44
50
@assert n< length (y) && n> 0 " Argument n out of bounds."
51
+ @assert size (y,2 ) == 1
52
+ @assert size (x,1 ) == n || size (x,1 ) == size (y,1 )
53
+ const_x = size (x,1 ) == n
45
54
out = zeros (y)
46
55
out[1 : n- 1 ] = NaN
47
- xi = collect (1.0 : n)
48
- xbar = mean (xi)
56
+ xbar = mean (x)
49
57
ybar = runmean (y, n= n, cumulative= false )
50
58
@inbounds for i = n: length (y)
51
59
yi = y[i- n+ 1 : i]
60
+ xi = const_x ? x : x[i- n+ 1 : i]
52
61
out[i] = ybar[i] - xbar* (cov (xi,yi)/ var (xi))
53
62
end
54
63
return out
0 commit comments