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

orthogonal polynomials #11

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ version = "0.1.1"

[deps]
Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
StatsAPI = "82ae8749-77ed-4fe6-ae5f-f523153014b0"
StatsModels = "3eaba693-59b7-5ba5-a881-562e759f1c8d"

[compat]
Expand Down
6 changes: 6 additions & 0 deletions src/RegressionFormulae.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module RegressionFormulae

using LinearAlgebra
using Statistics
using StatsAPI
using StatsModels
using Combinatorics
using Base.Iterators
Expand All @@ -13,4 +16,7 @@ include("fulldummy.jl")
include("power.jl")
include("nesting.jl")

include("poly.jl")
export poly

end # module
78 changes: 78 additions & 0 deletions src/poly.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
function poly(x, deg; raw=true)
raw == true && return [el^d for el in x, d in 1:deg]
length(unique(x)) < deg &&
error("Degree of orthogonal polynomial must be lower than the " *
"number of unique points")
x̄ = mean(x)
x = x .- mean(x)
mat = qr!([el^d for el in x, d in 0:deg])
cols = vcat(Diagonal(mat.R), zeros(length(x) - size(mat.R,1), size(mat.R, 2)))
cols = mat.Q * cols
colnorms = sum.(abs2, eachcol(cols))

for (j, cn) in zip(axes(cols, 2), colnorms)
cols[:, j] ./= sqrt(cn)
end

return cols[:, (begin+1):end]

end

# type of model where syntax applies: here this applies to any model type
const POLY_CONTEXT = RegressionModel

# struct for behavior
struct PolyTerm{T,D} <: AbstractTerm
term::T
deg::D
raw::Bool
end

Base.show(io::IO, p::PolyTerm) = print(io, "poly($(p.term), $(p.deg))")

# for `poly` use at run-time (outside @formula), return a schema-less PolyTerm
poly(t::Symbol, d::Int; raw::Bool=true) = PolyTerm(term(t), term(d), raw)

# for `poly` use inside @formula: create a schemaless PolyTerm and apply_schema
function StatsModels.apply_schema(t::FunctionTerm{typeof(poly)},
sch::StatsModels.Schema,
Mod::Type{<:POLY_CONTEXT})
apply_schema(PolyTerm(t.args_parsed...), sch, Mod)
end

# apply_schema to internal Terms and check for proper types
function StatsModels.apply_schema(t::PolyTerm,
sch::StatsModels.Schema,
Mod::Type{<:POLY_CONTEXT})
term = apply_schema(t.term, sch, Mod)
isa(term, ContinuousTerm) ||
throw(ArgumentError("PolyTerm only works with continuous terms (got $term)"))
isa(t.deg, ConstantTerm) ||
throw(ArgumentError("PolyTerm degree must be a number (got $t.deg)"))
PolyTerm(term, t.deg.n)
end

function StatsModels.modelcols(p::PolyTerm, d::NamedTuple)
col = modelcols(p.term, d)
p.raw && return reduce(hcat, [col.^n for n in 1:p.deg])

return mapreduce(hcat, 1:p.deg) do n


# INSERT HERE
end
end

# the basic terms contained within a PolyTerm (for schema extraction)
StatsModels.terms(p::PolyTerm) = terms(p.term)
# names variables from the data that a PolyTerm relies on
StatsModels.termvars(p::PolyTerm) = StatsModels.termvars(p.term)
# number of columns in the matrix this term produces
StatsModels.width(p::PolyTerm) = p.deg

# highlight the difference between raw and orthogonal in the output
function StatsAPI.coefnames(p::PolyTerm)
p.raw && return coefnames(p.term) .* "^" .* string.(1:p.deg)

return "poly(" .* coefnames(p.term) .* ", " .* string.(1:p.deg) .* ")"
end
22 changes: 22 additions & 0 deletions test/poly.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# > poly(1:10, 3)
# 1 2 3
# [1,] -0.49543369 0.52223297 -0.4534252
# [2,] -0.38533732 0.17407766 0.1511417
# [3,] -0.27524094 -0.08703883 0.3778543
# [4,] -0.16514456 -0.26111648 0.3346710
# [5,] -0.05504819 -0.34815531 0.1295501
# [6,] 0.05504819 -0.34815531 -0.1295501
# [7,] 0.16514456 -0.26111648 -0.3346710
# [8,] 0.27524094 -0.08703883 -0.3778543
# [9,] 0.38533732 0.17407766 -0.1511417
# [10,] 0.49543369 0.52223297 0.4534252

# > poly(-3:3, 5)
# 1 2 3 4 5
# [1,] -5.669467e-01 5.455447e-01 -4.082483e-01 0.2417469 -1.091089e-01
# [2,] -3.779645e-01 9.690821e-17 4.082483e-01 -0.5640761 4.364358e-01
# [3,] -1.889822e-01 -3.273268e-01 4.082483e-01 0.0805823 -5.455447e-01
# [4,] 2.098124e-17 -4.364358e-01 4.532467e-17 0.4834938 5.342065e-16
# [5,] 1.889822e-01 -3.273268e-01 -4.082483e-01 0.0805823 5.455447e-01
# [6,] 3.779645e-01 0.000000e+00 -4.082483e-01 -0.5640761 -4.364358e-01
# [7,] 5.669467e-01 5.455447e-01 4.082483e-01 0.2417469 1.091089e-01