Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue8 #9

Merged
merged 2 commits into from
Apr 18, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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