diff --git a/src/MinPakke.jl b/src/MinPakke.jl index d84a472..4e6bb9e 100644 --- a/src/MinPakke.jl +++ b/src/MinPakke.jl @@ -35,10 +35,21 @@ export calculateRMSECV export PCR export bidiag2 +export regularizationMatrix +export TRSVDDecomp +export TRRegCoeffs +export TRPress +export TRGCV +export TRLooCV +export PlotTRLooCV +export TRLooCVNum +export TRGCVNum + include("preprocessing.jl") include("convenience.jl") include("conveniencePlots.jl") include("variousRegressionFunctions.jl") +include("TR.jl") end \ No newline at end of file diff --git a/src/TR.jl b/src/TR.jl new file mode 100644 index 0000000..985e563 --- /dev/null +++ b/src/TR.jl @@ -0,0 +1,56 @@ + + + + + + +using Optimization +using OptimizationOptimJL + + +""" +### TO DO: ADD FRACTIONAL DERIVATIVE REGULARIZATION ### + + regularizationMatrix(X; regType="legendre", regParam1=0, regParam2=1e-14) + regularizationMatrix(p::Int64; regType="legendre", regParam1=0, regParam2=1e-14) + +Calculates and returns square regularization matrix. + +Inputs: + - X/p : Size of regularization matrix or data matrix (size of reg. mat. will then be size(X,2) + - regType : "L2" (returns identity matrix) + "bc" (boundary condition, forces zero on right endpoint for derivative regularization) or + "legendre" (no boundary condition, but fills out reg. mat. with lower order polynomial trends to get square matrix) + "std" (standardization, FILL OUT WHEN DONE) + - regParam1 : Int64, Indicates degree of derivative regularization (0 gives L\\_2) + - regParam2 : For regType=="plegendre" added polynomials are multiplied by sqrt(regParam2) + +Output: Square regularization matrix +""" +function regularizationMatrix(X; regType="L2", regParam1=0, regParam2=1e-14) + +if regType == "std" + regParam2 = Diagonal(vec(std(X, dims=1))); +end + +regMat = regularizationMatrix(size(X,2); regType, regParam1, regParam2) + +return regMat +end + +function regularizationMatrix(p::Int64; regType="L2", regParam1=0, regParam2=1e-14) + +if regType == "bc" + regMat = [I(p); zeros(regParam1,p)]; for i = 1:regParam1 regMat = diff(regMat, dims = 1); end +elseif regType == "legendre" + regMat = [I(p); zeros(regParam1,p)]; for i = 1:regParam1 regMat = diff(regMat, dims = 1); end + P, _ = plegendre(regParam1-1, p); + regMat[end-regParam1+1:end,:] = sqrt(regParam2) * P; +elseif regType == "L2" + regMat = I(size(X,2)); +elseif regType == "std" + regMat = regParam2; +end + +return regMat +end \ No newline at end of file