Skip to content

Commit

Permalink
make independent variable a parameter for moving linear regression
Browse files Browse the repository at this point in the history
  • Loading branch information
dysonance committed Apr 17, 2018
1 parent 0f358a5 commit 19466c0
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/reg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ 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(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]
out[i,2] = cov(xi,yi) / var(xi)
out[i,2] = cov(x,yi) / var(x)
out[i,1] = ybar[i] - out[i,2]*xbar
end
return out
Expand All @@ -23,14 +23,14 @@ 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(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]
out[i] = cov(xi,yi) / var(xi)
out[i] = cov(x,yi) / var(x)
end
return out
end
Expand All @@ -40,16 +40,16 @@ 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(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]
out[i] = ybar[i] - xbar*(cov(xi,yi)/var(xi))
out[i] = ybar[i] - xbar*(cov(x,yi)/var(x))
end
return out
end
Expand Down

0 comments on commit 19466c0

Please sign in to comment.