Skip to content

Commit

Permalink
Merge pull request #9 from dysonance/issue8
Browse files Browse the repository at this point in the history
Issue8
  • Loading branch information
dysonance committed Apr 18, 2018
2 parents 0f358a5 + 7ce336e commit 07904af
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/reg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ Moving linear regression intercept (column 1) and slope (column 2)
`mlr_beta{Float64}(y::Array{Float64}; n::Int64=10)::Array{Float64}`
""" ->
function mlr_beta{Float64}(y::Array{Float64}; n::Int64=10)::Matrix{Float64}
function mlr_beta{Float64}(y::Array{Float64}; n::Int64=10, x::Array{Float64}=collect(1.0:n))::Matrix{Float64}
@assert n<length(y) && n>0 "Argument n out of bounds."
@assert size(y,2) == 1
@assert size(x,1) == n || size(x,1) == size(y,1)
const_x = size(x,1) == n
out = zeros(Float64, (length(y),2))
out[1:n-1,:] = NaN
xi = collect(1.0:n)
xbar = mean(xi)
xbar = mean(x)
ybar = runmean(y, n=n, cumulative=false)
@inbounds for i = n:length(y)
yi = y[i-n+1:i]
xi = const_x ? x : x[i-n+1:i]
out[i,2] = cov(xi,yi) / var(xi)
out[i,1] = ybar[i] - out[i,2]*xbar
end
Expand All @@ -23,13 +26,16 @@ Moving linear regression slope
`mlr_slope{Float64}(y::Array{Float64}; n::Int64=10)::Array{Float64}`
""" ->
function mlr_slope{Float64}(y::Array{Float64}; n::Int64=10)::Array{Float64}
function mlr_slope{Float64}(y::Array{Float64}; n::Int64=10, x::Array{Float64}=collect(1.0:n))::Array{Float64}
@assert n<length(y) && n>0 "Argument n out of bounds."
@assert size(y,2) == 1
@assert size(x,1) == n || size(x,1) == size(y,1)
const_x = size(x,1) == n
out = zeros(y)
out[1:n-1] = NaN
xi = collect(1.0:n)
@inbounds for i = n:length(y)
yi = y[i-n+1:i]
xi = const_x ? x : x[i-n+1:i]
out[i] = cov(xi,yi) / var(xi)
end
return out
Expand All @@ -40,15 +46,18 @@ Moving linear regression y-intercept
`mlr_intercept{Float64}(y::Array{Float64}; n::Int64=10)::Array{Float64}`
""" ->
function mlr_intercept{Float64}(y::Array{Float64}; n::Int64=10)::Array{Float64}
function mlr_intercept{Float64}(y::Array{Float64}; n::Int64=10, x::Array{Float64}=collect(1.0:n))::Array{Float64}
@assert n<length(y) && n>0 "Argument n out of bounds."
@assert size(y,2) == 1
@assert size(x,1) == n || size(x,1) == size(y,1)
const_x = size(x,1) == n
out = zeros(y)
out[1:n-1] = NaN
xi = collect(1.0:n)
xbar = mean(xi)
xbar = mean(x)
ybar = runmean(y, n=n, cumulative=false)
@inbounds for i = n:length(y)
yi = y[i-n+1:i]
xi = const_x ? x : x[i-n+1:i]
out[i] = ybar[i] - xbar*(cov(xi,yi)/var(xi))
end
return out
Expand Down

0 comments on commit 07904af

Please sign in to comment.