LS lifting

This example illustrates "lifting" in linear regression using the Julia language.

This page comes from a single Julia file: ls-lift.jl.

You can access the source code for such Julia documentation using the 'Edit on GitHub' link in the top right. You can view the corresponding notebook in nbviewer here: ls-lift.ipynb, or open it in binder here: ls-lift.ipynb.

Setup

Add the Julia packages used in this demo. Change false to true in the following code block if you are using any of the following packages for the first time.

if false
    import Pkg
    Pkg.add([
        "InteractiveUtils"
        "LaTeXStrings"
        "LinearAlgebra"
        "MIRTjim"
        "Plots"
        "Random"
    ])
end

Tell Julia to use the following packages. Run Pkg.add() in the preceding code block first, if needed.

using InteractiveUtils: versioninfo
using LaTeXStrings
using LinearAlgebra: Diagonal, svd
using MIRTjim: prompt
using Plots: default, gr, plotly, plot!, scatter, surface!, savefig
using Plots.PlotMeasures: px
using Random: seed!
default(); default(label="", markerstrokecolor=:auto, widen=true, linewidth=2,
 markersize = 6, tickfontsize=14, labelfontsize = 18, legendfontsize=16)

The following line is helpful when running this jl-file as a script; this way it will prompt user to hit a key after each image is displayed.

isinteractive() && prompt(:prompt);

Noisy data

Samples of a nonlinear function.

seed!(1) # seed rng
sfun = (t) -> 1 - cos(π/2*t)
M = 25
tm = sort(rand(M)) # M random sample locations
σ = 0.02
y = sfun.(tm) + σ * randn(M); # noisy samples

t0 = range(0, 1, 101) # fine sampling for showing curve
p1 = scatter(tm, y, color=:blue, label=L"\mathrm{data\ } y",
	xaxis = (L"t", (0, 1), 0:0.5:1),
	yaxis = (L"y", (-0.1, 1.1), 0:0.5:1),
)
plot!(t0, sfun.(t0), color=:black, label=L"s(t)", legend=:topleft)
Example block output
prompt()

Polynomial fits

Afun = (tt, deg) -> [t.^i for t in tt, i in 1:deg] # matrix of monomials

A1 = Afun(tm, 1) # M × 1 matrix
A2 = Afun(tm, 2) # M × 2 matrix

x1 = A1 \ y # LS solution for degree=1
plot!(p1, t0, Afun(t0,1)*x1, color=:red, label="linear model fit")
Example block output
prompt()

x2 = A2 \ y # quadratic fit
plot!(p1, t0, Afun(t0,2)*x2, color=:orange, label="quadratic model fit")
Example block output
prompt()

# savefig(p1, "04-ls-lift-1.pdf")

Lifting

We can view quadratic polynomial fitting as nonlinear "lifting" from a 1D function of $t$ to a 2D function of $(t, t^2)$. After such lifting, regression with a linear model fits much better, as seen because the data points nearly lie on the 2D plane.

Use plotly() backend here to view surface interactively.

# plotly()
p2 = scatter(A2[:,1], A2[:,2], y, color=:blue, right_margin = 15px,
    cticks = [0,1],
    xaxis = (L"t", (0,1), -1:1),
    yaxis = (L"t^2", (0,1), -1:1),
    zaxis = (L"y", (0,1), -1:1),
)
t1 = range(0, 1, 101)
t2 = range(0, 1, 102)
surface!(t1, t2, (t1,t2) -> x2[1]*t1 + x2[2]*t2, alpha=0.3)
Example block output
prompt()

# gr(); # restore
# savefig(p2, "04-ls-lift-2.pdf") # with gr()

Reproducibility

This page was generated with the following version of Julia:

using InteractiveUtils: versioninfo
io = IOBuffer(); versioninfo(io); split(String(take!(io)), '\n')
11-element Vector{SubString{String}}:
 "Julia Version 1.11.2"
 "Commit 5e9a32e7af2 (2024-12-01 20:02 UTC)"
 "Build Info:"
 "  Official https://julialang.org/ release"
 "Platform Info:"
 "  OS: Linux (x86_64-linux-gnu)"
 "  CPU: 4 × AMD EPYC 7763 64-Core Processor"
 "  WORD_SIZE: 64"
 "  LLVM: libLLVM-16.0.6 (ORCJIT, znver3)"
 "Threads: 1 default, 0 interactive, 1 GC (on 4 virtual cores)"
 ""

And with the following package versions

import Pkg; Pkg.status()
Status `~/work/book-la-demo/book-la-demo/docs/Project.toml`
  [6e4b80f9] BenchmarkTools v1.5.0
  [aaaa29a8] Clustering v0.15.7
  [35d6a980] ColorSchemes v3.27.1
⌅ [3da002f7] ColorTypes v0.11.5
⌃ [c3611d14] ColorVectorSpace v0.10.0
⌅ [717857b8] DSP v0.7.10
  [72c85766] Demos v0.1.0 `~/work/book-la-demo/book-la-demo`
  [e30172f5] Documenter v1.8.0
  [4f61f5a4] FFTViews v0.3.2
  [7a1cc6ca] FFTW v1.8.0
  [587475ba] Flux v0.15.2
  [a09fc81d] ImageCore v0.10.5
  [71a99df6] ImagePhantoms v0.8.1
  [b964fa9f] LaTeXStrings v1.4.0
  [7031d0ef] LazyGrids v1.0.0
  [599c1a8e] LinearMapsAA v0.12.0
  [98b081ad] Literate v2.20.1
  [7035ae7a] MIRT v0.18.2
  [170b2178] MIRTjim v0.25.0
  [eb30cadb] MLDatasets v0.7.18
  [efe261a4] NFFT v0.13.5
  [6ef6ca0d] NMF v1.0.3
  [15e1cf62] NPZ v0.4.3
  [0b1bfda6] OneHotArrays v0.2.6
  [429524aa] Optim v1.10.0
  [91a5bcdd] Plots v1.40.9
  [f27b6e38] Polynomials v4.0.12
  [2913bbd2] StatsBase v0.34.4
  [d6d074c3] VideoIO v1.1.1
  [b77e0a4c] InteractiveUtils v1.11.0
  [37e2e46d] LinearAlgebra v1.11.0
  [44cfe95a] Pkg v1.11.0
  [9a3f8284] Random v1.11.0
Info Packages marked with ⌃ and ⌅ have new versions available. Those with ⌃ may be upgradable, but those with ⌅ are restricted by compatibility constraints from upgrading. To see why use `status --outdated`

This page was generated using Literate.jl.