-
Notifications
You must be signed in to change notification settings - Fork 18
/
ArrayFuncs.jl
73 lines (64 loc) · 1.63 KB
/
ArrayFuncs.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
module ArrayFuncs
import Statistics.mean
import LinearAlgebra.pinv
"""
lstsq(A,X)
Least-squares regression of array `A` using the pseudo-inverse.
Solves the equation `A X = B` by computing a vector `X` that
minimizes the Euclidean 2-norm `|| B - A X ||^2`.
# Arguments
- `A::AbstractArray`: Coefficient matrix.
- `X::AbstractArray`: Dependent variable.
"""
function lstsq(A::AbstractArray,X::AbstractArray)
coeff = pinv(A' * A) * A' * X
end
"""
detrend!(A)
Remove linear trend from array `A` using least-squares regression.
"""
function detrend!(X::AbstractArray{Float64,1})
N = length(X)
A = ones(N,2)
A[:,1] = Array(1:N) ./ N
coeff = lstsq(A,X)
X[:] = X .- A *coeff
return nothing
end
detrend(A::AbstractArray{Float64,1}) = (U = deepcopy(A);detrend!(U);return U)
function detrend!(X::AbstractArray{Float64,2})
M,N = size(X)
A = ones(M,2)
A[:,1] = Array(1:N) ./ N
for ii = 1:N
coeff = lstsq(A,X[:,ii])
X[:,ii] = X[:,ii] .- A *coeff
end
return X
end
detrend(A::AbstractArray{Float64,2}) = (U = deepcopy(A);detrend!(U);return U)
"""
demean!(A)
Remove mean from columns of array `A`.
"""
function demean!(A::AbstractArray{Float64,1})
μ = mean(A)
for ii = 1:length(A)
A[ii] -= μ
end
return A
end
demean(A::AbstractArray{Float64,1}) = (U = deepcopy(A);demean!(U);return U)
function demean!(A::AbstractArray{Float64,2})
M,N = size(A)
for ii = 1:N
μ = mean(A[:,ii])
for jj = 1:M
A[jj,ii] -= μ
end
end
return A
end
demean(A::AbstractArray{Float64,2}) = (U = deepcopy(A);demean!(U);return U)
end