Skip to content

Commit 07904af

Browse files
authored
Merge pull request #9 from dysonance/issue8
Issue8
2 parents 0f358a5 + 7ce336e commit 07904af

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/reg.jl

+17-8
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@ Moving linear regression intercept (column 1) and slope (column 2)
33
44
`mlr_beta{Float64}(y::Array{Float64}; n::Int64=10)::Array{Float64}`
55
""" ->
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}
77
@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
811
out = zeros(Float64, (length(y),2))
912
out[1:n-1,:] = NaN
10-
xi = collect(1.0:n)
11-
xbar = mean(xi)
13+
xbar = mean(x)
1214
ybar = runmean(y, n=n, cumulative=false)
1315
@inbounds for i = n:length(y)
1416
yi = y[i-n+1:i]
17+
xi = const_x ? x : x[i-n+1:i]
1518
out[i,2] = cov(xi,yi) / var(xi)
1619
out[i,1] = ybar[i] - out[i,2]*xbar
1720
end
@@ -23,13 +26,16 @@ Moving linear regression slope
2326
2427
`mlr_slope{Float64}(y::Array{Float64}; n::Int64=10)::Array{Float64}`
2528
""" ->
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}
2730
@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
2834
out = zeros(y)
2935
out[1:n-1] = NaN
30-
xi = collect(1.0:n)
3136
@inbounds for i = n:length(y)
3237
yi = y[i-n+1:i]
38+
xi = const_x ? x : x[i-n+1:i]
3339
out[i] = cov(xi,yi) / var(xi)
3440
end
3541
return out
@@ -40,15 +46,18 @@ Moving linear regression y-intercept
4046
4147
`mlr_intercept{Float64}(y::Array{Float64}; n::Int64=10)::Array{Float64}`
4248
""" ->
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}
4450
@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
4554
out = zeros(y)
4655
out[1:n-1] = NaN
47-
xi = collect(1.0:n)
48-
xbar = mean(xi)
56+
xbar = mean(x)
4957
ybar = runmean(y, n=n, cumulative=false)
5058
@inbounds for i = n:length(y)
5159
yi = y[i-n+1:i]
60+
xi = const_x ? x : x[i-n+1:i]
5261
out[i] = ybar[i] - xbar*(cov(xi,yi)/var(xi))
5362
end
5463
return out

0 commit comments

Comments
 (0)