You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fit(::Type{<:AbstractPolynomial}, x, y, deg=length(x)-1; [weights], var=:x)
71
71
72
-
Fit the given data as a polynomial type with the given degree. Uses linear least squares. When weights are given, as either a `Number`, `Vector` or `Matrix`, will use weighted linear least squares. The default polynomial type is [`Polynomial`](@ref). This will automatically scale your data to the [`domain`](@ref) of the polynomial type using [`mapdomain`](@ref)
72
+
Fit the given data as a polynomial type with the given degree. Uses
73
+
linear least squares to minimize the norm of `V⋅c - y`, where `V` is
74
+
the Vandermonde matrix and `c` are the coefficients of the polynomial
75
+
fit.
76
+
77
+
This will automatically scale your data to the [`domain`](@ref) of the
78
+
polynomial type using [`mapdomain`](@ref). The default polynomial type
79
+
is [`Polynomial`](@ref).
80
+
81
+
When weights are given, as either a `Number`, `Vector` or `Matrix`,
82
+
this will use weighted linear least squares. That is, the norm of
83
+
`W ⋅ (y - V ⋅ x)` is minimized. (As of now, the weights are specified
84
+
using their squares: for a number use `w^2`, for a vector `wᵢ^2`, and for a matrix
85
+
specify `W'*W`. This behavior may change in the future.)
86
+
73
87
"""
74
88
functionfit(P::Type{<:AbstractPolynomial},
75
89
x::AbstractVector{T},
@@ -112,17 +126,21 @@ function _fit(P::Type{<:AbstractPolynomial},
112
126
if weights !==nothing
113
127
coeffs =_wlstsq(vand, y, weights)
114
128
else
115
-
coeffs =pinv(vand) * y
129
+
coeffs =qr(vand) \ y
116
130
end
117
131
R =float(T)
118
132
returnP(R.(coeffs), var)
119
133
end
120
134
121
135
122
136
# Weighted linear least squares
137
+
#TODO: Breaking change for 2.0: use non-squared weights
123
138
_wlstsq(vand, y, W::Number) =_wlstsq(vand, y, fill!(similar(y), W))
124
-
_wlstsq(vand, y, W::AbstractVector) =_wlstsq(vand, y, Diagonal(W))
125
-
_wlstsq(vand, y, W::AbstractMatrix) = (vand'* W * vand) \ (vand'* W * y)
139
+
function_wlstsq(vand, y, w::AbstractVector)
140
+
W =Diagonal(sqrt.(w))
141
+
qr(W * vand) \ (W * y)
142
+
end
143
+
_wlstsq(vand, y, W::AbstractMatrix) =qr(vand'* W * vand) \ (vand'* W * y)
0 commit comments