Files
cmdla/project/testing/QR/QR.ipynb
2024-07-30 14:43:25 +02:00

177 lines
5.1 KiB
Plaintext
Generated

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "57a2b335-a7f3-4ace-a2d8-52219c4febc5",
"metadata": {},
"outputs": [],
"source": [
"include(\"../../QR/housQR.jl\")\n",
"include(\"../../utilities/genFunc.jl\")\n",
"using .housQR\n",
"using LinearAlgebra, BenchmarkTools, CSV, DataFrames\n",
"\n",
"baseDir = joinpath(\"../\", \"results/QR\")\n",
"mkpath(baseDir);"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "356e3edd",
"metadata": {},
"outputs": [],
"source": [
"### time with respect to lambda\n",
"\n",
"# parameters\n",
"lambdas = 10. .^(-15:6)\n",
"epss = [nothing]\n",
"m = 300\n",
"n = 20\n",
"\n",
"\n",
"# output csv\n",
"outputvsc = joinpath(baseDir, \"statisticsQR-lambda-m\" * string(m) * \"n\" * string(n) * \"--time.csv\");\n",
"accData = Dict(\n",
" :lambda => Array{Float64}(undef, 0),\n",
" :meantime => Array{Float64}(undef, 0),\n",
" :stdtime => Array{Float64}(undef, 0)\n",
" )\n",
"\n",
"# computation\n",
"for (λ, ϵ) ∈ Iterators.product(lambdas, epss) |> collect\n",
" gf = genFunc(:exactRandDataset, λ=λ, m=m, n=n)\n",
"\n",
" t = @benchmark begin\n",
" QR = qrfact($gf[:X_hat])\n",
" w = QR \\ $gf[:y_hat]\n",
" end\n",
"\n",
" push!(accData[:lambda], λ)\n",
" push!(accData[:meantime], mean(t.times))\n",
" push!(accData[:stdtime], std(t.times))\n",
" println(\"Done: λ \" * string(λ))\n",
" flush(stdout)\n",
"end\n",
"\n",
"CSV.write(outputvsc, DataFrame(accData));"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8b3d71f8-2f4f-44f0-8c97-dd9d78706163",
"metadata": {},
"outputs": [],
"source": [
"### error with respect to lambda\n",
"\n",
"# parameters\n",
"lambdas = 10. .^(-15:6)\n",
"epss = [nothing]\n",
"m = 300\n",
"n = 20\n",
"\n",
"\n",
"# output csv\n",
"outputvsc = joinpath(baseDir, \"statisticsQR-lambda-m\" * string(m) * \"n\" * string(n) * \"--error.csv\");\n",
"accData = Dict(\n",
" :lambda => Array{Float64}(undef, 0),\n",
" :relative => Array{Float64}(undef, 0),\n",
" :residual => Array{Float64}(undef, 0),\n",
" :stability => Array{Float64}(undef, 0)\n",
" )\n",
"\n",
"# computation\n",
"for (λ, ϵ) ∈ Iterators.product(lambdas, epss) |> collect\n",
" gf = genFunc(:exactRandDataset, λ=λ, m=m, n=n)\n",
"\n",
" QR = qrfact(gf[:X_hat])\n",
" w = QR \\ gf[:y_hat]\n",
" \n",
" relative_error = norm(w - gf[:w_star]) / norm(gf[:w_star])\n",
" residual = norm(gf[:X_hat] * w - gf[:y_hat]) / norm(gf[:y_hat])\n",
" stability = norm(Q_times_R - gf[:X_hat]) / norm(gf[:X_hat])\n",
"\n",
" push!(accData[:lambda], λ)\n",
" push!(accData[:relative], relative_error)\n",
" push!(accData[:residual], residual)\n",
" push!(accData[:stability], stability)\n",
" println(\"Done: λ \" * string(λ))\n",
" flush(stdout)\n",
"end\n",
"\n",
"CSV.write(outputvsc, DataFrame(accData));"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "387520b7",
"metadata": {},
"outputs": [],
"source": [
"### error with respect to lambda\n",
"\n",
"# parameters\n",
"lambdas = 10. .^(-15:6)\n",
"epss = [nothing]\n",
"m = 300\n",
"n = 20\n",
"perturbation = 1e-10\n",
"\n",
"\n",
"# output csv\n",
"outputvsc = joinpath(baseDir, \"statisticsQR-forward-m\" * string(m) * \"n\" * string(n) * \"--error.csv\");\n",
"accData = Dict(\n",
" :lambda => Array{Float64}(undef, 0),\n",
" :forwardQ => Array{Float64}(undef, 0),\n",
" :forwardR => Array{Float64}(undef, 0),\n",
" )\n",
"\n",
"# computation\n",
"for (λ, ϵ) ∈ Iterators.product(lambdas, epss) |> collect\n",
" gf = genFunc(:exactRandDataset, λ=λ, m=m, n=n)\n",
"\n",
" QR = qrfact(gf[:X_hat])\n",
" w = QR \\ gf[:y_hat]\n",
"\n",
" X_hat_p = gf[:X_hat] + perturbation * randn(size(gf[:X_hat]))\n",
" cond(gf[:X_hat]) |> display\n",
" # cond(X_hat_p) |> display\n",
" QR_p = qrfact(X_hat_p)\n",
"\n",
" forwardQ = norm(QR.Q - QR_p.Q)\n",
" forwardR = norm(QR.R - QR_p.R) / norm(QR.R)\n",
"\n",
" push!(accData[:lambda], λ)\n",
" push!(accData[:forwardQ], forwardQ)\n",
" push!(accData[:forwardR], forwardR)\n",
"\n",
" println(\"Done: λ \" * string(λ))\n",
" flush(stdout)\n",
"end\n",
"\n",
"CSV.write(outputvsc, DataFrame(accData));"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.9.3",
"language": "julia",
"name": "julia-1.9"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.9.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}